Sha256: 18ac754b7f546e4a71ac79748ab00de764299188020ab8b34b7ec46aa6294177
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
require 'faraday_middleware' require 'delhivery/faraday/raise_http_errors' require 'logger' require 'null_logger' module Delhivery class Connection 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 def log @logger end def get(route, params={}, headers={}) params = params.delete_if {|key, value| value.nil? } run_request(:get, build_url(route, params), nil, headers) end def build_url(route, params) @connection.build_url(route, params) end def post(route, body=nil, headers={}) run_request(:post, route, body, headers) end def put(route, body=nil, headers={}) run_request(:put, route, body, headers) end def delete(route, body=nil, headers={}) run_request(:delete, route, body, headers) end 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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
delhivery-0.1.1 | lib/delhivery/connection.rb |