Class: Infermedica::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/infermedica/connection.rb

Overview

Connection

handles the http communication No assumption is made about content or return values The caller is responsible for converting to/from json

Constant Summary collapse

ENDPOINT =

Default host. Can be overwritten with *endpoint: ‘/some/endpoint’ When creating the Connection

'https://api.infermedica.com'

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Connection

Returns a new instance of Connection.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/infermedica/connection.rb', line 15

def initialize(args)
  raise ArgumentError, 
  'Infermedica::Connection::initialize argument needs to be a Hash' unless
    args.is_a?(Hash)
  raise ArgumentError, 'api_id is required' unless args.key?(:api_id)
  raise ArgumentError, 'api_key is required' unless args.key?(:api_key)
  args[:endpoint] = ENDPOINT unless args.key?(:endpoint)

  # Probably need more argument validation here...
  args.each do |k, v|
    instance_variable_set(:"@#{k}", v)
  end

  uri = URI.parse(@endpoint)
  @http = Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = true

  @headers = {
    'App-Id':  @api_id,
    'App-Key': @api_key,
  }
  @headers['Dev-Mode'] = args[:dev_mode] if args.key?(:dev_mode)
end

Instance Method Details

#get(path) ⇒ Object

Send a get request to the given path



42
43
44
45
46
# File 'lib/infermedica/connection.rb', line 42

def get(path)
  # TODO: do more hardening on the path
  request = Net::HTTP::Get.new('/v2' + path, @headers)
  send_request(request)
end

#post(path, json, params = {}) ⇒ Object

Send a post request to the given path json is the data to be passed to the post command params are additional header entries



52
53
54
55
56
57
58
59
60
# File 'lib/infermedica/connection.rb', line 52

def post(path, json, params = {})
  request = Net::HTTP::Post.new('/v2' + path, @headers)
  request.add_field('Content-Type', 'application/json')
  request.body = json
  params.each do |k, v|
    request[k] = v
  end
  send_request(request)
end