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.



38
39
40
# File 'lib/lowdown/client.rb', line 38

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#default_topicObject (readonly)

Returns the value of attribute default_topic.



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

def default_topic
  @default_topic
end

Class Method Details

.client(uri, certificate_or_data) ⇒ Object



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

def self.client(uri, certificate_or_data)
  certificate = Lowdown.Certificate(certificate_or_data)
  client_with_connection(Connection.new(uri, certificate.ssl_context), certificate)
end

.client_with_connection(connection, certificate) ⇒ Object



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

def self.client_with_connection(connection, certificate)
  new(connection, certificate.universal? ? certificate.topics.first : nil)
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



57
58
59
# File 'lib/lowdown/client.rb', line 57

def close
  @connection.close
end

#connectObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/lowdown/client.rb', line 42

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

#flushObject



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

def flush
  @connection.flush
end

#send_notification(notification, &callback) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lowdown/client.rb', line 61

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