class NOMIS::API::Post

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

Attributes

auth_token[RW]
base_url[RW]
disable_ssl_verify[RW]
params[RW]
path[RW]

Public Class Methods

new(opts={}) click to toggle source
# 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]
  self.disable_ssl_verify = opts[:disable_ssl_verify]
end

Public Instance Methods

execute() click to toggle source
# File lib/nomis/api/post.rb, line 24
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

Protected Instance Methods

default_auth_token(params={}) click to toggle source
# File lib/nomis/api/post.rb, line 37
def default_auth_token(params={})
  ENV['NOMIS_API_AUTH_TOKEN'] || NOMIS::API::AuthToken.new(params).bearer_token
end
post_response(req) click to toggle source
# File lib/nomis/api/post.rb, line 42
def post_response(req)
  http = Net::HTTP.new(req.uri.hostname, req.uri.port)
  http.use_ssl = (req.uri.scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if disable_ssl_verify
  req.body = params.to_json
  http.request(req)
end
stringify_hash(data) click to toggle source
# File lib/nomis/api/post.rb, line 50
def stringify_hash(data)
  h={}
  data.each{|k,v| h[k.to_s] = v }
  h
end