Class: Parse::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/push.rb

Constant Summary collapse

ENDPOINT =
'push'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, parse_client = nil) ⇒ Push

Returns a new instance of Push.



27
28
29
30
31
32
33
34
35
36
# File 'lib/parse/push.rb', line 27

def initialize data={}, parse_client=nil
  parse_client = data if data.is_a? Parse::Client
  data = {'alert' => data} if data.is_a? String

  @parse_client = parse_client || Parse::Client.default
  @channels = []
  @data = data 
  @query = nil
  @push_time = nil
end

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



5
6
7
# File 'lib/parse/push.rb', line 5

def channels
  @channels
end

Class Method Details

.send(message_or_opts, opts = {}, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/parse/push.rb', line 8

def send message_or_opts, opts={}, &block
  opts[:data] = message_or_opts if message_or_opts.is_a? String
  raise ArgumentError.new 'data is nil' unless opts[:data]
  opts[:push_time] = opts[:at] if opts[:at]
  opts[:where] = opts[:condition] if opts[:condition]

  push = Parse::Push.new opts[:data]
  push.at opts[:push_time] if opts[:push_time]
  if opts[:where]
    if opts[:where].is_a? Proc
      push.where &opts[:where]
    else
      push.where opts[:where]
    end
  end
  push.send &block
end

Instance Method Details

#data=(val) ⇒ Object



38
39
40
# File 'lib/parse/push.rb', line 38

def data= val
  @data = val.is_a?(String) ? {'alert' => val} : val
end

#push_time=(val) ⇒ Object Also known as: at



47
48
49
50
# File 'lib/parse/push.rb', line 47

def push_time= val
  # TODO: refactoring: use ParseDate?
  @push_time = val.is_a?(Time) ? val.getutc.iso8601 : val
end

#send(opt = {}, &block) ⇒ Object



68
69
70
71
72
# File 'lib/parse/push.rb', line 68

def send opt={}, &block
  push_time = opt[:push_time] || opt[:at] 
  self.push_time = push_time if push_time
  @parse_client.call_api :post, ENDPOINT, to_json, &block
end

#to_jsonObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/parse/push.rb', line 53

def to_json
  raise ArgumentError.new '@data is nil' if @data.empty?

  json = {}
  json['channels'] = @channels unless @channels.empty?
  if @query
    where = @query.where
    def where.to_json *args; "{#{self.join ','}}" end
    json['where'] = where
  end
  json['push_time'] = @push_time if @push_time
  json['data'] = @data
  json.to_json
end

#where(hash = nil, &block) ⇒ Object



42
43
44
45
# File 'lib/parse/push.rb', line 42

def where hash=nil, &block
  @query = Parse::Query.new 'Parse::Notification', @parse_client
  @query.where hash, &block
end