Class: Dina::EndpointConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/dina/endpoints/endpoint_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ EndpointConnection

Returns a new instance of EndpointConnection.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dina/endpoints/endpoint_connection.rb', line 7

def initialize(options = {})
  connection_options = options.slice(:proxy, :ssl, :request, :headers, :params)
  adapter_options = Array(options.fetch(:adapter, Faraday.default_adapter))

  @faraday = Faraday.new(Dina.config.endpoint_url, connection_options) do |builder|
    builder.request :json
    builder.response :json
    builder.adapter(*adapter_options)
  end
  yield(self) if block_given?
end

Instance Attribute Details

#faradayObject (readonly)

Returns the value of attribute faraday.



5
6
7
# File 'lib/dina/endpoints/endpoint_connection.rb', line 5

def faraday
  @faraday
end

Instance Method Details

#custom_headersObject



19
20
21
# File 'lib/dina/endpoints/endpoint_connection.rb', line 19

def custom_headers
  { content_type: "application/json", authorization: Dina.header }
end

#run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/dina/endpoints/endpoint_connection.rb', line 23

def run(request_method, path, params: nil, headers: {}, body: nil)
  response = faraday.run_request(request_method, path, body, headers) do |request|
    request.params.update(params) if params
    request.headers = custom_headers
  end
  response.body
end

#use(*args) ⇒ Object



31
# File 'lib/dina/endpoints/endpoint_connection.rb', line 31

def use(*args); end