rack-rest_book 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile +6 -0
  2. data/README.markdown +22 -0
  3. data/lib/rack/rest_book.rb +21 -0
  4. data/test/test.rb +66 -0
  5. metadata +118 -0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://gems.rubyforge.org'
2
+ gem 'rack', '1.2.1'
3
+ gem 'sinatra', '1.1.2', :require => 'sinatra/base'
4
+ gem 'contest', '0.1.2'
5
+ gem 'ruby-debug', '0.10.4', :require => nil
6
+ gem 'rack-test', '0.5.7', :require => 'rack/test'
data/README.markdown ADDED
@@ -0,0 +1,22 @@
1
+ Rack::RestBook
2
+ ==============================================
3
+
4
+ Make all POST calls become GET. This allows us to do REST development with Facebook, which will always send a POST with iframes (as of March 10, 2011).
5
+ If you need to actually POST something, add this parameter to the query string: \_method=METHOD (so \_method=post, \_method=put, or \_method=delete).
6
+
7
+ Your application will still respond to real HTTP verbs as long as they are not POST. Rack::RestBook ignores other verbs.
8
+
9
+ The real documentation for this gem is in test/test.rb.
10
+
11
+ Installation and Usage
12
+ ---
13
+
14
+ $ gem install rack-rest_book
15
+
16
+ require 'rack/rest_book'
17
+ use Rack::RestBook
18
+
19
+ Testing
20
+ ---
21
+ $ bundle install
22
+ $ ruby test/test.rb
@@ -0,0 +1,21 @@
1
+ module Rack
2
+ class RestBook
3
+ class RestBookError < StandardError; end
4
+
5
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
6
+ METHOD_OVERRIDE_PARAM_KEY = '_method'.freeze
7
+ HTTP_METHOD_OVERRIDE_HEADER = 'HTTP_X_HTTP_METHOD_OVERRIDE'.freeze
8
+
9
+ def initialize(app); @app = app end
10
+
11
+ def call(env)
12
+ if env['REQUEST_METHOD'] == 'POST'
13
+ req = Request.new env
14
+ meth = (req.params[METHOD_OVERRIDE_PARAM_KEY] || 'GET').to_s.upcase
15
+ raise RestBookError, "invalid HTTP verb for method override: #{req.params[METHOD_OVERRIDE_PARAM_KEY]}" unless HTTP_METHODS.include?(meth)
16
+ env['rack.methodoverride.original_method'] = env['REQUEST_METHOD'] and env['REQUEST_METHOD'] = meth unless meth == env['REQUEST_METHOD']
17
+ end
18
+ @app.call env
19
+ end
20
+ end
21
+ end
data/test/test.rb ADDED
@@ -0,0 +1,66 @@
1
+ Bundler.setup
2
+ require 'bundler'
3
+ require 'test/unit'
4
+ require 'rack/test'
5
+ require 'contest'
6
+ require 'sinatra/base'
7
+ require 'lib/rack/rest_book'
8
+
9
+ def mock_app(base=Sinatra::Base, &block); @app = Sinatra.new base, &block end
10
+ def app; @app end
11
+
12
+ class RackRestBookTest < Test::Unit::TestCase
13
+ include Rack::Test::Methods
14
+
15
+ context 'an sinatra app using Rack::RestBook as middleware' do
16
+ setup do
17
+ mock_app do
18
+ set :show_exceptions, false
19
+ use Rack::RestBook
20
+ get('/') {'get'}
21
+ post('/') {'post'}
22
+ put('/') {'put'}
23
+ delete('/') {'delete'}
24
+ end
25
+ end
26
+
27
+ test 'should translate POST requests to GET if method override is missing' do
28
+ post '/'
29
+ assert_equal 'get', last_response.body
30
+ end
31
+
32
+ test 'should provide GET, PUT and DELETE when set in method override' do
33
+ %w{post put delete}.each do |meth|
34
+ send meth.to_sym, '/', '_method' => meth.upcase
35
+ assert_equal meth, last_response.body
36
+ end
37
+ end
38
+
39
+ test 'responds to real HTTP verbs except POST' do
40
+ %w{get put delete}.each do |meth|
41
+ send meth.to_sym, '/'
42
+ assert_equal meth, last_response.body
43
+ end
44
+ end
45
+
46
+ test 'ignores method override with real HTTP verbs except POST' do
47
+ %w{get put delete}.each do |meth|
48
+ send meth.to_sym, '/', '_method' => 'POST'
49
+ assert_equal meth, last_response.body
50
+ end
51
+ end
52
+
53
+ test 'should respond to method override in a case insensitive way' do
54
+ ['pOsT', 'pUt', 'DeLEtE', 'GeT'].each do |meth|
55
+ post '/', '_method' => meth
56
+ assert_equal meth.downcase, last_response.body
57
+ end
58
+ end
59
+
60
+ test 'should fail if given crazy method override' do
61
+ assert_raises Rack::RestBook::RestBookError do
62
+ post '/', '_method' => 'dade_murphy'
63
+ end
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-rest_book
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 2
10
+ version: 1.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Kyle Drake
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-02 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: sinatra
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 1
49
+ - 2
50
+ version: 1.1.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: contest
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ hash: 31
62
+ segments:
63
+ - 0
64
+ - 1
65
+ - 2
66
+ version: 0.1.2
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: Makes all POST calls assume GET. This allows us to do REST development with Facebook, which always sends POST
70
+ email:
71
+ - kyle.drake@dachisgroup.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - lib/rack/rest_book.rb
80
+ - test/test.rb
81
+ - Gemfile
82
+ - README.markdown
83
+ has_rdoc: true
84
+ homepage: http://github.com/dachisgroup/rack-rest_book
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project: rack-rest_book
113
+ rubygems_version: 1.3.7
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Makes all POST calls assume GET for Facebook Development
117
+ test_files: []
118
+