Class: Pushing::Platforms::ApnPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/pushing/platforms.rb

Constant Summary collapse

EMPTY_HASH =
{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, options, config = EMPTY_HASH) ⇒ ApnPayload

Returns a new instance of ApnPayload.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pushing/platforms.rb', line 28

def initialize(payload, options, config = EMPTY_HASH)
  @payload     = payload
  @environment = config[:environment]
  @headers     = config[:default_headers] || {}

  if config[:topic]
    ActiveSupport::Deprecation.warn "`config.apn.topic' is deprecated and will be removed in 0.3.0. " \
                                    "Please use `config.apn.default_headers' instead:\n\n" \
                                    "  config.apn.default_headers = {\n" \
                                    "    apns_topic: '#{config[:topic]}'\n" \
                                    "  }", caller

    @headers['apns-topic'] ||= config[:topic]
  end

  if options.is_a?(String)
    @device_token = options
  elsif options.is_a?(Hash)
    @device_token = options[:device_token]
    @environment  = options[:environment] || @environment
    @headers      = @headers.merge(options[:headers] || EMPTY_HASH)
  else
    raise TypeError, "The :apn key only takes a device token as a string or a hash that has `device_token: \"...\"'."
  end

  # raise("APNs environment is required.")  if @environment.nil?
  # raise("APNs device token is required.") if @device_token.nil?

  @environment = @environment.to_sym
end

Instance Attribute Details

#device_tokenObject (readonly)

Returns the value of attribute device_token.



20
21
22
# File 'lib/pushing/platforms.rb', line 20

def device_token
  @device_token
end

#environmentObject (readonly)

Returns the value of attribute environment.



20
21
22
# File 'lib/pushing/platforms.rb', line 20

def environment
  @environment
end

#payloadObject (readonly)

Returns the value of attribute payload.



20
21
22
# File 'lib/pushing/platforms.rb', line 20

def payload
  @payload
end

Class Method Details

.should_render?(options) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/pushing/platforms.rb', line 24

def self.should_render?(options)
  options.is_a?(Hash) ? options[:device_token].present? : options.present?
end

Instance Method Details

#headersObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pushing/platforms.rb', line 63

def headers
  @normalized_headers ||= begin
                            h = @headers.stringify_keys.transform_keys!(&:dasherize)

                            {
                              authorization:      h['authorization'],
                              'apns-id':          h['apns-id']          || h['id'],
                              'apns-expiration':  h['apns-expiration']  || h['expiration'],
                              'apns-priority':    h['apns-priority']    || h['priority'],
                              'apns-topic':       h['apns-topic']       || h['topic'],
                              'apns-collapse-id': h['apns-collapse-id'] || h['collapse-id'],
                            }
                          end
end

#recipientsObject



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

def recipients
  Array("#{@environment}/#{@device_token}")
end