class Scalingo::API::Response

Attributes

client[R]
data[R]
full_body[R]
headers[R]
meta[R]
status[R]

Public Class Methods

new(client:, status:, headers:, data:, full_body:, meta: nil) click to toggle source
# File lib/scalingo/api/response.rb, line 33
def initialize(client:, status:, headers:, data:, full_body:, meta: nil)
  @client = client
  @status = status
  @headers = headers
  @data = data
  @full_body = full_body
  @meta = meta
end
unpack(client, key: nil, keys: nil, &block) click to toggle source
# File lib/scalingo/api/response.rb, line 4
def self.unpack(client, key: nil, keys: nil, &block)
  response = block.call

  body = response.body
  has_hash_body = body.present? && body.respond_to?(:key)

  data = body
  meta = nil

  if has_hash_body
    keys = [key] if key.present?

    data = body.dig(*keys) if response.success? && keys.present?

    meta = body[:meta]
  end

  new(
    client: client,
    status: response.status,
    headers: response.headers,
    data: data,
    meta: meta,
    full_body: body,
  )
end

Public Instance Methods

inspect() click to toggle source
# File lib/scalingo/api/response.rb, line 42
def inspect
  %(<#{self.class}:0x#{object_id.to_s(16)} status:#{@status} data:#{@data} meta:#{@meta}>)
end
operation() click to toggle source
# File lib/scalingo/api/response.rb, line 54
def operation
  if operation? && client.respond_to?(:operations)
    client.operations.get(operation_url)
  end
end
operation?() click to toggle source
# File lib/scalingo/api/response.rb, line 64
def operation?
  operation_url.present?
end
operation_url() click to toggle source
# File lib/scalingo/api/response.rb, line 60
def operation_url
  headers[:location]
end
paginated?() click to toggle source
# File lib/scalingo/api/response.rb, line 50
def paginated?
  meta&.dig(:pagination).present?
end
successful?() click to toggle source
# File lib/scalingo/api/response.rb, line 46
def successful?
  status >= 200 && status < 300
end