Class: Pling::C2DM::Gateway

Inherits:
Gateway
  • Object
show all
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.

Examples:


Pling::C2DM::Gateway.new({
  :email    => 'your-email@gmail.com', # Your google account's email address (Required)
  :password => 'your-password',        # Your google account's password (Required)
  :source   => 'your-app-name',        # Your applications source identifier (Required)

  :authentication_url => 'http://...', # The authentication url to use (Optional, Default: C2DM default authentication url)
  :push_url           => 'http://...', # The push url to use (Optional, Default: C2DM default authentication url)
  :adapter            => :net_http,    # The Faraday adapter you want to use (Optional, Default: :net_http)
  :connection         => {}            # Options you want to pass to Faraday (Optional, Default: {})
})

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Gateway

#deliver, discover, handled_types, handles, #handles?, #teardown!

Constructor Details

- (Gateway) initialize(configuration)

Initializes a new gateway to Apple's Push Notification service

Parameters:

  • configuration (Hash)

Options Hash (configuration):

  • :email (String)

    Your C2DM enabled Google account (Required)

  • :password (String)

    Your Google account's password (Required)

  • :source (String)

    Your applications identifier (Required)

  • :authentication_url (String)

    The URL to authenticate with (Optional)

  • :push_url (String)

    The URL to push to (Optional)

  • :adapter (Symbol)

    The Faraday adapter to use (Optional)

  • :connection (String)

    Any options for Faraday (Optional)

Raises:

  • Pling::AuthenticationFailed



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

- (Object) token (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

- (Object) deliver!(message, device)

Sends the given message to the given device.

Parameters:

  • message (#to_pling_message)
  • device (#to_pling_device)

Raises:

  • Pling::DeliveryFailed



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!(message, device)
  data = {
    :registration_id => device.identifier,
    :data.body'  => message.body,
    :data.badge' => message.badge,
    :data.sound' => message.sound,
    :data.subject' => message.subject,
    :collapse_key => message.body.hash
  }.delete_if { |_, value| value.nil? }

  if configuration[:payload] && message.payload
    message.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}", message, device)
  end
end

- (Object) setup!



47
48
49
# File 'lib/pling/c2dm/gateway.rb', line 47

def setup!
  authenticate! unless token
end