Class: EasyPost::Connection
- Inherits:
-
Struct
- Object
- Struct
- EasyPost::Connection
- Defined in:
- lib/easypost/connection.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#call(method, path, api_key = nil, body = nil) ⇒ Hash
Make an HTTP request with Ruby’s Net::HTTP.
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config
3 4 5 |
# File 'lib/easypost/connection.rb', line 3 def config @config end |
#uri ⇒ Object
Returns the value of attribute uri
3 4 5 |
# File 'lib/easypost/connection.rb', line 3 def uri @uri end |
Instance Method Details
#call(method, path, api_key = nil, body = nil) ⇒ Hash
Make an HTTP request with Ruby’s Net::HTTP
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/easypost/connection.rb', line 12 def call(method, path, api_key = nil, body = nil) raise EasyPost::Errors::MissingParameterError.new('api_key') if api_key.nil? connection = if config[:proxy] proxy_uri = URI(config[:proxy]) Net::HTTP.new( uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password, ) else Net::HTTP.new(uri.host, uri.port) end connection.use_ssl = true config.each do |name, value| # Discrepancies between RestClient and Net::HTTP. case name when :verify_ssl name = :verify_mode when :timeout name = :read_timeout end # Handled in the creation of the client. if name == :proxy next end connection.public_send("#{name}=", value) end request = Net::HTTP.const_get(method.capitalize).new(path) request.body = JSON.dump(EasyPost::InternalUtilities.objects_to_ids(body)) if body EasyPost.default_headers.each_pair { |h, v| request[h] = v } request['Authorization'] = EasyPost.(api_key) response = connection.request(request) response_is_json = response['Content-Type'] ? response['Content-Type'].start_with?('application/json') : false EasyPost.parse_response( status: response.code.to_i, body: response.body, json: response_is_json, ) end |