class MtgApi::Request
builds and sends requests to the api server
Constants
- ROOT_URL
the root url of the api
Attributes
endpoint[RW]
the url for this request
Public Class Methods
new(endpoint)
click to toggle source
build a new request
# File lib/mtg_api/request.rb, line 13 def initialize(endpoint) self.endpoint = URI(ROOT_URL + endpoint) end
Public Instance Methods
response()
click to toggle source
get the response
# File lib/mtg_api/request.rb, line 18 def response puts "\e[32mGET #{endpoint}\e[0m" @response ||= JSON.parse(raw_response) end
response_for(response_key)
click to toggle source
the section of the response, formatted in snake case
# File lib/mtg_api/request.rb, line 24 def response_for(response_key) (response[response_key] || []).map do |entity| rubyify(entity) end end
Private Instance Methods
raw_response()
click to toggle source
the response from the server
# File lib/mtg_api/request.rb, line 33 def raw_response Net::HTTP.get(endpoint) end
rubyify(entity)
click to toggle source
format a hash to have snake case keys
# File lib/mtg_api/request.rb, line 38 def rubyify(entity) entity.map do |key, value| [key.to_s.gsub(/([A-Z])/) { '_' + $1.downcase }, value] end.to_h end