Class: Shippo::API::Request
- Inherits:
-
Object
- Object
- Shippo::API::Request
- 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
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parsed_response ⇒ Object
Result of the execute method is stored in #response and #parsed_response.
-
#password ⇒ Object
Returns the value of attribute password.
-
#redirection_history ⇒ Object
Result of the execute method is stored in #response and #parsed_response.
-
#response ⇒ Object
Result of the execute method is stored in #response and #parsed_response.
-
#url ⇒ Object
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(method:, uri:, params: {}, headers: {}) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(method:, uri:, params: {}, headers: {}) ⇒ Request
Returns a new instance of Request.
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
#headers ⇒ Object
Returns the value of attribute headers.
35 36 37 |
# File 'lib/shippo/api/request.rb', line 35 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
35 36 37 |
# File 'lib/shippo/api/request.rb', line 35 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
35 36 37 |
# File 'lib/shippo/api/request.rb', line 35 def params @params end |
#parsed_response ⇒ Object
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 |
#password ⇒ Object
Returns the value of attribute password.
34 35 36 |
# File 'lib/shippo/api/request.rb', line 34 def password @password end |
#redirection_history ⇒ Object
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 |
#response ⇒ Object
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 |
#url ⇒ Object
Returns the value of attribute url.
35 36 37 |
# File 'lib/shippo/api/request.rb', line 35 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
34 35 36 |
# File 'lib/shippo/api/request.rb', line 34 def username @username end |
Instance Method Details
#execute ⇒ Object
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.) 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.) end rescue ::JSON::JSONError, ::JSON::ParserError => e raise Shippo::Exceptions::InvalidJsonError.new(e.) rescue ::RestClient::BadRequest => e raise Shippo::Exceptions::InvalidInputError.new(e.inspect) rescue ::RestClient::Exception => e raise Shippo::Exceptions::ConnectionError.new((url, e)) rescue StandardError => e raise Shippo::Exceptions::ConnectionError.new((url, e)) if e. =~ /TCP|connection|getaddrinfo/ STDERR.puts "#{self.class.name}: Internal error occurred while connecting to #{url}: #{e.}" STDERR.puts 'Stack Trace' STDERR.puts e.backtrace.join("\n") raise Shippo::Exceptions::Error.new(e) end self.parsed_response end |