Class: GarageClient::Resource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(client, data)
  @client = client
  @data = Hashie::Mash.new(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/garage_client/resource.rb', line 36

def method_missing(name, *args, &block)
  if properties.include?(name)
    value = data[name]
    if self.class.resource?(value)
      GarageClient::Resource.new(client, value)
    else
      value
    end
  elsif query_method?(name)
    data.__send__(name)
  elsif links.include?(name)
    path = data._links[name].href
    client.get(path, *args)
  elsif nested_resource_creation_method?(name)
    path = data._links[name.to_s.sub(/create_/, '')].href
    client.post(path, *args)
  else
    raise NoMethodError.new("undefined method `#{name}' for #{data}")
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.resource?(hash) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/garage_client/resource.rb', line 7

def self.resource?(hash)
  hash.kind_of?(Hash) && hash.has_key?('_links')
end

Instance Method Details

#destroy(options = {}) ⇒ Object



32
33
34
# File 'lib/garage_client/resource.rb', line 32

def destroy(options = {})
  client.delete(self_path, options)
end


20
21
22
# File 'lib/garage_client/resource.rb', line 20

def links
  @links ||= data._links ? data._links.keys.map(&:to_sym) : []
end

#nested_resource_creation_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/garage_client/resource.rb', line 61

def nested_resource_creation_method?(name)
  !!(name =~ /\Acreate_(.+)\z/ && links.include?($1.to_sym))
end

#propertiesObject



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

def properties
  @properties ||= data.keys.map(&:to_sym)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/garage_client/resource.rb', line 57

def respond_to_missing?(name, include_private)
  !!(properties.include?(name) || query_method?(name) || links.include?(name) || nested_resource_creation_method?(name))
end

#self_pathObject



24
25
26
# File 'lib/garage_client/resource.rb', line 24

def self_path
  @self_path ||= data._links.self.href
end

#update(body = nil, options = {}) ⇒ Object



28
29
30
# File 'lib/garage_client/resource.rb', line 28

def update(body = nil, options = {})
  client.put(self_path, body, options)
end