Skip to content

Instantly share code, notes, and snippets.

@anasbgoncalves
Last active March 21, 2018 13:45
Show Gist options
  • Save anasbgoncalves/3887629dd1a8d0c1c8d51852150878ef to your computer and use it in GitHub Desktop.
Save anasbgoncalves/3887629dd1a8d0c1c8d51852150878ef to your computer and use it in GitHub Desktop.
class Request
class << self
def where(resource_path, cache_params, query = {}, options = {})
response, status = get_json(resource_path, cache_params, query)
status == 200 ? response : errors(response)
end
def get(id, cache_params)
response, status = get_json(id, cache_params)
status == 200 ? response : errors(response)
end
def errors(response)
error = { errors: { status: response["status"], message: response["message"] } }
response.merge(error)
end
def get_json(root_path, cache_params, query = {})
query_string = query.map{|k,v| "#{k}=#{v}"}.join("&")
path = query.empty?? root_path : "#{root_path}?#{query_string}"
response = Rails.cache.fetch(path, cache_params) do
api.get(path)
end
[JSON.parse(response.body), response.status]
end
def api
Connection.api
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment