Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RBS files for sinatra and sinatra-contrib gems #561

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions gems/sinatra-contrib/4.0/json.rbs
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
2 changes: 2 additions & 0 deletions gems/sinatra-contrib/_reviewers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reviewers:
- euglena1215
2 changes: 2 additions & 0 deletions gems/sinatra/4.0/_test/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
additional_gems:
- sinatra-contrib
45 changes: 45 additions & 0 deletions gems/sinatra/4.0/_test/test.rb
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
15 changes: 15 additions & 0 deletions gems/sinatra/4.0/_test/test.rbs
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
Loading
Loading