Class: AfterShip::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/after_ship/core/request.rb

Overview

Gather necessary pieces, assemble a ‘Typhoeus::Request`, run that, get a `Typhoeus::Response`, parse that, check for errors, return the parse JSON.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Request

Prepare the request to be run later.

Parameters:

  • options (Hash) (defaults to: {})
  • block (Proc)

    Response modifier callback.

Options Hash (options):

  • :api_key (String)

    Your API key.

  • :url (String)

    The full endpoint URL.

  • :method (Symbol)

    The HTTP method.

  • :body (Hash, nil)

    Body for the request as a hash.



62
63
64
65
66
67
68
69
# 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

Class Method Details

.get(options, &block) ⇒ Hash

Shorthand for GET request:

request  = Request.new(url: '...', api_key: '...', method: :get)
response = request.response

Parameters:

  • options (Hash)

Options Hash (options):

  • :api_key (String)

    Your API key.

  • :url (String)

    The full endpoint URL.

  • :body (Hash, nil)

    Body for the request as a hash.

Returns:

  • (Hash)


17
18
19
20
# File 'lib/after_ship/core/request.rb', line 17

def self.get(options, &block)
  options[:method] = :get
  new(options, &block).response
end

.post(options, &block) ⇒ Hash

Shorthand for POST request:

request  = Request.new(url: '...', api_key: '...', method: :post)
response = request.response

Parameters:

  • options (Hash)

Options Hash (options):

  • :api_key (String)

    Your API key.

  • :url (String)

    The full endpoint URL.

  • :body (Hash, nil)

    Body for the request as a hash.

Returns:

  • (Hash)


33
34
35
36
# 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) ⇒ Hash

Shorthand for PUT request:

request  = Request.new(url: '...', api_key: '...', method: :put)
response = request.response

Parameters:

  • options (Hash)

Options Hash (options):

  • :api_key (String)

    Your API key.

  • :url (String)

    The full endpoint URL.

  • :body (Hash, nil)

    Body for the request as a hash.

Returns:

  • (Hash)


49
50
51
52
# File 'lib/after_ship/core/request.rb', line 49

def self.put(options, &block)
  options[:method] = :put
  new(options, &block).response
end

Instance Method Details

#make_verbose(request) ⇒ Object (protected)

Print the low level cURL internals in the console as well as the request body and response body when it’s available.

Parameters:

  • request (Typhoeus::Request)


127
128
129
130
131
132
133
134
135
136
137
138
139
# 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

#responseObject

Do the request to the server and handle the response.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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::ParseError, e
end

#typhoeus_requestTyphoeus::Request (protected)

Make the ‘Typhoeus::Request` to be run later.

Returns:

  • (Typhoeus::Request)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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_responseTyphoeus::Request (protected)

Run the ‘Typhoeus::Request` and return the response.

Returns:

  • (Typhoeus::Request)


100
101
102
# File 'lib/after_ship/core/request.rb', line 100

def typhoeus_response
  @request.run
end