Class: Pyapns2
- Inherits:
-
Object
- Object
- Pyapns2
- Defined in:
- lib/pyapns2.rb,
lib/pyapns2/version.rb
Defined Under Namespace
Classes: Error, ProvisionedClient
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#feedback(app_id) ⇒ Object
Takes an app id and returns the list of feedback from pyapns.
-
#initialize(host = 'localhost', port = 7077) ⇒ Pyapns2
constructor
A new instance of Pyapns2.
- #inspect ⇒ Object
-
#notify(app_id, token, notification = nil) ⇒ Object
The main notification endpoint.
-
#provision(options) ⇒ Object
Given a hash of options, calls provision on the pyapns server.
Constructor Details
#initialize(host = 'localhost', port = 7077) ⇒ Pyapns2
Returns a new instance of Pyapns2.
48 49 50 51 52 53 54 55 |
# File 'lib/pyapns2.rb', line 48 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 @http = Net::HTTP.new host, port @xmlrpc = XML::XMLRPC::Client.new @http, "/" end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/pyapns2.rb', line 10 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
10 11 12 |
# File 'lib/pyapns2.rb', line 10 def port @port end |
Class Method Details
.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.
39 40 41 42 43 44 45 46 |
# File 'lib/pyapns2.rb', line 39 def self.provision( = {}) host, port = .delete(:host), .delete(:port) host ||= "localhost" port ||= 7077 client = new(host, port) client.provision() ProvisionedClient.new client, [:app_id] end |
Instance Method Details
#feedback(app_id) ⇒ Object
Takes an app id and returns the list of feedback from pyapns.
112 113 114 115 116 117 |
# File 'lib/pyapns2.rb', line 112 def feedback(app_id) raise ArgumentError, "app_id must be provided" unless app_id @xmlrpc.call('feedback', app_id).params rescue LibXML::XML::XMLRPC::RemoteCallError => e raise Error.new e. end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/pyapns2.rb', line 57 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:
-
A single token (as a string) and notification (as a dictionary)
-
A hash of token to notifications.
-
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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pyapns2.rb', line 94 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 'notify', app_id, token, notification true rescue LibXML::XML::XMLRPC::RemoteCallError => e raise Error.new e. 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pyapns2.rb', line 69 def provision() [:environment] = .delete(:env) if .has_key?(:env) app_id = [:app_id] timeout = [:timeout] cert = [:cert] env = [: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 'provision', app_id, cert, env, timeout true rescue LibXML::XML::XMLRPC::RemoteCallError => e raise Error.new e. end |