Class: Brapi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, base_url: nil, timeout: nil, open_timeout: nil, user_agent: nil, adapter: nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(token: nil, base_url: nil, timeout: nil, open_timeout: nil, user_agent: nil, adapter: nil)
  config = Brapi.configuration
  @token = token || config.token
  @base_url = base_url || config.base_url
  @timeout = timeout || config.timeout
  @open_timeout = open_timeout || config.open_timeout
  @user_agent = user_agent || config.user_agent
  @adapter = adapter || config.adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/brapi/client.rb', line 8

def adapter
  @adapter
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/brapi/client.rb', line 8

def base_url
  @base_url
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



8
9
10
# File 'lib/brapi/client.rb', line 8

def open_timeout
  @open_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



8
9
10
# File 'lib/brapi/client.rb', line 8

def timeout
  @timeout
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/brapi/client.rb', line 8

def token
  @token
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



8
9
10
# File 'lib/brapi/client.rb', line 8

def user_agent
  @user_agent
end

Instance Method Details

#availableObject



58
59
60
# File 'lib/brapi/client.rb', line 58

def available
  @available ||= Brapi::Resources::Available.new(self)
end

#connectionObject



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

def connection
  @connection ||= Faraday.new(url: base_url) do |f|
    f.headers["User-Agent"] = user_agent
    f.headers["Authorization"] = "Bearer #{token}" if token && !token.empty?
    f.headers["Accept"] = "application/json"

    f.request :retry,
              max: 2,
              interval: 0.5,
              backoff_factor: 2,
              retry_statuses: [502, 503, 504],
              methods: [:get]
    f.response :json, content_type: /\bjson$/

    f.options.timeout = timeout
    f.options.open_timeout = open_timeout

    if adapter
      f.adapter(*Array(adapter))
    else
      f.adapter Faraday.default_adapter
    end
  end
end

#quoteObject



54
55
56
# File 'lib/brapi/client.rb', line 54

def quote
  @quote ||= Brapi::Resources::Quote.new(self)
end

#request(method, path, params: {}) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/brapi/client.rb', line 45

def request(method, path, params: {})
  response = connection.public_send(method, path) do |req|
    req.params.update(params.compact) if params && !params.empty?
  end
  handle_response(response)
rescue Faraday::TimeoutError, Faraday::ConnectionFailed => e
  raise Brapi::ConnectionError, e.message
end

#v2Object



62
63
64
# File 'lib/brapi/client.rb', line 62

def v2
  @v2 ||= Brapi::Resources::V2.new(self)
end