Class: HyperTrack::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertrack/api_client.rb

Constant Summary collapse

BASE_URL =
"https://api.hypertrack.com/api"
VERB_MAP =
{
  :get  => Net::HTTP::Get,
  :post => Net::HTTP::Post,
  :patch => Net::HTTP::Patch
}
ACCEPTED_RESPONSE_CODES =
[200, 201]

Class Method Summary collapse

Class Method Details

.create(api_path, data = {}) ⇒ Object Also known as: update



16
17
18
19
20
21
# File 'lib/hypertrack/api_client.rb', line 16

def create(api_path, data={})
  api_uri = get_uri(api_path)
  request_object = create_request_object(api_uri, :post)
  request_object.body = data.to_json
  make_request(api_uri, request_object)
end

.create_request_object(api_uri, http_method) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/hypertrack/api_client.rb', line 56

def create_request_object(api_uri, http_method)
  if VERB_MAP[http_method].nil?
    raise HyperTrack::MethodNotAllowed.new("Unsupported HTTP verb used - Only #{VERB_MAP.keys.map{ |x| x.to_s.upcase }} allowed")
  end

  header = get_auth_header
  request_object = VERB_MAP[http_method].new(api_uri.request_uri, header)
  request_object['Content-Type'] = 'application/json'
  request_object
end

.fetch(api_path, query_params = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/hypertrack/api_client.rb', line 31

def fetch(api_path, query_params={})
  api_path = path_with_params(api_path, query_params) if query_params.is_a?(Hash) && query_params.keys.length > 0
  api_uri = get_uri(api_path)
  request_object = create_request_object(api_uri, :get)
  make_request(api_uri, request_object)
end

.get_auth_headerObject



43
44
45
46
47
48
49
# File 'lib/hypertrack/api_client.rb', line 43

def get_auth_header
  if Util.blank?(HyperTrack.secret_key)
    raise HyperTrack::InvalidAPIKey.new("HyperTrack.secret_key is invalid.")
  end

  { "Authorization" => "token #{HyperTrack.secret_key}" }
end

.get_uri(path) ⇒ Object



51
52
53
54
# File 'lib/hypertrack/api_client.rb', line 51

def get_uri(path)
  url = "#{BASE_URL}/#{HyperTrack.api_version}/#{path}"
  URI.parse(url)
end

.make_request(api_uri, request_object) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hypertrack/api_client.rb', line 67

def make_request(api_uri, request_object)
  parse_response(
    Net::HTTP.start(
        api_uri.host,
        api_uri.port,
        :read_timeout => ENV.fetch('NET_TIMEOUT', 5).to_i,
        :use_ssl => api_uri.scheme == 'https',
        :verify_mode => OpenSSL::SSL::VERIFY_PEER
      ) { |conn|
      conn.cert_store = OpenSSL::X509::Store.new
      conn.cert_store.set_default_paths
      conn.request(request_object)
    }
  )
end

.parse_response(response) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hypertrack/api_client.rb', line 83

def parse_response(response)
  response_code = response.code.to_i

  if valid_response_code?(response_code)
    begin
      JSON.parse(response.body)
    rescue JSON::ParserError => e
      raise HyperTrack::InvalidJSONResponse.new(response.body)
    end
  else
    error_klass = HyperTrack::Error.defined_codes[response_code] || HyperTrack::UnknownError
    raise error_klass.new(response.body, response_code)
  end
end

.patch(api_path, data = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/hypertrack/api_client.rb', line 24

def patch(api_path, data={})
  api_uri = get_uri(api_path)
  request_object = create_request_object(api_uri, :patch)
  request_object.body = data.to_json
  make_request(api_uri, request_object)
end

.path_with_params(path, params) ⇒ Object



38
39
40
41
# File 'lib/hypertrack/api_client.rb', line 38

def path_with_params(path, params)
  encoded_params = URI.encode_www_form(params)
  [path, encoded_params].join("?")
end

.valid_response_code?(code) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/hypertrack/api_client.rb', line 98

def valid_response_code?(code)
  ACCEPTED_RESPONSE_CODES.include?(code)
end