scrimmage 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.
- checksums.yaml +7 -0
- data/.env.example +6 -0
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +166 -0
- data/Rakefile +12 -0
- data/lib/scrimmage/client.rb +152 -0
- data/lib/scrimmage/config.rb +37 -0
- data/lib/scrimmage/errors.rb +38 -0
- data/lib/scrimmage/object.rb +8 -0
- data/lib/scrimmage/rewards.rb +47 -0
- data/lib/scrimmage/status.rb +40 -0
- data/lib/scrimmage/users.rb +21 -0
- data/lib/scrimmage/version.rb +5 -0
- data/lib/scrimmage.rb +34 -0
- data/sig/scrimmage.rbs +4 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2a817e143f6c56d5336728c7efd42fbf9dbd5daebac99191982ce23bf5014cab
|
4
|
+
data.tar.gz: 04da20f25b988024d168ff453117d9693287bc76e3fa473ebfb35f140aa45fef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6843f8d6425d643fdd699a3c0fd87eb440055d0436dd9cf1f9f1d219b1f3c251a6b50d1fa08d26e7ce255d560e8a8f99096ba03b86abf8f4a6d4b3f67528d63
|
7
|
+
data.tar.gz: 9d334bea87a3010133c1e08dfd674b8bef665c0ae03e22006265e19526f96ee916a5d9716e1da8e238cd3790bd0440bd736940e172d8cec8a104a8fe7dac84d2
|
data/.env.example
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-minitest
|
3
|
+
- rubocop-rake
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.0
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Style/StringLiterals:
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Style/StringLiteralsInInterpolation:
|
13
|
+
EnforcedStyle: double_quotes
|
14
|
+
|
15
|
+
Style/Documentation:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Style/OpenStructUse:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/MethodLength:
|
22
|
+
Max: 50
|
23
|
+
|
24
|
+
Naming/AccessorMethodName:
|
25
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rafe@existentialmutt.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Rafe Rosen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# Scrimmage Rewards
|
2
|
+
|
3
|
+
The low-code loyalty program maker.
|
4
|
+
|
5
|
+
This library is a part of the [Scrimmage Rewards Program](https://scrimmage.co)
|
6
|
+
that is providing a solution for loyalty programs and rewards.
|
7
|
+
|
8
|
+
Tutorial can be found at [Scrimmage Rewards Tutorial](https://scrimmage-rewards.readme.io/docs).
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
13
|
+
|
14
|
+
Install the gem and add to the application's Gemfile by executing:
|
15
|
+
|
16
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
17
|
+
|
18
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
19
|
+
|
20
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
1. Load the library (if not using bundler)
|
25
|
+
```ruby
|
26
|
+
require "scrimmage"
|
27
|
+
```
|
28
|
+
|
29
|
+
2. Initialize the library with global configuration
|
30
|
+
```ruby
|
31
|
+
# config/initializers/scrimmage.rb
|
32
|
+
Scrimmage.configure do |config|
|
33
|
+
config.api_server_endpoint = '<your api server endpoint>'
|
34
|
+
config.private_key = '<your private key>',
|
35
|
+
config.namespace = '<environment, e.g. staging or production>'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
3. Use the library
|
40
|
+
```ruby
|
41
|
+
Scrimmage::Rewards.track_rewardable(
|
42
|
+
'unique-user-id',
|
43
|
+
'Data Type Name',
|
44
|
+
{
|
45
|
+
'custom-property': 'custom-value',
|
46
|
+
'custom-property-2': {
|
47
|
+
'custom-property-2-1': 'custom-value-2-1',
|
48
|
+
},
|
49
|
+
},
|
50
|
+
)
|
51
|
+
```
|
52
|
+
|
53
|
+
For one-time events you can use `track_rewardable_once` method. Make sure to use unique event id.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
Scrimmage::Rewards.track_rewardable_once(
|
57
|
+
'unique-user-id',
|
58
|
+
'Data Type Name',
|
59
|
+
'unique-event-id',
|
60
|
+
{
|
61
|
+
'custom-property': 'custom-value',
|
62
|
+
'custom-property-2': {
|
63
|
+
'custom-property-2-1': 'custom-value-2-1',
|
64
|
+
},
|
65
|
+
},
|
66
|
+
);
|
67
|
+
```
|
68
|
+
|
69
|
+
4. Get user token
|
70
|
+
```ruby
|
71
|
+
token = Scrimmage::Users.get_user_token('unique-user-id')
|
72
|
+
```
|
73
|
+
or
|
74
|
+
```ruby
|
75
|
+
token = Scrimmage::Users.get_user_token('unique-user-id',
|
76
|
+
tags: ['tag1', 'tag2'],
|
77
|
+
properties: {
|
78
|
+
'custom-property': 'custom-value',
|
79
|
+
'custom-property-2': 12345,
|
80
|
+
},
|
81
|
+
)
|
82
|
+
```
|
83
|
+
|
84
|
+
Use this token to identify the user on the frontend. Make sure to deliver the token to the frontend securely.
|
85
|
+
|
86
|
+
## Multiple connections
|
87
|
+
If you want to use multiple connections, you need to create a new instance of `Scrimmage::Client`.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
rewarder_for_production = Scrimmage::Client.new(
|
91
|
+
api_server: '<your api server endpoint 1>',
|
92
|
+
private_key: '<your private key 1>',
|
93
|
+
namespace: '<environment 1, e.g. staging or production>',
|
94
|
+
)
|
95
|
+
|
96
|
+
rewarder_for_staging = Scrimmage::Client.new(
|
97
|
+
api_server: '<your api server endpoint 2>',
|
98
|
+
private_key: '<your private key 2>',
|
99
|
+
namespace: '<environment 2, e.g. staging or production>',
|
100
|
+
)
|
101
|
+
```
|
102
|
+
|
103
|
+
Then you can use the two instances simultaneously.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
rewarder_for_production.rewards.track_rewardable(
|
107
|
+
'unique-user-id',
|
108
|
+
'Data Type Name',
|
109
|
+
{
|
110
|
+
'custom-property': 'custom-value',
|
111
|
+
'custom-property-2': {
|
112
|
+
'custom-property-2-1': 'custom-value-2-1',
|
113
|
+
},
|
114
|
+
},
|
115
|
+
);
|
116
|
+
|
117
|
+
rewarder_for_staging.rewards.track_rewardable(
|
118
|
+
'unique-user-id',
|
119
|
+
'Data Type Name',
|
120
|
+
{
|
121
|
+
'custom-property': 'custom-value',
|
122
|
+
'custom-property-2': {
|
123
|
+
'custom-property-2-1': 'custom-value-2-1',
|
124
|
+
},
|
125
|
+
},
|
126
|
+
);
|
127
|
+
```
|
128
|
+
|
129
|
+
## Configuration
|
130
|
+
|
131
|
+
The following configuration parameters can be passed to `Scrimmage.configure` or `Scrimmage::Client.new`
|
132
|
+
|
133
|
+
- `api_server_endpoint` the URL of your API server
|
134
|
+
- `private_key` your private key
|
135
|
+
- `namespace` your environment (e.g. staging or production)
|
136
|
+
- `secure` set to `true` when using an https url for `api_server_endpoint` and false when using plain http (default `true`)
|
137
|
+
- `retry` set to `false` to skip retrying failed requests or pass a hash with the following keys to configure retry behavior:
|
138
|
+
- `tries` max number of retries (default 3)
|
139
|
+
- `sleep_base` the number of seconds for the initial retry. Subsequent retries will delay with exponential backoff. Pass a decimal for fractional seconds (default `0.1`)
|
140
|
+
- `sleep` a fixed number of seconds between retries or a proc that determines the delay based on the number of previous tries. (default `->(n) { config.retry[:sleep_base] * (2**n) }`)
|
141
|
+
|
142
|
+
## Usage on other platforms
|
143
|
+
|
144
|
+
- Using `<iframe />`: [github.com/Scrimmage-co/scrimmage-rewards-iframe](https://github.com/Scrimmage-co/scrimmage-rewards-iframe)
|
145
|
+
- Using Android: [github.com/Scrimmage-co/scrimmage-rewards-android](https://github.com/Scrimmage-co/scrimmage-rewards-android)
|
146
|
+
- Using iOS: [github.com/Scrimmage-co/scrimmage-rewards-ios](https://github.com/Scrimmage-co/scrimmage-rewards-ios)
|
147
|
+
- Using Flutter: [github.com/Scrimmage-co/scrimmage-rewards-flutter](https://github.com/Scrimmage-co/scrimmage-rewards-flutter)
|
148
|
+
- Using NodeJS: [github.com/Scrimmage-co/rewards/backend-library](https://github.com/Scrimmage-co/rewards/backend-library)
|
149
|
+
|
150
|
+
## Development
|
151
|
+
|
152
|
+
After checking out the repo, run `bin/setup` to install dependencies. To run the tests first copy `.env.example` to `.env` and fill it in with your API credentials. Then you can run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
153
|
+
|
154
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
155
|
+
|
156
|
+
## Contributing
|
157
|
+
|
158
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/scrimmage-rewards. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/scrimmage-co/scrimmage-rewards-ruby/blob/main/CODE_OF_CONDUCT.md).
|
159
|
+
|
160
|
+
## License
|
161
|
+
|
162
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
163
|
+
|
164
|
+
## Code of Conduct
|
165
|
+
|
166
|
+
Everyone interacting in the Scrimmage Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/scrimmage-co/scrimmage-rewards-ruby/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "http"
|
4
|
+
require "json"
|
5
|
+
require "retryable"
|
6
|
+
|
7
|
+
module Scrimmage
|
8
|
+
class Client
|
9
|
+
attr_reader :config, :rewards, :users, :status
|
10
|
+
|
11
|
+
HTTP_NOT_FOUND = 404
|
12
|
+
HTTP_UNAUTHORIZED = 401
|
13
|
+
HTTP_FORBIDDEN = 403
|
14
|
+
SERVICES = %w[api p2e fed nbc].freeze
|
15
|
+
|
16
|
+
# Initialize a new client, specifying any local configuration overrides
|
17
|
+
def initialize(**config_overrides)
|
18
|
+
config_attrs = Scrimmage.config.to_h.merge(config_overrides)
|
19
|
+
@config = Scrimmage::Config.new(**config_attrs)
|
20
|
+
|
21
|
+
@rewards = Scrimmage::Rewards.new(client: self)
|
22
|
+
@users = Scrimmage::Users.new(client: self)
|
23
|
+
@status = Scrimmage::Status.new(client: self)
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_integration_reward(user_id, data_type, event_id_or_reward, reward = nil)
|
27
|
+
if event_id_or_reward.is_a? String
|
28
|
+
event_id = event_id_or_reward
|
29
|
+
rewardable = reward.to_h
|
30
|
+
else
|
31
|
+
event_id = nil
|
32
|
+
rewardable = event_id_or_reward.to_h
|
33
|
+
end
|
34
|
+
|
35
|
+
response = http_request(
|
36
|
+
:post,
|
37
|
+
url("/integrations/rewards"),
|
38
|
+
json: {
|
39
|
+
eventId: event_id,
|
40
|
+
userId: user_id,
|
41
|
+
dataType: data_type,
|
42
|
+
body: rewardable
|
43
|
+
}
|
44
|
+
) do |my_response|
|
45
|
+
# handle errors
|
46
|
+
case my_response.code
|
47
|
+
when HTTP_NOT_FOUND
|
48
|
+
raise Scrimmage::Errors::AccountNotLinkedError, reward&.user_id
|
49
|
+
when HTTP_UNAUTHORIZED, HTTP_FORBIDDEN
|
50
|
+
raise Scrimmage::Errors::InvalidPrivateKeyError
|
51
|
+
when ->(code) { !(200..299).include?(code) }
|
52
|
+
raise Scrimmage::Errors::RequestFailedError, response
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
parse_data(response)
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_user_token(user_id, **options)
|
60
|
+
response = http_request(
|
61
|
+
:post,
|
62
|
+
url("/integrations/users"),
|
63
|
+
json: {
|
64
|
+
id: user_id,
|
65
|
+
tags: options[:tags].to_a,
|
66
|
+
properties: options[:properties].to_h
|
67
|
+
}
|
68
|
+
)
|
69
|
+
parse_data(response)&.token
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
# Requests Service Status
|
74
|
+
#
|
75
|
+
# @param [String] service the code for a scrimmage service e.g. ('api', 'p2e', 'fed', 'nbc')
|
76
|
+
#
|
77
|
+
# @return [Scrimmage::Object] an object detailing service status
|
78
|
+
#
|
79
|
+
def get_service_status(service)
|
80
|
+
my_url = url("/system/status", service: service)
|
81
|
+
response = http_request(:get, my_url)
|
82
|
+
parse_data(response)
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# Request overall status for all services
|
87
|
+
#
|
88
|
+
# @return [Boolean] Indicates whether all services are fulfilled
|
89
|
+
#
|
90
|
+
def get_overall_service_status
|
91
|
+
response_data = SERVICES.map do |service|
|
92
|
+
get_service_status(service)
|
93
|
+
end
|
94
|
+
response_data.all? { |d| d&.uptime&.positive? }
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_rewarder_key_details
|
98
|
+
url = url("/rewarders/keys/@me")
|
99
|
+
response = http_request(:get, url)
|
100
|
+
|
101
|
+
parse_data(response)
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def http_client
|
107
|
+
private_key = config.private_key!
|
108
|
+
namespace = config.namespace!
|
109
|
+
|
110
|
+
HTTP.auth("Token #{private_key}")
|
111
|
+
.headers("Scrimmage-Namespace" => namespace)
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# Initiate an authorized http request
|
116
|
+
#
|
117
|
+
# @param [Symbol] method HTTP method (get, post, etc)
|
118
|
+
# @param [String] uri Full URL of the request (build with #url)
|
119
|
+
# @param [Hash] options Options for the request (e.g. json body)
|
120
|
+
# @param [Proc] &block Block to override default response validation
|
121
|
+
#
|
122
|
+
# @return [HTTP::Response]
|
123
|
+
#
|
124
|
+
def http_request(method, uri, options = {}, &block)
|
125
|
+
request_proc = lambda { |*_args|
|
126
|
+
response = http_client.request(method, uri, options)
|
127
|
+
|
128
|
+
if block
|
129
|
+
block.call(response)
|
130
|
+
else
|
131
|
+
Scrimmage::Errors::RequestFailedError unless (200..299).include?(response.code)
|
132
|
+
end
|
133
|
+
response
|
134
|
+
}
|
135
|
+
|
136
|
+
if config.retry
|
137
|
+
Retryable.retryable(tries: config.retry[:tries], sleep: config.retry[:sleep], &request_proc)
|
138
|
+
else
|
139
|
+
request_proc.call
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def url(path, service: "api")
|
144
|
+
service_url = config.service_url(service)
|
145
|
+
service_url + path
|
146
|
+
end
|
147
|
+
|
148
|
+
def parse_data(response)
|
149
|
+
JSON.parse(response.body.to_s, object_class: Scrimmage::Object)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Scrimmage::Config = Struct.new(
|
4
|
+
:api_server_endpoint,
|
5
|
+
:private_key,
|
6
|
+
:namespace,
|
7
|
+
:secure,
|
8
|
+
:retry,
|
9
|
+
keyword_init: true
|
10
|
+
) do |klass|
|
11
|
+
def initialize(**args)
|
12
|
+
super
|
13
|
+
validate_protocol! if api_server_endpoint
|
14
|
+
end
|
15
|
+
|
16
|
+
# define e.g. private_key! to fetch struct member or raise an error
|
17
|
+
klass.members.each do |member_key|
|
18
|
+
define_method "#{member_key}!" do
|
19
|
+
raise Scrimmage::Errors::MissingConfigurationError(member_key) unless send(member_key)
|
20
|
+
|
21
|
+
send(member_key)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def service_url(service)
|
26
|
+
"#{api_server_endpoint}/#{service}"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def validate_protocol!
|
32
|
+
protocol_regex = secure ? %r{^https://.+} : %r{^https?://.+}
|
33
|
+
return if api_server_endpoint.match?(protocol_regex)
|
34
|
+
|
35
|
+
raise Scrimmage::Errors::ConfigurationError("Service URL must start with http#{secure ? "s" : ""}://")
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Scrimmage
|
4
|
+
module Errors
|
5
|
+
class Base < StandardError; end
|
6
|
+
|
7
|
+
class ConfigurationError < Base; end
|
8
|
+
|
9
|
+
class MissingConfigurationError < ConfigurationError
|
10
|
+
attr_reader :config_key
|
11
|
+
|
12
|
+
def initialize(config_key)
|
13
|
+
@config_key = config_key
|
14
|
+
super("#{config_key} not configured")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class AccountNotLinkedError < Base
|
19
|
+
attr_reader :user_id
|
20
|
+
|
21
|
+
def initialize(user_id)
|
22
|
+
@user_id = user_id
|
23
|
+
super("Account not linked")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class InvalidPrivateKeyError < Base; end
|
28
|
+
|
29
|
+
class RequestFailedError < Base
|
30
|
+
attr_reader :response
|
31
|
+
|
32
|
+
def initialize(response)
|
33
|
+
@response = response
|
34
|
+
super("HTTP request failed with status #{response.code}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Scrimmage
|
4
|
+
class Rewards
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(client: Scrimmage.default_client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Track one or multiple rewards
|
13
|
+
#
|
14
|
+
# @param [String] user_id
|
15
|
+
# @param [String] data_type
|
16
|
+
# @param [Array<Hash>] rewards
|
17
|
+
#
|
18
|
+
# @return [Array<Scrimmage::Object>]
|
19
|
+
#
|
20
|
+
def track_rewardable(user_id, data_type, rewards = [])
|
21
|
+
rewards = [rewards] unless rewards.is_a? Array
|
22
|
+
rewards.map do |reward|
|
23
|
+
client.create_integration_reward(user_id, data_type, reward)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Track Rewardable with a single event
|
29
|
+
#
|
30
|
+
# @param [String] user_id
|
31
|
+
# @param [String] data_type
|
32
|
+
# @param [String] unique_id
|
33
|
+
# @param [Hash] reward
|
34
|
+
#
|
35
|
+
# @return [Scrimmage::Object]
|
36
|
+
#
|
37
|
+
def track_rewardable_once(user_id, data_type, unique_id, reward)
|
38
|
+
client.create_integration_reward(user_id, data_type, unique_id, reward)
|
39
|
+
end
|
40
|
+
|
41
|
+
# delegate class methods to new instance
|
42
|
+
class << self
|
43
|
+
extend Forwardable
|
44
|
+
def_delegators :new, :track_rewardable, :track_rewardable_once
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Scrimmage
|
4
|
+
class Status
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(client: Scrimmage.default_client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Verify API Service Status
|
13
|
+
#
|
14
|
+
# @return [Hash{verified: Boolean, errors: Array<String>}] A hash object indicating service status
|
15
|
+
#
|
16
|
+
def verify
|
17
|
+
verified = true
|
18
|
+
errors = []
|
19
|
+
|
20
|
+
unless client.get_overall_service_status
|
21
|
+
verified = false
|
22
|
+
errors << "Rewarder API is not available"
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
client.get_rewarder_key_details
|
27
|
+
rescue Scrimmage::Errors::RequestFailedError
|
28
|
+
verified = false
|
29
|
+
errors << "Rewarder API key is invalid"
|
30
|
+
end
|
31
|
+
{ verified: verified, errors: errors }
|
32
|
+
end
|
33
|
+
|
34
|
+
# delegate class methods to new instance
|
35
|
+
class << self
|
36
|
+
extend Forwardable
|
37
|
+
def_delegators :new, :verify
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Scrimmage
|
4
|
+
class Users
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(client: Scrimmage.default_client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_user_token(user_id, **options)
|
12
|
+
client.get_user_token(user_id, **options)
|
13
|
+
end
|
14
|
+
|
15
|
+
# delegate class methods to new instance
|
16
|
+
class << self
|
17
|
+
extend Forwardable
|
18
|
+
def_delegators :new, :get_user_token
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/scrimmage.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "scrimmage/version"
|
4
|
+
|
5
|
+
Dir[File.join(__dir__, "scrimmage", "*.rb")].each { |file| require file }
|
6
|
+
|
7
|
+
module Scrimmage
|
8
|
+
@config = Scrimmage::Config.new(
|
9
|
+
secure: true,
|
10
|
+
retry: {
|
11
|
+
tries: 3,
|
12
|
+
sleep_base: (100 / 1000.0),
|
13
|
+
sleep: ->(n) { config.retry[:sleep_base] * (2**n) }
|
14
|
+
}
|
15
|
+
)
|
16
|
+
|
17
|
+
module_function
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield @config
|
21
|
+
end
|
22
|
+
|
23
|
+
def config
|
24
|
+
if block_given?
|
25
|
+
yield @config
|
26
|
+
else
|
27
|
+
@config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_client
|
32
|
+
Scrimmage::Client.new
|
33
|
+
end
|
34
|
+
end
|
data/sig/scrimmage.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scrimmage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scrimmage Co
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-12-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: retryable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.5
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.5
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- support@scrimmage.co
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".env.example"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/scrimmage.rb
|
56
|
+
- lib/scrimmage/client.rb
|
57
|
+
- lib/scrimmage/config.rb
|
58
|
+
- lib/scrimmage/errors.rb
|
59
|
+
- lib/scrimmage/object.rb
|
60
|
+
- lib/scrimmage/rewards.rb
|
61
|
+
- lib/scrimmage/status.rb
|
62
|
+
- lib/scrimmage/users.rb
|
63
|
+
- lib/scrimmage/version.rb
|
64
|
+
- sig/scrimmage.rbs
|
65
|
+
homepage: https://github.com/scrimmage-co/scrimmage-ruby
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata:
|
69
|
+
homepage_uri: https://github.com/scrimmage-co/scrimmage-ruby
|
70
|
+
source_code_uri: https://github.com/scrimmage-co/ruby-sdk
|
71
|
+
changelog_uri: https://github.com/scrimmage-co/ruby-sdk/CHANGELOG.md
|
72
|
+
rubygems_mfa_required: 'true'
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 3.0.0
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.3.5
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Ruby SDK for the Scrimmage Rewards API
|
92
|
+
test_files: []
|