Class: Pushy::Application
- Inherits:
-
Object
- Object
- Pushy::Application
- Includes:
- HTTParty
- Defined in:
- lib/pushy/application.rb
Instance Method Summary collapse
-
#initialize(args) ⇒ Application
constructor
A new instance of Application.
- #new_device(device_token, device_alias = "") ⇒ Object
- #send_notification(payload, options) ⇒ Object
Constructor Details
#initialize(args) ⇒ Application
Returns a new instance of Application.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/pushy/application.rb', line 7 def initialize(args) # We are using the hash argument idea from Rails raise TypeError, 'Argument must be a hash' unless args.is_a? Hash # Converting all the args keys to symbols args.each_pair do |k,v| args.delete(k) args[k.to_sym] = v if k.respond_to? :to_sym end @api_key = args[:api_key] @api_secret = args[:api_secret] raise ArgumentError, 'No API key specified' unless @api_key raise ArgumentError, 'No API secret specified' unless @api_secret raise TypeError, ":api_key must be a String" unless @api_key.is_a? String raise TypeError, ":api_secret must be a String" unless @api_secret.is_a? String self.class.basic_auth @api_key, @api_secret end |
Instance Method Details
#new_device(device_token, device_alias = "") ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/pushy/application.rb', line 28 def new_device(device_token, device_alias="") params = {} params[:token] = device_token params[:alias] = device_alias if device_alias self.class.post('/devices.json', :body => params) end |
#send_notification(payload, options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pushy/application.rb', line 37 def send_notification(payload, ) params = {} params[:alert] = payload[:alert] if payload[:alert] params[:badge] = payload[:badge] if payload[:badge] params[:sound] = payload[:sound] if payload[:sound] if [:aliases].is_a? Array [:aliases].each do |the_alias| params["aliases[]"] = the_alias end end if [:tokens].is_a? Array [:tokens].each do |the_token| params["tokens[]"] = the_token end end self.class.post('/notifications.json', :body => params) end |