Class: RouteNGNClient::Connection
- Inherits:
-
Object
- Object
- RouteNGNClient::Connection
- Defined in:
- lib/routengn_client/connection.rb
Constant Summary collapse
- USER_AGENT =
"routengn-client v#{RouteNGNClient::VERSION}"
Instance Attribute Summary collapse
-
#faraday ⇒ Object
Returns the value of attribute faraday.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #config_request(request, method, path, params, options) ⇒ Object
- #create_faraday(uri, api_key = nil) ⇒ Object
- #delete(path, params = {}, options = {}, &callback) ⇒ Object
- #get(path, params = {}, options = {}, &callback) ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(path, params = {}, options = {}, &callback) ⇒ Object
- #put(path, params = {}, options = {}, &callback) ⇒ Object
- #request(method, path, params, options, &callback) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 |
# File 'lib/routengn_client/connection.rb', line 8 def initialize( = {}) @options = @faraday = self.create_faraday(@options[:uri], @options[:api_key]) if @options[:uri] end |
Instance Attribute Details
#faraday ⇒ Object
Returns the value of attribute faraday.
6 7 8 |
# File 'lib/routengn_client/connection.rb', line 6 def faraday @faraday end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/routengn_client/connection.rb', line 6 def @options end |
Instance Method Details
#config_request(request, method, path, params, options) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/routengn_client/connection.rb', line 48 def config_request(request, method, path, params, ) case method.to_sym when :delete, :get request.url(path, params) when :post, :put request.path = path request.body = params unless params.empty? end request end |
#create_faraday(uri, api_key = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/routengn_client/connection.rb', line 13 def create_faraday(uri, api_key = nil) @faraday = Faraday.new uri do |c| c.headers['User-Agent'] = USER_AGENT c.request :oauth2, api_key, :param_name => 'api_key' if api_key c.request :multipart c.request :url_encoded c.request :multi_json c.response :xml, :content_type => /\bxml$/ c.response :multi_json, symbolize_keys: true, :content_type => /\bjson$/ c.response :routengn_response c.response :logger, RouteNGNClient.logger c.use :instrumentation c.adapter Faraday.default_adapter end end |
#delete(path, params = {}, options = {}, &callback) ⇒ Object
64 65 66 |
# File 'lib/routengn_client/connection.rb', line 64 def delete(path, params={}, ={}, &callback) request(:delete, path, params, , &callback) end |
#get(path, params = {}, options = {}, &callback) ⇒ Object
60 61 62 |
# File 'lib/routengn_client/connection.rb', line 60 def get(path, params={}, ={}, &callback) request(:get, path, params, , &callback) end |
#post(path, params = {}, options = {}, &callback) ⇒ Object
68 69 70 |
# File 'lib/routengn_client/connection.rb', line 68 def post(path, params={}, ={}, &callback) request(:post, path, params, , &callback) end |
#put(path, params = {}, options = {}, &callback) ⇒ Object
72 73 74 |
# File 'lib/routengn_client/connection.rb', line 72 def put(path, params={}, ={}, &callback) request(:put, path, params, , &callback) end |
#request(method, path, params, options, &callback) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/routengn_client/connection.rb', line 32 def request(method, path, params, , &callback) sent_at = nil response = @faraday.send(method) { |request| sent_at = Time.now request = config_request(request, method, path, params, ) }.on_complete { |env| env[:total_time] = Time.now.utc.to_f - sent_at.utc.to_f if sent_at env[:request_params] = params env[:request_options] = callback.call(env) if callback } response end |