lib/frenchy/resource.rb in frenchy-0.0.6 vs lib/frenchy/resource.rb in frenchy-0.0.7

- old
+ new

@@ -22,24 +22,36 @@ # Find multiple record using the "many" (or "default") endpoint and an array of ids def find_many(ids, params={}) find_with_endpoint([:many, :default], {ids: ids.join(",")}.merge(params)) end - # Find with a specific endpoint and params + # Call with a specific endpoint and params 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 + digest_response(response) + end + # Call with arbitrary method and path + def find_with_path(method, path, params={}) + options = {model: self.name.underscore, endpoint: "path"} + response = Frenchy::Request.new(@service, method.to_sym, path, params, options).value + digest_response(response) + end + + private + + # Converts a response into model data + def digest_response(response) if response.is_a?(Array) Frenchy::Collection.new(Array(response).map {|v| from_hash(v) }) else from_hash(response) end end - - private # Choose the first available endpoint def resolve_endpoints(endpoints) Array(endpoints).map(&:to_sym).each do |sym| if ep = @endpoints[sym]