Class: JSONAPI::Consumer::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/consumer/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
# File 'lib/jsonapi/consumer/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))
  @faraday = Faraday.new(site, connection_options) do |builder|
    builder.request :json
    builder.use Middleware::JsonRequest
    builder.use Middleware::Status
    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/jsonapi/consumer/connection.rb', line 4

def faraday
  @faraday
end

Instance Method Details

#delete(middleware) ⇒ Object



27
28
29
# File 'lib/jsonapi/consumer/connection.rb', line 27

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

#run(request_method, path, params = {}, headers = {}) ⇒ Object



31
32
33
# File 'lib/jsonapi/consumer/connection.rb', line 31

def run(request_method, path, params = {}, headers = {})
  faraday.send(request_method, path, params, headers)
end

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

insert middleware before ParseJson - middleware executed in reverse order -

inserted middleware will run after json parsed


22
23
24
25
# File 'lib/jsonapi/consumer/connection.rb', line 22

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