absmartly-sdk 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +180 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +21 -0
  8. data/Gemfile.lock +92 -0
  9. data/LICENSE.txt +21 -0
  10. data/README +180 -0
  11. data/Rakefile +12 -0
  12. data/example/example.rb +48 -0
  13. data/lib/a_b_smartly.rb +92 -0
  14. data/lib/a_b_smartly_config.rb +49 -0
  15. data/lib/absmartly/md5.rb +178 -0
  16. data/lib/absmartly/variant_assigner.rb +36 -0
  17. data/lib/absmartly/version.rb +5 -0
  18. data/lib/absmartly.rb +57 -0
  19. data/lib/audience_deserializer.rb +8 -0
  20. data/lib/audience_matcher.rb +38 -0
  21. data/lib/client.rb +80 -0
  22. data/lib/client_config.rb +43 -0
  23. data/lib/context.rb +528 -0
  24. data/lib/context_config.rb +70 -0
  25. data/lib/context_data_deserializer.rb +8 -0
  26. data/lib/context_data_provider.rb +8 -0
  27. data/lib/context_event_handler.rb +8 -0
  28. data/lib/context_event_logger.rb +9 -0
  29. data/lib/context_event_serializer.rb +8 -0
  30. data/lib/default_audience_deserializer.rb +13 -0
  31. data/lib/default_context_data_deserializer.rb +16 -0
  32. data/lib/default_context_data_provider.rb +15 -0
  33. data/lib/default_context_event_handler.rb +15 -0
  34. data/lib/default_context_event_serializer.rb +58 -0
  35. data/lib/default_http_client.rb +61 -0
  36. data/lib/default_http_client_config.rb +19 -0
  37. data/lib/default_variable_parser.rb +13 -0
  38. data/lib/hashing.rb +9 -0
  39. data/lib/http_client.rb +20 -0
  40. data/lib/json/attribute.rb +30 -0
  41. data/lib/json/context_data.rb +31 -0
  42. data/lib/json/experiment.rb +83 -0
  43. data/lib/json/experiment_application.rb +27 -0
  44. data/lib/json/experiment_variant.rb +29 -0
  45. data/lib/json/exposure.rb +60 -0
  46. data/lib/json/goal_achievement.rb +32 -0
  47. data/lib/json/publish_event.rb +42 -0
  48. data/lib/json/unit.rb +30 -0
  49. data/lib/json_expr/evaluator.rb +27 -0
  50. data/lib/json_expr/expr_evaluator.rb +122 -0
  51. data/lib/json_expr/json_expr.rb +37 -0
  52. data/lib/json_expr/operator.rb +8 -0
  53. data/lib/json_expr/operators/and_combinator.rb +14 -0
  54. data/lib/json_expr/operators/binary_operator.rb +22 -0
  55. data/lib/json_expr/operators/boolean_combinator.rb +15 -0
  56. data/lib/json_expr/operators/equals_operator.rb +12 -0
  57. data/lib/json_expr/operators/greater_than_operator.rb +12 -0
  58. data/lib/json_expr/operators/greater_than_or_equal_operator.rb +12 -0
  59. data/lib/json_expr/operators/in_operator.rb +23 -0
  60. data/lib/json_expr/operators/less_than_operator.rb +12 -0
  61. data/lib/json_expr/operators/less_than_or_equal_operator.rb +12 -0
  62. data/lib/json_expr/operators/match_operator.rb +17 -0
  63. data/lib/json_expr/operators/nil_operator.rb +11 -0
  64. data/lib/json_expr/operators/not_operator.rb +11 -0
  65. data/lib/json_expr/operators/or_combinator.rb +14 -0
  66. data/lib/json_expr/operators/unary_operator.rb +13 -0
  67. data/lib/json_expr/operators/value_operator.rb +11 -0
  68. data/lib/json_expr/operators/var_operator.rb +21 -0
  69. data/lib/scheduled_executor_service.rb +8 -0
  70. data/lib/scheduled_thread_pool_executor.rb +14 -0
  71. data/lib/string.rb +30 -0
  72. data/lib/variable_parser.rb +8 -0
  73. data/lib/variant_assigner.rb +47 -0
  74. data/sig/absmartly.rbs +4 -0
  75. metadata +120 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 16d7c8f31de8bdbceddf60618c90ddfc81ab73381403d957a7bd8a3fbdb43d3a
4
+ data.tar.gz: 2145b7d168f0e425164e1fcccb14d3efbd1ac4d91ddc3c5ebc120fca32734cdf
5
+ SHA512:
6
+ metadata.gz: 840d40d52e3878321a7d08ebfe1bb38e9db8994a3296afc2d3de43383ca01b8b0cc0513a2a0e4480b679ef38a8c3edda3688bd0bbcbb7b5a82c39a7f979ba595
7
+ data.tar.gz: cb15ab60ca2a4cd40e736b8b69b5a32b131f9a92ac8942cb3b48d16fb20b67915636035b30dd2b8d5c7039ec787509fcd3080773aa902fe23988742cce027269
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,180 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ DisabledByDefault: true
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - 'gemfiles/**/*'
7
+
8
+ Style/AndOr:
9
+ Enabled: true
10
+
11
+ Layout/CaseIndentation:
12
+ Enabled: true
13
+
14
+ Layout/ClosingHeredocIndentation:
15
+ Enabled: true
16
+
17
+ Layout/CommentIndentation:
18
+ Enabled: true
19
+
20
+ Layout/ElseAlignment:
21
+ Enabled: true
22
+
23
+ Layout/EndAlignment:
24
+ Enabled: true
25
+ EnforcedStyleAlignWith: variable
26
+ AutoCorrect: true
27
+
28
+ Layout/EmptyLineAfterMagicComment:
29
+ Enabled: true
30
+
31
+ Layout/EmptyLinesAroundAccessModifier:
32
+ Enabled: true
33
+ EnforcedStyle: only_before
34
+
35
+ Layout/EmptyLinesAroundBlockBody:
36
+ Enabled: true
37
+
38
+ Layout/EmptyLinesAroundClassBody:
39
+ Enabled: true
40
+
41
+ Layout/EmptyLinesAroundMethodBody:
42
+ Enabled: true
43
+
44
+ Layout/EmptyLinesAroundModuleBody:
45
+ Enabled: true
46
+
47
+ Style/HashSyntax:
48
+ Enabled: true
49
+
50
+ Layout/FirstArgumentIndentation:
51
+ Enabled: true
52
+
53
+ Layout/IndentationConsistency:
54
+ Enabled: true
55
+ EnforcedStyle: indented_internal_methods
56
+
57
+ Layout/IndentationWidth:
58
+ Enabled: true
59
+
60
+ Layout/LeadingCommentSpace:
61
+ Enabled: true
62
+
63
+ Layout/SpaceAfterColon:
64
+ Enabled: true
65
+
66
+ Layout/SpaceAfterComma:
67
+ Enabled: true
68
+
69
+ Layout/SpaceAfterSemicolon:
70
+ Enabled: true
71
+
72
+ Layout/SpaceAroundEqualsInParameterDefault:
73
+ Enabled: true
74
+
75
+ Layout/SpaceAroundKeyword:
76
+ Enabled: true
77
+
78
+ Layout/SpaceBeforeComma:
79
+ Enabled: true
80
+
81
+ Layout/SpaceBeforeComment:
82
+ Enabled: true
83
+
84
+ Layout/SpaceBeforeFirstArg:
85
+ Enabled: true
86
+
87
+ Style/DefWithParentheses:
88
+ Enabled: true
89
+
90
+ Style/MethodDefParentheses:
91
+ Enabled: true
92
+
93
+ Style/FrozenStringLiteralComment:
94
+ Enabled: true
95
+ EnforcedStyle: always
96
+
97
+ Style/RedundantFreeze:
98
+ Enabled: true
99
+
100
+ Layout/SpaceBeforeBlockBraces:
101
+ Enabled: true
102
+
103
+ Layout/SpaceInsideBlockBraces:
104
+ Enabled: true
105
+ EnforcedStyleForEmptyBraces: space
106
+
107
+ Layout/SpaceInsideHashLiteralBraces:
108
+ Enabled: true
109
+
110
+ Layout/SpaceInsideParens:
111
+ Enabled: true
112
+
113
+ Style/StringLiterals:
114
+ Enabled: true
115
+ EnforcedStyle: double_quotes
116
+
117
+ Layout/IndentationStyle:
118
+ Enabled: true
119
+
120
+ Layout/TrailingEmptyLines:
121
+ Enabled: true
122
+
123
+ Layout/TrailingWhitespace:
124
+ Enabled: true
125
+
126
+ Style/RedundantPercentQ:
127
+ Enabled: true
128
+
129
+ Lint/AmbiguousOperator:
130
+ Enabled: true
131
+
132
+ Lint/AmbiguousRegexpLiteral:
133
+ Enabled: true
134
+
135
+ Lint/ErbNewArguments:
136
+ Enabled: true
137
+
138
+ Lint/RequireParentheses:
139
+ Enabled: true
140
+
141
+ Lint/ShadowingOuterLocalVariable:
142
+ Enabled: true
143
+
144
+ Lint/RedundantStringCoercion:
145
+ Enabled: true
146
+
147
+ Lint/UriEscapeUnescape:
148
+ Enabled: true
149
+
150
+ Lint/UselessAssignment:
151
+ Enabled: true
152
+
153
+ Lint/DeprecatedClassMethods:
154
+ Enabled: true
155
+
156
+ Style/ParenthesesAroundCondition:
157
+ Enabled: true
158
+
159
+ Style/HashTransformKeys:
160
+ Enabled: true
161
+
162
+ Style/HashTransformValues:
163
+ Enabled: true
164
+
165
+ Style/RedundantBegin:
166
+ Enabled: true
167
+
168
+ Style/RedundantReturn:
169
+ Enabled: true
170
+ AllowMultipleReturnValues: true
171
+
172
+ Style/Semicolon:
173
+ Enabled: true
174
+ AllowAsExpressionSeparator: true
175
+
176
+ Style/ColonMethodCall:
177
+ Enabled: true
178
+
179
+ Style/TrivialAccessors:
180
+ Enabled: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-03
4
+
5
+ - Initial release
@@ -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 omairr.azam@gmail.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/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in absmartly.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "arraybuffer"
10
+ gem "murmurhash3"
11
+ gem "faraday"
12
+
13
+ group :test do
14
+ gem "rspec", "~> 3.5"
15
+ end
16
+
17
+ group :develop do
18
+ gem "irb"
19
+ gem "byebug"
20
+ gem "rubocop", "~> 1.21"
21
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ absmartly (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ arraybuffer (0.0.6)
10
+ ast (2.4.2)
11
+ byebug (11.1.3)
12
+ diff-lcs (1.5.0)
13
+ faraday (1.8.0)
14
+ faraday-em_http (~> 1.0)
15
+ faraday-em_synchrony (~> 1.0)
16
+ faraday-excon (~> 1.1)
17
+ faraday-httpclient (~> 1.0.1)
18
+ faraday-net_http (~> 1.0)
19
+ faraday-net_http_persistent (~> 1.1)
20
+ faraday-patron (~> 1.0)
21
+ faraday-rack (~> 1.0)
22
+ multipart-post (>= 1.2, < 3)
23
+ ruby2_keywords (>= 0.0.4)
24
+ faraday-em_http (1.0.0)
25
+ faraday-em_synchrony (1.0.0)
26
+ faraday-excon (1.1.0)
27
+ faraday-httpclient (1.0.1)
28
+ faraday-net_http (1.0.1)
29
+ faraday-net_http_persistent (1.2.0)
30
+ faraday-patron (1.0.0)
31
+ faraday-rack (1.0.0)
32
+ io-console (0.5.6)
33
+ irb (1.2.6)
34
+ reline (>= 0.1.5)
35
+ json (2.6.2)
36
+ multipart-post (2.1.1)
37
+ murmurhash3 (0.1.6)
38
+ parallel (1.22.1)
39
+ parser (3.1.2.0)
40
+ ast (~> 2.4.1)
41
+ rainbow (3.1.1)
42
+ rake (13.0.6)
43
+ regexp_parser (2.5.0)
44
+ reline (0.1.5)
45
+ io-console (~> 0.5)
46
+ rexml (3.2.5)
47
+ rspec (3.11.0)
48
+ rspec-core (~> 3.11.0)
49
+ rspec-expectations (~> 3.11.0)
50
+ rspec-mocks (~> 3.11.0)
51
+ rspec-core (3.11.0)
52
+ rspec-support (~> 3.11.0)
53
+ rspec-expectations (3.11.0)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.11.0)
56
+ rspec-mocks (3.11.1)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.11.0)
59
+ rspec-support (3.11.0)
60
+ rubocop (1.33.0)
61
+ json (~> 2.3)
62
+ parallel (~> 1.10)
63
+ parser (>= 3.1.0.0)
64
+ rainbow (>= 2.2.2, < 4.0)
65
+ regexp_parser (>= 1.8, < 3.0)
66
+ rexml (>= 3.2.5, < 4.0)
67
+ rubocop-ast (>= 1.19.1, < 2.0)
68
+ ruby-progressbar (~> 1.7)
69
+ unicode-display_width (>= 1.4.0, < 3.0)
70
+ rubocop-ast (1.19.1)
71
+ parser (>= 3.1.1.0)
72
+ ruby-progressbar (1.11.0)
73
+ ruby2_keywords (0.0.5)
74
+ unicode-display_width (2.2.0)
75
+
76
+ PLATFORMS
77
+ ruby
78
+ x86_64-darwin-20
79
+
80
+ DEPENDENCIES
81
+ absmartly!
82
+ arraybuffer
83
+ byebug
84
+ faraday
85
+ irb
86
+ murmurhash3
87
+ rake (~> 13.0)
88
+ rspec (~> 3.5)
89
+ rubocop (~> 1.21)
90
+
91
+ BUNDLED WITH
92
+ 2.3.24
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 omairazam
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 ADDED
@@ -0,0 +1,180 @@
1
+ # A/B Smartly SDK
2
+
3
+ A/B Smartly Ruby SDK
4
+
5
+ ## Compatibility
6
+
7
+ The A/B Smartly Ruby SDK is compatible with Ruby versions 2.7 and later. For the best performance and code readability, Ruby 3 or later is recommended. This SDK is being constantly tested with the nightly builds of Ruby, to ensure it is compatible with the latest Ruby version.
8
+
9
+ ## Getting Started
10
+
11
+ ### Install the SDK
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ $ bundle add absmartly
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ $ gem install absmartly
20
+
21
+ ## Basic Usage
22
+
23
+ Once the SDK is installed, it can be initialized in your project.
24
+
25
+ You can create an SDK instance using the API key, application name, environment, and the endpoint URL obtained from A/B Smartly.
26
+
27
+ ```Ruby
28
+ require 'absmartly'
29
+
30
+ Absmartly.configure_client do |config|
31
+ config.endpoint = "https://your-company.absmartly.io/v1"
32
+ config.api_key = "YOUR-API-KEY"
33
+ config.application = "website"
34
+ config.environment = "development"
35
+ end
36
+ ```
37
+ #### Creating a new Context with raw promises
38
+
39
+ ```Ruby
40
+ # define a new context request
41
+ context_config = Absmartly.create_context_config
42
+ context_config.set_unit("session_id", "bf06d8cb5d8137290c4abb64155584fbdb64d8")
43
+ context_config.set_unit("user_id", "123456")
44
+
45
+ context = Absmartly.create_context(context_config)
46
+ ```
47
+
48
+ ### Selecting A Treatment
49
+
50
+ ```Ruby
51
+ treatment = context.treatment('exp_test_experiment')
52
+
53
+ if treatment.zero?
54
+ # user is in control group (variant 0)
55
+ else
56
+ # user is in treatment group
57
+ end
58
+ ```
59
+
60
+ ### Treatment Variables
61
+
62
+ ```Ruby
63
+ default_button_color_value = 'red'
64
+ button_color = context.variable_value('button.color')
65
+ ```
66
+
67
+ ### Peek at Treatment Variants
68
+
69
+ Although generally not recommended, it is sometimes necessary to peek at a treatment or variable without triggering an exposure. The A/B Smartly SDK provides a `Context.peek_treatment()` method for that.
70
+
71
+ ```Ruby
72
+ treatment = context.peek_treatment('exp_test_experiment')
73
+
74
+ if treatment.zero?
75
+ # user is in control group (variant 0)
76
+ else
77
+ # user is in treatment group
78
+ end
79
+ ```
80
+
81
+ #### Peeking at variables
82
+
83
+ ```Ruby
84
+ button_color = context.peek_variable_value('button.color', 'red')
85
+ ```
86
+
87
+ ### Overriding Treatment Variants
88
+
89
+ During development, for example, it is useful to force a treatment for an
90
+ experiment. This can be achieved with the `Context.set_override()` and/or `Context.set_overrides()` methods. These methods can be called before the context is ready.
91
+
92
+ ```Ruby
93
+ context.set_override("exp_test_experiment", 1) # force variant 1 of treatment
94
+
95
+ context.set_overrides(
96
+ 'exp_test_experiment' => 1,
97
+ 'exp_another_experiment' => 0,
98
+ )
99
+ ```
100
+
101
+ ## Advanced
102
+
103
+ ### Context Attributes
104
+
105
+ Attributes are used to pass meta-data about the user and/or the request.
106
+ They can be used later in the Web Console to create segments or audiences.
107
+ They can be set using the `context.set_attribute()` or `context.set_attributes()` methods, before or after the context is ready.
108
+
109
+ ```Ruby
110
+ context.set_attribute('session_id', session_id)
111
+ context.set_attributes(
112
+ 'customer_age' => 'new_customer'
113
+ )
114
+ ```
115
+
116
+ ### Custom Assignments
117
+
118
+ Sometimes it may be necessary to override the automatic selection of a variant. For example, if you wish to have your variant chosen based on data from an API call. This can be accomplished using the `Context.set_custom_assignment()` method.
119
+
120
+ ```Ruby
121
+ chosen_variant = 1
122
+ context.set_custom_assignment("experiment_name", chosen_variant)
123
+ ```
124
+
125
+ If you are running multiple experiments and need to choose different custom assignments for each one, you can do so using the `Context->setCustomAssignments()` method.
126
+
127
+ ```Ruby
128
+ assignments = [
129
+ "experiment_name" => 1,
130
+ "another_experiment_name" => 0,
131
+ "a_third_experiment_name" => 2
132
+ ]
133
+
134
+ context.set_custom_assignments(assignments)
135
+ ```
136
+
137
+ ### Publish
138
+
139
+ Sometimes it is necessary to ensure all events have been published to the A/B Smartly collector, before proceeding. You can explicitly call the `context.publish()` method.
140
+
141
+ ```Ruby
142
+ context.publish
143
+ ```
144
+
145
+ ### Finalize
146
+
147
+ The `close()` method will ensure all events have been published to the A/B Smartly collector, like `context.publish()`, and will also "seal" the context, throwing an error if any method that could generate an event is called.
148
+
149
+ ```Ruby
150
+ context.close
151
+ ```
152
+
153
+ ### Tracking Goals
154
+
155
+ ```Ruby
156
+ context.track(
157
+ 'payment',
158
+ { item_count: 1, total_amount: 1999.99 }
159
+ )
160
+ ```
161
+
162
+
163
+
164
+ ## Development
165
+
166
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
167
+
168
+ 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).
169
+
170
+ ## Contributing
171
+
172
+ Bug reports and pull requests are welcome on GitHub at https://github.com/omairazam/absmartly. 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/omairazam/absmartly/blob/master/CODE_OF_CONDUCT.md).
173
+
174
+ ## License
175
+
176
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
177
+
178
+ ## Code of Conduct
179
+
180
+ Everyone interacting in the Absmartly project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/omairazam/absmartly/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,48 @@
1
+ require_relative "../lib/absmartly"
2
+
3
+ # config file
4
+ Absmartly.configure_client do |config|
5
+ config.endpoint = "https://demo.absmartly.io/v1"
6
+ config.api_key = "x3ZyxmeKmb6n3VilTGs5I6-tBdaS9gYyr3i4YQXmUZcpPhH8nd8ev44NoEL_3yvA"
7
+ config.application = "www"
8
+ config.environment = "prod"
9
+ end
10
+
11
+ # define a new context request
12
+ context_config = Absmartly.create_context_config
13
+ context_config.set_unit("session_id", "bf06d8cb5d8137290c4abb64155584fbdb64d8")
14
+ context_config.set_unit("user_id", "123456")
15
+
16
+ ctx = Absmartly.create_context(context_config)
17
+
18
+ treatment = ctx.treatment("exp_test_ab")
19
+ puts(treatment) # 0
20
+ treatment1 = ctx.treatment("net_seasons")
21
+ puts(treatment1) # 1
22
+ treatment2 = ctx.treatment("Experimento!")
23
+ puts(treatment2) # 1
24
+ treatment3 = ctx.treatment("test")
25
+ puts(treatment3) # 1
26
+
27
+ ctx.set_unit("db_user_id", 1000013)
28
+ ctx.set_units(db_user_id2: 1000013, session_id: 12311)
29
+
30
+ ctx.set_attribute("user_agent", "Chrome 2022")
31
+ ctx.set_attributes(
32
+ customer_age: "new_customer",
33
+ customer_point: 20,
34
+ )
35
+
36
+ ctx.set_override("new_exp", 3)
37
+ ctx.set_overrides(
38
+ exp_test_experiment: 1,
39
+ exp_another_experiment: 0,
40
+ )
41
+ ctx.publish
42
+ properties = {
43
+ value: 125,
44
+ fee: 125
45
+ }
46
+ ctx.track("payment", properties)
47
+
48
+ ctx.close