Class: MtgApi::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mtg_api/request.rb

Overview

builds and sends requests to the api server

Constant Summary collapse

ROOT_URL =

the root url of the api

'http://api.mtgapi.com/v2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Request

build a new request



13
14
15
# File 'lib/mtg_api/request.rb', line 13

def initialize(endpoint)
  self.endpoint = URI(ROOT_URL + endpoint)
end

Instance Attribute Details

#endpointObject

the url for this request



10
11
12
# File 'lib/mtg_api/request.rb', line 10

def endpoint
  @endpoint
end

Instance Method Details

#responseObject

get the response



18
19
20
21
# 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) ⇒ Object

the section of the response, formatted in snake case



24
25
26
27
28
# File 'lib/mtg_api/request.rb', line 24

def response_for(response_key)
  (response[response_key] || []).map do |entity|
    rubyify(entity)
  end
end