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



9
10
11
12
# File 'lib/frenchy/resource.rb', line 9

def find(params={})
  params = {"id" => params.to_s} if [Integer, 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



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

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



15
16
17
# File 'lib/frenchy/resource.rb', line 15

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

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

Call with a specific endpoint and params



25
26
27
28
29
30
31
32
33
# File 'lib/frenchy/resource.rb', line 25

def find_with_endpoint(endpoints, params={})
  params.stringify_keys!
  name, endpoint = resolve_endpoints(endpoints)
  method = endpoint["method"] || "get"
  extras = {model: self.name.underscore, endpoint: name}

  response = Frenchy::Request.new(@service, method, endpoint["path"], params, extras).value
  digest_response(response, endpoint)
end

#find_with_path(method, path, params = {}) ⇒ Object

Call with arbitrary method and path



36
37
38
39
40
41
# File 'lib/frenchy/resource.rb', line 36

def find_with_path(method, path, params={})
  params.stringify_keys!
  extras = {"model" => self.name, "endpoint" => "path"}
  response = Frenchy::Request.new(@service, method.to_s, path.to_s, params, extras).value
  digest_response(response, endpoint)
end