class Firebase::Messaging::Response

Attributes

body[R]
headers[R]
status[R]

Public Class Methods

bind(type, status: nil, body: nil, headers: nil) click to toggle source
# File lib/firebase/messaging/response.rb, line 46
def bind(type, status: nil, body: nil, headers: nil)          # binding.pry
  # TODO: use @parsed_body/success?, but here is class method scope...
  if json?(body) && !JSON.parse(body, symbolize_names: true).blank? && JSON.parse(body, symbolize_names: true).class == Hash
    "Firebase::Messaging::Response::#{type.to_s.classify}".constantize.new(status: status, body: body, headers: headers)
  else
    Firebase::Messaging.logger.error("Unexpected response. status: #{status}, body: #{body}")
    raise Firebase::Messaging::UnexpectedResponseError, { status: status, body: body, headers: headers }.to_s
  end
end
json?(json) click to toggle source
# File lib/firebase/messaging/response.rb, line 39
def json?(json)
  JSON.parse(json)
  true
rescue JSON::ParserError
  false
end
new(status: nil, body: nil, headers: nil) click to toggle source
# File lib/firebase/messaging/response.rb, line 12
def initialize(status: nil, body: nil, headers: nil)
  @status = status.to_i
  @body   = body
  @headers = headers
end

Public Instance Methods

errors() click to toggle source
# File lib/firebase/messaging/response.rb, line 26
def errors
  if parsed_body.key? :error
    [{ error: parsed_body[:error] }]
  else
    []
  end
end
failure?() click to toggle source
# File lib/firebase/messaging/response.rb, line 22
def failure?
  !success?
end
parsed_body() click to toggle source
# File lib/firebase/messaging/response.rb, line 34
def parsed_body
  @parsed_body ||= JSON.parse(body, symbolize_names: true)
end
success?() click to toggle source
# File lib/firebase/messaging/response.rb, line 18
def success?
  status >= 200 && status < 400
end