class JeraPush::Firebase::Client

Constants

FIREBASE_INSTANCE_ID_URL
FIREBASE_URL

Public Class Methods

instance() click to toggle source
# File lib/jera_push/firebase/client.rb, line 17
def self.instance
  @@client ||= JeraPush::Firebase::Client.new
end

Public Instance Methods

add_device_to_topic(topic:, device:) click to toggle source
# File lib/jera_push/firebase/client.rb, line 42
def add_device_to_topic(topic:, device:)
  url = "#{FIREBASE_INSTANCE_ID_URL}/v1/#{device.token}/rel/topics/#{topic}"
  response = HTTParty.post(url, { body: Hash.new.to_json, headers: default_headers })
  ApiResult.new(response)
end
add_devices_to_topic(topic:, devices: []) click to toggle source
# File lib/jera_push/firebase/client.rb, line 48
def add_devices_to_topic(topic:, devices: [])
  url = "#{FIREBASE_INSTANCE_ID_URL}/v1:batchAdd"
  body = {
    "to": "/topics/#{topic}",
    "registration_tokens": devices.map(&:token),
  }
  response = HTTParty.post(url, { body: body.to_json, headers: default_headers })
  ApiResult.new(response)
end
default_headers() click to toggle source
# File lib/jera_push/firebase/client.rb, line 10
def default_headers
  return {
    "Authorization" => "key=#{::JeraPush.firebase_api_key}",
    "Content-Type" => "application/json"
  }
end
device_details(device:) click to toggle source
# File lib/jera_push/firebase/client.rb, line 36
def device_details(device:)
  url = "#{FIREBASE_INSTANCE_ID_URL}/info/#{device.token}/"
  response = HTTParty.post(url, { body: Hash.new.to_json, headers: default_headers })
  ApiResult.new(response)
end
remove_device_from_topic(topic:, devices: []) click to toggle source
# File lib/jera_push/firebase/client.rb, line 58
def remove_device_from_topic(topic:, devices: [])
  url = "#{FIREBASE_INSTANCE_ID_URL}/v1:batchRemove"
  body = {
    "to": "/topics/#{topic}",
    "registration_tokens": devices.map(&:token),
  }
  response = HTTParty.post(url, { body: body.to_json, headers: default_headers })
  ApiResult.new(response)
end
send_message(message:, devices: []) click to toggle source
# File lib/jera_push/firebase/client.rb, line 21
def send_message(message:, devices: [])
  registration_ids = devices.map(&:token)
  body = { registration_ids: registration_ids, priority: 'high' }
  response = HTTParty.post(FIREBASE_URL, { body: body.merge!(message).to_json, headers: default_headers })
  puts response
  ApiResult.new(response, registration_ids: registration_ids)
end
send_message_to_topic(message:, topic:) click to toggle source
# File lib/jera_push/firebase/client.rb, line 29
def send_message_to_topic(message:, topic:)
  body = { to: "/topics/#{topic}", priority: 'high' }
  response = HTTParty.post(FIREBASE_URL, { body: body.merge!(message).to_json, headers: default_headers })
  puts response
  ApiResult.new(response, topic: topic)
end