Module: Finix::Resource::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#construct_from_response(payload) ⇒ Object

this is class method, not callable from instance



110
111
112
113
114
115
116
# File 'lib/finix/resources/resource.rb', line 110

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, retrieve



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/finix/resources/resource.rb', line 118

def fetch(*arguments)
  if arguments.nil? or arguments.empty? or arguments[0].nil? or arguments[0].to_s.empty?
    href = Finix.hypermedia_registry.key(self)
    return Finix::Utils.eval_class(self, Pagination).new :href => href
  end

  options = arguments.slice!(0) or {}
  if options.is_a? String and options =~ URI::regexp
    href = options
  else
    href = Finix.get_href(self) or Finix.get_href(self.class)
    if options.is_a? Hash
      options = Finix::Utils.indifferent_read_access options
      id = options.delete('id')
    elsif options.is_a? String
      id = options
    end
    href = "#{href}/#{id}" unless id.nil?
  end

  response = Finix.get href
  construct_from_response response.body
end

#pagination(*args) ⇒ Object



142
143
144
145
146
147
# File 'lib/finix/resources/resource.rb', line 142

def pagination(*args)
  href = Finix.hypermedia_registry.key(self)
  opts = args.slice!(0) || {}
  opts[:href] = href
  Finix::Utils.eval_class(self, Pagination).new opts
end