class DmtdVbmappData::RequestHelpers

Public Class Methods

get_authorized(opts = {}) click to toggle source

GET an authorized message from the vbmappdata server

Params:

end_point

The end point to post

params

The parameters to send to the end point (may be nil)

client_id

The client id under which to record the post (may be nil)

client_code

The client code under which to record the post (may be nil)

language

The language to fetch (may be nil)

# File lib/dmtd_vbmapp_data/request_helpers.rb, line 16
def self.get_authorized(opts = {})
  end_point = opts.fetch(:end_point)
  params = opts.fetch(:params, nil)
  client_id = opts.fetch(:client_id, nil)
  client_code = opts.fetch(:client_code, nil)
  language = opts.fetch(:language, nil)

  get url(end_point), params, client_id, client_code, language
end
post_authorized(opts = {}) click to toggle source

POST an authorized message to the vbmappdata server

Params:

end_point

The end point to post

params

The parameters to send to the end point (may be nil)

client_id

The client id under which to record the post (may be nil)

client_code

The client code under which to record the post (may be nil)

# File lib/dmtd_vbmapp_data/request_helpers.rb, line 33
def self.post_authorized(opts = {})
  end_point = opts.fetch(:end_point)
  params = opts.fetch(:params, nil)
  client_id = opts.fetch(:client_id, nil)
  client_code = opts.fetch(:client_code, nil)

  post url(end_point), params, client_id, client_code
end
process_json_response(response) click to toggle source

Process a JSON response from the server

Returns: { json: <json>, code: <response.code.to_i> }

# File lib/dmtd_vbmapp_data/request_helpers.rb, line 46
def self.process_json_response(response)
  json = nil
  server_response_code = response.code.to_i

  if server_response_code == 200
    json_body = JSON.parse(response.body).symbolize_hash_keys
    json = json_body[:response]
  end

  { json: json, code: server_response_code }
end

Private Class Methods

add_auth(request, client_id = nil, client_code = nil, language) click to toggle source
# File lib/dmtd_vbmapp_data/request_helpers.rb, line 82
def self.add_auth(request, client_id = nil, client_code = nil, language)
  session_token = DmtdVbmappData.config[:auth_token]

  request['Authorization'] = "Token token=\"#{session_token}\""
  request['Accept'] = 'application/json'
  request['Content-Type'] = 'application/json'
  request['X-ClientId'] = client_id unless client_id.nil?
  request['X-ClientCode'] = client_code unless client_code.nil?
  request['X-DocType'] = DmtdVbmappData.config[:document_type].downcase unless DmtdVbmappData.config[:document_type].nil?
  request['Accept-Language'] = language unless language.nil?
end
get(uri, params, client_id, client_code, language) click to toggle source
# File lib/dmtd_vbmapp_data/request_helpers.rb, line 60
def self.get(uri, params, client_id, client_code, language)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  add_auth(request, client_id, client_code, language)
  request.set_form_data params unless params.nil?

  http.request(request)
end
post(uri, params, client_id, client_code) click to toggle source
# File lib/dmtd_vbmapp_data/request_helpers.rb, line 69
def self.post(uri, params, client_id, client_code)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  add_auth(request, client_id, client_code, nil)
  request.body = JSON(params)

  http.request(request)
end
url(end_point) click to toggle source
# File lib/dmtd_vbmapp_data/request_helpers.rb, line 78
def self.url(end_point)
  URI.parse("#{DmtdVbmappData.config[:server_url]}/#{end_point}")
end