module Unchained::Request
Public Instance Methods
get(url, params={})
click to toggle source
Use RestClient to actually make the request to the API. If the response is a 200 (success), we will parse the response as JSON and return it. If the response is a known error (i.e. a 404), we will raise a custom Unchained
error (found in error.rb). If the response is an unkonwn error, we will return it exactly as we found it.
# File lib/unchained/request.rb, line 11 def get(url, params={}) RestClient.get(url, build_params(params)) do |resp, req, res, &block| case resp.code when 200 JSON.parse(resp) when 404 raise Unchained::NotFound.new(res.message) else resp.return!(req, res, &block) end end end
get_resource(url, resource_class, params={})
click to toggle source
If an API endpoint returns a single resource, not an Array of resources, we want to use this.
Returns an instance of `resource_class`.
# File lib/unchained/request.rb, line 28 def get_resource(url, resource_class, params={}) resource_class.from_hash(get(url, params), client: self) end
get_resources(url, resource_class, params={})
click to toggle source
If an API endpoint returns an Array of resources, we want to use this.
Returns an array of `resource_classes`.
# File lib/unchained/request.rb, line 35 def get_resources(url, resource_class, params={}) get(url, params).map do |result| resource_class.from_hash(result, client: self) end end
Private Instance Methods
build_params(params)
click to toggle source
# File lib/unchained/request.rb, line 50 def build_params(params) default_params.merge(params) end
default_params()
click to toggle source
# File lib/unchained/request.rb, line 43 def default_params { loginToken: login_token, accept: :json, } end