class AfterShip::Request
Gather necessary pieces, assemble a `Typhoeus::Request`, run that, get a `Typhoeus::Response`, parse that, check for errors, return the parse JSON.
Public Class Methods
Shorthand for GET request:
request = Request.new(url: '...', api_key: '...', method: :get) response = request.response
@param options [Hash] @option options :api_key [String] Your API key. @option options :url [String] The full endpoint URL. @option options :body [Hash, nil] Body for the request as a hash.
@return [Hash]
# File lib/after_ship/core/request.rb, line 17 def self.get(options, &block) options[:method] = :get new(options, &block).response end
Prepare the request to be run later.
@param options [Hash] @option options :api_key [String] Your API key. @option options :url [String] The full endpoint URL. @option options :method [Symbol] The HTTP method. @option options :body [Hash, nil] Body for the request as a hash. @param block [Proc] Response modifier callback.
# File lib/after_ship/core/request.rb, line 62 def initialize(options = {}, &block) @api_key = options.fetch(:api_key) @url = options.fetch(:url) @method = options.fetch(:method) @body = options[:body] @request = typhoeus_request @block = block end
Shorthand for POST request:
request = Request.new(url: '...', api_key: '...', method: :post) response = request.response
@param options [Hash] @option options :api_key [String] Your API key. @option options :url [String] The full endpoint URL. @option options :body [Hash, nil] Body for the request as a hash.
@return [Hash]
# File lib/after_ship/core/request.rb, line 33 def self.post(options, &block) options[:method] = :post new(options, &block).response end
Shorthand for PUT request:
request = Request.new(url: '...', api_key: '...', method: :put) response = request.response
@param options [Hash] @option options :api_key [String] Your API key. @option options :url [String] The full endpoint URL. @option options :body [Hash, nil] Body for the request as a hash.
@return [Hash]
# File lib/after_ship/core/request.rb, line 49 def self.put(options, &block) options[:method] = :put new(options, &block).response end
Public Instance Methods
Do the request to the server and handle the response.
# File lib/after_ship/core/request.rb, line 72 def response response = typhoeus_response ErrorHandler.precheck(response) parsed_body = MultiJson.load(response.body, JSON_OPTIONS) ErrorHandler.check(parsed_body.fetch(:meta)) if @block @block.call(parsed_body) else parsed_body end rescue MultiJson::ParseError => e logger = Logger.new($stdout) logger.error("#{e.class}: #{e.message}") if response logger.error('Response body:') logger.error(response.body.inspect) end raise Error::InternalError, 'AfterShip internal error, please try again.' end
Protected Instance Methods
Print the low level cURL internals in the console as well as the request body and response body when it's available.
@param request [Typhoeus::Request]
# File lib/after_ship/core/request.rb, line 127 def make_verbose(request) request.options[:verbose] = true request.on_complete do |response| puts puts 'Request body:' puts request.options[:body] puts puts 'Response body:' puts response.body puts end end
Make the `Typhoeus::Request` to be run later.
@return [Typhoeus::Request]
# File lib/after_ship/core/request.rb, line 107 def typhoeus_request # rubocop:disable Metrics/MethodLength request = Typhoeus::Request.new( @url, method: @method, headers: { 'aftership-api-key' => @api_key, 'Content-Type' => 'application/json' } ) request.options[:body] = MultiJson.dump(@body) if @body make_verbose(request) if AfterShip.debug request end
Run the `Typhoeus::Request` and return the response.
@return [Typhoeus::Request]
# File lib/after_ship/core/request.rb, line 100 def typhoeus_response @request.run end