Class: AxTrack::Client

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

Constant Summary collapse

APIKeyMissing =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, adapter: nil, stubs: nil) ⇒ Client

Returns a new instance of Client.

Raises:



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

def initialize(api_key: nil, adapter: nil, stubs: nil)
  @api_key  = api_key&.strip || ENV['AXTRACK_API_KEY']&.strip
  @adapter  = adapter || Faraday.default_adapter
  @stubs    = stubs

  raise APIKeyMissing, "No API key provided" if !defined?(api_key) || api_key.nil? ||  api_key.empty?
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



9
10
11
# File 'lib/ax_track/client.rb', line 9

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/ax_track/client.rb', line 9

def api_key
  @api_key
end

#connectionObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/ax_track/client.rb', line 29

def connection
  @connection ||= Faraday.new(AxTrack.base_url_api) do |conn|
    #conn.request :url_encoded
    conn.request :json
    conn.response :json, content_type: 'application/json'
    conn.adapter adapter, @stubs
    conn.headers['Authorization'] = "Token #{api_key}" unless api_key.empty?
    conn.options.timeout = 20
  end
end

Instance Method Details

#assetsObject



25
26
27
# File 'lib/ax_track/client.rb', line 25

def assets
  AssetResource.new(self)
end

#trackersObject



20
21
22
# File 'lib/ax_track/client.rb', line 20

def trackers
  TrackerResource.new(self)
end