Class: Postmen::Response
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Postmen::Response
- Defined in:
- lib/postmen/response.rb
Overview
This class parses the HTTP response and checks if it was successfull.
Instance Method Summary collapse
-
#api_rate_limit ⇒ Object
Returns current API rate limit.
-
#api_rate_limit_reset ⇒ Integer
Returns Unix timestamp when rate limit will be reset.
-
#api_rate_limit_reset_at ⇒ Time
Return time when rate limit will be reset.
-
#data ⇒ Hash
Holds the data.
-
#meta ⇒ Hash
Holds the meta data.
-
#parse_response! ⇒ Object
Parses response.
-
#parsed_response ⇒ Hash
Parses the json response.
-
#rate_limit_exceeded? ⇒ Boolean
Checks if rate limit was exceeded.
-
#remaining_api_calls ⇒ Object
Returns number of remaining API calls.
-
#success? ⇒ Boolean
Checks if response were successfull.
Instance Method Details
#api_rate_limit ⇒ Object
Returns current API rate limit
48 49 50 |
# File 'lib/postmen/response.rb', line 48 def api_rate_limit Integer(headers['X-RateLimit-Limit']) end |
#api_rate_limit_reset ⇒ Integer
Returns Unix timestamp when rate limit will be reset.
54 55 56 |
# File 'lib/postmen/response.rb', line 54 def api_rate_limit_reset Integer(headers['X-RateLimit-Reset']) end |
#api_rate_limit_reset_at ⇒ Time
Return time when rate limit will be reset.
60 61 62 |
# File 'lib/postmen/response.rb', line 60 def api_rate_limit_reset_at Time.at(api_rate_limit_reset) end |
#data ⇒ Hash
Holds the data
22 23 24 |
# File 'lib/postmen/response.rb', line 22 def data @data ||= parsed_response[:data] end |
#meta ⇒ Hash
Holds the meta data
15 16 17 |
# File 'lib/postmen/response.rb', line 15 def ||= parsed_response[:meta] end |
#parse_response! ⇒ Object
Parses response. Ensures that rate limit was not exceeded, and checks if resource was found
7 8 9 10 |
# File 'lib/postmen/response.rb', line 7 def parse_response! ensure_rate_limit! ensure_resource_found! end |
#parsed_response ⇒ Hash
Parses the json response
28 29 30 |
# File 'lib/postmen/response.rb', line 28 def parsed_response @parsed_response ||= JSON.parse(body, symbolize_names: true) end |
#rate_limit_exceeded? ⇒ Boolean
Checks if rate limit was exceeded
38 39 40 |
# File 'lib/postmen/response.rb', line 38 def rate_limit_exceeded? code == 429 end |
#remaining_api_calls ⇒ Object
Returns number of remaining API calls
43 44 45 |
# File 'lib/postmen/response.rb', line 43 def remaining_api_calls Integer(headers['X-RateLimit-Remaining']) end |
#success? ⇒ Boolean
Checks if response were successfull
33 34 35 |
# File 'lib/postmen/response.rb', line 33 def success? [:code] == 200 end |