class Asynk::Response

Attributes

body[R]
error_message[R]
status[R]

Public Class Methods

new(status: , body: nil, error_message: nil) click to toggle source
# File lib/asynk/response.rb, line 6
def initialize(status: , body: nil, error_message: nil)
  @status, @body, @error_message = status, body, error_message
end
try_parse_json(str) click to toggle source
# File lib/asynk/response.rb, line 49
def self.try_parse_json(str)
  begin
    JSON.parse(str)
  rescue JSON::ParserError => e
    return false
  end
end
try_to_create_from_hash(payload) click to toggle source
# File lib/asynk/response.rb, line 39
def self.try_to_create_from_hash(payload)
  return nil if payload.nil?
  parsed_payload = try_parse_json(payload)
  return payload unless parsed_payload
  return payload unless parsed_payload.kind_of?(Hash)
  hiwa = parsed_payload.with_indifferent_access
  return payload unless (hiwa.has_key?(:status) && hiwa.has_key?(:body) && hiwa.has_key?(:error_message))
  new(status: hiwa[:status], body: hiwa[:body], error_message: hiwa[:error_message])
end

Public Instance Methods

[](key) click to toggle source
# File lib/asynk/response.rb, line 23
def [](key)
  @body[key]
end
as_json(options = {})
Alias for: to_h
errors() click to toggle source
# File lib/asynk/response.rb, line 21
def errors; @error_message; end
fail?() click to toggle source
# File lib/asynk/response.rb, line 11
def fail?; !success?; end
inspect()
Alias for: to_s
success?() click to toggle source
# File lib/asynk/response.rb, line 10
def success?; @status.to_s == 'ok'; end
to_h(options = {}) click to toggle source
# File lib/asynk/response.rb, line 13
def to_h(options = {})
  { status: @status, body: @body, error_message: @error_message }
end
Also aliased as: to_s, as_json
to_json() click to toggle source
# File lib/asynk/response.rb, line 31
def to_json
  to_h.to_json
end
to_s() click to toggle source
# File lib/asynk/response.rb, line 27
def to_s
  to_h
end
Also aliased as: inspect