strait 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b40ba499b6bfc486b64738eef4de0a804d8d1a588f44ce2eeae8f2bc12bf3b70
4
+ data.tar.gz: 68bcbb30542760922e4c9719f333683a37698446a492585b7b6a6bc97f920e54
5
+ SHA512:
6
+ metadata.gz: f4fcd37bc5522944443799ed209b90c12a65f3bd550e5bb0b2d6d0c84feb68d6e7019465d65f1ee16782b4e5ca078224179178289bde4c9610257934a45a2b15
7
+ data.tar.gz: fa5e4c6717dfe3bd1512ad82276fcf938e9bf0392aaa8b9003f26963bd0c715b102e79292d4062d5462913008cb9075edff750db3b2711db931b12c3e9748ca8
data/.editorconfig ADDED
@@ -0,0 +1,15 @@
1
+ # EditorConfig helps developers define and maintain consistent
2
+ # coding styles between different editors and IDEs
3
+ # editorconfig.org
4
+
5
+ [*]
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+ indent_style = space
11
+ indent_size = 2
12
+ max_line_length = 100
13
+
14
+ [*.{diff,md}]
15
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,292 @@
1
+ AllCops:
2
+ Exclude:
3
+ - db/**/*
4
+ - bin/**/*
5
+ - Guardfile
6
+ DisplayCopNames: false
7
+ DisplayStyleGuide: true
8
+ StyleGuideCopsOnly: false
9
+ TargetRubyVersion: 2.3
10
+ Layout/AlignParameters:
11
+ EnforcedStyle: with_fixed_indentation
12
+ Style/BlockDelimiters:
13
+ EnforcedStyle: braces_for_chaining
14
+ Style/CollectionMethods:
15
+ PreferredMethods:
16
+ collect: map
17
+ collect!: map!
18
+ inject: reduce
19
+ detect: find
20
+ find_all: select
21
+ Style/CommentAnnotation:
22
+ Enabled: true
23
+ Keywords:
24
+ - TODO
25
+ - FIXME
26
+ - OPTIMIZE
27
+ - HACK
28
+ - REVIEW
29
+ - DEPRECATED
30
+ Layout/DotPosition:
31
+ EnforcedStyle: leading
32
+ Layout/EmptyLineAfterGuardClause:
33
+ Enabled: false
34
+ Naming/FileName:
35
+ Enabled: true
36
+ Style/FormatString:
37
+ Enabled: true
38
+ EnforcedStyle: format
39
+ Style/FrozenStringLiteralComment:
40
+ Enabled: true
41
+ Style/GlobalVars:
42
+ Enabled: false
43
+ Style/GuardClause:
44
+ Enabled: true
45
+ MinBodyLength: 3
46
+ Style/IfUnlessModifier:
47
+ Enabled: true
48
+ Layout/IndentFirstArrayElement:
49
+ EnforcedStyle: consistent
50
+ Layout/IndentFirstHashElement:
51
+ EnforcedStyle: consistent
52
+ Style/LambdaCall:
53
+ Enabled: true
54
+ Style/Next:
55
+ Enabled: true
56
+ MinBodyLength: 3
57
+ Style/NumericLiterals:
58
+ Enabled: true
59
+ MinDigits: 5
60
+ Style/PercentLiteralDelimiters:
61
+ Enabled: true
62
+ PreferredDelimiters:
63
+ "%": "[]"
64
+ "%i": "[]"
65
+ "%q": "[]"
66
+ "%Q": "[]"
67
+ "%r": "{}"
68
+ "%s": "()"
69
+ "%w": "[]"
70
+ "%W": "[]"
71
+ "%x": "[]"
72
+ Naming/PredicateName:
73
+ Enabled: true
74
+ NamePrefix:
75
+ - is_
76
+ - has_
77
+ - have_
78
+ NamePrefixBlacklist:
79
+ - is_
80
+ Style/RaiseArgs:
81
+ Enabled: true
82
+ EnforcedStyle: exploded
83
+ Style/SingleLineMethods:
84
+ Enabled: true
85
+ AllowIfMethodIsEmpty: true
86
+ Style/StringLiterals:
87
+ EnforcedStyle: single_quotes
88
+ Layout/SpaceAroundEqualsInParameterDefault:
89
+ Enabled: true
90
+ EnforcedStyle: space
91
+ Layout/SpaceBeforeBlockBraces:
92
+ Enabled: true
93
+ EnforcedStyle: space
94
+ Layout/SpaceInsideBlockBraces:
95
+ Enabled: true
96
+ EnforcedStyle: space
97
+ EnforcedStyleForEmptyBraces: no_space
98
+ SpaceBeforeBlockParameters: true
99
+ Layout/SpaceInsideHashLiteralBraces:
100
+ Enabled: true
101
+ EnforcedStyle: space
102
+ EnforcedStyleForEmptyBraces: no_space
103
+ Style/SymbolProc:
104
+ Enabled: true
105
+ Layout/TrailingBlankLines:
106
+ Enabled: true
107
+ EnforcedStyle: final_newline
108
+ Style/TrailingCommaInArguments:
109
+ Enabled: true
110
+ EnforcedStyleForMultiline: no_comma
111
+ Style/TrailingCommaInArrayLiteral:
112
+ Enabled: true
113
+ EnforcedStyleForMultiline: no_comma
114
+ Style/TrailingCommaInHashLiteral:
115
+ Enabled: true
116
+ EnforcedStyleForMultiline: no_comma
117
+ Style/TrivialAccessors:
118
+ Enabled: true
119
+ AllowDSLWriters: true
120
+ Naming/VariableName:
121
+ Enabled: true
122
+ EnforcedStyle: snake_case
123
+ Style/WordArray:
124
+ Enabled: true
125
+ MinSize: 2
126
+ Metrics/AbcSize:
127
+ Enabled: true
128
+ Max: 25
129
+ Metrics/BlockNesting:
130
+ Enabled: true
131
+ Max: 5
132
+ Metrics/BlockLength:
133
+ Enabled: false
134
+ Metrics/ClassLength:
135
+ Enabled: true
136
+ CountComments: false
137
+ Max: 500
138
+ Metrics/ModuleLength:
139
+ Enabled: true
140
+ CountComments: false
141
+ Max: 500
142
+ Metrics/CyclomaticComplexity:
143
+ Enabled: true
144
+ Max: 10
145
+ Metrics/LineLength:
146
+ Enabled: true
147
+ Max: 100
148
+ AllowURI: true
149
+ IgnoreCopDirectives: true
150
+ Exclude:
151
+ - config/routes.rb
152
+ Metrics/MethodLength:
153
+ Enabled: true
154
+ CountComments: false
155
+ Max: 100
156
+ Metrics/ParameterLists:
157
+ Enabled: true
158
+ Max: 5
159
+ CountKeywordArgs: false
160
+ Metrics/PerceivedComplexity:
161
+ Enabled: true
162
+ Max: 8
163
+ Lint/AssignmentInCondition:
164
+ Enabled: true
165
+ AllowSafeAssignment: true
166
+ Style/InlineComment:
167
+ Enabled: false
168
+ Style/MethodCalledOnDoEndBlock:
169
+ Enabled: true
170
+ Style/SymbolArray:
171
+ Enabled: true
172
+ Naming/AccessorMethodName:
173
+ Enabled: true
174
+ Style/Alias:
175
+ Enabled: true
176
+ EnforcedStyle: prefer_alias_method
177
+ Style/ArrayJoin:
178
+ Enabled: true
179
+ Style/AsciiComments:
180
+ Enabled: false
181
+ Naming/AsciiIdentifiers:
182
+ Enabled: true
183
+ Style/Attr:
184
+ Enabled: true
185
+ Style/BlockComments:
186
+ Enabled: true
187
+ Style/ColonMethodCall:
188
+ Enabled: true
189
+ Style/Documentation:
190
+ Enabled: false
191
+ Style/EmptyLiteral:
192
+ Enabled: true
193
+ Style/EvenOdd:
194
+ Enabled: true
195
+ Layout/FlipFlop:
196
+ Enabled: true
197
+ Style/IfWithSemicolon:
198
+ Enabled: true
199
+ Style/Lambda:
200
+ EnforcedStyle: literal
201
+ # HACK: Until Rubocop is released with EnforcedStyle
202
+ Enabled: true
203
+ Layout/LeadingCommentSpace:
204
+ Enabled: true
205
+ Style/MultilineBlockChain:
206
+ Enabled: true
207
+ Style/MultilineTernaryOperator:
208
+ Enabled: true
209
+ Style/NegatedIf:
210
+ Enabled: true
211
+ Style/NestedTernaryOperator:
212
+ Enabled: true
213
+ Style/NilComparison:
214
+ Enabled: true
215
+ Style/Not:
216
+ Enabled: true
217
+ Style/OneLineConditional:
218
+ Enabled: true
219
+ Style/PerlBackrefs:
220
+ Enabled: true
221
+ Style/RedundantBegin:
222
+ Enabled: true
223
+ Style/RedundantSelf:
224
+ Enabled: true
225
+ Style/RescueModifier:
226
+ Enabled: true
227
+ Style/SelfAssignment:
228
+ Enabled: true
229
+ Style/ClassAndModuleChildren:
230
+ Enabled: true
231
+ Exclude:
232
+ - app/graphql/**/*
233
+ Layout/SpaceBeforeFirstArg:
234
+ Enabled: true
235
+ Layout/SpaceAfterColon:
236
+ Enabled: true
237
+ Layout/SpaceAfterComma:
238
+ Enabled: true
239
+ Layout/SpaceAroundKeyword:
240
+ Enabled: true
241
+ Layout/SpaceAfterMethodName:
242
+ Enabled: true
243
+ Layout/SpaceAfterNot:
244
+ Enabled: true
245
+ Layout/SpaceAfterSemicolon:
246
+ Enabled: true
247
+ Layout/SpaceBeforeComma:
248
+ Enabled: true
249
+ Layout/SpaceBeforeComment:
250
+ Enabled: true
251
+ Layout/SpaceBeforeSemicolon:
252
+ Enabled: true
253
+ Layout/SpaceAroundOperators:
254
+ Enabled: true
255
+ Layout/SpaceInsideParens:
256
+ Enabled: true
257
+ Layout/SpaceInsideRangeLiteral:
258
+ Enabled: true
259
+ Style/SpecialGlobalVars:
260
+ Enabled: true
261
+ Layout/TrailingWhitespace:
262
+ Enabled: true
263
+ Style/UnlessElse:
264
+ Enabled: true
265
+ Style/VariableInterpolation:
266
+ Enabled: true
267
+ Style/WhenThen:
268
+ Enabled: true
269
+ Lint/Debugger:
270
+ Enabled: true
271
+ Lint/DeprecatedClassMethods:
272
+ Enabled: true
273
+ Security/Eval:
274
+ Enabled: true
275
+ Lint/HandleExceptions:
276
+ Enabled: true
277
+ Lint/LiteralInInterpolation:
278
+ Enabled: true
279
+ Lint/Loop:
280
+ Enabled: true
281
+ Lint/ParenthesesAsGroupedExpression:
282
+ Enabled: true
283
+ Lint/RequireParentheses:
284
+ Enabled: false
285
+ Lint/UnderscorePrefixedVariableName:
286
+ Enabled: true
287
+ Lint/UselessSetterCall:
288
+ Enabled: true
289
+ Rails/HasAndBelongsToMany:
290
+ Enabled: false
291
+ Rails/TimeZone:
292
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,19 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ env:
8
+ global:
9
+ - CC_TEST_REPORTER_ID=1f7851d1df80cc24da6903abf795ee7a81a60b2cceb7eedd147c59c4ec232dd4
10
+
11
+ before_install: gem install bundler -v 2.0.2
12
+ before_script:
13
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14
+ - chmod +x ./cc-test-reporter
15
+ - ./cc-test-reporter before-build
16
+ script:
17
+ - bundle exec rspec
18
+ after_script:
19
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in strait.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ strait (1.0.0)
5
+ connection_pool (>= 2.0, < 3.0)
6
+ redis (~> 4.0, >= 3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ connection_pool (2.2.2)
12
+ diff-lcs (1.3)
13
+ docile (1.3.2)
14
+ json (2.2.0)
15
+ mock_redis (0.21.0)
16
+ rake (12.3.2)
17
+ redis (4.1.2)
18
+ rspec (3.8.0)
19
+ rspec-core (~> 3.8.0)
20
+ rspec-expectations (~> 3.8.0)
21
+ rspec-mocks (~> 3.8.0)
22
+ rspec-core (3.8.2)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-expectations (3.8.4)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-mocks (3.8.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-support (3.8.2)
31
+ simplecov (0.17.0)
32
+ docile (~> 1.1)
33
+ json (>= 1.8, < 3)
34
+ simplecov-html (~> 0.10.0)
35
+ simplecov-html (0.10.2)
36
+ timecop (0.9.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (~> 2.0)
43
+ mock_redis (~> 0.21)
44
+ rake (~> 12.3)
45
+ rspec (~> 3.0)
46
+ simplecov (~> 0.17)
47
+ strait!
48
+ timecop (~> 0.9)
49
+
50
+ BUNDLED WITH
51
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2019 Kitsu, Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Strait
2
+
3
+ Strait is a rate-limiting library designed to provide security you don't need to think about. Whenever you have code to protect, put a Strait in front of it.
4
+
5
+ It strikes an excellent balance between accuracy and memory usage, with a default accuracy of 1/60th of the limit period.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'strait'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install strait
22
+
23
+ ## Usage
24
+
25
+ Let's say you have a Rails controller with a lot of DoS attack potential.
26
+
27
+ ```ruby
28
+ class SecureThingController
29
+ def do_a_scare
30
+ # Does some heavy work that could open it to a DoS attack!
31
+ end
32
+ end
33
+ ```
34
+
35
+ Well dang, that's no good. Anybody could send thousands of requests to this and take your entire site down, right as you're meeting with an important investor!
36
+
37
+ Let's put a Strait in front of it!
38
+
39
+ ```ruby
40
+ class SecureThingController
41
+ ScareLimiter = Strait.new('do_a_scare') do
42
+ limit 5, per: 1.minute
43
+ end
44
+
45
+ rescue_from Strait::RateLimitExceeded do
46
+ render :rate_limit_exceeded
47
+ end
48
+
49
+ def do_a_scare
50
+ ScareLimiter.limit!(current_user)
51
+ # Does heavy work, but only if the user hasn't exceeded their rate limit!
52
+ end
53
+ end
54
+ ```
55
+
56
+ Viola, just like that, we've got rate limiting. Now a user is limited to 5 per minute!
57
+
58
+ ## Accuracy
59
+
60
+ To understand why Strait isn't perfectly accurate, we should understand how it's implemented. Strait is based on [the bucketed-log pattern made popular by Figma][figma-post], which chooses lower memory usage over perfect accuracy. Despite this decreased accuracy, it fails secure, and should have enough accuracy to not be noticed.
61
+
62
+ Each rate limiter stores data as a set of _N buckets per period_. For example, with 10 buckets and a 1-hour period, each bucket covers 6 minutes. To check the limit, we sum all buckets which overlap the last hour. If the buckets are large (like 6 minutes) this can be up to one bucket longer than the period, resulting in a longer block than 100% accuracy.
63
+
64
+ The default accuracy in Strait is _60 buckets per period_. For a 1-hour period, this is up to 1 minute of inaccuracy. For a 1-minute period, it's up to 1-second. For a 1-day period, it's up to 24 minutes. You can adjust this to increase accuracy, but it will also use more memory.
65
+
66
+ [figma-post]: https://www.figma.com/blog/an-alternative-approach-to-rate-limiting/
67
+
68
+ ## Development
69
+
70
+ 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.
71
+
72
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hummingbird-me/strait.
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [Apache-2.0 License](https://opensource.org/licenses/Apache-2.0).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'strait'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'connection_pool'
4
+ require 'forwardable'
5
+
6
+ class Strait
7
+ class Configuration
8
+ extend Forwardable
9
+ attr_accessor :config
10
+ def_delegators :@config, :[], :[]=, :dig
11
+
12
+ class << self
13
+ attr_accessor :default
14
+ end
15
+
16
+ def initialize(config = {})
17
+ @config = config
18
+ end
19
+
20
+ def merge(new_config = nil)
21
+ return self if new_config.nil? || new_config.empty?
22
+
23
+ self.class.new(config.merge(new_config))
24
+ end
25
+
26
+ def redis_pool
27
+ @redis_pool ||= ConnectionPool.new(config[:pool] || {}) { Redis.new(config[:redis]) }
28
+ end
29
+ end
30
+ end
data/lib/strait/dsl.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Strait
4
+ class DSL
5
+ attr_reader :rules
6
+
7
+ def initialize(&block)
8
+ @rules = []
9
+ instance_eval(&block)
10
+ end
11
+
12
+ def limit(count, per:, buckets: 60)
13
+ @rules << { count: count, period: per, buckets: buckets }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Strait
4
+ class RateLimitExceeded < StandardError
5
+ def initialize(period:, count:, **_other)
6
+ @period = period
7
+ @count = count
8
+ end
9
+
10
+ def to_s
11
+ "Rate Limit Exceeded: #{@count} per #{@period} seconds"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Strait
4
+ class Rule
5
+ def initialize(name:, rule:, config:)
6
+ @name = name
7
+ @rule = rule
8
+ @config = config
9
+ end
10
+
11
+ def call(user)
12
+ period_count_for(user) <= @rule[:count]
13
+ end
14
+
15
+ def to_h
16
+ @rule
17
+ end
18
+
19
+ private
20
+
21
+ # Get the in-period hit count for a user
22
+ def period_count_for(user)
23
+ key = key_for(user)
24
+ # Round the timestamp to get the bucket
25
+ bucket_time = bucket_for(Time.now)
26
+ first_bucket = ((Time.now.to_i - @rule[:period]) / bucket_length).floor
27
+
28
+ @config.redis_pool.with do |redis|
29
+ redis.hincrby(key, bucket_time, 1)
30
+
31
+ # We need to iterate the list once to total the counts, so we use that one iteration to also
32
+ # find any buckets which should be removed.
33
+ results = redis.hgetall(key).each_with_object(total: 0, delete: []) do |(time, count), out|
34
+ if time.to_i < first_bucket
35
+ out[:delete] << time
36
+ else
37
+ out[:total] += count.to_i
38
+ end
39
+ end
40
+
41
+ redis.hdel(key, *results[:delete]) unless results[:delete].empty?
42
+ results[:total]
43
+ end
44
+ end
45
+
46
+ def bucket_for(time)
47
+ (time.to_f / bucket_length).floor * bucket_length
48
+ end
49
+
50
+ def bucket_length
51
+ @bucket_length ||= @rule[:period] / @rule[:buckets]
52
+ end
53
+
54
+ def key_for(user)
55
+ "strait:#{@name}-#{@rule[:count]}/#{@rule[:period]}:#{user}"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Strait
4
+ VERSION = '1.0.0'
5
+ end
data/lib/strait.rb ADDED
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'strait/configuration'
4
+ require 'strait/dsl'
5
+ require 'strait/rate_limit_exceeded'
6
+ require 'strait/rule'
7
+ require 'strait/version'
8
+
9
+ # A rate-limiter which provides natural defenses for your nation-state. Or your API.
10
+ class Strait
11
+ attr_reader :name, :config
12
+
13
+ def initialize(name, rules: [], **config)
14
+ @name = name
15
+ @raw_rules = rules
16
+ @config = Strait::Configuration.default.merge(config)
17
+
18
+ @raw_rules += Strait::DSL.new(&Proc.new).rules if block_given?
19
+ end
20
+
21
+ def limit!(user)
22
+ # Build a hash of { rule => acceptable } structure
23
+ results = rules.map { |rule| [rule, rule.call(user)] }.to_h
24
+
25
+ return if results.values.all?(true)
26
+
27
+ # Raise an exception for the first rate limit hit
28
+ results.each do |rule, acceptable|
29
+ raise Strait::RateLimitExceeded, rule.to_h unless acceptable
30
+ end
31
+ end
32
+
33
+ def self.configuration=(config)
34
+ Strait::Configuration.default = Strait::Configuration.new(config)
35
+ end
36
+
37
+ private
38
+
39
+ def rules
40
+ @rules ||= @raw_rules.map do |rule|
41
+ if rule.is_a?(Strait::Rule)
42
+ rule
43
+ else
44
+ Strait::Rule.new(config: @config, name: @name, rule: rule)
45
+ end
46
+ end
47
+ end
48
+ end
data/strait.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'strait/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'strait'
7
+ spec.version = Strait::VERSION
8
+ spec.authors = ['Emma Lejeck']
9
+ spec.email = ['nuck@kitsu.io']
10
+
11
+ spec.summary = 'Rate-limiting to defend your nation-state'
12
+ spec.description = <<~DESC
13
+ Strait is a rate-limiting library designed to provide security you don't need to think about.
14
+ Whenever you have code to protect, put a Strait in front of it.
15
+ DESC
16
+ spec.homepage = 'https://github.com/hummingbird-me/strait'
17
+ spec.license = 'Apache-2.0'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/hummingbird-me/strait'
21
+ # spec.metadata["changelog_uri"] = "https://github.com/hummingbird-me/strait/releases"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 2.0'
33
+ spec.add_development_dependency 'mock_redis', '~> 0.21'
34
+ spec.add_development_dependency 'rake', '~> 12.3'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'simplecov', '~> 0.17'
37
+ spec.add_development_dependency 'timecop', '~> 0.9'
38
+ spec.add_runtime_dependency 'connection_pool', '>= 2.0', '< 3.0'
39
+ spec.add_runtime_dependency 'redis', '>= 3.0', '~> 4.0'
40
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strait
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Emma Lejeck
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mock_redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.21'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.21'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: connection_pool
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '3.0'
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '2.0'
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: redis
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '3.0'
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: '4.0'
127
+ type: :runtime
128
+ prerelease: false
129
+ version_requirements: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '3.0'
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '4.0'
137
+ description: |
138
+ Strait is a rate-limiting library designed to provide security you don't need to think about.
139
+ Whenever you have code to protect, put a Strait in front of it.
140
+ email:
141
+ - nuck@kitsu.io
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".editorconfig"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - ".travis.yml"
151
+ - Gemfile
152
+ - Gemfile.lock
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - lib/strait.rb
159
+ - lib/strait/configuration.rb
160
+ - lib/strait/dsl.rb
161
+ - lib/strait/rate_limit_exceeded.rb
162
+ - lib/strait/rule.rb
163
+ - lib/strait/version.rb
164
+ - strait.gemspec
165
+ homepage: https://github.com/hummingbird-me/strait
166
+ licenses:
167
+ - Apache-2.0
168
+ metadata:
169
+ homepage_uri: https://github.com/hummingbird-me/strait
170
+ source_code_uri: https://github.com/hummingbird-me/strait
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubygems_version: 3.0.4
187
+ signing_key:
188
+ specification_version: 4
189
+ summary: Rate-limiting to defend your nation-state
190
+ test_files: []