punchtab 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +317 -0
- data/Rakefile +2 -0
- data/lib/punchtab.rb +68 -0
- data/lib/punchtab/api.rb +261 -0
- data/lib/punchtab/utils.rb +28 -0
- data/lib/punchtab/version.rb +3 -0
- data/punchtab-enable-sso.png +0 -0
- data/punchtab.gemspec +31 -0
- metadata +98 -0
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2013 Rupak Ganguly
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# punchtab
|
|
2
|
+
|
|
3
|
+
Ruby wrapper for [PunchTab API](http://www.punchtab.com/developer-docs), the world's first instant loyalty platform.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
gem install punchtab
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Prerequisites
|
|
12
|
+
|
|
13
|
+
1. Get a developer account at [PunchTab](http://www.punchtab.com).
|
|
14
|
+
2. Make sure your PunchTab account is enabled for SSO authentication. Do so, by going to the developer account page,
|
|
15
|
+
and checking the 'Single Sign On (SSO)' checkbox.
|
|
16
|
+
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
## Getting Started
|
|
20
|
+
|
|
21
|
+
### Authenticate using Single Sign On (SSO)
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# authenticate with PunchTab
|
|
25
|
+
client = Punchtab::Client.new(
|
|
26
|
+
:client_id => 'your client_id',
|
|
27
|
+
:access_key => 'your access_key',
|
|
28
|
+
:secret_key => 'your secret_key',
|
|
29
|
+
:domain => 'www.mydomain.com',
|
|
30
|
+
:user_info => {
|
|
31
|
+
:first_name => 'your first_name',
|
|
32
|
+
:last_name => 'your last_name',
|
|
33
|
+
:email => 'me@mydomain.com'}
|
|
34
|
+
)
|
|
35
|
+
# if authentication is successful, you should get an access token back
|
|
36
|
+
puts "Access Token: #{client.access_token}"
|
|
37
|
+
|
|
38
|
+
# if authentication fails, an exception is thrown
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
> Note: You can get all of the above values from your Punchtab [developer account page](https://www.punchtab.com/account/).
|
|
42
|
+
|
|
43
|
+
### Authentication
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
# check authentication status
|
|
47
|
+
client.status
|
|
48
|
+
=> {"status"=>"connected",
|
|
49
|
+
"authResponse"=>
|
|
50
|
+
{"userID"=>"111111_1111",
|
|
51
|
+
"uid"=>"111111",
|
|
52
|
+
"accessToken"=>"ed17a5f0ad9e52db0576f39602083dc7"}}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
# logout
|
|
57
|
+
client.logout
|
|
58
|
+
=> {"status"=>"disconnected"}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Activity
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
# get all activities
|
|
65
|
+
client.get_activity
|
|
66
|
+
=> [{"domain"=>"www.webintellix.com",
|
|
67
|
+
"display_name"=>"Webintellix",
|
|
68
|
+
"name"=>"comment",
|
|
69
|
+
"referrer"=>"http://www.webintellix.com",
|
|
70
|
+
"points"=>600,
|
|
71
|
+
"date_created"=>"2013-05-23 06:37:54",
|
|
72
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
73
|
+
"publisher_id"=>2222},
|
|
74
|
+
{"domain"=>"www.webintellix.com",
|
|
75
|
+
"display_name"=>"Webintellix",
|
|
76
|
+
"name"=>"comment",
|
|
77
|
+
"referrer"=>"http://www.webintellix.com",
|
|
78
|
+
"points"=>600,
|
|
79
|
+
"date_created"=>"2013-05-22 04:50:53",
|
|
80
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
81
|
+
"publisher_id"=>2222},
|
|
82
|
+
{"domain"=>"www.webintellix.com",
|
|
83
|
+
"display_name"=>"Webintellix",
|
|
84
|
+
"name"=>"plusone",
|
|
85
|
+
"referrer"=>"http://www.webintellix.com",
|
|
86
|
+
"points"=>500,
|
|
87
|
+
"date_created"=>"2013-05-22 03:36:13",
|
|
88
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
89
|
+
"publisher_id"=>2222},
|
|
90
|
+
{"domain"=>"www.webintellix.com",
|
|
91
|
+
"display_name"=>"Webintellix",
|
|
92
|
+
"name"=>"like",
|
|
93
|
+
"referrer"=>"http://www.webintellix.com",
|
|
94
|
+
"points"=>400,
|
|
95
|
+
"date_created"=>"2013-05-22 02:58:27",
|
|
96
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
97
|
+
"publisher_id"=>2222}]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
# get 2 activities
|
|
102
|
+
client.get_activity(:limit => 2)
|
|
103
|
+
=> [{"domain"=>"www.webintellix.com",
|
|
104
|
+
"display_name"=>"Webintellix",
|
|
105
|
+
"name"=>"comment",
|
|
106
|
+
"referrer"=>"http://www.webintellix.com",
|
|
107
|
+
"points"=>600,
|
|
108
|
+
"date_created"=>"2013-05-22 04:50:53",
|
|
109
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
110
|
+
"publisher_id"=>2222},
|
|
111
|
+
{"domain"=>"www.webintellix.com",
|
|
112
|
+
"display_name"=>"Webintellix",
|
|
113
|
+
"name"=>"plusone",
|
|
114
|
+
"referrer"=>"http://www.webintellix.com",
|
|
115
|
+
"points"=>500,
|
|
116
|
+
"date_created"=>"2013-05-22 03:36:13",
|
|
117
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
118
|
+
"publisher_id"=>2222}]
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
# get only 'like' activities
|
|
123
|
+
client.get_activity(:activity_name => :like)
|
|
124
|
+
=> [{"domain"=>"www.webintellix.com",
|
|
125
|
+
"display_name"=>"Webintellix",
|
|
126
|
+
"name"=>"like",
|
|
127
|
+
"referrer"=>"http://www.webintellix.com",
|
|
128
|
+
"points"=>400,
|
|
129
|
+
"date_created"=>"2013-05-22 02:58:27",
|
|
130
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
131
|
+
"publisher_id"=>2222}]
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
# create a new activity, and assign it relevant 'points'
|
|
136
|
+
client.create_activity(:comment, 600)
|
|
137
|
+
=> [{"domain"=>"www.webintellix.com",
|
|
138
|
+
"user_id"=>111111,
|
|
139
|
+
"name"=>"comment",
|
|
140
|
+
"referrer"=>"http://www.webintellix.com",
|
|
141
|
+
"points"=>600,
|
|
142
|
+
"date_created"=>"2013-05-22 03:54:42",
|
|
143
|
+
"_id"=>"xxxxxxxxxxxxxxxxxx",
|
|
144
|
+
"publisher_id"=>2222,
|
|
145
|
+
"display_name"=>"Webintellix"}]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
# redeem offer for an activity using a 'reward_id'
|
|
150
|
+
client.redeem_activity_offer(123)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### User
|
|
154
|
+
|
|
155
|
+
```ruby
|
|
156
|
+
# get details about the current user
|
|
157
|
+
client.get_user
|
|
158
|
+
=> {"first_name"=>"Rupak",
|
|
159
|
+
"last_name"=>"Ganguly",
|
|
160
|
+
"user_id"=>111111,
|
|
161
|
+
"name"=>"Rupak Ganguly",
|
|
162
|
+
"timeline"=>false,
|
|
163
|
+
"badge_count"=>0,
|
|
164
|
+
"foursquare"=>false,
|
|
165
|
+
"optedOut"=>false,
|
|
166
|
+
"redemptions"=>0,
|
|
167
|
+
"new_lb"=>1,
|
|
168
|
+
"redeemable_points"=>3200,
|
|
169
|
+
"total_points_earned"=>3200,
|
|
170
|
+
"avatar"=>
|
|
171
|
+
"https://s3.amazonaws.com/punchtab-static/img/default_facebook_avatar.jpg"}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Reward
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
# get all the rewards
|
|
178
|
+
client.get_reward
|
|
179
|
+
=> [{"merchantname"=>"Target",
|
|
180
|
+
"image"=>{},
|
|
181
|
+
"label"=>"$5 Target Gift Card",
|
|
182
|
+
"points"=>15000,
|
|
183
|
+
"redeemable"=>false,
|
|
184
|
+
"shipping_address"=>false,
|
|
185
|
+
"id"=>33333,
|
|
186
|
+
"reward_id"=>33333},
|
|
187
|
+
{"merchantname"=>"Starbucks",
|
|
188
|
+
"image"=>{},
|
|
189
|
+
"label"=>"$5 Starbucks Card",
|
|
190
|
+
"points"=>10000,
|
|
191
|
+
"redeemable"=>false,
|
|
192
|
+
"shipping_address"=>false,
|
|
193
|
+
"id"=>44444,
|
|
194
|
+
"reward_id"=>44444}]
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
```ruby
|
|
198
|
+
# get specified number of rewards
|
|
199
|
+
client.get_reward(:limit => 1)
|
|
200
|
+
=> [{"merchantname"=>"Target",
|
|
201
|
+
"image"=>{},
|
|
202
|
+
"label"=>"$5 Target Gift Card",
|
|
203
|
+
"points"=>15000,
|
|
204
|
+
"redeemable"=>false,
|
|
205
|
+
"shipping_address"=>false,
|
|
206
|
+
"id"=>33333,
|
|
207
|
+
"reward_id"=>33333}]
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Leaderboard
|
|
211
|
+
|
|
212
|
+
```ruby
|
|
213
|
+
# get the current user's leaderboard
|
|
214
|
+
client.get_leaderboard
|
|
215
|
+
=> [{"username"=>"111111_1111",
|
|
216
|
+
"recent_activity"=>
|
|
217
|
+
{"domain"=>"www.webintellix.com",
|
|
218
|
+
"display_name"=>"Webintellix",
|
|
219
|
+
"name"=>"comment",
|
|
220
|
+
"referrer"=>"",
|
|
221
|
+
"points"=>600,
|
|
222
|
+
"date_created"=>"2013-05-23 06:37:54.296496",
|
|
223
|
+
"_id"=>"",
|
|
224
|
+
"publisher_id"=>2222},
|
|
225
|
+
"name"=>"Rupak Ganguly",
|
|
226
|
+
"self"=>true,
|
|
227
|
+
"rank"=>1,
|
|
228
|
+
"points"=>3200,
|
|
229
|
+
"avatar"=>
|
|
230
|
+
"https://s3.amazonaws.com/punchtab-static/img/default_facebook_avatar.jpg",
|
|
231
|
+
"user_id"=>111111}]
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
# get the specified user's leaderboard
|
|
236
|
+
client.get_leaderboard(111111)
|
|
237
|
+
=> [{"username"=>"111111_1111",
|
|
238
|
+
"recent_activity"=>
|
|
239
|
+
{"domain"=>"www.webintellix.com",
|
|
240
|
+
"display_name"=>"Webintellix",
|
|
241
|
+
"name"=>"comment",
|
|
242
|
+
"referrer"=>"",
|
|
243
|
+
"points"=>600,
|
|
244
|
+
"date_created"=>"2013-05-23 06:37:54.296496",
|
|
245
|
+
"_id"=>"",
|
|
246
|
+
"publisher_id"=>2222},
|
|
247
|
+
"name"=>"Rupak Ganguly",
|
|
248
|
+
"self"=>true,
|
|
249
|
+
"rank"=>1,
|
|
250
|
+
"points"=>3200,
|
|
251
|
+
"avatar"=>
|
|
252
|
+
"https://s3.amazonaws.com/punchtab-static/img/default_facebook_avatar.jpg",
|
|
253
|
+
"user_id"=>111111}]
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
```ruby
|
|
257
|
+
# get leaderboard for current user with optional parameters
|
|
258
|
+
client.get_leaderboard(:days => 10, :limit => 3, :page => 1)
|
|
259
|
+
=> [{"username"=>"111111_1111",
|
|
260
|
+
"user_id"=>111111,
|
|
261
|
+
"name"=>"Rupak Ganguly",
|
|
262
|
+
"self"=>true,
|
|
263
|
+
"rank"=>1,
|
|
264
|
+
"points"=>3200,
|
|
265
|
+
"avatar"=>
|
|
266
|
+
"https://s3.amazonaws.com/punchtab-static/img/default_facebook_avatar.jpg",
|
|
267
|
+
"recent_activity"=>
|
|
268
|
+
{"domain"=>"www.webintellix.com",
|
|
269
|
+
"display_name"=>"Webintellix",
|
|
270
|
+
"name"=>"comment",
|
|
271
|
+
"referrer"=>"http://www.webintellix.com",
|
|
272
|
+
"points"=>600,
|
|
273
|
+
"date_created"=>"2013-05-23 06:37:54",
|
|
274
|
+
"_id"=>"xxxxxxxxxxxxxxxxxxx",
|
|
275
|
+
"publisher_id"=>2222}}]
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
```ruby
|
|
279
|
+
# get leaderboard for specified user's leaderboard, with optional parameters
|
|
280
|
+
client.get_leaderboard(:with => 111111, :days => 10, :limit => 3, :page => 1)
|
|
281
|
+
=> [{"username"=>"111111_1111",
|
|
282
|
+
"user_id"=>111111,
|
|
283
|
+
"name"=>"Rupak Ganguly",
|
|
284
|
+
"self"=>true,
|
|
285
|
+
"rank"=>1,
|
|
286
|
+
"points"=>3200,
|
|
287
|
+
"avatar"=>
|
|
288
|
+
"https://s3.amazonaws.com/punchtab-static/img/default_facebook_avatar.jpg",
|
|
289
|
+
"recent_activity"=>
|
|
290
|
+
{"domain"=>"www.webintellix.com",
|
|
291
|
+
"display_name"=>"Webintellix",
|
|
292
|
+
"name"=>"comment",
|
|
293
|
+
"referrer"=>"http://www.webintellix.com",
|
|
294
|
+
"points"=>600,
|
|
295
|
+
"date_created"=>"2013-05-23 06:37:54",
|
|
296
|
+
"_id"=>"xxxxxxxxxxxxxxxxxxx",
|
|
297
|
+
"publisher_id"=>2222}}]
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## Roadmap
|
|
301
|
+
|
|
302
|
+
* Add tests
|
|
303
|
+
|
|
304
|
+
## Contributions
|
|
305
|
+
|
|
306
|
+
Patches and Pull Requests are most welcome.
|
|
307
|
+
|
|
308
|
+
* Fork the project.
|
|
309
|
+
* Make your feature addition or bug fix.
|
|
310
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
|
311
|
+
* Commit, but do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
|
|
312
|
+
bump version in a commit by itself, so I can ignore when I pull)
|
|
313
|
+
* Send me a pull request. Bonus points for topic branches.
|
|
314
|
+
|
|
315
|
+
## Copyright
|
|
316
|
+
|
|
317
|
+
Copyright (c) 2013 [Rupak Ganguly](http://about.me/rupakg). See [LICENSE](https://github.com/rupakg/punchtab/blob/master/LICENSE) for details.
|
data/Rakefile
ADDED
data/lib/punchtab.rb
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'hashie'
|
|
5
|
+
|
|
6
|
+
require 'punchtab/version'
|
|
7
|
+
require 'punchtab/utils'
|
|
8
|
+
require 'punchtab/api'
|
|
9
|
+
|
|
10
|
+
module Punchtab
|
|
11
|
+
|
|
12
|
+
class Client
|
|
13
|
+
|
|
14
|
+
attr_reader :access_token
|
|
15
|
+
|
|
16
|
+
def self.version
|
|
17
|
+
Punchtab::VERSION
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# authentication api methods
|
|
21
|
+
def initialize(options = {})
|
|
22
|
+
# initialize the API
|
|
23
|
+
@api = Punchtab::API.new(options)
|
|
24
|
+
|
|
25
|
+
# authenticate
|
|
26
|
+
@access_token = @api.authenticate
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def status
|
|
31
|
+
@api.status
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def logout
|
|
35
|
+
@api.logout
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# activity api methods
|
|
39
|
+
def get_activity(options={})
|
|
40
|
+
@api.get_activity(options)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def create_activity(activity_name, options={})
|
|
44
|
+
@api.create_activity(activity_name, options)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def redeem_activity_offer(reward_id)
|
|
48
|
+
@api.redeem_activity_offer(reward_id)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# user api methods
|
|
52
|
+
def get_user
|
|
53
|
+
@api.get_user
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# reward api methods
|
|
57
|
+
def get_reward(options={})
|
|
58
|
+
@api.get_reward(options)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# leaderboard api methods
|
|
62
|
+
def get_leaderboard(options={})
|
|
63
|
+
@api.get_leaderboard(options)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
data/lib/punchtab/api.rb
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
module Punchtab
|
|
5
|
+
|
|
6
|
+
class API
|
|
7
|
+
include HTTParty
|
|
8
|
+
include Utils
|
|
9
|
+
|
|
10
|
+
BASE_API_URL = 'https://api.punchtab.com/v1'
|
|
11
|
+
ACTIVITIES = %w(visit tweet like plusone comment invite reply apply share purchase addtotimeline search download view checkin subscribe follow)
|
|
12
|
+
|
|
13
|
+
base_uri BASE_API_URL
|
|
14
|
+
format :json
|
|
15
|
+
# enable debug mode
|
|
16
|
+
if ENV['PUNCHTAB_DEBUG'] == '1'
|
|
17
|
+
debug_output
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attr_reader :access_token
|
|
21
|
+
|
|
22
|
+
def initialize(options = {})
|
|
23
|
+
@client_id = options[:client_id] # required
|
|
24
|
+
@access_key = options[:access_key] # required
|
|
25
|
+
@secret_key = options[:secret_key] # required
|
|
26
|
+
@domain = options[:domain] # required
|
|
27
|
+
@user_info = options[:user_info] # optional
|
|
28
|
+
|
|
29
|
+
Punchtab::API.headers 'Referer' => "http://#{@domain}"
|
|
30
|
+
|
|
31
|
+
unless @client_id && @access_key && @secret_key && @domain
|
|
32
|
+
raise Exception.new('Client Id, Access Key, Secret Key and Domain are required to authenticate, before using PunchTab services.')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
######### Authentication APIs
|
|
37
|
+
|
|
38
|
+
# https://api.punchtab.com/v1/auth/sso
|
|
39
|
+
def authenticate
|
|
40
|
+
# setup the user data structure
|
|
41
|
+
user_data = {:id => @client_id}
|
|
42
|
+
if @user_info
|
|
43
|
+
user_data.merge!(@user_info)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# prepare authentication params
|
|
47
|
+
time_stamp = Time.now.to_i
|
|
48
|
+
auth_request = Base64.encode64(JSON.dump(user_data))
|
|
49
|
+
string_to_sign = "#{auth_request} #{time_stamp}"
|
|
50
|
+
hmac = OpenSSL::HMAC.new(@secret_key, OpenSSL::Digest::SHA1.new)
|
|
51
|
+
signature = hmac.update(string_to_sign).hexdigest
|
|
52
|
+
|
|
53
|
+
# make the POST call
|
|
54
|
+
path = '/auth/sso'
|
|
55
|
+
|
|
56
|
+
# setup the post params
|
|
57
|
+
post_data = {
|
|
58
|
+
:client_id => @client_id,
|
|
59
|
+
:key => @access_key,
|
|
60
|
+
:auth_request => auth_request,
|
|
61
|
+
:timestamp => time_stamp,
|
|
62
|
+
:signature => signature
|
|
63
|
+
}
|
|
64
|
+
raw_response = Punchtab::API.post(path, :body => post_data)
|
|
65
|
+
response = Punchtab::Utils.process_response(raw_response)
|
|
66
|
+
# return the access token
|
|
67
|
+
@access_token = response.authResponse.accessToken
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Required Parameters
|
|
71
|
+
# None
|
|
72
|
+
# Optional Parameters
|
|
73
|
+
# None
|
|
74
|
+
# Return
|
|
75
|
+
# https://api.punchtab.com/v1/auth/logout
|
|
76
|
+
def logout
|
|
77
|
+
# make the POST call
|
|
78
|
+
path = '/auth/logout'
|
|
79
|
+
|
|
80
|
+
# setup the post params
|
|
81
|
+
post_data = {
|
|
82
|
+
:token => @access_token,
|
|
83
|
+
:key => @access_key
|
|
84
|
+
}
|
|
85
|
+
raw_response = Punchtab::API.post(path, :body => post_data)
|
|
86
|
+
Punchtab::Utils.process_response(raw_response)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Required Parameters
|
|
90
|
+
# None
|
|
91
|
+
# Optional Parameters
|
|
92
|
+
# None
|
|
93
|
+
# Return
|
|
94
|
+
# https://api.punchtab.com/v1/auth/status
|
|
95
|
+
def status
|
|
96
|
+
# make the POST call
|
|
97
|
+
path = '/auth/status'
|
|
98
|
+
|
|
99
|
+
# setup the post params
|
|
100
|
+
post_data = {
|
|
101
|
+
:token => @access_token,
|
|
102
|
+
:key => @access_key
|
|
103
|
+
}
|
|
104
|
+
raw_response = Punchtab::API.post(path, :body => post_data)
|
|
105
|
+
Punchtab::Utils.process_response(raw_response)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
######### Activity APIs
|
|
109
|
+
|
|
110
|
+
# Required Parameters
|
|
111
|
+
# None
|
|
112
|
+
# Optional Parameters
|
|
113
|
+
# options<~Hash>
|
|
114
|
+
# * 'activity_name'<~String> - retrieve only a list of activities for the activity.
|
|
115
|
+
# * 'limit'<~Integer> - limits the number of activities.
|
|
116
|
+
# * 'user_id'<~Integer> - retrieve the activity for a specific user_id, instead of the user currently logged in.
|
|
117
|
+
# Return
|
|
118
|
+
# https://api.punchtab.com/v1/activity/[activity_name]?access_token=<access_token>
|
|
119
|
+
def get_activity(options={})
|
|
120
|
+
activity_name = options.delete(:activity_name)
|
|
121
|
+
unless activity_name.nil? || activity_valid?(activity_name)
|
|
122
|
+
puts "Specify an activity from the list: '#{activity_list}'"
|
|
123
|
+
return
|
|
124
|
+
end
|
|
125
|
+
# make the GET call
|
|
126
|
+
if activity_name
|
|
127
|
+
path = "/activity/#{activity_name}"
|
|
128
|
+
else
|
|
129
|
+
path = '/activity'
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
options.merge!({:access_token => @access_token})
|
|
133
|
+
raw_response = Punchtab::API.get(path, :query => options)
|
|
134
|
+
Punchtab::Utils.process_response(raw_response)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Required Parameters
|
|
138
|
+
# * 'activity_name'<~String> - retrieve only a list of activity from the activity_name.
|
|
139
|
+
# * 'points'<~Integer> - points for the activity, default is 100
|
|
140
|
+
# Optional Parameters
|
|
141
|
+
# None
|
|
142
|
+
# Return
|
|
143
|
+
# curl i -X POST 'points=200' https://api.punchtab.com/v1/activity/<activity_name>?access_token=<access_token>
|
|
144
|
+
def create_activity(activity_name, points=100)
|
|
145
|
+
unless activity_valid?(activity_name)
|
|
146
|
+
puts "Specify an activity from the list: '#{activity_list}'"
|
|
147
|
+
return
|
|
148
|
+
end
|
|
149
|
+
# make the POST call
|
|
150
|
+
if activity_name
|
|
151
|
+
path = "/activity/#{activity_name}"
|
|
152
|
+
else
|
|
153
|
+
path = '/activity'
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
options = {:access_token => @access_token}
|
|
157
|
+
raw_response = Punchtab::API.post(path, {:body => "points=#{points}", :query => options})
|
|
158
|
+
Punchtab::Utils.process_response(raw_response)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Required Parameters
|
|
162
|
+
# * 'reward_id'<~Integer> - reward id for the activity offer to redeem
|
|
163
|
+
# Optional Parameters
|
|
164
|
+
# None
|
|
165
|
+
# Return
|
|
166
|
+
# curl i -X POST 'reward_id=123' https://api.punchtab.com/v1/activity/redeem?access_token=<access_token>
|
|
167
|
+
def redeem_activity_offer(reward_id)
|
|
168
|
+
|
|
169
|
+
# make the POST call
|
|
170
|
+
path = '/activity/redeem'
|
|
171
|
+
|
|
172
|
+
options = {:access_token => @access_token}
|
|
173
|
+
raw_response = Punchtab::API.post(path, {:body => "reward_id=#{reward_id}", :query => options })
|
|
174
|
+
Punchtab::Utils.process_response(raw_response)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
######### User APIs
|
|
178
|
+
|
|
179
|
+
# Required Parameters
|
|
180
|
+
# None
|
|
181
|
+
# Optional Parameters
|
|
182
|
+
# None
|
|
183
|
+
# Return
|
|
184
|
+
# https://api.punchtab.com/v1/user?access_token=<access_token>
|
|
185
|
+
def get_user
|
|
186
|
+
# make the GET call
|
|
187
|
+
path = '/user'
|
|
188
|
+
|
|
189
|
+
options = {:access_token => @access_token}
|
|
190
|
+
raw_response = Punchtab::API.get(path, :query => options)
|
|
191
|
+
Punchtab::Utils.process_response(raw_response)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
######### Reward APIs
|
|
195
|
+
|
|
196
|
+
# Required Parameters
|
|
197
|
+
# None
|
|
198
|
+
# Optional Parameters
|
|
199
|
+
# options<~Hash>
|
|
200
|
+
# * 'limit'<~Integer> - limits the number of rewards.
|
|
201
|
+
# Return
|
|
202
|
+
# https://api.punchtab.com/v1/reward?access_token=<access_token>
|
|
203
|
+
def get_reward(options={})
|
|
204
|
+
# make the GET call
|
|
205
|
+
path = '/reward'
|
|
206
|
+
|
|
207
|
+
options.merge!({:access_token => @access_token})
|
|
208
|
+
raw_response = Punchtab::API.get(path, :query => options)
|
|
209
|
+
Punchtab::Utils.process_response(raw_response)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
######### Leaderboard APIs
|
|
213
|
+
|
|
214
|
+
# Required Parameters
|
|
215
|
+
# None
|
|
216
|
+
# Optional Parameters
|
|
217
|
+
# options<~Hash>
|
|
218
|
+
# * 'with'<~String> - User Id or 'me'. Defaults to 'me', which will get a leaderboard with the current user.
|
|
219
|
+
# * 'days'<~String> - if set to 'all' returns the leaderboard from the beginning using redeemable points, otherwise it returns the leaderboard from the last 30 days.
|
|
220
|
+
# * 'limit'<~Integer> - limits the number of users in the leaderboard.
|
|
221
|
+
# * 'page'<~Integer> - specifies the page of result you want (rank will be relative to the page).
|
|
222
|
+
# Return
|
|
223
|
+
# https://api.punchtab.com/v1/leaderboard?access_token=<access_token>
|
|
224
|
+
def get_leaderboard(options={})
|
|
225
|
+
# make the GET call
|
|
226
|
+
path = '/leaderboard'
|
|
227
|
+
|
|
228
|
+
options.merge!({:with => 'me', :access_token => @access_token})
|
|
229
|
+
raw_response = Punchtab::API.get(path, :query => options)
|
|
230
|
+
Punchtab::Utils.process_response(raw_response)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
private
|
|
235
|
+
|
|
236
|
+
def activity_valid?(activity_name)
|
|
237
|
+
ACTIVITIES.include?(activity_name.to_s)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def activity_list
|
|
241
|
+
ACTIVITIES.join(',')
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
### Hack monkey-patch to rescue parsing errors
|
|
248
|
+
module HTTParty
|
|
249
|
+
class Request
|
|
250
|
+
|
|
251
|
+
private
|
|
252
|
+
def parse_response(body)
|
|
253
|
+
begin
|
|
254
|
+
parser.call(body, format)
|
|
255
|
+
rescue Exception
|
|
256
|
+
body
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
end
|
|
261
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Punchtab
|
|
2
|
+
|
|
3
|
+
module Utils
|
|
4
|
+
|
|
5
|
+
def self.process_response(raw_response)
|
|
6
|
+
if (500..599).include?(raw_response.code)
|
|
7
|
+
raise Exception.new("#{raw_response.code} Server Error")
|
|
8
|
+
elsif raw_response.code == 400
|
|
9
|
+
raise Exception.new("#{raw_response.code} Bad Request")
|
|
10
|
+
elsif (200..208).include?(raw_response.code) || raw_response.code == 226
|
|
11
|
+
begin
|
|
12
|
+
response = Hashie::Mash.new(raw_response)
|
|
13
|
+
rescue
|
|
14
|
+
return raw_response
|
|
15
|
+
end
|
|
16
|
+
begin
|
|
17
|
+
if response.error
|
|
18
|
+
raise Exception.new(raw_response)
|
|
19
|
+
else
|
|
20
|
+
return response
|
|
21
|
+
end
|
|
22
|
+
rescue
|
|
23
|
+
raise Exception.new(raw_response)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
Binary file
|
data/punchtab.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
|
3
|
+
require 'punchtab/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = 'punchtab'
|
|
7
|
+
s.version = Punchtab::VERSION
|
|
8
|
+
s.platform = Gem::Platform::RUBY
|
|
9
|
+
s.authors = ['Rupak Ganguly']
|
|
10
|
+
s.date = %q{2013-05-24}
|
|
11
|
+
s.email = ['rupakg@gmail.com']
|
|
12
|
+
s.homepage = %q{http://github.com/rupakg/punchtab}
|
|
13
|
+
s.summary = %q{Ruby wrapper for PunchTab API}
|
|
14
|
+
s.description = %q{Ruby wrapper for PunchTab API. PunchTab is the world's first instant loyalty platform.}
|
|
15
|
+
s.license = 'MIT'
|
|
16
|
+
|
|
17
|
+
s.files = `git ls-files`.split("\n")
|
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
|
+
s.require_paths = ['lib']
|
|
21
|
+
|
|
22
|
+
s.add_runtime_dependency('json', '~> 1.7.7')
|
|
23
|
+
s.add_runtime_dependency('hashie', '~> 2.0')
|
|
24
|
+
s.add_runtime_dependency('httparty', '~> 0.11')
|
|
25
|
+
|
|
26
|
+
#s.add_development_dependency(%q<shoulda>, ['>= 2.10.1'])
|
|
27
|
+
#s.add_development_dependency(%q<jnunemaker-matchy>, ['= 0.4.0'])
|
|
28
|
+
#s.add_development_dependency(%q<mocha>, ['~> 0.9.12'])
|
|
29
|
+
#s.add_development_dependency(%q<fakeweb>, ['~> 1.3.0'])
|
|
30
|
+
|
|
31
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: punchtab
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease:
|
|
5
|
+
version: 0.1.0
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Rupak Ganguly
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2013-05-24 00:00:00 Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: json
|
|
17
|
+
prerelease: false
|
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
19
|
+
none: false
|
|
20
|
+
requirements:
|
|
21
|
+
- - ~>
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: 1.7.7
|
|
24
|
+
type: :runtime
|
|
25
|
+
version_requirements: *id001
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: hashie
|
|
28
|
+
prerelease: false
|
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
30
|
+
none: false
|
|
31
|
+
requirements:
|
|
32
|
+
- - ~>
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: "2.0"
|
|
35
|
+
type: :runtime
|
|
36
|
+
version_requirements: *id002
|
|
37
|
+
- !ruby/object:Gem::Dependency
|
|
38
|
+
name: httparty
|
|
39
|
+
prerelease: false
|
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ~>
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0.11"
|
|
46
|
+
type: :runtime
|
|
47
|
+
version_requirements: *id003
|
|
48
|
+
description: Ruby wrapper for PunchTab API. PunchTab is the world's first instant loyalty platform.
|
|
49
|
+
email:
|
|
50
|
+
- rupakg@gmail.com
|
|
51
|
+
executables: []
|
|
52
|
+
|
|
53
|
+
extensions: []
|
|
54
|
+
|
|
55
|
+
extra_rdoc_files: []
|
|
56
|
+
|
|
57
|
+
files:
|
|
58
|
+
- .gitignore
|
|
59
|
+
- CHANGELOG.md
|
|
60
|
+
- Gemfile
|
|
61
|
+
- LICENSE
|
|
62
|
+
- README.md
|
|
63
|
+
- Rakefile
|
|
64
|
+
- lib/punchtab.rb
|
|
65
|
+
- lib/punchtab/api.rb
|
|
66
|
+
- lib/punchtab/utils.rb
|
|
67
|
+
- lib/punchtab/version.rb
|
|
68
|
+
- punchtab-enable-sso.png
|
|
69
|
+
- punchtab.gemspec
|
|
70
|
+
homepage: http://github.com/rupakg/punchtab
|
|
71
|
+
licenses:
|
|
72
|
+
- MIT
|
|
73
|
+
post_install_message:
|
|
74
|
+
rdoc_options: []
|
|
75
|
+
|
|
76
|
+
require_paths:
|
|
77
|
+
- lib
|
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
|
+
none: false
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: "0"
|
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
|
+
none: false
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: "0"
|
|
90
|
+
requirements: []
|
|
91
|
+
|
|
92
|
+
rubyforge_project:
|
|
93
|
+
rubygems_version: 1.8.24
|
|
94
|
+
signing_key:
|
|
95
|
+
specification_version: 3
|
|
96
|
+
summary: Ruby wrapper for PunchTab API
|
|
97
|
+
test_files: []
|
|
98
|
+
|