Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renamed #15

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# FlipgiveSDK

## Shop Cloud
## Rewards

Shop Cloud _(The Shop)_ is [FlipGive's](https://www.flipgive.com) drop-in cashback store. If you would like to know more please visit www.flipgive.com/cloud or contact us at [email protected].
Rewards is [FlipGive's](https://www.flipgive.com) drop-in cashback program. If you would like to know more please contact us at [email protected].

### Links of Interest

Expand All @@ -11,37 +11,37 @@ Shop Cloud _(The Shop)_ is [FlipGive's](https://www.flipgive.com) drop-in cashba

### Installation

To begin using `FlipGiveSDK::ShopCloud`, you should have obtained an `ID` and `Secret` pair from FlipGive, store these securely so that they are accessible in your application (env variables, rails credentials, etc). We'll be using env variables in our example below. If you haven't received credentials, please contact us at [email protected].
To begin using `FlipGiveSDK::Rewards`, you should have obtained an `ID` and `Secret` pair from FlipGive, store these securely so that they are accessible in your application (env variables, rails credentials, etc). We'll be using env variables in our example below. If you haven't received credentials, please contact us at [email protected].

Add the gem to your Gemfile:

```ruby
gem 'flipgive_sdk', git: "https://github.com/BetterTheWorld/FlipGiveSDK_Ruby.git"
```

After you have installed the gem include the code below to initialize the ShopCloud:
After you have installed the gem include the code below to initialize the SDK:

```ruby
FlipgiveSDK::ShopCloud.flip(ENV['shop_cloud_id'], ENV['shop_cloud_secret'])
FlipgiveSDK::Rewards.flip(ENV['rewards_id'], ENV['rewards_secret'])
```
We recommend using its own initializer file `myapp/config/initializers/shop_cloud.rb`.

ShopCloud is now ready to use.
We recommend using its own initializer file `myapp/config/initializers/rewards.rb`.

The SDK is now ready to use.
se
### Usage

The main purpose of `FlipgiveSDK::ShopCloud` is to generate Tokens to gain access to FlipGive's Shop Cloud API. There are 6 methods on the gem's public API.
The main purpose of `FlipgiveSDK::Rewards` is to generate Tokens to gain access to FlipGive's Rewards API. There are 6 methods on the gem's public API.

#### :flip
This method is used to initialize the SDK, as described on the setup section of this document. It takes 2 arguments, the `shop_cloud_id` and the `shop_cloud_Secret`.
This method is used to initialize the SDK, as described on the setup section of this document. It takes 2 arguments, the `rewards_id` and the `rewards_secret`.

#### :read_token
This method is used to decode a token that has been generated with your credentials. It takes a single string as an argument and, if able to decode the token, it will return a hash.

```ruby
token = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..demoToken.g8PZPWb1KDFcAkTsufZq0w@A2DE537C"

FlipgiveSDK::ShopCloud.read_token(token)
FlipgiveSDK::Rewards.read_token(token)
=> { user_data: { id: 1, name: 'Emmett Brown', email: '[email protected]', country: 'USA' } }
```

Expand All @@ -56,7 +56,7 @@ payload = {
organization_data: organization_data
}

FlipgiveSDK::ShopCloud.identified_token(payload)
FlipgiveSDK::Rewards.identified_token(payload)
=> "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..demoToken.g8PZPWb1KDFcAkTsufZq0w@A2DE537C"
```

Expand Down Expand Up @@ -158,15 +158,15 @@ payload = {
user_data: user_data
}

FlipgiveSDK::ShopCloud.valid_identified?(payload)
FlipgiveSDK::Rewards.valid_identified?(payload)
=> true
```

#### :partner_token
This method is used to generate a token that can **only** be used by the Shop Cloud partner (that's you) to access reports and other API endpoints. It is only valid for an hour.
This method is used to generate a token that can **only** be used by the Rewards partner (that's you) to access reports and other API endpoints. It is only valid for an hour.

```ruby
FlipgiveSDK::ShopCloud.partner_token
FlipgiveSDK::Rewards.partner_token
=> "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..demoToken.h9QXQEn2LFGVSlTdiGXW1e@A2DE537C"
```

Expand All @@ -180,11 +180,11 @@ payload = {
user_data: user_data
}

FlipgiveSDK::ShopCloud.valid_identified?(payload)
FlipgiveSDK::Rewards.valid_identified?(payload)

# FlipgiveSDK::Error (Invalid payload.)

FlipgiveSDK::ShopCloud.errors
FlipgiveSDK::Rewards.errors

=> [{:user_data=>"Country must be one of: 'CAN, USA'."}]
```
Expand Down
4 changes: 2 additions & 2 deletions flipgive_sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
spec.authors = ["Alejandro"]
spec.email = ["[email protected]"]

spec.summary = "FlipGive's Shop Cloud Ruby SDK."
spec.description = "FlipGive's Shop Cloud Ruby SDK."
spec.summary = "FlipGive's Ruby SDK."
spec.description = "FlipGive's Ruby SDK."
spec.homepage = "https://github.com/BetterTheWorld/FlipGiveSDK_Ruby"
spec.required_ruby_version = ">= 2.6.0"

Expand Down
2 changes: 1 addition & 1 deletion lib/flipgive_sdk.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative "flipgive_sdk/version"
require_relative "flipgive_sdk/shop_cloud"
require_relative "flipgive_sdk/rewards"

module FlipgiveSDK
class Error < StandardError; end
Expand Down
20 changes: 10 additions & 10 deletions lib/flipgive_sdk/shop_cloud.rb → lib/flipgive_sdk/rewards.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "jwe"

# class to handle shop cloud tokens
class FlipgiveSDK::ShopCloud
class FlipgiveSDK::Rewards
COUNTRIES = %w[CAN USA].freeze
PARTNER_TOKEN_TTL = 3600 # SECONDS

class << self
def flip(cloud_shop_id, secret)
@instance = new(cloud_shop_id, secret)
def flip(id, secret)
@instance = new(id, secret)
:initialized
end

Expand All @@ -32,15 +32,15 @@ def errors
end
end

def initialize(cloud_shop_id, secret)
@cloud_shop_id = cloud_shop_id
def initialize(id, secret)
@id = id
@secret = secret.gsub("sk_", nil.to_s)
@errors = []
end

def read_token(token)
encrypted_string, shop_id = token.split("@")
raise invalid_token_error if shop_id != cloud_shop_id
raise invalid_token_error if shop_id != id

json = JWE.decrypt(encrypted_string, secret)
JSON.parse(json)
Expand All @@ -50,7 +50,7 @@ def identified_token(payload)
raise validation_error unless valid_identified?(payload)

token = JWE.encrypt(payload.to_json, secret, alg: "dir")
[token, cloud_shop_id].join("@")
[token, id].join("@")
end

def valid_identified?(payload)
Expand All @@ -69,16 +69,16 @@ def valid_identified?(payload)
end

def partner_token
payload = { type: "partner", expires: partner_token_expiration }
payload = { type: "partner", expires: partner_token_expiration }
token = JWE.encrypt(payload.to_json, secret, alg: "dir")
[token, cloud_shop_id].join("@")
[token, id].join("@")
end

attr_reader :errors

private

attr_reader :secret, :cloud_shop_id
attr_reader :secret, :id

def invalid_token_error
FlipgiveSDK::Error.new("Invalid Token.")
Expand Down
147 changes: 147 additions & 0 deletions spec/flipgive_sdk/rewards_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# frozen_string_literal: true

require "securerandom"
# verysecrettoken57e6323c7c74bba0e9fd4d702aeea756c0901e3a6b453671a

RSpec.describe FlipgiveSDK::Rewards do
before(:all) do
FlipgiveSDK::Rewards.flip("BB126923", "sk_61c394cf3346077b")
end

let(:secret) { "61c394cf3346077b" }
let(:id) { "BB126923" }

let(:user_data) { RewardsHelper.person_data }
let(:campaign_data) { RewardsHelper.campaign_data }
let(:group_data) { RewardsHelper.group_data }
let(:organization_data) { RewardsHelper.organization_data }

let(:payload) do
{
"user_data" => user_data,
"campaign_data" => campaign_data
}
end

context "#Rewards" do
it "expects Rewards to exist" do
expect(FlipgiveSDK::Rewards).not_to be nil
end
end
context "#initialize" do
it "expects secret and partner id" do
fg_shop_cloud = FlipgiveSDK::Rewards.new("BB126923", "sk_61c394cf3346077b")
expect(fg_shop_cloud).to receive(:secret).and_return(secret)
expect(fg_shop_cloud).to receive(:id).and_return(id)
allow(fg_shop_cloud).to receive(:valid_identified?).and_return true

fg_shop_cloud.identified_token({ foo: "bar" })
end
end

context "#token" do
it "expects token to be generated and append id" do
token = FlipgiveSDK::Rewards.identified_token(payload)
regexp = Regexp.new("#{id}\\z")

expect(token).to be_kind_of(String)
expect(token).to match(regexp)
end

it "expects invalid params error" do
payload = "foobar"

expect { FlipgiveSDK::Rewards.identified_token(payload) }.to raise_error(FlipgiveSDK::Error)
end

it "expects invalid user_data_errors" do
payload["user_data"] = {}
payload["campaign_data"] = nil

expect { FlipgiveSDK::Rewards.identified_token(payload) }.to raise_error(FlipgiveSDK::Error)
expect(FlipgiveSDK::Rewards.errors).not_to be_empty
expect(FlipgiveSDK::Rewards.errors.count).to eq(5)
expect(FlipgiveSDK::Rewards.errors[0][:payload]).to eq("At least must contain user_data or campaign_data.")
expect(FlipgiveSDK::Rewards.errors[1][:user_data]).to eq("id missing.")
expect(FlipgiveSDK::Rewards.errors[2][:user_data]).to eq("name missing.")
expect(FlipgiveSDK::Rewards.errors[3][:user_data]).to eq("email missing.")
expect(FlipgiveSDK::Rewards.errors[4][:user_data]).to eq("country must be one of: 'CAN, USA'.")
end

it "expects invalid campaign_data" do
payload["user_data"] = nil
payload["campaign_data"] = {}

expect { FlipgiveSDK::Rewards.identified_token(payload) }.to raise_error(FlipgiveSDK::Error)
expect(FlipgiveSDK::Rewards.errors).not_to be_empty
expect(FlipgiveSDK::Rewards.errors.count).to eq(9)
expect(FlipgiveSDK::Rewards.errors[0][:payload]).to eq("At least must contain user_data or campaign_data.")
expect(FlipgiveSDK::Rewards.errors[1][:campaign_data]).to eq("id missing.")
expect(FlipgiveSDK::Rewards.errors[2][:campaign_data]).to eq("name missing.")
expect(FlipgiveSDK::Rewards.errors[3][:campaign_data]).to eq("category missing.")
expect(FlipgiveSDK::Rewards.errors[4][:campaign_data]).to eq("country must be one of: 'CAN, USA'.")
expect(FlipgiveSDK::Rewards.errors[5][:campaign_admin_data]).to eq("id missing.")
expect(FlipgiveSDK::Rewards.errors[6][:campaign_admin_data]).to eq("name missing.")
expect(FlipgiveSDK::Rewards.errors[7][:campaign_admin_data]).to eq("email missing.")
expect(FlipgiveSDK::Rewards.errors[8][:campaign_admin_data]).to eq("country must be one of: 'CAN, USA'.")
end

it "expects token to be successfully decoded" do
token = FlipgiveSDK::Rewards.identified_token(payload)
data = FlipgiveSDK::Rewards.read_token(token)
expect(data).to eq(payload)
end

it "Partner token has partner_token type" do
token = FlipgiveSDK::Rewards.partner_token
data = FlipgiveSDK::Rewards.read_token(token)
expect(data["type"]).to eq("partner")
expect(data["expires"]).to eq(Time.now.to_i + 3600)
end
end

context "Group Data" do
let(:payload) do
{
"user_data" => user_data,
"campaign_data" => campaign_data,
"group_data" => group_data
}
end
it "expects token to be successfully decoded" do
token = FlipgiveSDK::Rewards.identified_token(payload)
data = FlipgiveSDK::Rewards.read_token(token)
expect(data).to eq(payload)
end
it "expects error when group_data missing" do
payload["group_data"] = {}
expect { FlipgiveSDK::Rewards.identified_token(payload) }.to raise_error(FlipgiveSDK::Error)
expect(FlipgiveSDK::Rewards.errors).not_to be_empty
expect(FlipgiveSDK::Rewards.errors.count).to eq(1)
expect(FlipgiveSDK::Rewards.errors[0][:group_data]).to eq("name missing.")
end
end

context "Organization Data" do
let(:payload) do
{
"user_data" => user_data,
"campaign_data" => campaign_data,
"organization_data" => organization_data
}
end
it "expects token to be successfully decoded" do
token = FlipgiveSDK::Rewards.identified_token(payload)
data = FlipgiveSDK::Rewards.read_token(token)
expect(data).to eq(payload)
end
it "expects error when organization_data missing" do
payload["organization_data"] = {}
expect { FlipgiveSDK::Rewards.identified_token(payload) }.to raise_error(FlipgiveSDK::Error)
expect(FlipgiveSDK::Rewards.errors).not_to be_empty
expect(FlipgiveSDK::Rewards.errors.count).to eq(6)
expect(FlipgiveSDK::Rewards.errors[0][:organization_data]).to eq("id missing.")
expect(FlipgiveSDK::Rewards.errors[1][:organization_data]).to eq("name missing.")
end
end
end
Loading
Loading