Class: BMC::FCM::Request

Inherits:
Object
  • Object
show all
Defined in:
app/libs/bmc/fcm/request.rb

Constant Summary collapse

URL =
"https://fcm.googleapis.com/fcm/send"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.api_keyObject



9
10
11
# File 'app/libs/bmc/fcm/request.rb', line 9

def api_key
  @api_key ||= ENV["FCM_API_KEY"]
end

Instance Attribute Details

#response_jsonObject (readonly)

Returns the value of attribute response_json.



16
17
18
# File 'app/libs/bmc/fcm/request.rb', line 16

def response_json
  @response_json
end

Class Method Details

.callObject



18
19
20
# File 'app/libs/bmc/fcm/request.rb', line 18

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/libs/bmc/fcm/request.rb', line 22

def call
  response_body = HTTParty.post(URL,
    :body    => request_body.to_json,
    :headers => {
      "Content-Type"  => "application/json",
      "Authorization" => "key=#{self.class.api_key}",
    },
  ).body

  @response_json = JSON.parse(response_body).deep_symbolize_keys

  self
rescue JSON::ParserError
  @response_json = {success: 0, failure: 1, results: [{:error => "InvalidJsonResponse"}]}
  self
end

#error?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/libs/bmc/fcm/request.rb', line 43

def error?
  !ok?
end

#errorsObject



47
48
49
# File 'app/libs/bmc/fcm/request.rb', line 47

def errors
  response_json[:results].pluck(:error).compact
end

#invalid_token?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/libs/bmc/fcm/request.rb', line 51

def invalid_token?
  errors.include?("NotRegistered") || errors.include?("InvalidRegistration")
end

#ok?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/libs/bmc/fcm/request.rb', line 39

def ok?
  response_json[:success].positive? && response_json[:failure].zero?
end