Class: Change::Resources::Resource

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

Direct Known Subclasses

CollectionResource, MemberResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, properties = {}) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
13
14
# File 'lib/resources/resource.rb', line 7

def initialize(client, properties = {})
  @client = client
  @auth_keys = []

  initial_auth_key = properties.delete(:auth_key)
  add_new_auth_key(initial_auth_key) unless initial_auth_key.nil?
  @properties = properties
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#auth_key(key_number = 0) ⇒ Object



34
35
36
# File 'lib/resources/resource.rb', line 34

def auth_key(key_number = 0)
  @auth_keys[key_number]
end

#auth_key=(new_key) ⇒ Object



38
39
40
# File 'lib/resources/resource.rb', line 38

def auth_key=(new_key)
  add_new_auth_key(new_key)
end

#endpoint(request_on, action_or_collection = nil) ⇒ Object



20
21
22
23
24
# File 'lib/resources/resource.rb', line 20

def endpoint(request_on, action_or_collection = nil)
  path_parts = send("#{request_on.to_s}_path", action_or_collection)
  path = path_parts.join('/')
  path.prepend('/')
end

#make_request(request_on, request_type, params = {}) ⇒ Object



16
17
18
# File 'lib/resources/resource.rb', line 16

def make_request(request_on, request_type, params = {})
  @client.request(request_on, request_type, self, params)
end

#needs_authorization?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/resources/resource.rb', line 30

def needs_authorization?(action = nil)
  action != :auth_keys
end

#needs_request_signature?(method) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/resources/resource.rb', line 26

def needs_request_signature?(method)
  method != :get
end

#request_auth_key(params) ⇒ Object

Shared resource requests



44
45
46
47
48
49
50
51
52
# File 'lib/resources/resource.rb', line 44

def request_auth_key(params)
  response = make_request(:member, { :method => :post, :collection => :auth_keys }, params)
  if response['status'] == 'granted'
    add_new_auth_key(response)
    true
  else
    false
  end
end