Class: AfterShip::Request
- Inherits:
-
Object
- Object
- AfterShip::Request
- 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
-
.get(options, &block) ⇒ Hash
Shorthand for GET request:.
-
.post(options, &block) ⇒ Hash
Shorthand for POST request:.
-
.put(options, &block) ⇒ Hash
Shorthand for PUT request:.
Instance Method Summary collapse
-
#initialize(options = {}, &block) ⇒ Request
constructor
Prepare the request to be run later.
-
#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.
-
#response ⇒ Object
Do the request to the server and handle the response.
-
#typhoeus_request ⇒ Typhoeus::Request
protected
Make the ‘Typhoeus::Request` to be run later.
-
#typhoeus_response ⇒ Typhoeus::Request
protected
Run the ‘Typhoeus::Request` and return the response.
Constructor Details
#initialize(options = {}, &block) ⇒ Request
Prepare the request to be run later.
62 63 64 65 66 67 68 69 |
# File 'lib/after_ship/core/request.rb', line 62 def initialize( = {}, &block) @api_key = .fetch(:api_key) @url = .fetch(:url) @method = .fetch(:method) @body = [:body] @request = typhoeus_request @block = block end |
Class Method Details
.get(options, &block) ⇒ Hash
17 18 19 20 |
# File 'lib/after_ship/core/request.rb', line 17 def self.get(, &block) [:method] = :get new(, &block).response end |
.post(options, &block) ⇒ Hash
33 34 35 36 |
# File 'lib/after_ship/core/request.rb', line 33 def self.post(, &block) [:method] = :post new(, &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.
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.[:verbose] = true request.on_complete do |response| puts puts 'Request body:' puts request.[:body] puts puts 'Response body:' puts response.body puts end end |
#response ⇒ Object
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.}") if response logger.error('Response body:') logger.error(response.body.inspect) end raise Error::ParseError, e end |
#typhoeus_request ⇒ Typhoeus::Request (protected)
Make the ‘Typhoeus::Request` to be run later.
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.[:body] = MultiJson.dump(@body) if @body make_verbose(request) if AfterShip.debug request end |
#typhoeus_response ⇒ Typhoeus::Request (protected)
Run the ‘Typhoeus::Request` and return the response.
100 101 102 |
# File 'lib/after_ship/core/request.rb', line 100 def typhoeus_response @request.run end |