Class: Postmen::Response

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/postmen/response.rb

Overview

This class parses the HTTP response and checks if it was successfull.

Instance Method Summary collapse

Instance Method Details

#api_rate_limitObject

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_resetInteger

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_atTime

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

#dataHash

Holds the data

See Also:



22
23
24
# File 'lib/postmen/response.rb', line 22

def data
  @data ||= parsed_response[:data]
end

#metaHash

Holds the meta data

See Also:



15
16
17
# File 'lib/postmen/response.rb', line 15

def meta
  @meta ||= 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_responseHash

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_callsObject

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?
  meta[:code] == 200
end