Module: APN

Extended by:
Connection
Defined in:
lib/apn.rb,
lib/apn/client.rb,
lib/apn/backend.rb,
lib/apn/railtie.rb,
lib/apn/version.rb,
lib/apn/feedback.rb,
lib/apn/connection.rb,
lib/apn/notification.rb,
lib/apn/sender_daemon.rb,
lib/apn/backend/resque.rb,
lib/apn/backend/sidekiq.rb

Defined Under Namespace

Modules: Backend, Connection, Jobs Classes: Client, Feedback, FeedbackItem, Notification, Railtie, SenderDaemon

Constant Summary collapse

VERSION =
"2.0.2"

Instance Attribute Summary

Attributes included from Connection

#full_certificate_path, #host, #password, #pool_size, #pool_timeout, #port, #root

Class Method Summary collapse

Methods included from Connection

certificate, certificate_name, certificate_name=, certificate_path, connection_pool, with_connection

Class Method Details

.backendObject



43
44
45
# File 'lib/apn.rb', line 43

def backend
  @backend ||= APN::Backend::Simple.new
end

.backend=(backend) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apn.rb', line 31

def backend=(backend)
  @backend =
    case backend
    when Symbol
      APN::Backend.const_get(backend.to_s.camelize).new
    when nil
      APN::Backend::Simple.new
    else
      backend
    end
end

.log(level, message = nil) ⇒ Object

Log message to any logger provided by the user (e.g. the Rails logger). Accepts log_level, message, since that seems to make the most sense, and just message, to be compatible with Resque’s log method and to enable sending verbose and very_verbose worker messages to e.g. the rails logger.

Perhaps a method definition of +message, level would make more sense, but that’s also the complete opposite of what anyone comming from rails would expect.



70
71
72
73
74
75
# File 'lib/apn.rb', line 70

def log(level, message = nil)
  level, message = 'info', level if message.nil? # Handle only one argument if called from Resque, which expects only message

  return false unless logger && logger.respond_to?(level)
  logger.send(level, "#{Time.now}: #{message}")
end

.log_and_die(msg) ⇒ Object

Log the message first, to ensure it reports what went wrong if in daemon mode. Then die, because something went horribly wrong.



79
80
81
82
# File 'lib/apn.rb', line 79

def log_and_die(msg)
  logger.fatal(msg)
  raise msg
end

.loggerObject



51
52
53
# File 'lib/apn.rb', line 51

def logger
  @logger ||= Logger.new(STDOUT)
end

.logger=(logger) ⇒ Object



47
48
49
# File 'lib/apn.rb', line 47

def logger=(logger)
  @logger = logger
end

.notify_async(token, opts = {}) ⇒ Object



15
16
17
18
# File 'lib/apn.rb', line 15

def notify_async(token, opts = {})
  token = token.to_s.gsub(/\W/, '')
  backend.notify(token, opts)
end

.notify_sync(token, opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/apn.rb', line 20

def notify_sync(token, opts)
  token = token.to_s.gsub(/\W/, '')
  msg = APN::Notification.new(token, opts)
  raise "Invalid notification options (did you provide :alert, :badge, or :sound?): #{opts.inspect}" unless msg.valid?

  APN.log(:debug, "Sending to token '#{token}' message '#{opts.to_s}'")
  APN.with_connection do |client|
    client.push(msg)
  end
end

.truncate_alertObject



55
56
57
# File 'lib/apn.rb', line 55

def truncate_alert
  @truncate_alert ||= false
end

.truncate_alert=(truncate) ⇒ Object



59
60
61
# File 'lib/apn.rb', line 59

def truncate_alert=(truncate)
  @truncate_alert = truncate
end