Class: Pling::C2DM::Gateway
- Defined in:
- lib/pling/c2dm/gateway.rb
Overview
Pling gateway to communicate with Google’s Android C2DM service.
The gateway is implemented using Faraday. It defaults to Faraday’s :net_http adapter. You can customize the adapter by passing the :adapter configuration.
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#deliver!(message, device) ⇒ Object
Sends the given message to the given device.
-
#initialize(configuration) ⇒ Gateway
constructor
Initializes a new gateway to Apple’s Push Notification service.
- #setup! ⇒ Object
Methods inherited from Gateway
#deliver, discover, handled_types, handles, #handles?, #teardown!
Constructor Details
#initialize(configuration) ⇒ Gateway
Initializes a new gateway to Apple’s Push Notification service
41 42 43 44 45 |
# File 'lib/pling/c2dm/gateway.rb', line 41 def initialize(configuration) super require_configuration([:email, :password, :source]) setup! end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
27 28 29 |
# File 'lib/pling/c2dm/gateway.rb', line 27 def token @token end |
Instance Method Details
#deliver!(message, device) ⇒ Object
Sends the given message to the given device.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pling/c2dm/gateway.rb', line 57 def deliver!(, device) data = { :registration_id => device.identifier, :'data.body' => .body, :'data.badge' => .badge, :'data.sound' => .sound, :'data.subject' => .subject, :collapse_key => .body.hash }.delete_if { |_, value| value.nil? } if configuration[:payload] && .payload .payload.each do |key, value| data["data.#{key}".to_sym] = value end end response = connection.post(configuration[:push_url], data, { :Authorization => "GoogleLogin auth=#{@token}"}) if !response.success? || response.body =~ /^Error=(.+)$/ error_class = Pling::C2DM.const_get($1) rescue Pling::DeliveryFailed raise error_class.new("C2DM Delivery failed: [#{response.status}] #{response.body}", , device) end end |
#setup! ⇒ Object
47 48 49 |
# File 'lib/pling/c2dm/gateway.rb', line 47 def setup! authenticate! unless token end |