parsecom 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ module Parse
2
+ class Push
3
+ ENDPOINT = 'push'
4
+
5
+ attr_accessor :channels
6
+
7
+ class <<self
8
+ def send message_or_opts, opts={}, &block
9
+ opts[:data] = message_or_opts if message_or_opts.is_a? String
10
+ raise ArgumentError.new 'data is nil' unless opts[:data]
11
+ opts[:push_time] = opts[:at] if opts[:at]
12
+ opts[:where] = opts[:condition] if opts[:condition]
13
+
14
+ push = Parse::Push.new opts[:data]
15
+ push.at opts[:push_time] if opts[:push_time]
16
+ if opts[:where]
17
+ if opts[:where].is_a? Proc
18
+ push.where &opts[:where]
19
+ else
20
+ push.where opts[:where]
21
+ end
22
+ end
23
+ push.send &block
24
+ end
25
+ end
26
+
27
+ def initialize data={}, parse_client=nil
28
+ parse_client = data if data.is_a? Parse::Client
29
+ data = {'alert' => data} if data.is_a? String
30
+
31
+ @parse_client = parse_client || Parse::Client.default
32
+ @channels = []
33
+ @data = data
34
+ @query = nil
35
+ @push_time = nil
36
+ end
37
+
38
+ def data= val
39
+ @data = val.is_a?(String) ? {'alert' => val} : val
40
+ end
41
+
42
+ def where hash=nil, &block
43
+ @query = Parse::Query.new 'Parse::Notification', @parse_client
44
+ @query.where hash, &block
45
+ end
46
+
47
+ def push_time= val
48
+ # TODO: refactoring: use ParseDate?
49
+ @push_time = val.is_a?(Time) ? val.getutc.iso8601 : val
50
+ end
51
+ alias at push_time=
52
+
53
+ def to_json
54
+ raise ArgumentError.new '@data is nil' if @data.empty?
55
+
56
+ json = {}
57
+ json['channels'] = @channels unless @channels.empty?
58
+ if @query
59
+ where = @query.where
60
+ def where.to_json *args; "{#{self.join ','}}" end
61
+ json['where'] = where
62
+ end
63
+ json['push_time'] = @push_time if @push_time
64
+ json['data'] = @data
65
+ json.to_json
66
+ end
67
+
68
+ def send opt={}, &block
69
+ push_time = opt[:push_time] || opt[:at]
70
+ self.push_time = push_time if push_time
71
+ @parse_client.call_api :post, ENDPOINT, to_json, &block
72
+ end
73
+ end
74
+ end
@@ -1,6 +1,6 @@
1
1
  module Parse
2
2
  class Query
3
- attr_reader :keys
3
+ attr_reader :keys, :where
4
4
  attr_accessor :parse_class_name, :parse_client
5
5
 
6
6
  def initialize parse_class_name=nil, parse_client=nil
@@ -203,7 +203,15 @@ module Parse
203
203
  alias greater_that_or_equal_to gte
204
204
  alias not_equal_to ne
205
205
 
206
- def between range
206
+ def between *range
207
+ if range.size == 1 && range.first.is_a?(Range)
208
+ range = range.first
209
+ elsif range.size == 2
210
+ range = range[0]..range[1]
211
+ else
212
+ raise ArgumentErrr
213
+ end
214
+
207
215
  if range.exclude_end?
208
216
  self.gt(range.begin).lt(range.end)
209
217
  else
@@ -1,3 +1,3 @@
1
1
  module Parse
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -37,6 +37,7 @@ require 'parse/batch_http_client'
37
37
  require 'parse/event'
38
38
  require 'parse/event/app_opened'
39
39
  require 'parse/event/error'
40
+ require 'parse/push'
40
41
 
41
42
  module Parse
42
43
  @@application_id = ENV['PARSE_APPLICATION_ID']
@@ -0,0 +1,67 @@
1
+ # coding:utf-8
2
+ require 'spec_helper'
3
+ require 'parsecom'
4
+
5
+ describe Parse::Push, 'when push' do
6
+ it 'should send a simple push message' do
7
+ push = Parse::Push.new
8
+ push.channels = ['Giants', 'Mets']
9
+ push.data = 'The Giants won against the Mets 2-3.'
10
+ push.to_json.should == '{"channels":["Giants","Mets"],"data":{"alert":"The Giants won against the Mets 2-3."}}'
11
+ end
12
+
13
+ it 'should send a conditional push message' do
14
+ push = Parse::Push.new
15
+ push.where 'injuryReports' => true
16
+ push.data = 'Willie Hayes injured by own pop fly.'
17
+ push.to_json.should == '{"where":{"injuryReports":true},"data":{"alert":"Willie Hayes injured by own pop fly."}}'
18
+
19
+ push = Parse::Push.new
20
+ push.where 'channels' => 'Giants', 'scores' => true
21
+ push.data = 'The Giants scored a run! The score is now 2-2.'
22
+ push.to_json.should == '{"where":{"channels":"Giants","scores":true},"data":{"alert":"The Giants scored a run! The score is now 2-2."}}'
23
+ =begin
24
+ TODO:
25
+ curl -X POST \
26
+ -H "X-Parse-Application-Id: " \
27
+ -H "X-Parse-REST-API-Key: " \
28
+ -H "Content-Type: application/json" \
29
+ -d '{
30
+ "where": {
31
+ "user": {
32
+ "$inQuery": {
33
+ "location": {
34
+ "$nearSphere": {
35
+ "__type": "GeoPoint",
36
+ "longitude": -20.0
37
+ },
38
+ "$maxDistanceInMiles": 1.0
39
+ }
40
+ }
41
+ }
42
+ },
43
+ "data": {
44
+ "alert": "Free hotdogs at the Parse concession stand!"
45
+ }
46
+ }' \
47
+ https://api.parse.com/1/push
48
+ =end
49
+ end
50
+
51
+ # it 'should send a customized push message' do
52
+ # end
53
+
54
+ # it 'should send a push message with expiration date' do
55
+ # end
56
+
57
+ # it 'should send a push message with target' do
58
+ # end
59
+
60
+ it 'should send a schedued push message' do
61
+ push = Parse::Push.new
62
+ push.where 'user_id' => 'user_123'
63
+ push.at Time.new(2014, 12, 3, 21, 0, 0, '+09:00')
64
+ push.data = 'You previously created a reminder for the game today'
65
+ push.to_json.should == '{"where":{"user_id":"user_123"},"push_time":"2014-12-03T12:00:00Z","data":{"alert":"You previously created a reminder for the game today"}}'
66
+ end
67
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-10 00:00:00.000000000 Z
12
+ date: 2014-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -96,6 +96,7 @@ files:
96
96
  - lib/parse/op/remove.rb
97
97
  - lib/parse/op/remove_relation.rb
98
98
  - lib/parse/pointer.rb
99
+ - lib/parse/push.rb
99
100
  - lib/parse/query.rb
100
101
  - lib/parse/relation.rb
101
102
  - lib/parse/role.rb
@@ -115,6 +116,7 @@ files:
115
116
  - spec/parse_client_spec.rb
116
117
  - spec/parse_date_spec.rb
117
118
  - spec/parse_object_spec.rb
119
+ - spec/parse_push_spec.rb
118
120
  - spec/parse_query_spec.rb
119
121
  - spec/parse_user_spec.rb
120
122
  - spec/spec_helper.rb
@@ -155,6 +157,7 @@ test_files:
155
157
  - spec/parse_client_spec.rb
156
158
  - spec/parse_date_spec.rb
157
159
  - spec/parse_object_spec.rb
160
+ - spec/parse_push_spec.rb
158
161
  - spec/parse_query_spec.rb
159
162
  - spec/parse_user_spec.rb
160
163
  - spec/spec_helper.rb