Class: NOMIS::API::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/nomis/api/post.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 = {}) ⇒ Post

Returns a new instance of Post.



16
17
18
19
20
21
# File 'lib/nomis/api/post.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/post.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/post.rb', line 14

def base_url
  @base_url
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#executeObject



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

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

  req = Net::HTTP::Post.new(uri)
  req['Authorization'] = auth_token
  req['Accept'] = 'application/json, */*'
  req['Content-type'] = 'application/json'

  ParsedResponse.new(post_response(req))
end