Class: SimpleGCM::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_gcm/sender.rb

Constant Summary collapse

BASE_ENDPOINT_URL =
'https://android.googleapis.com'
SEND_PATH =
'/gcm/send'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sender

Returns a new instance of Sender.



7
8
9
# File 'lib/simple_gcm/sender.rb', line 7

def initialize(options)
  @api_key = options.delete(:api_key)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/simple_gcm/sender.rb', line 5

def api_key
  @api_key
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/simple_gcm/sender.rb', line 5

def connection
  @connection
end

Instance Method Details

#send(options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_gcm/sender.rb', line 11

def send(options)
  registration_id = options.delete(:registration_id)
  message         = options.delete(:message)
  http_parameters = { :registration_id => registration_id }
  message.data.each_pair do |k,v|
    http_parameters["data.#{k}"] = v.to_s
  end
  http_parameters[:collapse_key] = message.collapse_key if message.collapse_key
  http_parameters[:delay_while_idle] = "1" if message.delay_while_idle
  http_parameters[:time_to_live] = message.time_to_live if message.time_to_live
  response = connection.post SEND_PATH do |req|
    req.headers['Content-Type']  = 'text/plain'
    req.headers['Authorization'] = "key=#{api_key}"
    req.params = http_parameters
  end
  response.env[:gcm_result]
end