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
21
# 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.use ::FaradayMiddleware::Gzip
    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



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

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

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



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

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


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

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