Class: Pyapns2

Inherits:
Object
  • Object
show all
Defined in:
lib/pyapns2.rb,
lib/pyapns2/version.rb

Defined Under Namespace

Classes: Deserializer, Error, ProvisionedClient

Constant Summary collapse

VERSION =
"2.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 7077) ⇒ Pyapns2

Returns a new instance of Pyapns2.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
# File 'lib/pyapns2.rb', line 57

def initialize(host = 'localhost', port = 7077)
  raise ArgumentError, "host must be a string" unless host.is_a?(String)
  raise ArgumentError, "port must be a number" unless port.is_a?(Numeric)
  @host   = host
  @port   = port
  @xmlrpc = Rapuncel::Client.new :host => @host, :port => @port, :path => "/", :raise_on => :both
  def @xmlrpc.deserializer; Pyapns2::Deserializer; end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



19
20
21
# File 'lib/pyapns2.rb', line 19

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



19
20
21
# File 'lib/pyapns2.rb', line 19

def port
  @port
end

Class Method Details

.deserializerObject



63
# File 'lib/pyapns2.rb', line 63

def @xmlrpc.deserializer; Pyapns2::Deserializer; end

.provision(options = {}) ⇒ Object

Returns a pre-provisioned client that also automatically prepends the app_id automatically to all api calls, making giving a simpler interface.



48
49
50
51
52
53
54
55
# File 'lib/pyapns2.rb', line 48

def self.provision(options = {})
  host, port = options.delete(:host), options.delete(:port)
  host ||= "localhost"
  port ||= 7077
  client = new(host, port)
  client.provision(options)
  ProvisionedClient.new client, options[:app_id]
end

Instance Method Details

#feedback(app_id) ⇒ Object

Takes an app id and returns the list of feedback from pyapns.



121
122
123
124
125
126
# File 'lib/pyapns2.rb', line 121

def feedback(app_id)
  raise ArgumentError, "app_id must be provided" unless app_id
  @xmlrpc.call_to_ruby('feedback', app_id)
rescue  Rapuncel::Response::Exception => e
  raise Error.new e.message
end

#inspectObject



66
67
68
# File 'lib/pyapns2.rb', line 66

def inspect
  "#<#{self.class.name} server=#{host}:#{port}>"
end

#notify(app_id, token, notification = nil) ⇒ Object

The main notification endpoint. Takes the app_id as the first argument, and then one of three sets of notification data:

  1. A single token (as a string) and notification (as a dictionary)

  2. A hash of token to notifications.

  3. An array of tokens mapping to an array of notifications.

Under the hook, it will automatically convert it to the most appropriate form before continuing. Will raise ArgumentError if you attempt to pass in bad information.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pyapns2.rb', line 103

def notify(app_id, token, notification = nil)
  if token.is_a?(Hash)
    token, notification = extra_notification_info_from_hash token
  end
  raise ArgumentError, "Please ensure you provide an app_id" unless app_id
  raise ArgumentError, "Please ensure you provide a single notification or an array of notifications" unless typed_item_of(notification, Hash)
  raise ArgumentError, "Please ensure you provide device tokens or a string of tokens" unless typed_item_of(token, String)
  types = [notification.is_a?(Array), token.is_a?(Array)]
  if types.any? && !types.all?
    raise ArgumentError, "The notifications and the strings must both  be arrays if one is."
  end
  @xmlrpc.call_to_ruby 'notify', app_id, token, notification
  true
rescue  Rapuncel::Response::Exception => e
  raise Error.new e.message
end

#provision(options) ⇒ Object

Given a hash of options, calls provision on the pyapns server. This expects the following options and will raise an ArgumentError if they’re not given:

:app_id - A String name for your application :timeout - A number (e.g. 15) for how long to time out after when connecting to the apn server :env / :environment - One of production or sandbox. The type of server to connect to. :cert - Either a path to the certificate file or the certificate contents as a string.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pyapns2.rb', line 78

def provision(options)
  options[:environment] = options.delete(:env) if options.has_key?(:env)
  app_id  = options[:app_id]
  timeout = options[:timeout]
  cert    = options[:cert]
  env     = options[:environment]
  raise ArgumentError, ":app_id must be a string" unless app_id.is_a?(String) && !app_id.strip.empty?
  raise ArgumentError, ":cert must be a string" unless cert.is_a?(String) && !cert.strip.empty?
  raise ArgumentError, ":environment (or :env) must be one of sandbox or production" unless %w(production sandbox).include?(env)
  raise ArgumentError, ":timeout must be a valid integer" unless timeout.is_a?(Numeric) && timeout >= 0
  @xmlrpc.call_to_ruby 'provision', app_id, cert, env, timeout
  true
rescue  Rapuncel::Response::Exception => e
  raise Error.new e.message
end