by Lin Jen-Shin (godfat)
LINKS:
DESCRIPTION:
Extra Jellyfish extensions.
FEATURES:
- Jellyfish::MultiActions
- Jellyfish::NewRelic
- Jellyfish::Sinatra
- Jellyfish::Swagger
REQUIREMENTS:
- Tested with MRI (official CRuby) and JRuby.
INSTALLATION:
gem install jellyfish-contrib
SYNOPSIS:
You could also take a look at config.ru as an example, which also uses Swagger to generate API documentation.
Using multiple extensions with custom controller
This is effectively the same as using Jellyfish::Sinatra extension. Note that the controller should be assigned lastly in order to include modules remembered in controller_include.
require 'jellyfish-contrib'
class Tank
include Jellyfish
class MyController < Jellyfish::Controller
include Jellyfish::MultiActions
end
controller_include NormalizedParams, NormalizedPath
controller MyController
get do # wildcard before filter
@state = 'jumps'
end
get %r{^/(?<id>\d+)$} do
"Jelly ##{params[:id]} #{@state}.\n"
end
end
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
run Tank.new
Extension: MultiActions (Filters)
require 'jellyfish-contrib'
class Tank
include Jellyfish
controller_include Jellyfish::MultiActions
get do # wildcard before filter
@state = 'jumps'
end
get do
"Jelly #{@state}.\n"
end
end
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
run Tank.new
Halt in before action
require 'jellyfish-contrib'
class Tank
include Jellyfish
controller_include Jellyfish::MultiActions
get do # wildcard before filter
body "Done!\n"
halt
end
get '/' do
"Never reach.\n"
end
end
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
run Tank.new
Extension: NewRelic
require 'jellyfish'
class Tank
include Jellyfish
controller_include Jellyfish::NewRelic
get '/' do
"OK\n"
end
end
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
require 'cgi' # newrelic dev mode needs this and it won't require it itself
require 'new_relic/rack/developer_mode'
use NewRelic::Rack::DeveloperMode # GET /newrelic to read stats
run Tank.new
NewRelic::Agent.manual_start(:developer_mode => true)
Extension: Sinatra flavoured controller
It's an extension collection contains:
- MultiActions
- NormalizedParams
- NormalizedPath
require 'jellyfish-contrib'
class Tank
include Jellyfish
controller_include Jellyfish::Sinatra
get do # wildcard before filter
@state = 'jumps'
end
get %r{^/(?<id>\d+)$} do
"Jelly ##{params[:id]} #{@state}.\n"
end
end
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
run Tank.new
Extension: Swagger API documentation
For a complete example, checkout config.ru.
require 'jellyfish-contrib'
class Tank
include Jellyfish
get %r{^/(?<id>\d+)$}, :notes => 'This is an API note' do |match|
"Jelly ##{match[:id]}\n"
end
end
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
map '/swagger' do
run Jellyfish::Swagger.new('', Tank)
end
run Tank.new
CONTRIBUTORS:
- Fumin (@fumin)
- Lin Jen-Shin (@godfat)
LICENSE:
Apache License 2.0
Copyright (c) 2012-2019, Lin Jen-Shin (godfat)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.