Class: JsonApiClient::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.

Yields:

  • (_self)

Yield Parameters:


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/json_api_client/connection.rb', line 6

def initialize(options = {})
  site = options.fetch(:site)
  connection_options = options.slice(:proxy, :ssl, :request, :headers, :params)
  adapter_options = Array(options.fetch(:adapter, Faraday.default_adapter))
  status_middleware_options = {}
  status_middleware_options[:custom_handlers] = options[:status_handlers] if options[:status_handlers].present?
  @faraday = Faraday.new(site, connection_options) do |builder|
    builder.request :json
    builder.use Middleware::JsonRequest
    builder.use Middleware::Status, status_middleware_options
    builder.use Middleware::ParseJson
    builder.adapter(*adapter_options)
  end
  yield(self) if block_given?
end

Instance Attribute Details

#faradayObject (readonly)

Returns the value of attribute faraday


4
5
6
# File 'lib/json_api_client/connection.rb', line 4

def faraday
  @faraday
end

Instance Method Details

#delete(middleware) ⇒ Object


29
30
31
# File 'lib/json_api_client/connection.rb', line 29

def delete(middleware)
  faraday.builder.delete(middleware)
end

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


33
34
35
36
37
# File 'lib/json_api_client/connection.rb', line 33

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

#use(middleware, *args, &block) ⇒ Object

insert middleware before ParseJson - middleware executed in reverse order -

inserted middleware will run after json parsed

24
25
26
27
# File 'lib/json_api_client/connection.rb', line 24

def use(middleware, *args, &block)
  return if faraday.builder.locked?
  faraday.builder.insert_before(Middleware::ParseJson, middleware, *args, &block)
end