Class: Itrp::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/itrp/client/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ Response

Returns a new instance of Response.



3
4
5
6
# File 'lib/itrp/client/response.rb', line 3

def initialize(request, response)
  @request = request
  @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

Parameters:

  • keys:

    a single key or a key-path separated by comma



65
66
67
68
69
# File 'lib/itrp/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

#bodyObject



17
18
19
# File 'lib/itrp/client/response.rb', line 17

def body
  @response.body
end

#current_pageObject

pagination - current page



83
84
85
# File 'lib/itrp/client/response.rb', line 83

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/itrp/client/response.rb', line 52

def empty?
  @response.body.blank?
end

#jsonObject

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’



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/itrp/client/response.rb', line 23

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.message.blank? ? 'empty body' : @response.message.strip}
  end
  begin
    data ||= JSON.parse(@response.body)
  rescue ::Exception => e
    data = { message: "Invalid JSON - #{e.message} 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

#messageObject

the error message in case the response is not valid?



47
48
49
# File 'lib/itrp/client/response.rb', line 47

def message
  @message ||= json.is_a?(Hash) ? json[:message] : nil
end

pagination urls (full paths with server) - relations :first, :prev, :next, :last Link: <api.itrp.com/v1/requests?page=1&per_page=25>; rel=“first”, <api.itrp.com/v1/requests?page=2&per_page=25>; rel=“prev”, etc.



99
100
101
102
# File 'lib/itrp/client/response.rb', line 99

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.to_s}"\s*$/, 1] }.compact.first
end

pagination urls (relative paths without server) - relations :first, :prev, :next, :last



105
106
107
# File 'lib/itrp/client/response.rb', line 105

def pagination_relative_link(relation)
  (@pagination_relative_links ||= {})[relation] ||= pagination_link(relation) && pagination_link(relation)[/^https?:\/\/[^\/]*(.*)/, 1]
end

#per_pageObject

pagination - per page



78
79
80
# File 'lib/itrp/client/response.rb', line 78

def per_page
  @per_page ||= @response.header['X-Pagination-Per-Page'].to_i
end

#requestObject



8
9
10
# File 'lib/itrp/client/response.rb', line 8

def request
  @request
end

#responseObject Also known as: raw



12
13
14
# File 'lib/itrp/client/response.rb', line 12

def response
  @response
end

#retry_afterObject



114
115
116
# File 'lib/itrp/client/response.rb', line 114

def retry_after
  @current_page ||= @response.header['Retry-After'].to_i
end

#sizeObject Also known as: count

The nr of resources found



72
73
74
# File 'lib/itrp/client/response.rb', line 72

def size
  @size ||= message ? 0 : json.is_a?(Array) ? json.size : 1
end

#throttled?Boolean

true if the response is invalid because of throttling

Returns:

  • (Boolean)


110
111
112
# File 'lib/itrp/client/response.rb', line 110

def throttled?
  !!(@response.code.to_s == '429' || (message && message =~ /Too Many Requests/))
end

#to_sObject



118
119
120
# File 'lib/itrp/client/response.rb', line 118

def to_s
  valid? ? json.to_s : message
end

#total_entriesObject

pagination - total entries



93
94
95
# File 'lib/itrp/client/response.rb', line 93

def total_entries
  @total_entries ||= @response.header['X-Pagination-Total-Entries'].to_i
end

#total_pagesObject

pagination - total pages



88
89
90
# File 'lib/itrp/client/response.rb', line 88

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)

Returns:

  • (Boolean)


57
58
59
# File 'lib/itrp/client/response.rb', line 57

def valid?
  message.nil?
end