class GoogleInstanceId

Constants

VERSION

Attributes

api_key[RW]
timeout[RW]

Public Class Methods

new(api_key, client_options = {}) click to toggle source
# File lib/google_instance_id.rb, line 15
def initialize(api_key, client_options = {})
  @api_key = api_key
  @client_options = client_options
end

Public Instance Methods

add_topic(registration_tokens, topic) click to toggle source
# File lib/google_instance_id.rb, line 20
def add_topic(registration_tokens, topic)
  manage_relationship_maps(Array(registration_tokens), topic, :add)
end
info(registration_token) click to toggle source
# File lib/google_instance_id.rb, line 28
def info(registration_token)
  response = self.class.get("/info/#{registration_token}?details=true", build_params)
  Hashie::Mash.new(response) if response.code == 200
end
remove_topic(registration_tokens, topic) click to toggle source
# File lib/google_instance_id.rb, line 24
def remove_topic(registration_tokens, topic)
  manage_relationship_maps(Array(registration_tokens), topic, :remove)
end

Private Instance Methods

build_manage_relationship_response(response, registration_tokens) click to toggle source

Reference developers.google.com/instance-id/reference/server#create_a_relation_mapping_for_an_app_instance

# File lib/google_instance_id.rb, line 54
def build_manage_relationship_response(response, registration_tokens)
  body = response.body.nil? ?  {} : JSON.parse(response.body)
  response_hash = { body: body, headers: response.headers, status_code: response.code, errors: [] }
  if response.code == 200
    body['results'].each_with_index do |result, i|
      if result['error']
        response_hash[:errors] << { error: result['error'], registration_token: registration_tokens[i] }
      end
    end
  end
  response_hash
end
build_params(body = nil) click to toggle source
# File lib/google_instance_id.rb, line 46
def build_params(body = nil)
  params = { headers: headers }
  params[:body] = body.to_json if body
  params.merge(@client_options)
  params
end
headers() click to toggle source
# File lib/google_instance_id.rb, line 35
def headers
  { 'Authorization' => "key=#{@api_key}", 'Content-Type' => 'application/json' }
end
manage_relationship_maps(registration_tokens, topic, action = :add) click to toggle source
# File lib/google_instance_id.rb, line 39
def manage_relationship_maps(registration_tokens, topic, action = :add)
  body = { registration_tokens: registration_tokens, to: "/topics/#{topic}" }
  path = action == :add ? '/v1:batchAdd' : '/v1:batchRemove'
  response = self.class.post(path, build_params(body))
  Hashie::Mash.new(build_manage_relationship_response(response, registration_tokens))
end