Class: Shark::Client::Connection
- Inherits:
-
Object
- Object
- Shark::Client::Connection
- Defined in:
- lib/shark/client/connection.rb
Instance Attribute Summary collapse
-
#connection_options ⇒ Object
readonly
Returns the value of attribute connection_options.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #run(request_action, path, params: {}, headers: {}, body: nil) ⇒ Faraday::Response (also: #request) private
- #use(middleware, *args, &block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/shark/client/connection.rb', line 8 def initialize( = {}) @site = .fetch(:site) @connection_options = { headers: [:headers] || {}, params: [:params] || {} } @connection = Faraday.new do |conn| conn.use Shark::Middleware::ComposeRequest conn.use Shark::Middleware::Status conn.response :json, content_type: /\bjson$/ conn.adapter Faraday.default_adapter end end |
Instance Attribute Details
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
6 7 8 |
# File 'lib/shark/client/connection.rb', line 6 def @connection_options end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
6 7 8 |
# File 'lib/shark/client/connection.rb', line 6 def site @site end |
Instance Method Details
#run(request_action, path, params: {}, headers: {}, body: nil) ⇒ Faraday::Response Also known as: request
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/shark/client/connection.rb', line 33 def run(request_action, path, params: {}, headers: {}, body: nil) raise ArgumentError, 'Configuration :site cannot be nil' if site.blank? raise ArgumentError, 'Parameter :path cannot be nil' if path.blank? url = File.join(site, path) request_headers = .merge(headers || {}) request_params = .merge(params || {}) if Shark.service_token.present? request_headers['Authorization'] = "Bearer #{Shark.service_token}" end @connection.send(request_action) do |request| request.url(url) request.headers.merge!(request_headers) request.body = body request.params.merge!(request_params) end end |
#use(middleware, *args, &block) ⇒ Object
23 |
# File 'lib/shark/client/connection.rb', line 23 def use(middleware, *args, &block); end |