Module: Frenchy::Resource::ClassMethods

Defined in:
lib/frenchy/resource.rb

Instance Method Summary collapse

Instance Method Details

#find(params = {}) ⇒ Object

Find record(s) using the default endpoint and flexible input



12
13
14
15
# File 'lib/frenchy/resource.rb', line 12

def find(params={})
  params = {id: params.to_s} if [Fixnum, String].any? {|c| params.is_a? c }
  find_with_endpoint(:default, params)
end

#find_many(ids, params = {}) ⇒ Object

Find multiple record using the “many” (or “default”) endpoint and an array of ids



23
24
25
# File 'lib/frenchy/resource.rb', line 23

def find_many(ids, params={})
  find_with_endpoint([:many, :default], {ids: ids.join(",")}.merge(params))
end

#find_one(id, params = {}) ⇒ Object

Find a single record using the “one” (or “default”) endpoint and an id



18
19
20
# File 'lib/frenchy/resource.rb', line 18

def find_one(id, params={})
  find_with_endpoint([:one, :default], {id: id}.merge(params))
end

#find_with_endpoint(endpoints, params = {}) ⇒ Object

Find with a specific endpoint and params



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/frenchy/resource.rb', line 28

def find_with_endpoint(endpoints, params={})
  name, endpoint = resolve_endpoints(endpoints)
  method = (endpoint[:method] || :get).to_sym
  options = {model: self.name.underscore, endpoint: name.to_s}
  response = Frenchy::Request.new(@service, method, endpoint[:path], params, options).value

  if response.is_a?(Array)
    Frenchy::Collection.new(Array(response).map {|v| from_hash(v) })
  else
    from_hash(response)
  end
end