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



82
83
84
85
86
87
# File 'lib/brine/requester.rb', line 82

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/brine/requester.rb', line 64

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



50
51
52
53
# File 'lib/brine/requester.rb', line 50

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

#with_oauth2_token(&block) ⇒ Object



55
56
57
58
# File 'lib/brine/requester.rb', line 55

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