-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RBS files for sinatra and sinatra-contrib gems (#561)
Generated initial RBS files using the `rbs prototype rb` command, and added type definitions for both the Sinatra core and contributed extensions. https://github.com/sinatra/sinatra/tree/v4.0.0 I have set myself as the reviewer for this change because I believe the sinatra gem is an important and widely-used web framework. However, I am not very familiar with sinatra, so if there is someone who uses sinatra regularly and is motivated to maintain these type definitions, I would appreciate it if they could take over the reviewer role.
- Loading branch information
1 parent
438153d
commit 4c8290d
Showing
9 changed files
with
1,113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
module Sinatra | ||
# = Sinatra::JSON | ||
# | ||
# <tt>Sinatra::JSON</tt> adds a helper method, called +json+, for (obviously) | ||
# json generation. | ||
# | ||
# == Usage | ||
# | ||
# === Classic Application | ||
# | ||
# In a classic application simply require the helper, and start using it: | ||
# | ||
# require "sinatra" | ||
# require "sinatra/json" | ||
# | ||
# # define a route that uses the helper | ||
# get '/' do | ||
# json :foo => 'bar' | ||
# end | ||
# | ||
# # The rest of your classic application code goes here... | ||
# | ||
# === Modular Application | ||
# | ||
# In a modular application you need to require the helper, and then tell the | ||
# application you will use it: | ||
# | ||
# require "sinatra/base" | ||
# require "sinatra/json" | ||
# | ||
# class MyApp < Sinatra::Base | ||
# | ||
# # define a route that uses the helper | ||
# get '/' do | ||
# json :foo => 'bar' | ||
# end | ||
# | ||
# # The rest of your modular application code goes here... | ||
# end | ||
# | ||
# === Encoders | ||
# | ||
# By default it will try to call +to_json+ on the object, but if it doesn't | ||
# respond to that message, it will use its own rather simple encoder. You can | ||
# easily change that anyways. To use +JSON+, simply require it: | ||
# | ||
# require 'json' | ||
# | ||
# The same goes for <tt>Yajl::Encoder</tt>: | ||
# | ||
# require 'yajl' | ||
# | ||
# For other encoders, besides requiring them, you need to define the | ||
# <tt>:json_encoder</tt> setting. For instance, for the +Whatever+ encoder: | ||
# | ||
# require 'whatever' | ||
# set :json_encoder, Whatever | ||
# | ||
# To force +json+ to simply call +to_json+ on the object: | ||
# | ||
# set :json_encoder, :to_json | ||
# | ||
# Actually, it can call any method: | ||
# | ||
# set :json_encoder, :my_fancy_json_method | ||
# | ||
# === Content-Type | ||
# | ||
# It will automatically set the content type to "application/json". As | ||
# usual, you can easily change that, with the <tt>:json_content_type</tt> | ||
# setting: | ||
# | ||
# set :json_content_type, :js | ||
# | ||
# === Overriding the Encoder and the Content-Type | ||
# | ||
# The +json+ helper will also take two options <tt>:encoder</tt> and | ||
# <tt>:content_type</tt>. The values of this options are the same as the | ||
# <tt>:json_encoder</tt> and <tt>:json_content_type</tt> settings, | ||
# respectively. You can also pass those to the json method: | ||
# | ||
# get '/' do | ||
# json({:foo => 'bar'}, :encoder => :to_json, :content_type => :js) | ||
# end | ||
# | ||
module JSON | ||
def self.encode: (untyped object) -> untyped | ||
|
||
def json: (Hash[untyped, untyped] object, ?::Hash[untyped, untyped] options) -> untyped | ||
|
||
private | ||
|
||
def resolve_content_type: (?::Hash[untyped, untyped] options) -> untyped | ||
|
||
def resolve_encoder: (?::Hash[untyped, untyped] options) -> untyped | ||
|
||
def resolve_encoder_action: (untyped object, untyped encoder) -> untyped | ||
end | ||
|
||
class Base | ||
extend JSON | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
reviewers: | ||
- euglena1215 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
additional_gems: | ||
- sinatra-contrib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "sinatra/base" | ||
require "sinatra/json" | ||
|
||
class MyApp < Sinatra::Base | ||
enable :logging | ||
set :sessions, domain: 'example.dev', path: '/', expire_after: 1000*60 | ||
|
||
class HttpError < StandardError | ||
# @dynamic code | ||
attr_reader :code | ||
|
||
def initialize(code) | ||
super("HTTP error #{code}") | ||
@code = code | ||
end | ||
end | ||
|
||
error HttpError do | ||
status 400 | ||
json(error: 'HTTP error') | ||
end | ||
|
||
# @dynamic self.foo, self.bar | ||
helpers do | ||
def foo | ||
'foo' | ||
end | ||
|
||
def bar | ||
foo | ||
end | ||
end | ||
|
||
get '/' do | ||
json(foo: foo, bar: bar) | ||
end | ||
|
||
post '/' do | ||
json(foo: foo, bar: bar) | ||
end | ||
|
||
patch '/' do | ||
raise HttpError.new(401) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class MyApp < Sinatra::Base | ||
# Methods defined in the helpers block must be recognized as instance | ||
# methods for method calls from within other helper blocks, and as | ||
# singleton methods for calls from HTTP verbs (get, post, patch, etc.). | ||
def self.foo: () -> String | ||
def foo: () -> String | ||
def self.bar: () -> String | ||
def bar: () -> String | ||
|
||
class HttpError < StandardError | ||
attr_reader code: Integer | ||
|
||
def initialize: (Integer) -> void | ||
end | ||
end |
Oops, something went wrong.