Module: ClientBuilding

Included in:
ClientBuilder, Requesting
Defined in:
lib/brine/requester.rb

Overview

Construct a Faraday client to be used to send built requests

Instance Method Summary collapse

Instance Method Details

#client_for_host(host, logging: ) ⇒ Object



57
58
59
60
61
62
# File 'lib/brine/requester.rb', line 57

def client_for_host(host, logging: ENV['BRINE_LOG_HTTP'])
  @logging = logging
  Faraday.new(host) do |conn|
    connection_handlers.each{|h| h.call(conn) }
  end
end

#connection_handlersObject

This is represented as list of functions so that it can be more easily customized for unexpected use cases. It should likely be broken up a bit more sensibly and more useful insertion commands added… but it’s likely enough of a power feature and platform specific to leave pretty raw.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/brine/requester.rb', line 39

def connection_handlers
  @connection_handlers ||= [
    proc do |conn|
      conn.request :json
      if @oauth2
        conn.request :oauth2, @oauth2.token, :token_type => @oauth2.token_type
      end
    end,
    proc do |conn|
      if @logging
        conn.response :logger, nil, :bodies => (@logging.casecmp('DEBUG') == 0)
      end
      conn.response :json, :content_type => /\bjson$/
    end,
    proc{|conn| conn.adapter Faraday.default_adapter }
  ]
end

#use_oauth2_token(&block) ⇒ Object

authenticate using provided info and save token for use in later requests



25
26
27
28
# File 'lib/brine/requester.rb', line 25

def use_oauth2_token(&block)
  @oauth2 = OAuth2Params.new
  @oauth2.instance_eval(&block)
end

#with_oauth2_token(&block) ⇒ Object



30
31
32
33
# File 'lib/brine/requester.rb', line 30

def with_oauth2_token(&block)
  use_oauth2_token(&block)
  self
end