module Frenchy::Resource::ClassMethods
Public Instance Methods
find(params={})
click to toggle source
Find record(s) using the default endpoint and flexible input
# 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={})
click to toggle source
Find multiple record using the “many” (or “default”) endpoint and an array of ids
# 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={})
click to toggle source
Find a single record using the “one” (or “default”) endpoint and an id
# 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={})
click to toggle source
Call with a specific endpoint and params
# 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={})
click to toggle source
Call with arbitrary method and path
# 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
Private Instance Methods
digest_response(response, endpoint)
click to toggle source
Converts a response into model data
# File lib/frenchy/resource.rb, line 46 def digest_response(response, endpoint) if endpoint["nesting"] Array(endpoint["nesting"]).map(&:to_s).each do |key| response = response.fetch(key) end end if response.is_a?(Array) Frenchy::Collection.new(Array(response).map {|v| new(v) }) else new(response) end end
resolve_endpoints(endpoints)
click to toggle source
Choose the first available endpoint
# File lib/frenchy/resource.rb, line 61 def resolve_endpoints(endpoints) Array(endpoints).map(&:to_s).each do |s| if ep = @endpoints[s] return s, ep end end raise(Frenchy::Error, "Resource does not contain any endpoints: #{Array(endpoints).join(", ")}") end
resource(options={})
click to toggle source
Macro to set the location pattern for this request
# File lib/frenchy/resource.rb, line 72 def resource(options={}) options.stringify_keys! @service = options.delete("service").to_s || raise(Frenchy::Error, "Resource must specify a service") if endpoints = options.delete("endpoints") @endpoints = validate_endpoints(endpoints) elsif endpoint = options.delete("endpoint") @endpoints = validate_endpoints({"default" => endpoint}) else @endpoints = {} end end
validate_endpoints(endpoints={})
click to toggle source
# File lib/frenchy/resource.rb, line 86 def validate_endpoints(endpoints={}) endpoints.stringify_keys! Hash[endpoints.map do |k,v| v.stringify_keys! raise(Frenchy::Error, "Endpoint #{k} does not specify a path") unless v["path"] [k, v] end] end