Last active
March 21, 2018 13:45
-
-
Save anasbgoncalves/3887629dd1a8d0c1c8d51852150878ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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