The Reward Sciences gem lets you integrate with our API to track activities, assign points to customers and and let them redeem rewards without leaving your application.
gem install reward_sciences
Or add it to your gemfile
gem 'reward_sciences'
-
Contact us at [email protected] to get a test account on our sandbox environment.
-
Log in to our web app and get your API token from the 'API Tokens' area available under 'Settings'.
-
Define the methods you'll use accross the app
module Rewardable
def identify_reward_sciences_user(user)
session[:reward_sciences_user_id] = reward_sciences.users.identify(user.email)['id']
end
def track_reward_sciences_activity(activity_slug)
reward_sciences.activities.track(session[:reward_sciences_user_id], activity_slug)
end
def list_reward_sciences_rewards
reward_sciences.rewards.list
end
def show_reward_sciences_reward(reward_id)
reward_sciences.rewards.show(reward_id)
end
def redeem_reward_sciences_reward(reward_id)
reward_sciences.rewards.redeem(session[:reward_sciences_user_id], reward_id)
rescue RewardSciences::APIException => e
redirect_to :back, alert: e.message.split(':').last
end
private
def reward_sciences
RewardSciences::RewardSciencesClient.new(
Rails.application.secrets.reward_sciences_access_token,
Rails.application.secrets.reward_sciences_environment
)
end
end
- Use the methods defined in the previous step
Check out our Sample Ruby on Rails project.
If you have any questions feel free to email us at [email protected]. We are happy to help.
The singleton instance of the Rewards
class can be accessed from the API Client.
rewards = client.rewards
Bid on a reward auction.
def bid(user_id,
reward_id,
amount); end
Parameter | Tags | Description |
---|---|---|
user_id | Required |
The ID of the user who is bidding on the reward auction. |
reward_id | Required |
The ID of the reward auction to be bid on. |
amount | Required |
Can be either 'max' (when max bidding) or the number of points the user wants to bid. |
user_id = 225
reward_id = 225
amount = 'amount'
result = rewards.bid(user_id, reward_id, amount)
List all the available rewards.
def list(category_id = nil,
limit = 25,
offset = 0); end
Parameter | Tags | Description |
---|---|---|
category_id | Optional |
The id of the category to filter rewards by |
limit | Optional DefaultValue |
The number of rewards you want to be retrieved. |
offset | Optional DefaultValue |
The number of rewards you want to skip before starting the retrieval. |
category_id = 225
limit = 25
offset = 0
result = rewards.list(category_id, limit, offset)
Redeem a reward.
def redeem(user_id,
reward_id); end
Parameter | Tags | Description |
---|---|---|
user_id | Required |
The ID of the user who is redeeming the reward. |
reward_id | Required |
The ID of the reward to be redeemed. |
user_id = 225
reward_id = 225
result = rewards.redeem(user_id, reward_id)
Show a reward's details.
def show(reward_id); end
Parameter | Tags | Description |
---|---|---|
reward_id | Required |
The ID of the reward to be retrieved. |
reward_id = 225
result = rewards.show(reward_id)
The singleton instance of the RewardCategories
class can be accessed from the API Client.
rewardCategories = client.reward_categories
List all the available reward categories.
def list(limit = 25,
offset = 0); end
Parameter | Tags | Description |
---|---|---|
limit | Optional DefaultValue |
The number of reward categories you want to be retrieved. |
offset | Optional DefaultValue |
The number of reward categories you want to skip before starting the retrieval. |
limit = 25
offset = 0
result = rewardCategories.list(limit, offset)
The singleton instance of the Users
class can be accessed from the API Client.
users = client.users
This endpoint lets retrieve a user's details.
def show(user_id); end
Parameter | Tags | Description |
---|---|---|
user_id | Required |
The ID of the user to be retrieved. |
user_id = 225
result = users.show(user_id)
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.
def identify(email,
first_name = nil,
last_name = nil); end
Parameter | Tags | Description |
---|---|---|
Required |
The user's email address | |
first_name | Optional |
The user's first name |
last_name | Optional |
The user's last name |
email = 'email'
first_name = 'first_name'
last_name = 'last_name'
result = users.identify(email, first_name, last_name)
The singleton instance of the Activities
class can be accessed from the API Client.
activities = client.activities
This endpoint lets you track the activities your users perform.
def track(user_id,
activity_type,
price = nil,
record_id = nil); end
Parameter | Tags | Description |
---|---|---|
user_id | Required |
The ID of the user who is performing the activity. |
activity_type | Required |
The type of activity the user is performing. Example: 'purchased-a-product' |
price | Optional |
The price related to the activity, if any. Expressed in USD |
record_id | Optional |
The ID for the record associated with the activity in your database. |
user_id = 225
activity_type = 'activity_type'
price = 225
record_id = 'record_id'
result = activities.track(user_id, activity_type, price, record_id)