Class: ShiprocketAPI::Connection
- Inherits:
-
ActiveResource::Connection
- Object
- ActiveResource::Connection
- ShiprocketAPI::Connection
- Defined in:
- lib/shiprocket_api/connection.rb
Instance Method Summary collapse
-
#get(path, headers = {}, body: nil) ⇒ Object
override #get to handle get with body.
-
#handle_response(response) ⇒ Object
Handles response and error codes from the remote service.
- #request(method, path, *arguments) ⇒ Object
Instance Method Details
#get(path, headers = {}, body: nil) ⇒ Object
override #get to handle get with body
4 5 6 7 8 9 10 11 |
# File 'lib/shiprocket_api/connection.rb', line 4 def get(path, headers = {}, body: nil) arguments = if body.nil? [path, build_request_headers(headers, :get, site.merge(path))] else [path, body, build_request_headers(headers, :get, site.merge(path))] end with_auth { request(:get, *arguments) } end |
#handle_response(response) ⇒ Object
Handles response and error codes from the remote service.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shiprocket_api/connection.rb', line 33 def handle_response(response) body = JSON.parse(response.body) if response.code.to_i == 200 && (body['message'] || body.dig('payload', 'error_message')) raise CreationError, response else super end rescue JSON::ParserError super end |
#request(method, path, *arguments) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/shiprocket_api/connection.rb', line 13 def request(method, path, *arguments) result = ActiveSupport::Notifications.instrument('request.active_resource') do |payload| payload[:method] = method payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}" payload[:result] = if method == :get && arguments.length == 2 HTTP.headers(arguments[1].symbolize_keys) .get(payload[:request_uri], json: arguments[0]) else http.send(method, path, *arguments) end payload[:result] end handle_response(result) rescue Timeout::Error => e raise ActiveResource::TimeoutError, e. rescue OpenSSL::SSL::SSLError => e raise ActiveResource::SSLError, e. end |