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

get(options, &block) click to toggle source

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
new(options = {}, &block) click to toggle source

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
post(options, &block) click to toggle source

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
put(options, &block) click to toggle source

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

response() click to toggle source

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

make_verbose(request) click to toggle source

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
typhoeus_request() click to toggle source

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
typhoeus_response() click to toggle source

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