Class: CoachClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/coach_client/resource.rb

Overview

A resource of the CyberCoach service.

Direct Known Subclasses

Entry, Partnership, Sport, Subscription, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ CoachClient::Resource

Creates a new resource.

Parameters:



11
12
13
# File 'lib/coach_client/resource.rb', line 11

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientCoachClient::Client

Returns:



5
6
7
# File 'lib/coach_client/resource.rb', line 5

def client
  @client
end

Instance Method Details

#exist?(username: nil, password: nil) ⇒ Boolean

Returns whether the resource exists on the CyberCoach service.

Parameters:

  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# 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_hHash

Returns the hash representation of the resource.

Returns:

  • (Hash)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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