Class: LucidShopify::SendRequest
- Inherits:
-
Object
- Object
- LucidShopify::SendRequest
- Defined in:
- lib/lucid_shopify/send_request.rb
Defined Under Namespace
Classes: NetworkError
Instance Method Summary collapse
-
#call(request, attempts: default_attempts) ⇒ Hash
The parsed response body.
-
#initialize(http: Container[:http], strategy: ->(*, &block) { block.() }) ⇒ SendRequest
constructor
A new instance of SendRequest.
- #log_request(request) ⇒ Object
- #log_response(request, response) ⇒ Object
Constructor Details
#initialize(http: Container[:http], strategy: ->(*, &block) { block.() }) ⇒ SendRequest
Returns a new instance of SendRequest.
18 19 20 21 22 |
# File 'lib/lucid_shopify/send_request.rb', line 18 def initialize(http: Container[:http], strategy: ->(*, &block) { block.() }) @http = http @strategy = strategy end |
Instance Method Details
#call(request, attempts: default_attempts) ⇒ Hash
Returns the parsed response body.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lucid_shopify/send_request.rb', line 34 def call(request, attempts: default_attempts) req = request log_request(req) res = @strategy.(req) do res = send(req) Response.new(req, res.code, res.headers.to_h, res.to_s) end log_response(req, res) res.assert! rescue HTTP::ConnectionError, HTTP::ResponseError, HTTP::TimeoutError => e raise NetworkError.new(e), e. if attempts.zero? call(req, attempts: attempts - 1) end |
#log_request(request) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lucid_shopify/send_request.rb', line 70 def log_request(request) req = request LucidShopify.config.logger.info('<%s> [%i] %s %s %s' % [ self.class.to_s, req.object_id, req.http_method.to_s.upcase, req.url, req.[:params]&.to_json || '{}', ]) end |
#log_response(request, response) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/lucid_shopify/send_request.rb', line 86 def log_response(request, response) req = request res = response LucidShopify.config.logger.info('<%s> [%i] %i (%s)' % [ self.class.to_s, req.object_id, res.status_code, res.headers['X-Shopify-Shop-Api-Call-Limit'], ]) end |