stash-rewards 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 503fe2ea040583ac425b980d0ddcfcb3181898aece084742d499bb8886e0adee
4
- data.tar.gz: c164dc745da1a1818f65bfb26aefb5b8084ef9f0a803d43b88f2b20c6554990f
3
+ metadata.gz: 5c6a8ba357b79f85fd23167ffdb6f8d1f306b001cca8a91711b5d8ce96f90d90
4
+ data.tar.gz: 4528c55f7730654f3af5dabd7be153d0397d81a9492c172ca630d85d07945636
5
5
  SHA512:
6
- metadata.gz: 24c07dad1d0649838d940664303468435ed8249a3ab87931f3d95475808f6266a139b0b8671cf97a2d5ab425f9628e6d05b2fb51ae03cf7879efa11a0edf963a
7
- data.tar.gz: feb77b704c8b22d54fbe964b9b4ecb7696060dadada4435b847a7a3b74bc73680497ea1906a0709816436ef7549d6274fe748b02135660adde3eba746c5ef08e
6
+ metadata.gz: b25467f2784b0a27f1a9e8a00cb39f7832bab3c22274e4a698534c91d1e15b68e40dd75bb9eb09df14b669d7990515f0b47871e22377622a995e0577d31f7b2f
7
+ data.tar.gz: 0b4b5b1db5e410f89c0599387fad5515fff07ca6441cf298712e4fd1b8c60ccc3b7482e76b8f77d2842b297dcf68df96cbfb61d1a51618b1d7640cc79a2538cf
@@ -0,0 +1,30 @@
1
+ name: CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ rspec:
7
+ runs-on: ubuntu-latest
8
+ container:
9
+ image: ruby:2.6.5
10
+
11
+ steps:
12
+ - uses: actions/checkout@v1
13
+
14
+ - name: Gem cache
15
+ id: cache-bundle
16
+ uses: actions/cache@v1
17
+ with:
18
+ path: vendor/bundle
19
+ key: bundle-${{ hashFiles('**/Gemfile.lock') }}
20
+
21
+ - name: Bundle install
22
+ env:
23
+ RAILS_ENV: test
24
+ run: |
25
+ gem install bundler
26
+ bundle install --jobs 4 --retry 3 --path vendor/bundle
27
+
28
+ - name: Run tests
29
+ run: |
30
+ bundle exec rspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stash-rewards (0.0.2)
4
+ stash-rewards (0.0.3)
5
5
  faraday (~> 1)
6
6
  typhoeus (~> 1)
7
7
 
data/README.md CHANGED
@@ -18,7 +18,7 @@ And then execute:
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install stash-rewards --pre
21
+ $ gem install stash-rewards
22
22
 
23
23
  ## Usage
24
24
 
@@ -6,14 +6,14 @@ module Stash
6
6
  module Rewards
7
7
  class CreateUser < ApiWrapper
8
8
  def call(user_identifier:)
9
- response = api_wrapper.post('users') do |req|
9
+ api_response = api_wrapper.post('users') do |req|
10
10
  req.body = user_payload(user_identifier)
11
11
  end
12
- parsed_response = JSON.parse(response.body)
13
12
 
14
- raise Stash::Rewards::Error, parsed_response['message'] unless response.success?
13
+ response = Stash::Rewards::Response.new(api_response)
14
+ raise Stash::Rewards::Error, response.error_message if response.error?
15
15
 
16
- parsed_response
16
+ response
17
17
  rescue Faraday::Error => e
18
18
  raise Stash::Rewards::Error, e.message
19
19
  end
@@ -6,14 +6,14 @@ module Stash
6
6
  module Rewards
7
7
  class EnrolUserInCampaign < ApiWrapper
8
8
  def call(campaign_id:, user_identifier:)
9
- response = api_wrapper.post("campaigns/#{campaign_id}/users") do |req|
9
+ api_response = api_wrapper.post("campaigns/#{campaign_id}/users") do |req|
10
10
  req.body = user_payload(user_identifier)
11
11
  end
12
- parsed_response = JSON.parse(response.body)
13
12
 
14
- raise Stash::Rewards::Error, parsed_response['message'] unless response.success?
13
+ response = Stash::Rewards::Response.new(api_response)
14
+ raise Stash::Rewards::Error, response.error_message if response.error?
15
15
 
16
- parsed_response
16
+ response
17
17
  rescue Faraday::Error => e
18
18
  raise Stash::Rewards::Error, e.message
19
19
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_wrapper'
4
+
5
+ module Stash
6
+ module Rewards
7
+ class GetReward < ApiWrapper
8
+ def call(campaign_id:, reward_id:)
9
+ api_response = api_wrapper.get("campaigns/#{campaign_id}/rewards/#{reward_id}")
10
+ response = Stash::Rewards::Response.new(api_response)
11
+ raise Stash::Rewards::Error, response.error_message if response.error?
12
+
13
+ response
14
+ rescue Faraday::Error => e
15
+ raise Stash::Rewards::Error, e.message
16
+ end
17
+ end
18
+ end
19
+ end
@@ -6,14 +6,13 @@ module Stash
6
6
  module Rewards
7
7
  class GetRewards < ApiWrapper
8
8
  def call(campaign_id:, page_no: 0, page_size: 500)
9
- response = api_wrapper.get("campaigns/#{campaign_id}/rewards") do |req|
9
+ api_response = api_wrapper.get("campaigns/#{campaign_id}/rewards") do |req|
10
10
  req.params = query_params(page_no, page_size)
11
11
  end
12
- parsed_response = JSON.parse(response.body)
12
+ response = Stash::Rewards::Response.new(api_response)
13
+ raise Stash::Rewards::Error, response.error_message if response.error?
13
14
 
14
- raise Stash::Rewards::Error, parsed_response['message'] unless response.success?
15
-
16
- parsed_response
15
+ response
17
16
  rescue Faraday::Error => e
18
17
  raise Stash::Rewards::Error, e.message
19
18
  end
@@ -5,28 +5,39 @@ require_relative 'api_wrapper'
5
5
  module Stash
6
6
  module Rewards
7
7
  class IssueVoucher < ApiWrapper
8
- def call(campaign_id:, user_identifier:, reward_id:)
9
- response = api_wrapper.post("campaigns/#{campaign_id}/users/refId_#{user_identifier}/rewards/order") do |req|
10
- req.body = order_payload(reward_id)
8
+ def call(campaign_id:, user_identifier:, reward_id:, quantity: 1)
9
+ price = reward_price(reward_id, campaign_id)
10
+
11
+ api_response = api_wrapper.post("campaigns/#{campaign_id}/users/refId_#{user_identifier}/rewards/order") do |req|
12
+ req.body = order_payload(reward_id, quantity, price)
11
13
  end
12
- parsed_response = JSON.parse(response.body)
13
- raise Stash::Rewards::Error, parsed_response['message'] unless response.success?
14
+ response = Stash::Rewards::Response.new(api_response)
15
+ raise Stash::Rewards::Error, response.error_message if response.error?
14
16
 
15
- parsed_response
17
+ response
16
18
  rescue Faraday::Error => e
17
19
  raise Stash::Rewards::Error, e.message
18
20
  end
19
21
 
20
22
  private
21
23
 
22
- def order_payload(reward_id)
24
+ def reward_price(reward_id, campaign_id)
25
+ get_reward_api = ::Stash::Rewards::GetReward.new(@config)
26
+ response = get_reward_api.call(reward_id: reward_id, campaign_id: campaign_id)
27
+ raise Stash::Rewards::Error, response.error_message if response.error?
28
+
29
+ reward = Stash::Rewards::StashReward.new(response.payload)
30
+ reward.prices[0]['price'] || 0
31
+ end
32
+
33
+ def order_payload(reward_id, quantity, price)
23
34
  {
24
35
  "userInformation": {},
25
36
  "products": [
26
37
  {
27
38
  "rewardId": reward_id,
28
- "price": 0,
29
- "quantity": 0
39
+ "price": price,
40
+ "quantity": quantity
30
41
  }
31
42
  ]
32
43
  }.to_json
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stash
4
+ module Rewards
5
+ class Response
6
+ def initialize(faraday_response)
7
+ @response = faraday_response
8
+ end
9
+
10
+ def error?
11
+ !@response.success? || (error_code != 0)
12
+ end
13
+
14
+ def payload
15
+ @payload ||= JSON.parse(body)
16
+ end
17
+
18
+ def status
19
+ @status ||= @response.status
20
+ end
21
+
22
+ def body
23
+ @body ||= @response.body
24
+ end
25
+
26
+ def error_code
27
+ @error_code ||= payload.dig('error', 'code').to_i
28
+ end
29
+
30
+ def error_message
31
+ api_error_message || error_payload
32
+ end
33
+
34
+ private
35
+
36
+ def error_payload
37
+ error_data = payload.dig('error', 'data')
38
+ return nil if error_data.empty?
39
+
40
+ error_data.map { |error| error['message'] }.join(', ')
41
+ end
42
+
43
+ def api_error_message
44
+ payload['message']
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stash
4
+ module Rewards
5
+ class StashReward
6
+ attr_reader :reward_name, :reward_id, :description, :prices
7
+
8
+ def initialize(reward_payload)
9
+ @reward_name = reward_payload['rewardName']
10
+ @reward_id = reward_payload['rewardId']
11
+ @description = reward_payload['description']
12
+ @prices = reward_payload['denominations'] || []
13
+ end
14
+ end
15
+ end
16
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Stash
4
4
  module Rewards
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.3'
6
6
  end
7
7
  end
data/lib/stash/rewards.rb CHANGED
@@ -1,10 +1,13 @@
1
1
  require_relative 'rewards/version'
2
2
  require_relative 'rewards/api_wrapper'
3
+ require_relative 'rewards/response'
3
4
  require_relative 'rewards/config'
4
5
  require_relative 'rewards/create_user'
5
6
  require_relative 'rewards/enrol_user_in_campaign'
6
7
  require_relative 'rewards/get_rewards'
8
+ require_relative 'rewards/get_reward'
7
9
  require_relative 'rewards/issue_voucher'
10
+ require_relative 'rewards/stash_reward'
8
11
 
9
12
  module Stash
10
13
  module Rewards
@@ -3,8 +3,8 @@ require_relative 'lib/stash/rewards/version'
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'stash-rewards'
5
5
  spec.version = Stash::Rewards::VERSION
6
- spec.authors = ['Rui Baltazar']
7
- spec.email = ['rui.p.baltazar@gmail.com']
6
+ spec.authors = ['Rui Baltazar', 'Aayush Jain']
7
+ spec.email = ['rui.p.baltazar@gmail.com', 'aayush@perxtech.com']
8
8
 
9
9
  spec.summary = 'Gem with API wrapper for Stash Rewards'
10
10
  spec.description = 'Helper Library to make api calls to stash rewards'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stash-rewards
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rui Baltazar
8
+ - Aayush Jain
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2022-06-24 00:00:00.000000000 Z
12
+ date: 2022-06-28 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: faraday
@@ -83,10 +84,12 @@ dependencies:
83
84
  description: Helper Library to make api calls to stash rewards
84
85
  email:
85
86
  - rui.p.baltazar@gmail.com
87
+ - aayush@perxtech.com
86
88
  executables: []
87
89
  extensions: []
88
90
  extra_rdoc_files: []
89
91
  files:
92
+ - ".github/workflows/test.yml"
90
93
  - ".gitignore"
91
94
  - ".travis.yml"
92
95
  - CODE_OF_CONDUCT.md
@@ -103,8 +106,11 @@ files:
103
106
  - lib/stash/rewards/config.rb
104
107
  - lib/stash/rewards/create_user.rb
105
108
  - lib/stash/rewards/enrol_user_in_campaign.rb
109
+ - lib/stash/rewards/get_reward.rb
106
110
  - lib/stash/rewards/get_rewards.rb
107
111
  - lib/stash/rewards/issue_voucher.rb
112
+ - lib/stash/rewards/response.rb
113
+ - lib/stash/rewards/stash_reward.rb
108
114
  - lib/stash/rewards/version.rb
109
115
  - stash-rewards.gemspec
110
116
  homepage: https://github.com/PerxTech/stash-rewards