class Sdk4me::Response

Attributes

raw[R]
request[R]
response[R]

Public Class Methods

new(request, response) click to toggle source
# File lib/sdk4me/client/response.rb, line 6
def initialize(request, response)
  @request = request
  @response = response
end

Public Instance Methods

[](*keys) click to toggle source

retrieve a value from the resource if the JSON value is an Array a array with the value for each resource will be given @param keys: a single key or a key-path separated by comma

# 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() click to toggle source
# File lib/sdk4me/client/response.rb, line 11
def body
  @response.body
end
count()
Alias for: size
current_page() click to toggle source

pagination - current page

# File lib/sdk4me/client/response.rb, line 87
def current_page
  @current_page ||= @response.header['X-Pagination-Current-Page'].to_i
end
empty?() click to toggle source

true if the server did not respond at all

# File lib/sdk4me/client/response.rb, line 47
def empty?
  @response.body.blank?
end
failure?() click to toggle source

true in case of a HTTP 5xx error

# 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() click to toggle source

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'

# 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.message.blank? ? 'empty body' : @response.message.strip }
  end
  begin
    data ||= JSON.parse(@response.body)
  rescue StandardError => 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
message() click to toggle source

the error message in case the response is not valid?

# File lib/sdk4me/client/response.rb, line 42
def message
  @message ||= json.is_a?(Hash) ? json[:message] : nil
end
per_page() click to toggle source

pagination - per page

# File lib/sdk4me/client/response.rb, line 82
def per_page
  @per_page ||= @response.header['X-Pagination-Per-Page'].to_i
end
retry_after() click to toggle source
# File lib/sdk4me/client/response.rb, line 118
def retry_after
  @retry_after ||= @response.header['Retry-After'].to_i
end
size() click to toggle source

The nr of resources found

# File lib/sdk4me/client/response.rb, line 72
def size
  @size ||= if message
              0
            else
              json.is_a?(Array) ? json.size : 1
            end
end
Also aliased as: count
success?()
Alias for: valid?
throttled?() click to toggle source

true if the response is invalid because of throttling

# File lib/sdk4me/client/response.rb, line 114
def throttled?
  !!(@response.code.to_s == '429' || (message && message =~ /Too Many Requests/))
end
to_s() click to toggle source
# File lib/sdk4me/client/response.rb, line 122
def to_s
  valid? ? json.to_s : message
end
total_entries() click to toggle source

pagination - total entries

# File lib/sdk4me/client/response.rb, line 97
def total_entries
  @total_entries ||= @response.header['X-Pagination-Total-Entries'].to_i
end
total_pages() click to toggle source

pagination - total pages

# File lib/sdk4me/client/response.rb, line 92
def total_pages
  @total_pages ||= @response.header['X-Pagination-Total-Pages'].to_i
end
valid?() click to toggle source

true if no 'message' is given (and the JSON could be parsed)

# File lib/sdk4me/client/response.rb, line 52
def valid?
  message.nil?
end
Also aliased as: success?