Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Apr 7, 2011
0 parents commit 6b2ab8e
Show file tree
Hide file tree
Showing 37 changed files with 1,134 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
pkg
tmp
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color --format documentation
2 changes: 2 additions & 0 deletions Gemfile
@@ -0,0 +1,2 @@
source :rubygems
gemspec
49 changes: 49 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,49 @@
PATH
remote: .
specs:
rack-api (0.1.0)
activesupport (~> 3.0.6)
rack (~> 1.2.1)
rack-mount (~> 0.6.14)

GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.6)
archive-tar-minitar (0.5.2)
columnize (0.3.2)
diff-lcs (1.1.2)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
rack (1.2.2)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)

PLATFORMS
ruby

DEPENDENCIES
rack-api!
rack-test (~> 0.5.7)
rspec (~> 2.5.0)
ruby-debug19
54 changes: 54 additions & 0 deletions README.rdoc
@@ -0,0 +1,54 @@
= Rack::API

Create web app APIs that respond to one or more formats using an elegant DSL.

== Installation

gem install rack-api

== Usage

=== Basic example

Rack::API.app do
prefix "api"

version :v1 do
get "users(.:format)" do
User.all
end

get "users/:id(.:format)" do
User.find(params[:id])
end
end
end

For additional examples, see https://github.com/fnando/rack-api/tree/master/examples.

== Maintainer

* Nando Vieira (http://nandovieira.com.br)

== License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions Rakefile
@@ -0,0 +1,5 @@
require "bundler"
Bundler::GemHelper.install_tasks

require "rspec/core/rake_task"
RSpec::Core::RakeTask.new
22 changes: 22 additions & 0 deletions examples/basic_auth.rb
@@ -0,0 +1,22 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/basic_auth.rb` and then use something like
# `curl -u admin:test http://localhost:2345/api/v1/`.

require "rack/api"

Rack::API.app do
prefix "api"

basic_auth do |user, pass|
user == "admin" && pass == "test"
end

version :v1 do
get "/" do
{:message => "Hello, awesome API!"}
end
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
28 changes: 28 additions & 0 deletions examples/custom_class.rb
@@ -0,0 +1,28 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/custom_class.rb` and then use something like
# `curl http://localhost:2345/api/v1/` and `curl http://localhost:2345/api/v2/`.

require "rack/api"

class MyApp < Rack::API
prefix "api"

version :v1 do
get "/" do
{:message => "Using API v1"}
end
end
end

class MyApp < Rack::API
prefix "api"

version :v2 do
get "/" do
{:message => "Using API v2"}
end
end
end

Rack::Handler::Thin.run MyApp, :Port => 2345
19 changes: 19 additions & 0 deletions examples/custom_headers.rb
@@ -0,0 +1,19 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/custom_headers.rb` and then use something like
# `curl -i http://localhost:2345/api/v1/`.

require "rack/api"

Rack::API.app do
prefix "api"

version :v1 do
get "/" do
headers["X-Awesome"] = "U R Awesome!"
{:message => "Hello, awesome API!"}
end
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
34 changes: 34 additions & 0 deletions examples/middleware.rb
@@ -0,0 +1,34 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/middleware.rb` and then use something like
# `curl http://localhost:2345/api/v1/`.

require "rack/api"
require "json"

class ResponseTime
def initialize(app)
@app = app
end

def call(env)
start = Time.now
status, headers, response = @app.call(env)
elapsed = Time.now - start
response = JSON.load(response.first).merge(:response_time => elapsed)
[status, headers, [response.to_json]]
end
end

Rack::API.app do
prefix "api"
use ResponseTime

version :v1 do
get "/" do
{:message => "Hello, awesome API!"}
end
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
24 changes: 24 additions & 0 deletions examples/multiple_versions.rb
@@ -0,0 +1,24 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/multiple_versions.rb` and then use something like
# `curl http://localhost:2345/api/v1/` and `curl http://localhost:2345/api/v2`.

require "rack/api"

Rack::API.app do
prefix "api"

version :v1 do
get "/" do
{:message => "You're using API v1"}
end
end

version :v2 do
get "/" do
{:message => "You're using API v2"}
end
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
18 changes: 18 additions & 0 deletions examples/params.rb
@@ -0,0 +1,18 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/params.rb` and then use something like
# `curl http://localhost:2345/api/v1/hello/John`.

require "rack/api"

Rack::API.app do
prefix "api"

version :v1 do
get "/hello/:name" do
{:message => "Hello, #{params[:name]}"}
end
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
18 changes: 18 additions & 0 deletions examples/simple.rb
@@ -0,0 +1,18 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/simple.rb` and then use something like
# `curl http://localhost:2345/api/v1/`.

require "rack/api"

Rack::API.app do
prefix "api"

version :v1 do
get "/" do
{:message => "Hello, awesome API!"}
end
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
83 changes: 83 additions & 0 deletions lib/rack/api.rb
@@ -0,0 +1,83 @@
require "rack"
require "rack/mount"
require "active_support/hash_with_indifferent_access"
require "json"
require "logger"

module Rack
class API
autoload :App, "rack/api/app"
autoload :Formatter, "rack/api/formatter"
autoload :Runner, "rack/api/runner"
autoload :Response, "rack/api/response"
autoload :Version, "rack/api/version"

# A shortcut for defining new APIs. Instead of creating a
# class that inherits from Rack::API, you can simply pass a
# block to the Rack::API.app method.
#
# Rack::API.app do
# # define your API
# end
#
def self.app(&block)
runner.instance_eval(&block)
runner
end

# Add a middleware to the stack execution.
#
# Rack::API.app do
# use MyMiddleware
# end
#
def self.use(m)
runner.use(m)
end

# Create a new API version.
#
# Rack::API.app do
# version "v1" do
# # define your API
# end
# end
#
def self.version(name, &block)
runner.version(name, &block)
end

# Set an additional url prefix.
#
# Rack::API.app do
# prefix "api"
# version("v1") {}
# end
#
# This API will be available through <tt>/api/v1</tt> path.
#
def self.prefix(name)
runner.prefix(name)
end

# Reset all API definitions while using the Rack::API.app method.
#
def self.reset!
@runner = nil
end

# Required by Rack.
#
def self.call(env) # :nodoc:
runner.call(env)
end

private
# Initialize a new Rack::API::Middleware instance, so
# we can use it on other class methods.
#
def self.runner
@runner ||= Runner.new
end
end
end

0 comments on commit 6b2ab8e

Please sign in to comment.