Class: Shippo::API::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/shippo/api/request.rb

Overview

This class is the primary internal Interface to the Shippo API.

Public consumers should use Model API, and perform actions on models rather than submit requests directly using this class.

Request instance is created with the intention to execute a single API call and once executed, it stores response object. Used requests can not be re-executed.

Example

@request = Shippo::API::Request.new(
         method: :get,
            uri: '/address,
         params: { object_id: 1 },
        headers: { 'Last-Modified' => '1213145' }
begin
  @response = @request.execute
  Shippo::Address.from(@response)
# =>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method:, uri:, params: {}, headers: {}) ⇒ Request

Returns a new instance of Request.

Parameters:

  • method (symbol)

    :get or any other method such as :put, :post, etc.

  • uri (String)

    URI component appended to the base URL

  • params (Hash) (defaults to: {})

    parameters to append to the URL

  • headers (Hash) (defaults to: {})

    headers hash sent to the server



44
45
46
47
48
49
50
# File 'lib/shippo/api/request.rb', line 44

def initialize(method:, uri:, params: {}, headers: {})
  self.method   = method
  self.params   = params
  self.headers  = headers
  self.url      = api_url(uri)
  self.response = nil
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



35
36
37
# File 'lib/shippo/api/request.rb', line 35

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



35
36
37
# File 'lib/shippo/api/request.rb', line 35

def method
  @method
end

#paramsObject

Returns the value of attribute params.



35
36
37
# File 'lib/shippo/api/request.rb', line 35

def params
  @params
end

#parsed_responseObject

Result of the execute method is stored in #response and #parsed_response



38
39
40
# File 'lib/shippo/api/request.rb', line 38

def parsed_response
  @parsed_response
end

#passwordObject

Returns the value of attribute password.



34
35
36
# File 'lib/shippo/api/request.rb', line 34

def password
  @password
end

#redirection_historyObject

Result of the execute method is stored in #response and #parsed_response



38
39
40
# File 'lib/shippo/api/request.rb', line 38

def redirection_history
  @redirection_history
end

#responseObject

Result of the execute method is stored in #response and #parsed_response



38
39
40
# File 'lib/shippo/api/request.rb', line 38

def response
  @response
end

#urlObject

Returns the value of attribute url.



35
36
37
# File 'lib/shippo/api/request.rb', line 35

def url
  @url
end

#usernameObject

Returns the value of attribute username.



34
35
36
# File 'lib/shippo/api/request.rb', line 34

def username
  @username
end

Instance Method Details

#executeObject

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shippo/api/request.rb', line 52

def execute
  raise ArgumentError.new('Response is already defined, create another Request object.') if self.response
  validate!
  begin
    self.response        = shippo_phone_home
    self.parsed_response = JSON::parse(response.body, { symbolize_names: true })

  rescue ::RestClient::Unauthorized => e
    raise Shippo::Exceptions::AuthenticationError.new(e.message)

  rescue ::RestClient::BadRequest => e
    if e.respond_to?(:response) && e.response.is_a?(RestClient::Response)
      awesome_print_response(e) if Shippo::API.debug?
      raise Shippo::Exceptions::APIServerError.new('Backend responded with an error',
                                                   self, e.response, e.message)
    end

  rescue ::JSON::JSONError, ::JSON::ParserError => e
    raise Shippo::Exceptions::InvalidJsonError.new(e.message)

  rescue ::RestClient::BadRequest => e
    raise Shippo::Exceptions::InvalidInputError.new(e.inspect)

  rescue ::RestClient::Exception => e
    raise Shippo::Exceptions::ConnectionError.new(connection_error_message(url, e))

  rescue StandardError => e
    raise Shippo::Exceptions::ConnectionError.new(connection_error_message(url, e)) if e.message =~ /TCP|connection|getaddrinfo/

    STDERR.puts "#{self.class.name}: Internal error occurred while connecting to #{url}: #{e.message}"
    STDERR.puts 'Stack Trace'
    STDERR.puts e.backtrace.join("\n")
    raise Shippo::Exceptions::Error.new(e)
  end
  self.parsed_response
end