Skip to content

Instantly share code, notes, and snippets.

@anasbgoncalves
Last active May 20, 2020 17:17
Show Gist options
  • Save anasbgoncalves/a0bbc3998b559c945ac9a336cd4029b7 to your computer and use it in GitHub Desktop.
Save anasbgoncalves/a0bbc3998b559c945ac9a336cd4029b7 to your computer and use it in GitHub Desktop.
class Request
class << self
def where(resource_path, query = {}, options = {})
response, status = get_json(resource_path, query)
status == 200 ? response : errors(response)
end
def get(id)
response, status = get_json(id)
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, query = {})
query_string = query.map{|k,v| "#{k}=#{v}"}.join("&")
path = query.empty?? root_path : "#{root_path}?#{query_string}"
response = api.get(path)
[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