Class: Delhivery::Connection
- Inherits:
-
Object
- Object
- Delhivery::Connection
- Defined in:
- lib/delhivery/connection.rb
Instance Method Summary collapse
- #build_url(route, params) ⇒ Object
- #delete(route, body = nil, headers = {}) ⇒ Object
- #get(route, params = {}, headers = {}) ⇒ Object
-
#initialize(opts) ⇒ Connection
constructor
A new instance of Connection.
- #log ⇒ Object
- #post(route, body = nil, headers = {}) ⇒ Object
- #put(route, body = nil, headers = {}) ⇒ Object
- #run_request(method, route, body, headers) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/delhivery/connection.rb', line 8 def initialize(opts) @token = opts[:api_key] base_url = opts[:production] ? "https://track.delhivery.com/" : "https://test.delhivery.com" @logger = ::Logger.new(STDOUT) #@logger = opts[:logger] || NullLogger.instance @connection = ::Faraday.new(base_url) do |conn| conn.response :logger, @logger, bodies: true conn.request :json conn.request :url_encoded conn.response :mashify conn.response :json, :content_type => /\bjson$/ conn.use Delhivery::Faraday::Response::RaiseHttpError conn.adapter ::Faraday.default_adapter end end |
Instance Method Details
#build_url(route, params) ⇒ Object
33 34 35 |
# File 'lib/delhivery/connection.rb', line 33 def build_url(route, params) @connection.build_url(route, params) end |
#delete(route, body = nil, headers = {}) ⇒ Object
45 46 47 |
# File 'lib/delhivery/connection.rb', line 45 def delete(route, body=nil, headers={}) run_request(:delete, route, body, headers) end |
#get(route, params = {}, headers = {}) ⇒ Object
28 29 30 31 |
# File 'lib/delhivery/connection.rb', line 28 def get(route, params={}, headers={}) params = params.delete_if {|key, value| value.nil? } run_request(:get, build_url(route, params), nil, headers) end |
#log ⇒ Object
24 25 26 |
# File 'lib/delhivery/connection.rb', line 24 def log @logger end |
#post(route, body = nil, headers = {}) ⇒ Object
37 38 39 |
# File 'lib/delhivery/connection.rb', line 37 def post(route, body=nil, headers={}) run_request(:post, route, body, headers) end |
#put(route, body = nil, headers = {}) ⇒ Object
41 42 43 |
# File 'lib/delhivery/connection.rb', line 41 def put(route, body=nil, headers={}) run_request(:put, route, body, headers) end |
#run_request(method, route, body, headers) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/delhivery/connection.rb', line 49 def run_request(method, route, body, headers) default_headers = { 'Authorization' => "Token #{@token}", 'Accept' => 'application/json', 'Content-Type': 'application/json'}.freeze response = {} response = @connection.run_request( method, route, body, default_headers.merge(headers) ) log.debug(response.body) response end |