Module: Finix::Resource::ClassMethods

Defined in:
lib/finix/resources/resource.rb

Instance Method Summary collapse

Instance Method Details

#construct_from_response(payload) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/finix/resources/resource.rb', line 103

def construct_from_response(payload)
  payload = Finix::Utils.indifferent_read_access payload

  links = payload.delete('_links') || {}
  instance = self.new payload
  instance.hydrate(links)
  instance
end

#fetch(*arguments) ⇒ Object Also known as: find



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/finix/resources/resource.rb', line 112

def fetch(*arguments)
  if arguments.nil? or arguments.empty? or arguments[0].nil? or arguments[0].to_s.empty? # no symbol.empty? in 1.8.7
    raise Finix::NotFound
  end

  options = arguments.slice!(0) or {}
  if options.is_a? String
    href = options
  else
    href = Finix.hypermedia_registry.key(self) or Finix.hypermedia_registry.key(self.class)
    id = options.delete(:id)
    href = "#{href}/#{id}" unless id.nil?
  end

  response = Finix.get href
  construct_from_response response.body

end