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.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/after_ship/core/request.rb', line 117 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 |
# 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 end |
#typhoeus_request ⇒ Typhoeus::Request (protected)
Make the ‘Typhoeus::Request` to be run later.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/after_ship/core/request.rb', line 97 def typhoeus_request 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.
90 91 92 |
# File 'lib/after_ship/core/request.rb', line 90 def typhoeus_response @request.run end |