Class: Lowdown::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lowdown/client.rb

Constant Summary collapse

DEVELOPMENT_URI =
URI.parse("https://api.development.push.apple.com:443")
PRODUCTION_URI =
URI.parse("https://api.push.apple.com:443")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, default_topic = nil) ⇒ Client

Returns a new instance of Client.



35
36
37
# File 'lib/lowdown/client.rb', line 35

def initialize(connection, default_topic = nil)
  @connection, @default_topic = connection, default_topic
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



33
34
35
# File 'lib/lowdown/client.rb', line 33

def connection
  @connection
end

#default_topicObject (readonly)

Returns the value of attribute default_topic.



33
34
35
# File 'lib/lowdown/client.rb', line 33

def default_topic
  @default_topic
end

Class Method Details

.client(uri, certificate_or_data) ⇒ Object



27
28
29
30
31
# File 'lib/lowdown/client.rb', line 27

def self.client(uri, certificate_or_data)
  certificate = Lowdown.Certificate(certificate_or_data)
  default_topic = certificate.topics.first if certificate.universal?
  new(Connection.new(uri, certificate.ssl_context), default_topic)
end

.production(production, certificate_or_data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lowdown/client.rb', line 13

def self.production(production, certificate_or_data)
  certificate = Lowdown.Certificate(certificate_or_data)
  if production
    unless certificate.production?
      raise ArgumentError, "The specified certificate is not usable with the production environment."
    end
  else
    unless certificate.development?
      raise ArgumentError, "The specified certificate is not usable with the development environment."
    end
  end
  client(production ? PRODUCTION_URI : DEVELOPMENT_URI, certificate)
end

Instance Method Details

#closeObject



54
55
56
# File 'lib/lowdown/client.rb', line 54

def close
  @connection.close
end

#connectObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/lowdown/client.rb', line 39

def connect
  @connection.open
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

#flushObject



50
51
52
# File 'lib/lowdown/client.rb', line 50

def flush
  @connection.flush
end

#send_notification(notification, &callback) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lowdown/client.rb', line 58

def send_notification(notification, &callback)
  raise ArgumentError, "Invalid notification: #{notification.inspect}" unless notification.valid?

  topic = notification.topic || @default_topic
  headers = {}
  headers["apns-expiration"] = (notification.expiration || 0).to_i
  headers["apns-id"]         = notification.formatted_id if notification.id
  headers["apns-priority"]   = notification.priority     if notification.priority
  headers["apns-topic"]      = topic                     if topic

  body = notification.formatted_payload.to_json

  @connection.post("/3/device/#{notification.token}", headers, body, &callback)
end