Module: APNS

Defined in:
lib/apns/core.rb,
lib/apns/version.rb,
lib/apns/truncate.rb,
lib/apns/apns_json.rb,
lib/apns/exceptions.rb

Overview

Copyright © 2013 William Denniss

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: APNSException, ApnsJSON, TrucateException, Truncate

Constant Summary collapse

VERSION =
"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_connectionsObject

Returns the value of attribute cache_connections.



56
57
58
# File 'lib/apns/core.rb', line 56

def cache_connections
  @cache_connections
end

.feedback_hostObject

Returns the value of attribute feedback_host.



56
57
58
# File 'lib/apns/core.rb', line 56

def feedback_host
  @feedback_host
end

.feedback_portObject

Returns the value of attribute feedback_port.



56
57
58
# File 'lib/apns/core.rb', line 56

def feedback_port
  @feedback_port
end

.hostObject

Returns the value of attribute host.



56
57
58
# File 'lib/apns/core.rb', line 56

def host
  @host
end

.loggingObject

Returns the value of attribute logging.



56
57
58
# File 'lib/apns/core.rb', line 56

def logging
  @logging
end

.message_clean_whitespaceObject

Returns the value of attribute message_clean_whitespace.



56
57
58
# File 'lib/apns/core.rb', line 56

def message_clean_whitespace
  @message_clean_whitespace
end

.passObject

Returns the value of attribute pass.



56
57
58
# File 'lib/apns/core.rb', line 56

def pass
  @pass
end

.pemObject

Returns the value of attribute pem.



56
57
58
# File 'lib/apns/core.rb', line 56

def pem
  @pem
end

.portObject

Returns the value of attribute port.



56
57
58
# File 'lib/apns/core.rb', line 56

def port
  @port
end

.truncate_ellipsis_strObject

Returns the value of attribute truncate_ellipsis_str.



56
57
58
# File 'lib/apns/core.rb', line 56

def truncate_ellipsis_str
  @truncate_ellipsis_str
end

.truncate_modeObject

Returns the value of attribute truncate_mode.



56
57
58
# File 'lib/apns/core.rb', line 56

def truncate_mode
  @truncate_mode
end

.truncate_soft_max_choppedObject

Returns the value of attribute truncate_soft_max_chopped.



56
57
58
# File 'lib/apns/core.rb', line 56

def truncate_soft_max_chopped
  @truncate_soft_max_chopped
end

Class Method Details

.establish_notification_connectionObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/apns/core.rb', line 59

def self.establish_notification_connection
  if @cache_connections
    begin
      self.get_connection(self.host, self.port)
      return true
    rescue
    end
  end
  return false
end

.feedbackObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/apns/core.rb', line 92

def self.feedback
  apns_feedback = []
  self.with_feedback_connection do |conn|
    # Read buffers data from the OS, so it's probably not
    # too inefficient to do the small reads
    while data = conn.read(38)
      apns_feedback << self.parse_feedback_tuple(data)
    end
  end
  
  return apns_feedback
end

.has_notification_connection?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/apns/core.rb', line 70

def self.has_notification_connection?
  return self.has_connection?(self.host, self.port)
end

.send_notification(device_token, message, notification_id = rand(9999), expiry = (Time.now + 1.year)) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/apns/core.rb', line 74

def self.send_notification(device_token, message, notification_id = rand(9999), expiry = (Time.now + 1.year))
  self.with_notification_connection do |conn|
    conn.write(self.packaged_notification(device_token, message, notification_id, expiry))
    conn.flush
    puts "[APNS] send 1 notification package to #{@host}" if @logging
  end
end

.send_notifications(notifications) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/apns/core.rb', line 82

def self.send_notifications(notifications)
  self.with_notification_connection do |conn|
    notifications.each do |n|
      conn.write(self.packaged_notification(n[0], n[1], (n[2] or rand(9999)), (n[3] or (Time.now + 1.year))))
    end
    conn.flush
    puts "[APNS] sent #{notifications.count} notification package(s) to #{@host}" if @logging
  end
end