sinatra-rest-helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Cyril Rohr
2
+ Copyright (c) 2009 INRIA Rennes - Bretagne Atlantique
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = sinatra-rest-helpers
2
+
3
+ A set of helpers for sinatra apps that expose REST resources.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Cyril Rohr. See LICENSE for details.
19
+ Copyright (c) 2009 INRIA Rennes - Bretagne Atlantique. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sinatra-rest-helpers"
8
+ gem.summary = %Q{Adds useful helpers for REST applications developed with Sinatra.}
9
+ gem.description = %Q{TODO: longer description of your gem}
10
+ gem.email = "cyril.rohr@gmail.com"
11
+ gem.homepage = "http://github.com/cryx/sinatra-rest-helpers"
12
+ gem.authors = ["Cyril Rohr"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+
34
+
35
+ task :default => :spec
36
+
37
+ begin
38
+ require 'yard'
39
+ YARD::Rake::YardocTask.new
40
+ rescue LoadError
41
+ task :yardoc do
42
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
43
+ end
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,41 @@
1
+ require 'rack'
2
+
3
+ module Sinatra
4
+ module RestHelpers
5
+ INFINITY = 1/0.0
6
+ # e.g.:
7
+ # get '/x/y/z' do
8
+ # provides "application/json", :xml, :zip, "text/html;level=5"
9
+ # end
10
+ def provides *formats
11
+ generate_type_hash = Proc.new{ |header|
12
+ type, *params = header.split(/;\s*/)
13
+ Hash[*params.map{|p| p.split(/\s*=\s*/)}.flatten].merge("type" => type)
14
+ }
15
+ supported_formats = formats.map do |f|
16
+ # selects the correct mime type if a symbol is given
17
+ f.is_a?(Symbol) ? ::Rack::Mime::MIME_TYPES[".#{f.to_s}"] : f
18
+ end.compact.map do |f|
19
+ generate_type_hash.call(f)
20
+ end
21
+ accepted_formats = request.accept.split(/,\s*/).map do |f|
22
+ generate_type_hash.call(f)
23
+ end
24
+ selected_format = supported_formats.detect{ |supported_format|
25
+ !accepted_formats.detect{ |accepted_format|
26
+ accepted_format["type"] == supported_format["type"] &&
27
+ (accepted_format["level"] || INFINITY).to_f >= (supported_format["level"] || 0).to_f
28
+ }.nil?
29
+ }
30
+ if selected_format.nil?
31
+ halt 406, supported_formats.map{|f|
32
+ output = f["type"]
33
+ output += ";level=#{f["level"]}" if f.has_key?("level")
34
+ }.join(", ")
35
+ else
36
+ response.headers['Content-Type'] = "#{selected_format["type"]}#{selected_format["level"].nil? ? "" : ";level=#{selected_format["level"]}"}"
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+
4
+ class App
5
+ include Sinatra::RestHelpers
6
+
7
+ attr_accessor :request, :response
8
+ def initialize(request)
9
+ @request = request
10
+ @response = OpenStruct.new(:headers => {})
11
+ end
12
+
13
+ def test_provides!(*params)
14
+ provides *params
15
+ end
16
+ end
17
+
18
+ def mime(ext, type)
19
+ ext = ".#{ext}" unless ext.to_s[0] == ?.
20
+ Rack::Mime::MIME_TYPES[ext.to_s] = type
21
+ end
22
+
23
+ describe "SinatraRestHelpers" do
24
+ before(:each) do
25
+ Rack::Mime::MIME_TYPES.clear
26
+ end
27
+ it "should throw a 406 if the client requested only unsupported formats" do
28
+ request = mock("request", :accept => "application/json")
29
+ app = App.new(request)
30
+ app.should_receive(:halt).with(406, "")
31
+ app.test_provides!(:json)
32
+ end
33
+ it "should not throw a 406 if the client requested a supported format" do
34
+ request = mock("request", :accept => "application/json")
35
+ mime :json, "application/json"
36
+ app = App.new(request)
37
+ app.should_not_receive(:halt)
38
+ app.test_provides!(:json)
39
+ app.response.headers['Content-Type'].should == "application/json"
40
+ end
41
+ it "should not accept a request with a type level lower than what is supported" do
42
+ request = mock("request", :accept => "application/json;level=1")
43
+ app = App.new(request)
44
+ app.should_receive(:halt).with(406, "application/json;level=3, application/json;level=2")
45
+ app.test_provides!("application/json;level=3", "application/json;level=2")
46
+ end
47
+ it "should accept a request having a supported mime type, but with no level" do
48
+ request = mock("request", :accept => "application/json")
49
+ app = App.new(request)
50
+ app.should_not_receive(:halt)
51
+ app.test_provides!("application/json;level=2")
52
+ app.response.headers['Content-Type'].should == "application/json;level=2"
53
+ end
54
+ it "should select the first type matching the criteria" do
55
+ request = mock("request", :accept => "application/json;level=2, application/xml, application/vnd.fr.grid5000.api.Cluster+json;level=2")
56
+ app = App.new(request)
57
+ app.should_not_receive(:halt)
58
+ app.test_provides!("application/json;level=3", "application/vnd.fr.grid5000.api.Cluster+json;level=2")
59
+ app.response.headers['Content-Type'].should == "application/vnd.fr.grid5000.api.Cluster+json;level=2"
60
+ end
61
+ it "should accept requests with a level, even if the developer didn't explicitely defined one" do
62
+ request = mock("request", :accept => "application/json;level=1")
63
+ app = App.new(request)
64
+ app.should_not_receive(:halt)
65
+ app.test_provides!("application/json")
66
+ app.response.headers['Content-Type'].should == "application/json"
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'sinatra/rest_helpers'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-rest-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyril Rohr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A set of helpers for sinatra apps that expose REST resources.
17
+ email: cyril.rohr@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/sinatra/rest_helpers.rb
33
+ - spec/sinatra-rest-helpers_spec.rb
34
+ - spec/spec_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/crohr/sinatra-rest-helpers
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Adds useful helpers for REST applications developed with Sinatra.
63
+ test_files:
64
+ - spec/sinatra-rest-helpers_spec.rb
65
+ - spec/spec_helper.rb