File tree 10 files changed +124
-3
lines changed
10 files changed +124
-3
lines changed Original file line number Diff line number Diff line change 17
17
coverage
18
18
rdoc
19
19
pkg
20
+ .rvmrc
21
+ .bundle
20
22
21
23
# # PROJECT::SPECIFIC
Original file line number Diff line number Diff line change @@ -2,6 +2,9 @@ gem 'rack'
2
2
gem 'rack-mount'
3
3
gem 'rack-jsonp'
4
4
5
+ gem 'json'
6
+ gem 'multi_json'
7
+
5
8
group :test do
6
9
gem 'rspec' , '>= 2.0.0.beta.19'
7
10
gem 'rack-test'
Original file line number Diff line number Diff line change 10
10
diff-lcs (1.1.2 )
11
11
gherkin (2.1.5 )
12
12
trollop (~> 1.16.2 )
13
+ json (1.4.3 )
13
14
json_pure (1.4.3 )
15
+ multi_json (0.0.4 )
14
16
rack (1.2.1 )
15
17
rack-jsonp (1.0.0 )
16
18
rack-mount (0.6.9 )
@@ -33,6 +35,8 @@ PLATFORMS
33
35
34
36
DEPENDENCIES
35
37
cucumber (>= 0.8.5 )
38
+ json
39
+ multi_json
36
40
rack
37
41
rack-jsonp
38
42
rack-mount
Original file line number Diff line number Diff line change 10
10
gem . email = "michael@intridea.com"
11
11
gem . homepage = "http://github.com/intridea/grape"
12
12
gem . authors = [ "Michael Bleigh" ]
13
+ gem . add_dependency 'rack'
14
+ gem . add_dependency 'multi_json'
13
15
gem . add_development_dependency "rspec" , ">= 1.2.9"
14
16
gem . add_development_dependency "cucumber" , ">= 0"
15
17
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
Original file line number Diff line number Diff line change 3
3
4
4
require 'grape/middleware/base'
5
5
require 'grape/middleware/prefixer'
6
- require 'grape/middleware/versioner'
6
+ require 'grape/middleware/versioner'
7
+ require 'grape/middleware/formatter'
Original file line number Diff line number Diff line change
1
+ require 'grape/middleware/base'
2
+
3
+ module Grape
4
+ module Middleware
5
+ class Error < Base
6
+
7
+ end
8
+ end
9
+ end
Original file line number Diff line number Diff line change
1
+ require 'grape/middleware/base'
2
+ require 'multi_json'
3
+
4
+ module Grape
5
+ module Middleware
6
+ class Formatter < Base
7
+ CONTENT_TYPES = {
8
+ :xml => 'application/xml' ,
9
+ :json => 'application/json' ,
10
+ :atom => 'application/atom+xml' ,
11
+ :rss => 'application/rss+xml'
12
+ }
13
+
14
+ def default_options
15
+ {
16
+ :default_format => :json ,
17
+ :content_types => { }
18
+ }
19
+ end
20
+
21
+ def content_types
22
+ CONTENT_TYPES . merge ( options [ :content_types ] )
23
+ end
24
+
25
+ def before
26
+ fmt = format_from_extension || format_from_header || options [ :default_format ]
27
+
28
+ if content_types . key? ( fmt )
29
+ env [ 'api.format' ] = fmt
30
+ else
31
+ env [ 'api.error.status' ] = 406
32
+ env [ 'api.error.message' ] = 'The requested format is not supported.'
33
+ end
34
+ end
35
+
36
+ def format_from_extension
37
+ parts = request . path . split ( '.' )
38
+ hit = parts . last . to_sym
39
+
40
+ if parts . size <= 1
41
+ nil
42
+ else
43
+ hit
44
+ end
45
+ end
46
+
47
+ def format_from_header
48
+ # TODO: Implement Accept header parsing.
49
+ end
50
+
51
+ def after
52
+ status , headers , bodies = *@app_response
53
+ bodies . map! do |body |
54
+ MultiJson . encode ( body )
55
+ end
56
+ [ status , headers , bodies ]
57
+ end
58
+ end
59
+ end
60
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Grape ::Middleware ::Error do
4
+
5
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Grape ::Middleware ::Formatter do
4
+ subject { Grape ::Middleware ::Formatter . new ( app ) }
5
+ before { subject . stub! ( :dup ) . and_return ( subject ) }
6
+
7
+ let ( :app ) { lambda { |env | [ 200 , { } , [ @body ] ] } }
8
+
9
+ context 'serialization' do
10
+ it 'should look at the bodies for possibly serializable data' do
11
+ @body = { "abc" => "def" }
12
+ status , headers , bodies = *subject . call ( { 'PATH_INFO' => '/somewhere' } )
13
+ bodies . first . should == MultiJson . encode ( @body )
14
+ end
15
+ end
16
+
17
+ context 'detection' do
18
+ it 'should use the extension if one is provided' do
19
+ subject . call ( { 'PATH_INFO' => '/info.xml' } )
20
+ subject . env [ 'api.format' ] . should == :xml
21
+ subject . call ( { 'PATH_INFO' => '/info.json' } )
22
+ subject . env [ 'api.format' ] . should == :json
23
+ end
24
+
25
+ it 'should use the default format if none is provided' do
26
+ subject . call ( { 'PATH_INFO' => '/info' } )
27
+ subject . env [ 'api.format' ] . should == :json
28
+ end
29
+
30
+ it 'should throw an error on an unrecognized format' do
31
+ subject . call ( { 'PATH_INFO' => '/info.barklar' } )
32
+ subject . env [ 'api.error.status' ] . should == 406
33
+ end
34
+ end
35
+ end
Original file line number Diff line number Diff line change 5
5
6
6
require 'rubygems'
7
7
require 'bundler'
8
- Bundler . setup :test
8
+ Bundler . setup :default , : test
9
9
10
10
require 'rspec'
11
11
require 'rack/test'
12
12
13
13
RSpec . configure do |config |
14
-
14
+ config . include Rack :: Test :: Methods
15
15
end
You can’t perform that action at this time.
0 commit comments