Class: Shark::Client::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @site = options.fetch(:site)
  @connection_options = {
    headers: options[:headers] || {},
    params: options[: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_optionsObject (readonly)

Returns the value of attribute connection_options.



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

def connection_options
  @connection_options
end

#siteObject (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.

Parameters:

  • request_action (Symbol)

    One of :get, :post, :put, :patch, :delete.

  • path (String)

    The url path

  • options (Hash)
    • params [Hash] The request params

    • headers [Hash] The request headers

    • body [Hash]

Returns:

  • (Faraday::Response)

Raises:

  • (ArgumentError)


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 = connection_options_headers.merge(headers || {})
  request_params = connection_options_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