biceps 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +89 -0
- data/Rakefile +24 -0
- data/lib/biceps.rb +5 -0
- data/lib/biceps/api_version.rb +36 -0
- data/lib/biceps/routing.rb +17 -0
- data/lib/biceps/version.rb +3 -0
- metadata +52 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Evome
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Biceps
|
2
|
+
|
3
|
+
Easily route your rails-based versionned API
|
4
|
+
[![Travis](https://secure.travis-ci.org/evome/biceps.png)](http://travis-ci.org/evome/biceps)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Biceps heavily uses the convention over configuration principle.
|
9
|
+
To install it, you just need to add it to your Gemfile.
|
10
|
+
|
11
|
+
gem 'biceps'
|
12
|
+
|
13
|
+
## Defining routes
|
14
|
+
|
15
|
+
Once Biceps is installed, you can start adding api-versionned routes.
|
16
|
+
Your `config/routes.rb` file could look like the following :
|
17
|
+
|
18
|
+
MyApp:Application.routes.draw do
|
19
|
+
root :to => "home#index"
|
20
|
+
|
21
|
+
api_version(1) do
|
22
|
+
get '/me' => "users#show"
|
23
|
+
end
|
24
|
+
|
25
|
+
api_version(2) do
|
26
|
+
get '/user' => "users#show"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
This will create two routes :
|
31
|
+
|
32
|
+
GET /me(.:format) {:controller=>"v1/users", :action=>"show"}
|
33
|
+
GET /user(.:format) {:controller=>"v2/users", :action=>"show"}
|
34
|
+
|
35
|
+
As you can see in the routing, both are leading to different namespaces
|
36
|
+
: v1 and v2.
|
37
|
+
Both namespaces are the version of your API.
|
38
|
+
|
39
|
+
## Calling the API
|
40
|
+
|
41
|
+
When you want to call the API, you need to specify the Accept header
|
42
|
+
like this :
|
43
|
+
|
44
|
+
application/json,application/vnd.my_app;ver=1
|
45
|
+
|
46
|
+
When my_app is your application's name (based on the module name at
|
47
|
+
`MyApp::Application`).
|
48
|
+
|
49
|
+
Here is, for example, how you could do it with [faraday](https://github.com/technoweenie/faraday)
|
50
|
+
|
51
|
+
connexion = Faraday.new(:url => 'http://api.yourapplication')
|
52
|
+
connexion.get do |req|
|
53
|
+
req.url '/me'
|
54
|
+
req.headers['HTTP_ACCEPT'] = 'application/json, application/vnd.my_app;ver=1'
|
55
|
+
req.params['access_token'] = 'xxx'
|
56
|
+
end
|
57
|
+
|
58
|
+
Or, with jQuery, we do it like this :
|
59
|
+
|
60
|
+
$.ajaxSetup({
|
61
|
+
accepts: {
|
62
|
+
my_app: "application/json,application/vnd.my_app;ver=1"
|
63
|
+
}
|
64
|
+
});
|
65
|
+
|
66
|
+
$.ajax({
|
67
|
+
url: '/me'
|
68
|
+
dataType: 'my_app'
|
69
|
+
}).always(function(response) {
|
70
|
+
json = JSON.parse(response.responseText)
|
71
|
+
});
|
72
|
+
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
We're open to any contribution. It has to be tested properly though.
|
77
|
+
|
78
|
+
* [Fork](http://help.github.com/forking/) the project
|
79
|
+
* Do your changes and commit them to your repository
|
80
|
+
* Test your changes. We won't accept any untested contributions (except if they're not testable).
|
81
|
+
* Create an [issue](https://github.com/evome/biceps/issues) with a link to your commits.
|
82
|
+
|
83
|
+
## Maintainers
|
84
|
+
|
85
|
+
* Evome ([evome.fr](http://evome.fr))
|
86
|
+
* Damien MATHIEU ([github/dmathieu](http://github.com/dmathieu), [dmathieu.com](http://dmathieu.com))
|
87
|
+
|
88
|
+
## License
|
89
|
+
MIT License. Copyright 2010 Evome. http://evome.fr
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rake'
|
10
|
+
require 'rake/rdoctask'
|
11
|
+
|
12
|
+
|
13
|
+
require 'rspec/core'
|
14
|
+
require 'rspec/core/rake_task'
|
15
|
+
task :default => :spec
|
16
|
+
RSpec::Core::RakeTask.new(:spec)
|
17
|
+
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Oauned'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
data/lib/biceps.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Biceps
|
2
|
+
class ApiVersion
|
3
|
+
attr_accessor :version, :accept
|
4
|
+
|
5
|
+
def initialize(version)
|
6
|
+
@version = version
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(request)
|
10
|
+
@accept = request.accept
|
11
|
+
valid_api_version?
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
private
|
16
|
+
def valid_api_version?
|
17
|
+
request_version == version
|
18
|
+
end
|
19
|
+
|
20
|
+
def request_version
|
21
|
+
is_api_call?[1].to_i if is_api_call?
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_api_call?
|
25
|
+
@is_api_call ||= accept.match(regex)
|
26
|
+
end
|
27
|
+
|
28
|
+
def regex
|
29
|
+
Regexp.new("application/vnd.#{app_name};ver=([0-9]+)")
|
30
|
+
end
|
31
|
+
|
32
|
+
def app_name
|
33
|
+
Rails.application.class.to_s.split('::').first.underscore
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#
|
2
|
+
# Monkey Patching routes to add the `api_version` method
|
3
|
+
# This allows us to define both the namespace and the constraint all together
|
4
|
+
#
|
5
|
+
module ActionDispatch::Routing
|
6
|
+
class Mapper
|
7
|
+
|
8
|
+
def api_version(version)
|
9
|
+
|
10
|
+
scope :module => "v#{version}" do
|
11
|
+
constraints Biceps::ApiVersion.new(version) do
|
12
|
+
yield if block_given?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: biceps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Evome
|
9
|
+
- Damien MATHIEU
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-09-30 00:00:00.000000000Z
|
14
|
+
dependencies: []
|
15
|
+
description: Create a versionned API with rails
|
16
|
+
email:
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/biceps/api_version.rb
|
22
|
+
- lib/biceps/routing.rb
|
23
|
+
- lib/biceps/version.rb
|
24
|
+
- lib/biceps.rb
|
25
|
+
- MIT-LICENSE
|
26
|
+
- Rakefile
|
27
|
+
- README.md
|
28
|
+
homepage:
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.10
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Easy versionned API routing
|
52
|
+
test_files: []
|