Class: Firebase::Messaging::Request::For

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase/messaging/request/for.rb

Constant Summary collapse

TOPIC_REGEX =
/\A\/topics\/(.+)\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directions) ⇒ For

Returns a new instance of For.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/firebase/messaging/request/for.rb', line 9

def initialize(directions)
  if directions.is_a? String
    @to = directions
  elsif directions.is_a? Array
    if directions.flatten.any? { |direction| TOPIC_REGEX === direction }
      if directions.flatten.size == 1
        @to = directions.first
      else
        @topics = directions
      end
    else
      @registration_ids = directions
    end
  end
end

Instance Attribute Details

#registration_idsObject (readonly)

Returns the value of attribute registration_ids.



7
8
9
# File 'lib/firebase/messaging/request/for.rb', line 7

def registration_ids
  @registration_ids
end

#toObject (readonly)

Returns the value of attribute to.



7
8
9
# File 'lib/firebase/messaging/request/for.rb', line 7

def to
  @to
end

#topicsObject (readonly)

Returns the value of attribute topics.



7
8
9
# File 'lib/firebase/messaging/request/for.rb', line 7

def topics
  @topics
end

Instance Method Details

#payloadObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/firebase/messaging/request/for.rb', line 25

def payload
  return { to: @to } if @to
  return { registration_ids: @registration_ids } if @registration_ids

  conditions = @topics.inject([]) do |arr, topic|
    if topic.is_a? Array
      arr.push "(#{or_condition(topic)})"
    else
      arr.push topic_condition(topic)
    end
  end

  { condition: conditions.join(' && ') }
end

#typeObject



40
41
42
43
44
45
46
# File 'lib/firebase/messaging/request/for.rb', line 40

def type
  if (@to && !(TOPIC_REGEX === @to)) || @registration_ids
    :down_stream_http_message
  else
    :topic_message
  end
end