Class: Sdk4me::Response
- Inherits:
-
Object
- Object
- Sdk4me::Response
- Defined in:
- lib/sdk4me/client/response.rb
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
(also: #raw)
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#[](*keys) ⇒ Object
retrieve a value from the resource if the JSON value is an Array a array with the value for each resource will be given.
- #body ⇒ Object
-
#current_page ⇒ Object
pagination - current page.
-
#empty? ⇒ Boolean
true
if the server did not respond at all. -
#failure? ⇒ Boolean
true
in case of a HTTP 5xx error. -
#initialize(request, response) ⇒ Response
constructor
A new instance of Response.
-
#json ⇒ Object
The JSON value, if single resource is queried this is a Hash, if multiple resources where queried it is an Array If the response is not
valid?
it is a Hash with ‘message’ and optionally ‘errors’. -
#message ⇒ Object
the error message in case the response is not
valid?
. -
#pagination_link(relation) ⇒ Object
pagination urls (full paths with server) - relations :first, :prev Link: <api.4me.com/v1/requests?page=1&per_page=25>; rel=“first”, <api.4me.com/v1/requests?page=2&per_page=25>; rel=“prev”, etc.
-
#pagination_relative_link(relation) ⇒ Object
pagination urls (relative paths without server) - relations :first, :prev, :next.
-
#per_page ⇒ Object
pagination - per page.
- #retry_after ⇒ Object
-
#size ⇒ Object
(also: #count)
The nr of resources found.
-
#throttled? ⇒ Boolean
true
if the response is invalid because of throttling. - #to_s ⇒ Object
-
#total_entries ⇒ Object
pagination - total entries.
-
#total_pages ⇒ Object
pagination - total pages.
-
#valid? ⇒ Boolean
(also: #success?)
true
if no ‘message’ is given (and the JSON could be parsed).
Constructor Details
#initialize(request, response) ⇒ Response
Returns a new instance of Response.
6 7 8 9 |
# File 'lib/sdk4me/client/response.rb', line 6 def initialize(request, response) @request = request @response = response end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
3 4 5 |
# File 'lib/sdk4me/client/response.rb', line 3 def request @request end |
#response ⇒ Object (readonly) Also known as: raw
Returns the value of attribute response.
3 4 5 |
# File 'lib/sdk4me/client/response.rb', line 3 def response @response end |
Instance Method Details
#[](*keys) ⇒ Object
retrieve a value from the resource if the JSON value is an Array a array with the value for each resource will be given
65 66 67 68 69 |
# File 'lib/sdk4me/client/response.rb', line 65 def[](*keys) values = json.is_a?(Array) ? json : [json] keys.each { |key| values = values.map { |value| value.is_a?(Hash) ? value[key] : nil } } json.is_a?(Array) ? values : values.first end |
#body ⇒ Object
11 12 13 |
# File 'lib/sdk4me/client/response.rb', line 11 def body @response.body end |
#current_page ⇒ Object
pagination - current page
87 88 89 |
# File 'lib/sdk4me/client/response.rb', line 87 def current_page @current_page ||= @response.header['X-Pagination-Current-Page'].to_i end |
#empty? ⇒ Boolean
true
if the server did not respond at all
47 48 49 |
# File 'lib/sdk4me/client/response.rb', line 47 def empty? @response.body.blank? end |
#failure? ⇒ Boolean
true
in case of a HTTP 5xx error
58 59 60 |
# File 'lib/sdk4me/client/response.rb', line 58 def failure? !success? && (@response.code.to_s.blank? || @response.code.to_s =~ /5\d\d/) end |
#json ⇒ Object
The JSON value, if single resource is queried this is a Hash, if multiple resources where queried it is an Array If the response is not valid?
it is a Hash with ‘message’ and optionally ‘errors’
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sdk4me/client/response.rb', line 17 def json return @json if defined?(@json) # no content, no JSON if @response.code.to_s == '204' data = {} elsif @response.body.blank? # no body, no json data = { message: @response..blank? ? 'empty body' : @response..strip } end begin data ||= JSON.parse(@response.body) rescue StandardError => e data = { message: "Invalid JSON - #{e.} for:\n#{@response.body}" } end # indifferent access to hashes data = data.is_a?(Array) ? data.map(&:with_indifferent_access) : data.with_indifferent_access # empty OK response is not seen as an error data = {} if data.is_a?(Hash) && data.size == 1 && data[:message] == 'OK' # prepend HTTP response code to message data[:message] = "#{response.code}: #{data[:message]}" unless @response.is_a?(Net::HTTPSuccess) @json = data end |
#message ⇒ Object
the error message in case the response is not valid?
42 43 44 |
# File 'lib/sdk4me/client/response.rb', line 42 def @message ||= json.is_a?(Hash) ? json[:message] : nil end |
#pagination_link(relation) ⇒ Object
pagination urls (full paths with server) - relations :first, :prev Link: <api.4me.com/v1/requests?page=1&per_page=25>; rel=“first”, <api.4me.com/v1/requests?page=2&per_page=25>; rel=“prev”, etc.
103 104 105 106 107 108 109 |
# File 'lib/sdk4me/client/response.rb', line 103 def pagination_link(relation) # split on ',' select the [url] in '<[url]>; rel="[relation]"', compact to all url's found (at most one) and take the first (@pagination_links ||= {})[relation] ||= @response.header['Link'] && @response.header['Link'] .split(/,\s*(?:<|$)/) .map { |link| link[/^\s*<?(.*?)>?;\s*rel="#{relation}"\s*$/, 1] }.compact.first end |
#pagination_relative_link(relation) ⇒ Object
pagination urls (relative paths without server) - relations :first, :prev, :next
112 113 114 |
# File 'lib/sdk4me/client/response.rb', line 112 def pagination_relative_link(relation) (@pagination_relative_links ||= {})[relation] ||= pagination_link(relation) && pagination_link(relation)[%r{^https?://[^/]*(.*)}, 1] end |
#per_page ⇒ Object
pagination - per page
82 83 84 |
# File 'lib/sdk4me/client/response.rb', line 82 def per_page @per_page ||= @response.header['X-Pagination-Per-Page'].to_i end |
#retry_after ⇒ Object
121 122 123 |
# File 'lib/sdk4me/client/response.rb', line 121 def retry_after @retry_after ||= @response.header['Retry-After'].to_i end |
#size ⇒ Object Also known as: count
The nr of resources found
72 73 74 75 76 77 78 |
# File 'lib/sdk4me/client/response.rb', line 72 def size @size ||= if 0 else json.is_a?(Array) ? json.size : 1 end end |
#throttled? ⇒ Boolean
true
if the response is invalid because of throttling
117 118 119 |
# File 'lib/sdk4me/client/response.rb', line 117 def throttled? !!(@response.code.to_s == '429' || ( && =~ /Too Many Requests/)) end |
#to_s ⇒ Object
125 126 127 |
# File 'lib/sdk4me/client/response.rb', line 125 def to_s valid? ? json.to_s : end |
#total_entries ⇒ Object
pagination - total entries
97 98 99 |
# File 'lib/sdk4me/client/response.rb', line 97 def total_entries @total_entries ||= @response.header['X-Pagination-Total-Entries'].to_i end |
#total_pages ⇒ Object
pagination - total pages
92 93 94 |
# File 'lib/sdk4me/client/response.rb', line 92 def total_pages @total_pages ||= @response.header['X-Pagination-Total-Pages'].to_i end |
#valid? ⇒ Boolean Also known as: success?
true
if no ‘message’ is given (and the JSON could be parsed)
52 53 54 |
# File 'lib/sdk4me/client/response.rb', line 52 def valid? .nil? end |