Class: GnipApi::Response
- Inherits:
-
Object
- Object
- GnipApi::Response
- Defined in:
- lib/gnip_api/response.rb
Constant Summary collapse
- OK_STATUSES =
List of codes that are considered OK
[200, 201, 202, 203, 204, 205, 206, 207, 208, 226]
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #check_for_errors! ⇒ Object
- #error_message ⇒ Object
-
#initialize(request, status, body, headers) ⇒ Response
constructor
A new instance of Response.
- #ok? ⇒ Boolean
- #request_method ⇒ Object
- #request_uri ⇒ Object
Constructor Details
#initialize(request, status, body, headers) ⇒ Response
Returns a new instance of Response.
8 9 10 11 12 13 |
# File 'lib/gnip_api/response.rb', line 8 def initialize request, status, body, headers @status = status @body = body @headers = headers @request = request end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/gnip_api/response.rb', line 6 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/gnip_api/response.rb', line 6 def headers @headers end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
6 7 8 |
# File 'lib/gnip_api/response.rb', line 6 def request @request end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
6 7 8 |
# File 'lib/gnip_api/response.rb', line 6 def status @status end |
Instance Method Details
#check_for_errors! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gnip_api/response.rb', line 46 def check_for_errors! if ok? GnipApi.logger.info "#{request_method} request to #{request_uri} returned with status #{status} OK" GnipApi.logger.debug "Headers -> #{headers.inspect}" GnipApi.logger.debug "Body -> #{body.inspect}" GnipApi.logger.debug "Request headers -> #{request.headers.inspect}" GnipApi.logger.debug "Request payload -> #{request.payload.inspect}" else GnipApi.logger.error "#{request_method} request to #{request_uri} returned with status #{status} FAIL" GnipApi.logger.debug "Headers -> #{headers.inspect}" GnipApi.logger.debug "Body -> #{body.inspect}" GnipApi.logger.debug "Request headers -> #{request.headers.inspect}" GnipApi.logger.debug "Request payload -> #{request.payload.inspect}" raise GnipApi::Errors::Adapter::GnipSoftwareError.new if status == 503 raise GnipApi::Errors::Adapter::RateLimitError.new if status == 429 raise GnipApi::Errors::Adapter::InvalidInput.new() if status == 422 raise GnipApi::Errors::Adapter::RequestError.new("Status #{status} #{error_message}") end end |
#error_message ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/gnip_api/response.rb', line 27 def if @body && !@body.empty? parsed = GnipApi::JsonParser.new.parse(@body) if rules_summary? parsed created = parsed["summary"]["created"] failed = parsed["summary"]["not_created"] timstamp = parsed["sent"] log_rule_failures(parsed) return "Invalid rules: created #{created}, failed #{failed} verify and try again" elsif generic_error? parsed = parsed['error']['message'] += " - TID: #{parsed['error']['transactionId']}" if parsed['error']['transactionId'] return else return "Unknown error" end end end |
#ok? ⇒ Boolean
23 24 25 |
# File 'lib/gnip_api/response.rb', line 23 def ok? OK_STATUSES.include? @status end |
#request_method ⇒ Object
15 16 17 |
# File 'lib/gnip_api/response.rb', line 15 def request_method @request.request_method end |
#request_uri ⇒ Object
19 20 21 |
# File 'lib/gnip_api/response.rb', line 19 def request_uri @request.uri end |