class CoachClient::Resource

A resource of the CyberCoach service.

Attributes

client[RW]

@return [CoachClient::Client]

Public Class Methods

new(client) click to toggle source

Creates a new resource.

@param [CoachClient::Client] client @return [CoachClient::Resource]

# File lib/coach_client/resource.rb, line 11
def initialize(client)
  @client = client
end

Public Instance Methods

exist?(username: nil, password: nil) click to toggle source

Returns whether the resource exists on the CyberCoach service.

@param [String] username @param [String] password @return [Boolean]

# File lib/coach_client/resource.rb, line 20
def exist?(username: nil, password: nil)
  begin
    CoachClient::Request.get(url, username: username, password: password)
    true
  rescue CoachClient::NotFound
    false
  end
end
to_h() click to toggle source

Returns the hash representation of the resource.

@return [Hash]

# File lib/coach_client/resource.rb, line 32
def to_h
  hash = {}
  instance_variables.each do |var|
    next if var.to_s == '@client'
    value = instance_variable_get(var)
    hash[var.to_s.delete('@').to_sym] =
      if value && value.respond_to?(:to_h) && !value.is_a?(Array)
        value.to_h
      else
        value
      end
  end
  hash
end

Private Instance Methods

next?(links) click to toggle source
# File lib/coach_client/resource.rb, line 49
def next?(links)
  return false if links.nil?
  links.any? { |link| link[:description] == "next" }
end