Class: Firebase::Messaging::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase/messaging/response.rb,
lib/firebase/messaging/response/topic_message.rb,
lib/firebase/messaging/response/down_stream_http_message.rb

Direct Known Subclasses

DownStreamHttpMessage, TopicMessage

Defined Under Namespace

Classes: DownStreamHttpMessage, TopicMessage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: nil, body: nil, headers: nil) ⇒ Response

Returns a new instance of Response.



12
13
14
15
16
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/firebase/messaging/response.rb', line 10

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/firebase/messaging/response.rb', line 10

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/firebase/messaging/response.rb', line 10

def status
  @status
end

Class Method Details

.bind(type, status: nil, body: nil, headers: nil) ⇒ Object

binding.pry



46
47
48
49
50
51
52
53
54
# 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) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/firebase/messaging/response.rb', line 39

def json?(json)
  JSON.parse(json)
  true
rescue JSON::ParserError
  false
end

Instance Method Details

#errorsObject



26
27
28
29
30
31
32
# File 'lib/firebase/messaging/response.rb', line 26

def errors
  if parsed_body.key? :error
    [{ error: parsed_body[:error] }]
  else
    []
  end
end

#failure?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/firebase/messaging/response.rb', line 22

def failure?
  !success?
end

#parsed_bodyObject



34
35
36
# File 'lib/firebase/messaging/response.rb', line 34

def parsed_body
  @parsed_body ||= JSON.parse(body, symbolize_names: true)
end

#success?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/firebase/messaging/response.rb', line 18

def success?
  status >= 200 && status < 400
end