Class: SpyriApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/spyri_api/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/spyri_api/client.rb', line 6

def initialize(config = {})
  set_config(config)
end

Instance Method Details

#call_api(http_method, path, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spyri_api/client.rb', line 20

def call_api(http_method, path, opts = {})
  headers = {'Content-Type' => "application/json"}
  headers[:Authorization] = "Bearer #{@access_token}"

  conn = Excon.new(@host + path, headers: headers, read_timeout: @read_timeout, write_timeout: @write_timeout, connect_timeout: @connect_timeout)

  res = nil
  case http_method.to_sym.downcase
  when :post, :put, :patch, :delete
    res = conn.request(method: http_method, query: opts[:query_params], body: opts[:body].to_json)
  when :get
    res = conn.get(query: opts[:query_params])
  end

  data = nil
  data = JSON.parse(res.body) if res.status == 200

  if (!data)
    return Error.new(res.status, res.body)
  end

  return data
end

#set_config(config = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/spyri_api/client.rb', line 10

def set_config(config = {})
  @access_token = config[:access_token] || ''
  @host = config[:host] || ''

  timeout = config[:timeout] || 120
  @write_timeout = config[:write_timeout] || timeout
  @read_timeout = config[:read_timeout] || timeout
  @connect_timeout = config[:connect_timeout] || timeout
end