Class: BayonetClient::ApiHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/bayonet_client/api_helper.rb

Class Method Summary collapse

Class Method Details

.fully_qualified_api_host_name(route) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bayonet_client/api_helper.rb', line 45

def self.fully_qualified_api_host_name(route)
  default_domain = 'api.bayonet.io'
  api_version_namespace = 'v' + BayonetClient.version
  if route == '/get-fingerprint-data'
    default_domain = 'fingerprinting.bayonet.io'
    if BayonetClient.version == '2'
      api_version_namespace = 'v1'
    end
  end
  "https://#{default_domain}/#{api_version_namespace}"
end

.json_from_params(params, route) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/bayonet_client/api_helper.rb', line 9

def self.json_from_params(params, route)
  # Add api_key to params
  if route == '/get-fingerprint-data'
    params[:api_key] = BayonetClient.api_key
  else
    params[:auth] = {}
    params[:auth][:api_key] = BayonetClient.api_key
  end
  params.to_json
end

.request(route, params) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/bayonet_client/api_helper.rb', line 20

def self.request(route, params)
  unless validate_params(params)
    raise BayonetClient::BayonetError.new(params, '', '', 'Invalid params. Please make sure you pass a params hash')
  end
  json_params = json_from_params(params, route)
  request_json_string(route, json_params)
end

.request_json_string(route, request_json_args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bayonet_client/api_helper.rb', line 28

def self.request_json_string(route, request_json_args)
  fq_hostname = fully_qualified_api_host_name(route)
  url = "#{fq_hostname}#{route}"

  headers = {'User-Agent' => 'OfficialBayonetRubySDK',
             'Content-Type' => 'application/json', 'Accept' => 'application/json'}

  raw_resp = HTTParty.post(
      url, body: request_json_args, headers: headers, verify: false)

  if raw_resp.code == 200
    BayonetClient::BayonetResponse.new(raw_resp)
  else
    raise BayonetClient::BayonetError.new(request_json_args, headers, raw_resp.code, raw_resp)
  end
end

.validate_params(params) ⇒ Object



5
6
7
# File 'lib/bayonet_client/api_helper.rb', line 5

def self.validate_params(params)
  params.class == Hash
end