Class: Brapi::Client
- Inherits:
-
Object
- Object
- Brapi::Client
- Defined in:
- lib/brapi/client.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #available ⇒ Object
- #connection ⇒ Object
-
#initialize(token: nil, base_url: nil, timeout: nil, open_timeout: nil, user_agent: nil, adapter: nil) ⇒ Client
constructor
A new instance of Client.
- #quote ⇒ Object
- #request(method, path, params: {}) ⇒ Object
- #v2 ⇒ Object
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
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
8 9 10 |
# File 'lib/brapi/client.rb', line 8 def adapter @adapter end |
#base_url ⇒ Object (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_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
8 9 10 |
# File 'lib/brapi/client.rb', line 8 def open_timeout @open_timeout end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/brapi/client.rb', line 8 def timeout @timeout end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
8 9 10 |
# File 'lib/brapi/client.rb', line 8 def token @token end |
#user_agent ⇒ Object (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
#available ⇒ Object
58 59 60 |
# File 'lib/brapi/client.rb', line 58 def available @available ||= Brapi::Resources::Available.new(self) end |
#connection ⇒ Object
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..timeout = timeout f..open_timeout = open_timeout if adapter f.adapter(*Array(adapter)) else f.adapter Faraday.default_adapter end end end |
#quote ⇒ Object
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. end |