Module: Firebase::Admin::Messaging::Utils

Included in:
MessageEncoder
Defined in:
lib/firebase/admin/messaging/utils.rb

Instance Method Summary collapse

Instance Method Details

#check_analytics_label(label, value) ⇒ Object

Raises:



46
47
48
49
50
51
# File 'lib/firebase/admin/messaging/utils.rb', line 46

def check_analytics_label(label, value)
  return nil unless value
  value = check_string(label, value)
  raise ArgumentError, "#{label} is malformed" unless /\A[a-zA-Z0-9\-_.~%]{1,50}\Z/.match?(value)
  value
end

#check_color(label, value, allow_alpha: false, required: false) ⇒ String?

Returns:

  • (String, nil)

Raises:



60
61
62
63
64
65
66
67
68
# File 'lib/firebase/admin/messaging/utils.rb', line 60

def check_color(label, value, allow_alpha: false, required: false)
  return nil unless value || required
  raise ArgumentError, "#{label} is required" unless value
  raise ArgumentError, "#{label} must be a string" unless value.is_a?(String)
  return value if /\A#[0-9a-fA-F]{6}\Z/.match?(value)
  return value if /\A#[0-9a-fA-F]{8}\Z/.match?(value) && allow_alpha
  raise ArgumentError, "#{label} must be in the form #RRGGBB" unless allow_alpha
  raise ArgumentError, "#{label} must be in the form #RRGGBB or #RRGGBBAA"
end

#check_numeric(label, value) ⇒ Object

Raises:



13
14
15
16
17
# File 'lib/firebase/admin/messaging/utils.rb', line 13

def check_numeric(label, value)
  return nil unless value
  raise ArgumentError, "#{label} must be a number." unless value.is_a?(Numeric)
  value
end

#check_numeric_array(label, value) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/firebase/admin/messaging/utils.rb', line 38

def check_numeric_array(label, value)
  return nil if value.nil?
  raise ArgumentError, "#{label} must be an array of numbers." unless value.is_a?(Array)
  return nil if value.empty?
  raise ArgumentError, "#{label} must not contain non-numeric values" unless value.all?(Numeric)
  value
end

#check_string(label, value, non_empty: false) ⇒ Object

Raises:



5
6
7
8
9
10
11
# File 'lib/firebase/admin/messaging/utils.rb', line 5

def check_string(label, value, non_empty: false)
  return nil unless value
  raise ArgumentError, "#{label} must be a string." unless value.is_a?(String) || non_empty
  raise ArgumentError, "#{label} must be a non-empty string." unless value.is_a?(String) || !non_empty
  raise ArgumentError, "#{label} must be a non-empty string." if value.empty? && non_empty
  value
end

#check_string_array(label, value) ⇒ Object

Raises:



30
31
32
33
34
35
36
# File 'lib/firebase/admin/messaging/utils.rb', line 30

def check_string_array(label, value)
  return nil if value.nil?
  raise ArgumentError, "#{label} must be an array of strings." unless value.is_a?(Array)
  return nil if value.empty?
  raise ArgumentError, "#{label} must not contain non-string values" unless value.all?(String)
  value
end

#check_string_hash(label, value) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
# File 'lib/firebase/admin/messaging/utils.rb', line 19

def check_string_hash(label, value)
  return nil if value.nil?
  raise ArgumentError, "#{label} must be a hash." unless value.is_a?(Hash)
  return nil if value.empty?
  raise ArgumentError, "#{label} must not contain non-string values" unless value.values.all?(String)
  unless value.keys.all? { |k| k.is_a?(String) || k.is_a?(Symbol) }
    raise ArgumentError, "#{label} must not contain non-string or non-symbol values"
  end
  value
end

#check_time(label, value) ⇒ Object

Raises:



53
54
55
56
57
# File 'lib/firebase/admin/messaging/utils.rb', line 53

def check_time(label, value)
  return nil unless value
  raise ArgumentError, "#{label} must be a time." unless value.is_a?(Time)
  value
end

#to_seconds_string(seconds) ⇒ Object



70
71
72
73
# File 'lib/firebase/admin/messaging/utils.rb', line 70

def to_seconds_string(seconds)
  has_nanos = (seconds - seconds.floor) > 0
  format(has_nanos ? "%0.9fs" : "%ds", seconds)
end