Class: Gravel::APNS::Notification::Localization

Inherits:
Object
  • Object
show all
Defined in:
lib/gravel/apns/notification/localization.rb

Overview

This class can be used to localize part of a notification. You can set a notification’s title, subtitle or body attribute to an instance of this class to localize it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, *args) ⇒ Gravel::APNS::Notification::Localization

Create a new localization.

Parameters:

  • key (String)

    The localization key.

  • args (Splat)

    Arguments to pass into the localizable string.



27
28
29
30
31
32
33
34
# File 'lib/gravel/apns/notification/localization.rb', line 27

def initialize(key, *args)
  unless key.is_a?(String)
    raise 'The localization key must be a string.'
  end

  @key = key.to_s
  self.arguments = args
end

Instance Attribute Details

#argumentsArray

Additional arguments to pass into the localizable string.

Returns:

  • (Array)

    Additional arguments.



19
20
21
# File 'lib/gravel/apns/notification/localization.rb', line 19

def arguments
  @arguments
end

#keyString (readonly)

The localization key as defined in your app’s localization file.

Returns:

  • (String)

    The localization key.



13
14
15
# File 'lib/gravel/apns/notification/localization.rb', line 13

def key
  @key
end

Instance Method Details

#arguments?Boolean

Check if there are any additional arguments.

Returns:

  • (Boolean)

    Whether or not there are any additional arguments.



59
60
61
# File 'lib/gravel/apns/notification/localization.rb', line 59

def arguments?
  self.arguments && self.arguments.any?
end

#payload(type) ⇒ Hash

Convert the localization into APNS payload components.

Parameters:

  • type (Symbol)

    The localization type (title/subtitle/body).

Returns:

  • (Hash)

    The APNS payload components.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gravel/apns/notification/localization.rb', line 68

def payload(type)
  components = Hash.new

  case type
  when :title
    components['title-loc-key'] = @key
    components['title-loc-args'] = self.arguments if arguments?
  when :subtitle
    components['subtitle-loc-key'] = @key
    components['subtitle-loc-args'] = self.arguments if arguments?
  when :body
    components['loc-key'] = @key
    components['loc-args'] = self.arguments if arguments?
  end

  components
end