Class: NOMIS::API::Get

Inherits:
Object
  • Object
show all
Defined in:
lib/nomis/api/get.rb

Overview

Convenience wrapper around an API call Manages defaulting of params from env vars, and parsing the returned JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Get

Returns a new instance of Get.



16
17
18
19
20
21
# File 'lib/nomis/api/get.rb', line 16

def initialize(opts={})
  self.auth_token = opts[:auth_token] || default_auth_token(opts)
  self.base_url   = opts[:base_url] || ENV['NOMIS_API_BASE_URL']
  self.params = opts[:params] || {}
  self.path = opts[:path]
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



14
15
16
# File 'lib/nomis/api/get.rb', line 14

def auth_token
  @auth_token
end

#base_urlObject

Returns the value of attribute base_url.



14
15
16
# File 'lib/nomis/api/get.rb', line 14

def base_url
  @base_url
end

#paramsObject

Returns the value of attribute params.



14
15
16
# File 'lib/nomis/api/get.rb', line 14

def params
  @params
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/nomis/api/get.rb', line 14

def path
  @path
end

Instance Method Details

#executeObject



23
24
25
26
27
28
29
30
31
# File 'lib/nomis/api/get.rb', line 23

def execute
  uri = URI.join(base_url, path)
  uri.query = URI.encode_www_form(params)

  req = Net::HTTP::Get.new(uri)
  req['Authorization'] = auth_token

  ParsedResponse.new(get_response(req))
end