grape_sorbet 0.0.1

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: 68395f140413d15f9db05463e554cdf86ec2e60915226877fd738cf1b7d17183
4
+ data.tar.gz: 8c4a99e44696bd706c216c1629c18c48a359902710100542061f65fd01fae256
5
+ SHA512:
6
+ metadata.gz: db6b2f00da52ea9382f4cc6fc05c38b569614e95f7f81061d684d884716ea761a2b8d23a090606dccab3c5e54faa715fcc5e4b59d598b7e66b279ec2cfae0dce
7
+ data.tar.gz: ef8ffe995841f9cb5ca094e36ea7ef82d0ccc05626a3a68e8a252469ff1197460cfa5556e58d93c361964f2a4617a846b3e873d3d228a58da276fbdf0eae3fc9
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # EditorConfig is awesome: https://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ [*]
7
+ indent_style = space
8
+ indent_size = 2
9
+ end_of_line = lf
10
+ charset = utf-8
11
+ trim_trailing_whitespace = true
12
+ insert_final_newline = true
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ # Opinionated cops - see https://ruby-style-guide.shopify.dev/ for explanations
2
+ inherit_gem:
3
+ rubocop-shopify: rubocop.yml
4
+
5
+ require:
6
+ - rubocop-minitest
7
+ - rubocop-performance
8
+ - rubocop-rake
9
+ - rubocop-sorbet
10
+
11
+ AllCops:
12
+ NewCops: enable
13
+ SuggestExtensions: false
14
+ TargetRubyVersion: 3.0
15
+
16
+ Naming/ConstantName:
17
+ Enabled: false
18
+
19
+ Sorbet/StrictSigil:
20
+ Enabled: true
21
+
22
+ Style/MutableConstant:
23
+ Enabled: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.2
@@ -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 14116796+olivierbellone@users.noreply.github.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,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in grape_sorbet.gemspec
8
+ gemspec
9
+
10
+ gem "rake", "~> 13.0"
11
+
12
+ group :development, :test do
13
+ gem "irb"
14
+ end
15
+
16
+ group :development do
17
+ gem "sorbet", "~> 0.5.10736"
18
+ gem "tapioca", require: false
19
+
20
+ gem "rubocop", require: false
21
+ gem "rubocop-minitest", require: false
22
+ gem "rubocop-performance", require: false
23
+ gem "rubocop-rake", require: false
24
+ gem "rubocop-shopify", require: false
25
+ gem "rubocop-sorbet", require: false
26
+
27
+ gem "bundler-audit", require: false
28
+ end
29
+
30
+ group :test do
31
+ gem "minitest", "~> 5.0"
32
+ gem "minitest-reporters", "~> 1.4"
33
+
34
+ gem "simplecov", require: false
35
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,191 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ grape_sorbet (0.0.1)
5
+ activesupport
6
+ grape (~> 2.0)
7
+ sorbet-runtime (~> 0.5.10741)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.1.3.3)
13
+ base64
14
+ bigdecimal
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ connection_pool (>= 2.2.5)
17
+ drb
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ mutex_m
21
+ tzinfo (~> 2.0)
22
+ ansi (1.5.0)
23
+ ast (2.4.2)
24
+ base64 (0.2.0)
25
+ bigdecimal (3.1.8)
26
+ builder (3.2.4)
27
+ bundler-audit (0.9.1)
28
+ bundler (>= 1.2.0, < 3)
29
+ thor (~> 1.0)
30
+ concurrent-ruby (1.3.1)
31
+ connection_pool (2.4.1)
32
+ docile (1.4.0)
33
+ drb (2.2.1)
34
+ dry-core (1.0.1)
35
+ concurrent-ruby (~> 1.0)
36
+ zeitwerk (~> 2.6)
37
+ dry-inflector (1.0.0)
38
+ dry-logic (1.5.0)
39
+ concurrent-ruby (~> 1.0)
40
+ dry-core (~> 1.0, < 2)
41
+ zeitwerk (~> 2.6)
42
+ dry-types (1.7.2)
43
+ bigdecimal (~> 3.0)
44
+ concurrent-ruby (~> 1.0)
45
+ dry-core (~> 1.0)
46
+ dry-inflector (~> 1.0)
47
+ dry-logic (~> 1.4)
48
+ zeitwerk (~> 2.6)
49
+ erubi (1.12.0)
50
+ grape (2.0.0)
51
+ activesupport (>= 5)
52
+ builder
53
+ dry-types (>= 1.1)
54
+ mustermann-grape (~> 1.0.0)
55
+ rack (>= 1.3.0)
56
+ rack-accept
57
+ i18n (1.14.5)
58
+ concurrent-ruby (~> 1.0)
59
+ io-console (0.7.2)
60
+ irb (1.13.1)
61
+ rdoc (>= 4.0.0)
62
+ reline (>= 0.4.2)
63
+ json (2.7.2)
64
+ language_server-protocol (3.17.0.3)
65
+ minitest (5.23.1)
66
+ minitest-reporters (1.6.1)
67
+ ansi
68
+ builder
69
+ minitest (>= 5.0)
70
+ ruby-progressbar
71
+ mustermann (3.0.0)
72
+ ruby2_keywords (~> 0.0.1)
73
+ mustermann-grape (1.0.2)
74
+ mustermann (>= 1.0.0)
75
+ mutex_m (0.2.0)
76
+ netrc (0.11.0)
77
+ parallel (1.24.0)
78
+ parser (3.3.2.0)
79
+ ast (~> 2.4.1)
80
+ racc
81
+ prism (0.29.0)
82
+ psych (5.1.2)
83
+ stringio
84
+ racc (1.8.0)
85
+ rack (3.0.11)
86
+ rack-accept (0.4.5)
87
+ rack (>= 0.4)
88
+ rainbow (3.1.1)
89
+ rake (13.2.1)
90
+ rbi (0.1.13)
91
+ prism (>= 0.18.0, < 1.0.0)
92
+ sorbet-runtime (>= 0.5.9204)
93
+ rdoc (6.7.0)
94
+ psych (>= 4.0.0)
95
+ regexp_parser (2.9.2)
96
+ reline (0.5.8)
97
+ io-console (~> 0.5)
98
+ rexml (3.2.8)
99
+ strscan (>= 3.0.9)
100
+ rubocop (1.64.1)
101
+ json (~> 2.3)
102
+ language_server-protocol (>= 3.17.0)
103
+ parallel (~> 1.10)
104
+ parser (>= 3.3.0.2)
105
+ rainbow (>= 2.2.2, < 4.0)
106
+ regexp_parser (>= 1.8, < 3.0)
107
+ rexml (>= 3.2.5, < 4.0)
108
+ rubocop-ast (>= 1.31.1, < 2.0)
109
+ ruby-progressbar (~> 1.7)
110
+ unicode-display_width (>= 2.4.0, < 3.0)
111
+ rubocop-ast (1.31.3)
112
+ parser (>= 3.3.1.0)
113
+ rubocop-minitest (0.35.0)
114
+ rubocop (>= 1.61, < 2.0)
115
+ rubocop-ast (>= 1.31.1, < 2.0)
116
+ rubocop-performance (1.21.0)
117
+ rubocop (>= 1.48.1, < 2.0)
118
+ rubocop-ast (>= 1.31.1, < 2.0)
119
+ rubocop-rake (0.6.0)
120
+ rubocop (~> 1.0)
121
+ rubocop-shopify (2.15.1)
122
+ rubocop (~> 1.51)
123
+ rubocop-sorbet (0.8.3)
124
+ rubocop (>= 0.90.0)
125
+ ruby-progressbar (1.13.0)
126
+ ruby2_keywords (0.0.5)
127
+ simplecov (0.22.0)
128
+ docile (~> 1.1)
129
+ simplecov-html (~> 0.11)
130
+ simplecov_json_formatter (~> 0.1)
131
+ simplecov-html (0.12.3)
132
+ simplecov_json_formatter (0.1.4)
133
+ sorbet (0.5.11406)
134
+ sorbet-static (= 0.5.11406)
135
+ sorbet-runtime (0.5.11406)
136
+ sorbet-static (0.5.11406-aarch64-linux)
137
+ sorbet-static (0.5.11406-universal-darwin)
138
+ sorbet-static (0.5.11406-x86_64-linux)
139
+ sorbet-static-and-runtime (0.5.11406)
140
+ sorbet (= 0.5.11406)
141
+ sorbet-runtime (= 0.5.11406)
142
+ spoom (1.3.2)
143
+ erubi (>= 1.10.0)
144
+ prism (>= 0.19.0)
145
+ sorbet-static-and-runtime (>= 0.5.10187)
146
+ thor (>= 0.19.2)
147
+ stringio (3.1.0)
148
+ strscan (3.1.0)
149
+ tapioca (0.14.3)
150
+ bundler (>= 2.2.25)
151
+ netrc (>= 0.11.0)
152
+ parallel (>= 1.21.0)
153
+ rbi (>= 0.1.4, < 0.2)
154
+ sorbet-static-and-runtime (>= 0.5.11087)
155
+ spoom (>= 1.2.0)
156
+ thor (>= 1.2.0)
157
+ yard-sorbet
158
+ thor (1.3.1)
159
+ tzinfo (2.0.6)
160
+ concurrent-ruby (~> 1.0)
161
+ unicode-display_width (2.5.0)
162
+ yard (0.9.36)
163
+ yard-sorbet (0.8.1)
164
+ sorbet-runtime (>= 0.5)
165
+ yard (>= 0.9)
166
+ zeitwerk (2.6.15)
167
+
168
+ PLATFORMS
169
+ aarch64-linux
170
+ universal-darwin
171
+ x86_64-linux
172
+
173
+ DEPENDENCIES
174
+ bundler-audit
175
+ grape_sorbet!
176
+ irb
177
+ minitest (~> 5.0)
178
+ minitest-reporters (~> 1.4)
179
+ rake (~> 13.0)
180
+ rubocop
181
+ rubocop-minitest
182
+ rubocop-performance
183
+ rubocop-rake
184
+ rubocop-shopify
185
+ rubocop-sorbet
186
+ simplecov
187
+ sorbet (~> 0.5.10736)
188
+ tapioca
189
+
190
+ BUNDLED WITH
191
+ 2.5.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Thatch Health, Inc.
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,111 @@
1
+ # grape_sorbet
2
+
3
+ [![Build Status](https://github.com/thatch-health/grape_sorbet/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/thatch-health/grape_sorbet/actions?query=branch%3Amain)
4
+
5
+ grape_sorbet is a gem that provides hand written signatures and a Tapioca DSL compiler that makes it more pleasant to use the [Grape](https://github.com/ruby-grape/grape) API framework in Ruby projects that use the [Sorbet](https://sorbet.org/) typechecker.
6
+
7
+
8
+ ## Installation
9
+
10
+ Install the gem and add to the application's Gemfile by executing:
11
+
12
+ $ bundle add r --github=https://github.com/thatch-health/grape_sorbet
13
+
14
+ If bundler is not being used to manage dependencies, install the gem by executing:
15
+
16
+ $ gem install specific_install
17
+ $ gem specific_install https://github.com/thatch-health/grape_sorbet.git
18
+
19
+ After installing the gem, make sure to run `bundle exec tapioca gem grape_sorbet` in your project to import the hand written signatures.
20
+
21
+ ## Usage
22
+
23
+ Starting with the following example from grape's documentation:
24
+
25
+ ```ruby
26
+ module Twitter
27
+ class API < Grape::API
28
+ version 'v1', using: :header, vendor: 'twitter'
29
+ format :json
30
+ prefix :api
31
+
32
+ helpers do
33
+ def current_user
34
+ @current_user ||= User.authorize!(env)
35
+ end
36
+
37
+ def authenticate!
38
+ error!('401 Unauthorized', 401) unless current_user
39
+ end
40
+ end
41
+
42
+ resource :statuses do
43
+ desc 'Return a personal timeline.'
44
+ get :home_timeline do
45
+ authenticate!
46
+ current_user.statuses.limit(20)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ First, you'll need to make the following changes:
54
+
55
+ * derive from `Grape::API::Instance` rather than `Grape::API`
56
+ * change the `helpers` call to replace the block parameter with a named module
57
+
58
+ You'll also need to use `T.bind(self, T.all(Grape::Endpoint, <HelpersModuleName>))` in helper methods that use methods provided by Grape (e.g. `env` in `current_user` or `error!` in `authenticate!`) or other methods from the same helper module (e.g. `current_user` in `authenticate!`).
59
+
60
+ With the changes:
61
+
62
+ ```ruby
63
+ module Twitter
64
+ class API < Grape::API::Instance
65
+ version 'v1', using: :header, vendor: 'twitter'
66
+ format :json
67
+ prefix :api
68
+
69
+ module Helpers
70
+ def current_user
71
+ T.bind(self, T.all(Grape::Endpoint, Helpers))
72
+ @current_user ||= User.authorize!(env)
73
+ end
74
+
75
+ def authenticate!
76
+ T.bind(self, T.all(Grape::Endpoint, Helpers))
77
+ error!('401 Unauthorized', 401) unless current_user
78
+ end
79
+ end
80
+ helpers Helpers
81
+
82
+ resource :statuses do
83
+ desc 'Return a personal timeline.'
84
+ get :home_timeline do
85
+ authenticate!
86
+ current_user.statuses.limit(20)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ ```
92
+
93
+ At this point Sorbet is still reporting errors in the `get :home_timeline` block, because it doesn't know that these blocks are executed in a context where the `Helpers` module methods will be available. The Tapioca compiler provided by this gem will fix that. Run `bundle exec tapioca dsl`. This should generate a `sorbet/rbi/dsl/twitter/api.rbi` file (assuming the source file is in `lib/twitter/api.rb`). After this,
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ 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).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thatch-health/grape_sorbet. 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/thatch-health/grape_sorbet/main/CODE_OF_CONDUCT.md).
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the TestGem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/thatch-health/grape_sorbet/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "rubocop/rake_task"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "spec"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["spec/**/*_spec.rb"]
11
+ end
12
+ task default: :test
13
+
14
+ RuboCop::RakeTask.new do |t|
15
+ t.options = ["--parallel"]
16
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/grape_sorbet/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "grape_sorbet"
7
+ spec.version = GrapeSorbet::VERSION
8
+ spec.authors = ["Thatch Health, Inc."]
9
+ spec.email = ["grape-sorbet@thatch.ai"]
10
+
11
+ spec.summary = "Sorbet signatures and Tapioca DSL compiler for Grape."
12
+ spec.description = "This gem provides Sorbet signatures and a Tapioca DSL compiler that enable using " \
13
+ "the Grape API framework in a Sorbet-typed Ruby project."
14
+ spec.homepage = "https://github.com/thatch-health/grape_sorbet"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 3.1.0"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "https://github.com/thatch-health/grape_sorbet/blob/main/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ %x(git ls-files -z).split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features|sorbet)/|\.(?:git|circleci|vscode)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ spec.add_runtime_dependency("activesupport")
35
+ spec.add_runtime_dependency("grape", "~> 2.0")
36
+ spec.add_runtime_dependency("sorbet-runtime", "~> 0.5.10741")
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
@@ -0,0 +1,6 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module GrapeSorbet
5
+ VERSION = "0.0.1"
6
+ end
@@ -0,0 +1,7 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module GrapeSorbet
5
+ end
6
+
7
+ require_relative "grape_sorbet/version"
@@ -0,0 +1,164 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ begin
5
+ require "grape"
6
+ rescue LoadError
7
+ return
8
+ end
9
+
10
+ require "active_support/core_ext/class/subclasses"
11
+
12
+ require "tapioca/dsl/helpers/grape_constants_helper"
13
+
14
+ module Tapioca
15
+ module Dsl
16
+ module Compilers
17
+ # `Tapioca::Compilers::GrapeEndpoints` decorates RBI files for subclasses of `Grape::API::Instance`.
18
+ #
19
+ # For example, with the following `Grape::API::Instance` subclass:
20
+ #
21
+ # ~~~rb
22
+ # class API < Grape::API::Instance
23
+ # helpers Helpers
24
+ # end
25
+ # ~~~
26
+ #
27
+ # this compiler will produce the RBI file `api.rbi` with the following content:
28
+ # ~~~rbi
29
+ # # api.rbi
30
+ # # typed: true
31
+ #
32
+ # class API
33
+ # extend GeneratedRoutingMethods
34
+ #
35
+ # module GeneratedRoutingMethods
36
+ # sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
37
+ # def get(*args, &blk); end
38
+ #
39
+ # # ...
40
+ #
41
+ # sig do
42
+ # params(
43
+ # param: Symbol,
44
+ # options: T.nilable(T::Hash[Symbol, T.untyped]),
45
+ # blk: T.nilable(T.proc.bind(T.class_of(PrivateAPIInstance)).void)
46
+ # ).void
47
+ # end
48
+ # def route_param(param, options = nil, &blk); end
49
+ # end
50
+ #
51
+ # class PrivateAPIInstance < ::Grape::API::Instance
52
+ # extend GeneratedRoutingMethods
53
+ # end
54
+ #
55
+ # class PrivateEndpoint < ::Grape::Endpoint
56
+ # include Helpers
57
+ # end
58
+ # end
59
+ # ~~~
60
+ #
61
+ class GrapeEndpoints < Tapioca::Dsl::Compiler
62
+ extend T::Sig
63
+ include Helpers::GrapeConstantsHelper
64
+
65
+ ConstantType = type_member { { fixed: T.class_of(::Grape::API::Instance) } }
66
+
67
+ sig { override.void }
68
+ def decorate
69
+ create_classes_and_includes
70
+ create_routing_methods
71
+ end
72
+
73
+ class << self
74
+ extend T::Sig
75
+
76
+ sig { override.returns(T::Enumerable[Module]) }
77
+ def gather_constants
78
+ ::Grape::API::Instance.descendants
79
+ end
80
+ end
81
+
82
+ HTTP_VERB_METHODS = T.let(
83
+ [:get, :post, :put, :patch, :delete, :head, :options].freeze,
84
+ T::Array[Symbol],
85
+ )
86
+
87
+ private
88
+
89
+ sig { returns(RBI::Scope) }
90
+ def api
91
+ @api ||= T.let(
92
+ root.create_path(constant),
93
+ T.nilable(RBI::Scope),
94
+ )
95
+ end
96
+
97
+ sig { returns(RBI::Scope) }
98
+ def routing_methods_module
99
+ @routing_methods_module ||= T.let(
100
+ api.create_module(RoutingMethodsModuleName),
101
+ T.nilable(RBI::Scope),
102
+ )
103
+ end
104
+
105
+ sig { void }
106
+ def create_classes_and_includes
107
+ api.create_extend(RoutingMethodsModuleName)
108
+ create_api_class
109
+ create_endpoint_class
110
+ end
111
+
112
+ sig { void }
113
+ def create_api_class
114
+ superclass = "::Grape::API::Instance"
115
+
116
+ api.create_class(APIInstanceClassName, superclass_name: superclass) do |klass|
117
+ klass.create_extend(RoutingMethodsModuleName)
118
+ end
119
+ end
120
+
121
+ sig { void }
122
+ def create_endpoint_class
123
+ superclass = "::Grape::Endpoint"
124
+
125
+ helper_mods = constant.namespace_stackable(:helpers)
126
+
127
+ if helper_mods.any? { |mod| mod.name.nil? }
128
+ raise "Cannot compile Grape API with anonymous helpers"
129
+ end
130
+
131
+ api.create_class(EndpointClassName, superclass_name: superclass) do |klass|
132
+ helper_mods.each do |mod|
133
+ klass.create_include(mod.name)
134
+ end
135
+ end
136
+ end
137
+
138
+ sig { void }
139
+ def create_routing_methods
140
+ HTTP_VERB_METHODS.each do |verb|
141
+ routing_methods_module.create_method(
142
+ verb.to_s,
143
+ parameters: [
144
+ create_rest_param("args", type: "T.untyped"),
145
+ create_block_param("blk", type: "T.nilable(T.proc.bind(#{EndpointClassName}).void)"),
146
+ ],
147
+ return_type: "void",
148
+ )
149
+ end
150
+
151
+ routing_methods_module.create_method(
152
+ "route_param",
153
+ parameters: [
154
+ create_param("param", type: "Symbol"),
155
+ create_opt_param("options", type: "T.nilable(T::Hash[Symbol, T.untyped])", default: "nil"),
156
+ create_block_param("blk", type: "T.nilable(T.proc.bind(T.class_of(#{APIInstanceClassName})).void)"),
157
+ ],
158
+ return_type: "void",
159
+ )
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,17 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Tapioca
5
+ module Dsl
6
+ module Helpers
7
+ module GrapeConstantsHelper
8
+ extend T::Sig
9
+
10
+ RoutingMethodsModuleName = "GeneratedRoutingMethods"
11
+
12
+ APIInstanceClassName = "PrivateAPIInstance"
13
+ EndpointClassName = "PrivateEndpoint"
14
+ end
15
+ end
16
+ end
17
+ end
data/rbi/grape.rbi ADDED
@@ -0,0 +1,108 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Grape
5
+ module DSL::Callbacks::ClassMethods
6
+ sig { params(block: T.proc.bind(Grape::Endpoint).void).void }
7
+ def before(&block); end
8
+ end
9
+
10
+ module DSL::Desc
11
+ # grape evaluates config_block in the context of a dynamically created module that implements the DSL it exposes
12
+ # at runtime. There's no good way to represent this statically, so block is just typed as T.untyped to prevent
13
+ # Sorbet from complaining that the DSL methods don't exist.
14
+ sig do
15
+ params(
16
+ description: String,
17
+ options: T.nilable(T::Hash[Symbol, T.untyped]),
18
+ config_block: T.nilable(T.proc.bind(T.untyped).void),
19
+ ).void
20
+ end
21
+ def desc(description, options = T.unsafe(nil), &config_block); end
22
+ end
23
+
24
+ module DSL::RequestResponse::ClassMethods
25
+ sig { params(args: T.untyped, block: T.proc.bind(Grape::Endpoint).void).void }
26
+ def rescue_from(*args, &block); end
27
+ end
28
+
29
+ module DSL::Routing::ClassMethods
30
+ # @shim: https://github.com/ruby-grape/grape/blob/v2.0.0/lib/grape/dsl/routing.rb#L148-L154
31
+ sig do
32
+ params(
33
+ args: T.untyped,
34
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
35
+ ).void
36
+ end
37
+ def delete(*args, &block); end
38
+
39
+ # @shim: https://github.com/ruby-grape/grape/blob/v2.0.0/lib/grape/dsl/routing.rb#L148-L154
40
+ sig do
41
+ params(
42
+ args: T.untyped,
43
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
44
+ ).void
45
+ end
46
+ def get(*args, &block); end
47
+
48
+ # @shim: https://github.com/ruby-grape/grape/blob/v2.0.0/lib/grape/dsl/routing.rb#L148-L154
49
+ sig do
50
+ params(
51
+ args: T.untyped,
52
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
53
+ ).void
54
+ end
55
+ def options(*args, &block); end
56
+
57
+ # @shim: https://github.com/ruby-grape/grape/blob/v2.0.0/lib/grape/dsl/routing.rb#L148-L154
58
+ sig do
59
+ params(
60
+ args: T.untyped,
61
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
62
+ ).void
63
+ end
64
+ def patch(*args, &block); end
65
+
66
+ # @shim: https://github.com/ruby-grape/grape/blob/v2.0.0/lib/grape/dsl/routing.rb#L148-L154
67
+ sig do
68
+ params(
69
+ args: T.untyped,
70
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
71
+ ).void
72
+ end
73
+ def post(*args, &block); end
74
+
75
+ # @shim: https://github.com/ruby-grape/grape/blob/v2.0.0/lib/grape/dsl/routing.rb#L148-L154
76
+ sig do
77
+ params(
78
+ args: T.untyped,
79
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
80
+ ).void
81
+ end
82
+ def put(*args, &block); end
83
+
84
+ sig do
85
+ params(
86
+ methods: T.any(Symbol, String, T::Array[String]),
87
+ paths: T.nilable(T.any(String, T::Array[String])),
88
+ route_options: T.nilable(T::Hash[Symbol, T.untyped]),
89
+ block: T.nilable(T.proc.bind(Grape::Endpoint).void),
90
+ ).void
91
+ end
92
+ def route(methods, paths = T.unsafe(nil), route_options = T.unsafe(nil), &block); end
93
+
94
+ sig do
95
+ params(
96
+ param: Symbol,
97
+ options: T.nilable(T::Hash[Symbol, T.untyped]),
98
+ block: T.nilable(T.proc.bind(T.class_of(Grape::API::Instance)).void),
99
+ ).void
100
+ end
101
+ def route_param(param, options = T.unsafe(nil), &block); end
102
+ end
103
+
104
+ module DSL::Validations::ClassMethods
105
+ sig { params(block: T.proc.bind(Grape::Validations::ParamsScope).void).void }
106
+ def params(&block); end
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape_sorbet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thatch Health, Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: grape
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sorbet-runtime
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.10741
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.10741
55
+ description: This gem provides Sorbet signatures and a Tapioca DSL compiler that enable
56
+ using the Grape API framework in a Sorbet-typed Ruby project.
57
+ email:
58
+ - grape-sorbet@thatch.ai
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".editorconfig"
64
+ - ".rubocop.yml"
65
+ - ".ruby-version"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - grape_sorbet.gemspec
73
+ - lib/grape_sorbet.rb
74
+ - lib/grape_sorbet/version.rb
75
+ - lib/tapioca/dsl/compilers/grape_endpoints.rb
76
+ - lib/tapioca/dsl/helpers/grape_constants_helper.rb
77
+ - rbi/grape.rbi
78
+ homepage: https://github.com/thatch-health/grape_sorbet
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ homepage_uri: https://github.com/thatch-health/grape_sorbet
83
+ source_code_uri: https://github.com/thatch-health/grape_sorbet
84
+ changelog_uri: https://github.com/thatch-health/grape_sorbet/blob/main/CHANGELOG.md
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 3.1.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.5.9
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Sorbet signatures and Tapioca DSL compiler for Grape.
104
+ test_files: []