reward_sciences 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +24 -0
- data/README.md +380 -0
- data/lib/reward_sciences/api_helper.rb +151 -0
- data/lib/reward_sciences/configuration.rb +10 -0
- data/lib/reward_sciences/controllers/activities.rb +75 -0
- data/lib/reward_sciences/controllers/base_controller.rb +32 -0
- data/lib/reward_sciences/controllers/reward_categories.rb +61 -0
- data/lib/reward_sciences/controllers/rewards.rb +246 -0
- data/lib/reward_sciences/controllers/users.rb +123 -0
- data/lib/reward_sciences/exceptions/api_exception.rb +28 -0
- data/lib/reward_sciences/http/http_call_back.rb +17 -0
- data/lib/reward_sciences/http/http_client.rb +112 -0
- data/lib/reward_sciences/http/http_context.rb +15 -0
- data/lib/reward_sciences/http/http_method_enum.rb +7 -0
- data/lib/reward_sciences/http/http_request.rb +28 -0
- data/lib/reward_sciences/http/http_response.rb +19 -0
- data/lib/reward_sciences/http/unirest_client.rb +44 -0
- data/lib/reward_sciences/reward_sciences_client.rb +37 -0
- data/lib/reward_sciences.rb +28 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d3287bcbfff41333a60c6808e7453211285da59
|
4
|
+
data.tar.gz: f4bea6053d3d024b0bfd3b7b4d189ec50abfbd37
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd0c396879afd04439c44c165d59126fb6a18ab6ad5685b75f9fc4792435155210579718b25de65549dcc73528c4f569cbd5202493cfbea019c24b8df8abb076
|
7
|
+
data.tar.gz: 9901514c1a87a92db4bc57a0609681b4b67644ddfa14ac5df04e0daad1a292b1ed028d0c93d744041de46e753ff773b03b46b86cb822787744b8f9487322d570
|
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
License:
|
2
|
+
========
|
3
|
+
The MIT License (MIT)
|
4
|
+
http://opensource.org/licenses/MIT
|
5
|
+
|
6
|
+
Copyright (c) 2014 - 2016 APIMATIC Limited
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
16
|
+
all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,380 @@
|
|
1
|
+
# Getting Started
|
2
|
+
## How to Build
|
3
|
+
|
4
|
+
This client library is a Ruby gem which can be compiled and used in your Ruby and Ruby on Rails project. This library requires a few gems from the RubyGems repository.
|
5
|
+
|
6
|
+
1. Open the command line interface or the terminal and navigate to the folder containing the source code.
|
7
|
+
2. Run ``` gem build reward_sciences.gemspec ``` to build the gem.
|
8
|
+
3. Once built, the gem can be installed on the current work environment using ``` gem install reward_sciences-1.0.gem ```
|
9
|
+
|
10
|
+
![Building Gem](http://apidocs.io/illustration/ruby?step=buildSDK&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
11
|
+
|
12
|
+
## How to Use
|
13
|
+
|
14
|
+
The following section explains how to use the RewardSciences Ruby Gem in a new Rails project using RubyMine™. The basic workflow presented here is also applicable if you prefer using a different editor or IDE.
|
15
|
+
|
16
|
+
#### 1. Starting a new project
|
17
|
+
Close any existing projects in RubyMine™ by selecting ``` File -> Close Project ```. Next, click on ``` Create New Project ``` to create a new project from scratch.
|
18
|
+
|
19
|
+
![Create a new project in RubyMine](http://apidocs.io/illustration/ruby?step=createNewProject0&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
20
|
+
|
21
|
+
Next, provide ``` TestApp ``` as the project name, choose ``` Rails Application ``` as the project type, and click ``` OK ```.
|
22
|
+
|
23
|
+
![Create a new Rails Application in RubyMine - step 1](http://apidocs.io/illustration/ruby?step=createNewProject1&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
24
|
+
|
25
|
+
In the next dialog make sure that correct *Ruby SDK* is being used (minimum 2.0.0) and click ``` OK ```.
|
26
|
+
|
27
|
+
![Create a new Rails Application in RubyMine - step 2](http://apidocs.io/illustration/ruby?step=createNewProject2&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
28
|
+
|
29
|
+
This will create a new Rails Application project with an existing set of files and folder.
|
30
|
+
|
31
|
+
#### 2. Add reference of the gem
|
32
|
+
In order to use the RewardSciences gem in the new project we must add a gem reference. Locate the ``` Gemfile ``` in the *Project Explorer* window under the ``` TestApp ``` project node. The file contains references to all gems being used in the project. Here, add the reference to the library gem by adding the following line: ``` gem 'reward_sciences', '~> 1.0' ```
|
33
|
+
|
34
|
+
![Add references of the Gemfile](http://apidocs.io/illustration/ruby?step=addReference&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
35
|
+
|
36
|
+
|
37
|
+
#### 3. Adding a new Rails Controller
|
38
|
+
Once the ``` TestApp ``` project is created, a folder named ``` controllers ``` will be visible in the *Project Explorer* under the following path: ``` TestApp > app > controllers ```. Right click on this folder and select ``` New -> Run Rails Generator... ```.
|
39
|
+
|
40
|
+
![Run Rails Generator on Controllers Folder](http://apidocs.io/illustration/ruby?step=addCode0&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
41
|
+
|
42
|
+
Selecting the said option will popup a small window where the generator names are displayed. Here, select the ``` controller ``` template.
|
43
|
+
|
44
|
+
![Create a new Controller](http://apidocs.io/illustration/ruby?step=addCode1&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
45
|
+
|
46
|
+
Next, a popup window will ask you for a Controller name and included Actions. For controller name provide ``` Hello ``` and include an action named ``` Index ``` and click ``` OK ```.
|
47
|
+
|
48
|
+
![Add a new Controller](http://apidocs.io/illustration/ruby?step=addCode2&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
49
|
+
|
50
|
+
A new controller class anmed ``` HelloController ``` will be created in a file named ``` hello_controller.rb ``` containing a method named ``` Index ```. In this method, add code for initialization and a sample for its usage.
|
51
|
+
|
52
|
+
![Initialize the library](http://apidocs.io/illustration/ruby?step=addCode3&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0)
|
53
|
+
|
54
|
+
## How to Test
|
55
|
+
|
56
|
+
You can test the generated SDK and the server with automatically generated test
|
57
|
+
cases as follows:
|
58
|
+
|
59
|
+
1. From terminal/cmd navigate to the root directory of the SDK.
|
60
|
+
2. Invoke: `bundle exec rake`
|
61
|
+
|
62
|
+
## Initialization
|
63
|
+
|
64
|
+
#### Authentication and Initialization
|
65
|
+
In order to setup authentication and initialization of the API client, you need the following information.
|
66
|
+
|
67
|
+
| Parameter | Description |
|
68
|
+
|-----------|-------------|
|
69
|
+
| o_auth_access_token | The OAuth 2.0 access token to be set before API calls |
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
API client can be initialized as following.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
# Configuration parameters and credentials
|
77
|
+
o_auth_access_token = "o_auth_access_token"; # The OAuth 2.0 access token to be set before API calls
|
78
|
+
|
79
|
+
client = RewardSciences::RewardSciencesClient.new(o_auth_access_token)
|
80
|
+
```
|
81
|
+
|
82
|
+
The added initlization code can be debugged by putting a breakpoint in the ``` Index ``` method and running the project in debug mode by selecting ``` Run -> Debug 'Development: TestApp' ```.
|
83
|
+
|
84
|
+
![Debug the TestApp](http://apidocs.io/illustration/ruby?step=addCode4&workspaceFolder=Reward Sciences-Ruby&workspaceName=RewardSciences&projectName=reward_sciences&gemName=reward_sciences&gemVer=1.0&initLine=client%20%3D%20RewardSciencesClient.new%28%27o_auth_access_token%27%29)
|
85
|
+
|
86
|
+
# Class Reference
|
87
|
+
## <a name="list_of_controllers"></a>List of Controllers
|
88
|
+
|
89
|
+
* [Rewards](#rewards)
|
90
|
+
* [RewardCategories](#reward_categories)
|
91
|
+
* [Users](#users)
|
92
|
+
* [Activities](#activities)
|
93
|
+
|
94
|
+
## <a name="rewards"></a>![Class: ](http://apidocs.io/img/class.png ".Rewards") Rewards
|
95
|
+
|
96
|
+
#### Get singleton instance
|
97
|
+
The singleton instance of the ``` Rewards ``` class can be accessed from the API Client.
|
98
|
+
```ruby
|
99
|
+
rewards = client.rewards
|
100
|
+
```
|
101
|
+
|
102
|
+
### <a name="bid"></a>![Method: ](http://apidocs.io/img/method.png ".Rewards.bid") bid
|
103
|
+
|
104
|
+
> Bid on a reward auction.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
def bid(user_id,
|
108
|
+
reward_id,
|
109
|
+
amount); end
|
110
|
+
```
|
111
|
+
|
112
|
+
#### Parameters:
|
113
|
+
|
114
|
+
| Parameter | Tags | Description |
|
115
|
+
|-----------|------|-------------|
|
116
|
+
| user_id | ``` Required ``` | The ID of the user who is bidding on the reward auction. |
|
117
|
+
| reward_id | ``` Required ``` | The ID of the reward auction to be bid on. |
|
118
|
+
| amount | ``` Required ``` | Can be either 'max' (when max bidding) or the number of points the user wants to bid. |
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
#### Example Usage:
|
123
|
+
```ruby
|
124
|
+
user_id = 225
|
125
|
+
reward_id = 225
|
126
|
+
amount = 'amount'
|
127
|
+
|
128
|
+
result = rewards.bid(user_id, reward_id, amount)
|
129
|
+
|
130
|
+
```
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
### <a name="list"></a>![Method: ](http://apidocs.io/img/method.png ".Rewards.list") list
|
136
|
+
|
137
|
+
> List all the available rewards.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
def list(category_id = nil,
|
141
|
+
limit = 25,
|
142
|
+
offset = 0); end
|
143
|
+
```
|
144
|
+
|
145
|
+
#### Parameters:
|
146
|
+
|
147
|
+
| Parameter | Tags | Description |
|
148
|
+
|-----------|------|-------------|
|
149
|
+
| category_id | ``` Optional ``` | The id of the category to filter rewards by |
|
150
|
+
| limit | ``` Optional ``` ``` DefaultValue ``` | The number of rewards you want to be retrieved. |
|
151
|
+
| offset | ``` Optional ``` ``` DefaultValue ``` | The number of rewards you want to skip before starting the retrieval. |
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
#### Example Usage:
|
156
|
+
```ruby
|
157
|
+
category_id = 225
|
158
|
+
limit = 25
|
159
|
+
offset = 0
|
160
|
+
|
161
|
+
result = rewards.list(category_id, limit, offset)
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
### <a name="redeem"></a>![Method: ](http://apidocs.io/img/method.png ".Rewards.redeem") redeem
|
169
|
+
|
170
|
+
> Redeem a reward.
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
def redeem(user_id,
|
174
|
+
reward_id); end
|
175
|
+
```
|
176
|
+
|
177
|
+
#### Parameters:
|
178
|
+
|
179
|
+
| Parameter | Tags | Description |
|
180
|
+
|-----------|------|-------------|
|
181
|
+
| user_id | ``` Required ``` | The ID of the user who is redeeming the reward. |
|
182
|
+
| reward_id | ``` Required ``` | The ID of the reward to be redeemed. |
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
#### Example Usage:
|
187
|
+
```ruby
|
188
|
+
user_id = 225
|
189
|
+
reward_id = 225
|
190
|
+
|
191
|
+
result = rewards.redeem(user_id, reward_id)
|
192
|
+
|
193
|
+
```
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
### <a name="show"></a>![Method: ](http://apidocs.io/img/method.png ".Rewards.show") show
|
199
|
+
|
200
|
+
> Show a reward's details.
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
def show(reward_id); end
|
204
|
+
```
|
205
|
+
|
206
|
+
#### Parameters:
|
207
|
+
|
208
|
+
| Parameter | Tags | Description |
|
209
|
+
|-----------|------|-------------|
|
210
|
+
| reward_id | ``` Required ``` | The ID of the reward to be retrieved. |
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
#### Example Usage:
|
215
|
+
```ruby
|
216
|
+
reward_id = 225
|
217
|
+
|
218
|
+
result = rewards.show(reward_id)
|
219
|
+
|
220
|
+
```
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
[Back to List of Controllers](#list_of_controllers)
|
226
|
+
## <a name="reward_categories"></a>![Class: ](http://apidocs.io/img/class.png ".RewardCategories") RewardCategories
|
227
|
+
|
228
|
+
#### Get singleton instance
|
229
|
+
The singleton instance of the ``` RewardCategories ``` class can be accessed from the API Client.
|
230
|
+
```ruby
|
231
|
+
rewardCategories = client.reward_categories
|
232
|
+
```
|
233
|
+
|
234
|
+
### <a name="list"></a>![Method: ](http://apidocs.io/img/method.png ".RewardCategories.list") list
|
235
|
+
|
236
|
+
> List all the available reward categories.
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
def list(limit = 25,
|
240
|
+
offset = 0); end
|
241
|
+
```
|
242
|
+
|
243
|
+
#### Parameters:
|
244
|
+
|
245
|
+
| Parameter | Tags | Description |
|
246
|
+
|-----------|------|-------------|
|
247
|
+
| limit | ``` Optional ``` ``` DefaultValue ``` | The number of reward categories you want to be retrieved. |
|
248
|
+
| offset | ``` Optional ``` ``` DefaultValue ``` | The number of reward categories you want to skip before starting the retrieval. |
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
#### Example Usage:
|
253
|
+
```ruby
|
254
|
+
limit = 25
|
255
|
+
offset = 0
|
256
|
+
|
257
|
+
result = rewardCategories.list(limit, offset)
|
258
|
+
|
259
|
+
```
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
[Back to List of Controllers](#list_of_controllers)
|
265
|
+
## <a name="users"></a>![Class: ](http://apidocs.io/img/class.png ".Users") Users
|
266
|
+
|
267
|
+
#### Get singleton instance
|
268
|
+
The singleton instance of the ``` Users ``` class can be accessed from the API Client.
|
269
|
+
```ruby
|
270
|
+
users = client.users
|
271
|
+
```
|
272
|
+
|
273
|
+
### <a name="show"></a>![Method: ](http://apidocs.io/img/method.png ".Users.show") show
|
274
|
+
|
275
|
+
> This endpoint lets retrieve a user's details.
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
def show(user_id); end
|
279
|
+
```
|
280
|
+
|
281
|
+
#### Parameters:
|
282
|
+
|
283
|
+
| Parameter | Tags | Description |
|
284
|
+
|-----------|------|-------------|
|
285
|
+
| user_id | ``` Required ``` | The ID of the user to be retrieved. |
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
#### Example Usage:
|
290
|
+
```ruby
|
291
|
+
user_id = 225
|
292
|
+
|
293
|
+
result = users.show(user_id)
|
294
|
+
|
295
|
+
```
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
### <a name="identify"></a>![Method: ](http://apidocs.io/img/method.png ".Users.identify") identify
|
301
|
+
|
302
|
+
> This endpoint lets you tie a user with his/her activities. You’ll want to identify a user with any relevant information as soon as they log-in or sign-up.
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
def identify(email,
|
306
|
+
first_name = nil,
|
307
|
+
last_name = nil); end
|
308
|
+
```
|
309
|
+
|
310
|
+
#### Parameters:
|
311
|
+
|
312
|
+
| Parameter | Tags | Description |
|
313
|
+
|-----------|------|-------------|
|
314
|
+
| email | ``` Required ``` | The user's email address |
|
315
|
+
| first_name | ``` Optional ``` | The user's first name |
|
316
|
+
| last_name | ``` Optional ``` | The user's last name |
|
317
|
+
|
318
|
+
|
319
|
+
|
320
|
+
#### Example Usage:
|
321
|
+
```ruby
|
322
|
+
email = 'email'
|
323
|
+
first_name = 'first_name'
|
324
|
+
last_name = 'last_name'
|
325
|
+
|
326
|
+
result = users.identify(email, first_name, last_name)
|
327
|
+
|
328
|
+
```
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
[Back to List of Controllers](#list_of_controllers)
|
334
|
+
## <a name="activities"></a>![Class: ](http://apidocs.io/img/class.png ".Activities") Activities
|
335
|
+
|
336
|
+
#### Get singleton instance
|
337
|
+
The singleton instance of the ``` Activities ``` class can be accessed from the API Client.
|
338
|
+
```ruby
|
339
|
+
activities = client.activities
|
340
|
+
```
|
341
|
+
|
342
|
+
### <a name="track"></a>![Method: ](http://apidocs.io/img/method.png ".Activities.track") track
|
343
|
+
|
344
|
+
> This endpoint lets you track the activities your users perform.
|
345
|
+
|
346
|
+
```ruby
|
347
|
+
def track(user_id,
|
348
|
+
activity_type,
|
349
|
+
price = nil,
|
350
|
+
record_id = nil); end
|
351
|
+
```
|
352
|
+
|
353
|
+
#### Parameters:
|
354
|
+
|
355
|
+
| Parameter | Tags | Description |
|
356
|
+
|-----------|------|-------------|
|
357
|
+
| user_id | ``` Required ``` | The ID of the user who is performing the activity. |
|
358
|
+
| activity_type | ``` Required ``` | The type of activity the user is performing. Example: 'purchased-a-product' |
|
359
|
+
| price | ``` Optional ``` | The price related to the activity, if any. Expressed in USD |
|
360
|
+
| record_id | ``` Optional ``` | The ID for the record associated with the activity in your database. |
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
#### Example Usage:
|
365
|
+
```ruby
|
366
|
+
user_id = 225
|
367
|
+
activity_type = 'activity_type'
|
368
|
+
price = 225
|
369
|
+
record_id = 'record_id'
|
370
|
+
|
371
|
+
result = activities.track(user_id, activity_type, price, record_id)
|
372
|
+
|
373
|
+
```
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
|
378
|
+
[Back to List of Controllers](#list_of_controllers)
|
379
|
+
|
380
|
+
|
@@ -0,0 +1,151 @@
|
|
1
|
+
# This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
|
2
|
+
|
3
|
+
module RewardSciences
|
4
|
+
class APIHelper
|
5
|
+
# Replaces template parameters in the given url
|
6
|
+
# @param [String] The query string builder to replace the template parameters
|
7
|
+
# @param [Array] The parameters to replace in the url
|
8
|
+
def self.append_url_with_template_parameters(query_builder, parameters)
|
9
|
+
# perform parameter validation
|
10
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
11
|
+
|
12
|
+
# return if there are no parameters to replace
|
13
|
+
if parameters.nil?
|
14
|
+
query_builder
|
15
|
+
else
|
16
|
+
# iterate and append parameters
|
17
|
+
parameters.each do |key, value|
|
18
|
+
replace_value = ''
|
19
|
+
|
20
|
+
if value.nil?
|
21
|
+
replace_value = ''
|
22
|
+
elsif value.instance_of? Array
|
23
|
+
value.map!{|element| CGI.escape(element.to_s)}
|
24
|
+
replace_value = value.join('/')
|
25
|
+
else
|
26
|
+
replace_value = CGI.escape(value.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
# find the template parameter and replace it with its value
|
30
|
+
query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return query_builder
|
34
|
+
end
|
35
|
+
|
36
|
+
# Appends the given set of parameters to the given query string
|
37
|
+
# @param [String] The query string builder to replace the template parameters
|
38
|
+
# @param [Array] The parameters to append
|
39
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
40
|
+
# perform parameter validation
|
41
|
+
raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
|
42
|
+
|
43
|
+
# return if there are no parameters to replace
|
44
|
+
if parameters.nil?
|
45
|
+
return query_builder
|
46
|
+
else
|
47
|
+
# remove any nil values
|
48
|
+
parameters = parameters.reject { |_key, value| value.nil? }
|
49
|
+
|
50
|
+
# does the query string already has parameters
|
51
|
+
has_params = query_builder.include? '?'
|
52
|
+
separator = has_params ? '&' : '?'
|
53
|
+
|
54
|
+
# append query with separator and parameters and return
|
55
|
+
return query_builder << separator << URI.encode_www_form(parameters)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Validates and processes the given Url
|
60
|
+
# @param [String] The given Url to process
|
61
|
+
# @return [String] Pre-processed Url as string
|
62
|
+
def self.clean_url(url)
|
63
|
+
# perform parameter validation
|
64
|
+
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
65
|
+
|
66
|
+
# ensure that the urls are absolute
|
67
|
+
matches = url.match(%r{^(https?:\/\/[^\/]+)})
|
68
|
+
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
69
|
+
|
70
|
+
# get the http protocol match
|
71
|
+
protocol = matches[1]
|
72
|
+
|
73
|
+
# check if parameters exist
|
74
|
+
index = url.index('?')
|
75
|
+
|
76
|
+
# remove redundant forward slashes
|
77
|
+
query = url[protocol.length...(index != nil ? index : url.length)]
|
78
|
+
query.gsub!(%r{\/\/+}, '/')
|
79
|
+
|
80
|
+
# get the parameters
|
81
|
+
parameters = index != nil ? url[url.index('?')...url.length] : ""
|
82
|
+
|
83
|
+
# return processed url
|
84
|
+
return protocol + query + parameters
|
85
|
+
end
|
86
|
+
|
87
|
+
# Parses JSON string.
|
88
|
+
# @param [String] A JSON string.
|
89
|
+
def self.json_deserialize(json)
|
90
|
+
begin
|
91
|
+
return JSON.parse(json)
|
92
|
+
rescue
|
93
|
+
raise TypeError, "Server responded with invalid JSON."
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Form encodes a hash of parameters.
|
98
|
+
# @param [Hash] The hash of parameters to encode.
|
99
|
+
# @return [Hash] A hash with the same parameters form encoded.
|
100
|
+
def self.form_encode_parameters(form_parameters)
|
101
|
+
encoded = Hash.new
|
102
|
+
form_parameters.each do |key, value|
|
103
|
+
encoded.merge!(APIHelper.form_encode value, key)
|
104
|
+
end
|
105
|
+
return encoded
|
106
|
+
end
|
107
|
+
|
108
|
+
# Form encodes an object.
|
109
|
+
# @param [Dynamic] An object to form encode.
|
110
|
+
# @param [String] The name of the object.
|
111
|
+
# @return [Hash] A form encoded representation of the object in the form of a hash.
|
112
|
+
def self.form_encode(obj, instance_name)
|
113
|
+
retval = Hash.new
|
114
|
+
|
115
|
+
# If this is a structure, resolve it's field names.
|
116
|
+
if obj.kind_of? BaseModel
|
117
|
+
obj = obj.to_hash
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a form encoded hash for this object.
|
121
|
+
if obj == nil
|
122
|
+
nil
|
123
|
+
elsif obj.instance_of? Array
|
124
|
+
obj.each_with_index do |value, index|
|
125
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]"))
|
126
|
+
end
|
127
|
+
elsif obj.instance_of? Hash
|
128
|
+
obj.each do |key, value|
|
129
|
+
retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]"))
|
130
|
+
end
|
131
|
+
else
|
132
|
+
retval[instance_name] = obj
|
133
|
+
end
|
134
|
+
return retval
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# extend types to support to_bool
|
140
|
+
module ToBoolean
|
141
|
+
def to_bool
|
142
|
+
return true if self == true || self.to_s.strip =~ /^(true|yes|y|1)$/i
|
143
|
+
return false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
class NilClass; include ToBoolean; end
|
148
|
+
class TrueClass; include ToBoolean; end
|
149
|
+
class FalseClass; include ToBoolean; end
|
150
|
+
class Numeric; include ToBoolean; end
|
151
|
+
class String; include ToBoolean; end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
|
2
|
+
|
3
|
+
module RewardSciences
|
4
|
+
class Activities < BaseController
|
5
|
+
@@instance = Activities.new
|
6
|
+
# Singleton instance of the controller class
|
7
|
+
def self.instance
|
8
|
+
@@instance
|
9
|
+
end
|
10
|
+
|
11
|
+
# This endpoint lets you track the activities your users perform.
|
12
|
+
# @param [Integer] user_id Required parameter: The ID of the user who is performing the activity.
|
13
|
+
# @param [String] activity_type Required parameter: The type of activity the user is performing. Example: 'purchased-a-product'
|
14
|
+
# @param [Integer] price Optional parameter: The price related to the activity, if any. Expressed in USD
|
15
|
+
# @param [String] record_id Optional parameter: The ID for the record associated with the activity in your database.
|
16
|
+
# @return Mixed response from the API call
|
17
|
+
def track(user_id,
|
18
|
+
activity_type,
|
19
|
+
price = nil,
|
20
|
+
record_id = nil)
|
21
|
+
|
22
|
+
# Validate required parameters
|
23
|
+
if user_id == nil
|
24
|
+
raise ArgumentError.new "Required parameter 'user_id' cannot be nil."
|
25
|
+
elsif activity_type == nil
|
26
|
+
raise ArgumentError.new "Required parameter 'activity_type' cannot be nil."
|
27
|
+
end
|
28
|
+
|
29
|
+
# the base uri for api requests
|
30
|
+
_query_builder = Configuration.base_uri.dup
|
31
|
+
|
32
|
+
# prepare query string for API call
|
33
|
+
_query_builder << '/activities'
|
34
|
+
|
35
|
+
# process optional query parameters
|
36
|
+
_query_builder = APIHelper.append_url_with_query_parameters _query_builder, {
|
37
|
+
'user_id' => user_id,
|
38
|
+
'activity_type' => activity_type,
|
39
|
+
'price' => price,
|
40
|
+
'record_id' => record_id
|
41
|
+
}
|
42
|
+
|
43
|
+
# validate and preprocess url
|
44
|
+
_query_url = APIHelper.clean_url _query_builder
|
45
|
+
|
46
|
+
# prepare headers
|
47
|
+
_headers = {
|
48
|
+
'user-agent' => 'APIMATIC 2.0',
|
49
|
+
'Authorization' => 'Bearer %s' % (Configuration.o_auth_access_token)
|
50
|
+
}
|
51
|
+
|
52
|
+
# Create the HttpRequest object for the call
|
53
|
+
_request = @http_client.post _query_url, headers: _headers
|
54
|
+
|
55
|
+
# Call the on_before_request callback
|
56
|
+
@http_call_back.on_before_request(_request) if @http_call_back
|
57
|
+
|
58
|
+
# Invoke the API call and get the response
|
59
|
+
_response = @http_client.execute_as_string(_request)
|
60
|
+
|
61
|
+
# Wrap the request and response in an HttpContext object
|
62
|
+
_context = HttpContext.new(_request, _response)
|
63
|
+
|
64
|
+
# Call the on_after_response callback
|
65
|
+
@http_call_back.on_after_response(_context) if @http_call_back
|
66
|
+
|
67
|
+
# Global error handling using HTTP status codes.
|
68
|
+
validate_response(_context)
|
69
|
+
|
70
|
+
# Return appropriate response type
|
71
|
+
decoded = APIHelper.json_deserialize(_response.raw_body) if not (_response.raw_body.nil? or _response.raw_body.to_s.strip.empty?)
|
72
|
+
return decoded
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
|
2
|
+
|
3
|
+
module RewardSciences
|
4
|
+
class BaseController
|
5
|
+
attr_accessor :http_client, :http_call_back
|
6
|
+
|
7
|
+
@@http_client = UnirestClient.new
|
8
|
+
|
9
|
+
def initialize(http_client: nil, http_call_back: nil)
|
10
|
+
@http_client = http_client ||= @@http_client
|
11
|
+
@http_call_back = http_call_back
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_response(context)
|
15
|
+
if context.response.status_code == 400
|
16
|
+
raise APIException.new "400 - Bad Request", context
|
17
|
+
elsif context.response.status_code == 401
|
18
|
+
raise APIException.new "401 - Unauthorized", context
|
19
|
+
elsif context.response.status_code == 403
|
20
|
+
raise APIException.new "403 - Forbidden", context
|
21
|
+
elsif context.response.status_code == 404
|
22
|
+
raise APIException.new "404 - Not Found", context
|
23
|
+
elsif context.response.status_code == 415
|
24
|
+
raise APIException.new "415 - Unsupported Media Type", context
|
25
|
+
elsif context.response.status_code == 500
|
26
|
+
raise APIException.new "500 - Server Error", context
|
27
|
+
elsif !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK
|
28
|
+
raise APIException.new 'HTTP Response Not OK', context
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|