grape 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grape might be problematic. Click here for more details.

Files changed (42) hide show
  1. data/.yardopts +2 -0
  2. data/CHANGELOG.markdown +19 -0
  3. data/Gemfile +2 -0
  4. data/README.markdown +166 -131
  5. data/Rakefile +1 -3
  6. data/lib/grape.rb +0 -3
  7. data/lib/grape/api.rb +16 -4
  8. data/lib/grape/cookies.rb +32 -30
  9. data/lib/grape/endpoint.rb +25 -11
  10. data/lib/grape/entity.rb +5 -0
  11. data/lib/grape/error_formatter/base.rb +2 -1
  12. data/lib/grape/middleware/base.rb +9 -2
  13. data/lib/grape/middleware/error.rb +11 -11
  14. data/lib/grape/middleware/formatter.rb +8 -5
  15. data/lib/grape/middleware/versioner/header.rb +1 -3
  16. data/lib/grape/middleware/versioner/path.rb +15 -2
  17. data/lib/grape/util/deep_merge.rb +4 -4
  18. data/lib/grape/validations.rb +2 -3
  19. data/lib/grape/version.rb +1 -1
  20. data/spec/grape/api_spec.rb +316 -175
  21. data/spec/grape/endpoint_spec.rb +159 -57
  22. data/spec/grape/entity_spec.rb +80 -80
  23. data/spec/grape/middleware/auth/basic_spec.rb +3 -3
  24. data/spec/grape/middleware/auth/digest_spec.rb +4 -4
  25. data/spec/grape/middleware/auth/oauth2_spec.rb +4 -4
  26. data/spec/grape/middleware/base_spec.rb +9 -9
  27. data/spec/grape/middleware/error_spec.rb +4 -4
  28. data/spec/grape/middleware/exception_spec.rb +13 -13
  29. data/spec/grape/middleware/formatter_spec.rb +25 -25
  30. data/spec/grape/middleware/versioner/header_spec.rb +23 -23
  31. data/spec/grape/middleware/versioner/param_spec.rb +8 -8
  32. data/spec/grape/middleware/versioner/path_spec.rb +8 -8
  33. data/spec/grape/middleware/versioner_spec.rb +6 -3
  34. data/spec/grape/util/hash_stack_spec.rb +20 -20
  35. data/spec/grape/validations/presence_spec.rb +1 -1
  36. data/spec/grape/validations/regexp_spec.rb +2 -2
  37. data/spec/grape/validations_spec.rb +4 -4
  38. data/spec/shared/versioning_examples.rb +48 -20
  39. metadata +5 -7
  40. data/.document +0 -5
  41. data/lib/grape/middleware/prefixer.rb +0 -21
  42. data/spec/grape/middleware/prefixer_spec.rb +0 -30
@@ -5,29 +5,29 @@ describe Grape::Middleware::Versioner::Param do
5
5
  let(:app) { lambda{|env| [200, env, env['api.version']]} }
6
6
  subject { Grape::Middleware::Versioner::Param.new(app, @options || {}) }
7
7
 
8
- it 'should set the API version based on the default param (apiver)' do
8
+ it 'sets the API version based on the default param (apiver)' do
9
9
  env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1"}})
10
10
  subject.call(env)[1]["api.version"].should == 'v1'
11
11
  end
12
12
 
13
- it 'should cut (only) the version out of the params', :focus => true do
13
+ it 'cuts (only) the version out of the params', :focus => true do
14
14
  env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1", "other_param" => "5"}})
15
15
  subject.call(env)[1]['rack.request.query_hash']["apiver"].should be_nil
16
16
  subject.call(env)[1]['rack.request.query_hash']["other_param"].should == "5"
17
17
  end
18
18
 
19
- it 'should provide a nil version if no version is given' do
19
+ it 'provides a nil version if no version is given' do
20
20
  env = Rack::MockRequest.env_for("/")
21
21
  subject.call(env).last.should be_nil
22
22
  end
23
23
 
24
24
  context 'with specified parameter name' do
25
25
  before{ @options = {:parameter => ['v']}}
26
- it 'should set the API version based on the custom parameter name' do
26
+ it 'sets the API version based on the custom parameter name' do
27
27
  env = Rack::MockRequest.env_for("/awesome", {:params => {"v" => "v1"}})
28
28
  s = subject.call(env)[1]["api.version"] == "v1"
29
29
  end
30
- it 'should not set the API version based on the default param' do
30
+ it 'does not set the API version based on the default param' do
31
31
  env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1"}})
32
32
  s = subject.call(env)[1]["api.version"] == nil
33
33
  end
@@ -35,18 +35,18 @@ describe Grape::Middleware::Versioner::Param do
35
35
 
36
36
  context 'with specified versions' do
37
37
  before{ @options = {:versions => ['v1', 'v2']}}
38
- it 'should throw an error if a non-allowed version is specified' do
38
+ it 'throws an error if a non-allowed version is specified' do
39
39
  env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v3"}})
40
40
  catch(:error){subject.call(env)}[:status].should == 404
41
41
  end
42
42
 
43
- it 'should allow versions that have been specified' do
43
+ it 'allows versions that have been specified' do
44
44
  env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1"}})
45
45
  subject.call(env)[1]["api.version"].should == 'v1'
46
46
  end
47
47
  end
48
48
 
49
- it 'should return a 200 when no version is set (matches the first version found)' do
49
+ it 'returns a 200 when no version is set (matches the first version found)' do
50
50
  @options = {
51
51
  :versions => ['v1'],
52
52
  :version_options => {:using => :header}
@@ -4,36 +4,36 @@ describe Grape::Middleware::Versioner::Path do
4
4
  let(:app) { lambda{|env| [200, env, env['api.version']]} }
5
5
  subject { Grape::Middleware::Versioner::Path.new(app, @options || {}) }
6
6
 
7
- it 'should set the API version based on the first path' do
7
+ it 'sets the API version based on the first path' do
8
8
  subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1'
9
9
  end
10
10
 
11
- it 'should cut the version out of the path' do
12
- subject.call('PATH_INFO' => '/v1/awesome')[1]['PATH_INFO'].should == '/awesome'
11
+ it 'does not cut the version out of the path' do
12
+ subject.call('PATH_INFO' => '/v1/awesome')[1]['PATH_INFO'].should == '/v1/awesome'
13
13
  end
14
14
 
15
- it 'should provide a nil version if no path is given' do
15
+ it 'provides a nil version if no path is given' do
16
16
  subject.call('PATH_INFO' => '/').last.should be_nil
17
17
  end
18
18
 
19
19
  context 'with a pattern' do
20
20
  before{ @options = {:pattern => /v./i} }
21
- it 'should set the version if it matches' do
21
+ it 'sets the version if it matches' do
22
22
  subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1'
23
23
  end
24
24
 
25
- it 'should ignore the version if it fails to match' do
25
+ it 'ignores the version if it fails to match' do
26
26
  subject.call('PATH_INFO' => '/awesome/radical').last.should be_nil
27
27
  end
28
28
  end
29
29
 
30
30
  context 'with specified versions' do
31
31
  before{ @options = {:versions => ['v1', 'v2']}}
32
- it 'should throw an error if a non-allowed version is specified' do
32
+ it 'throws an error if a non-allowed version is specified' do
33
33
  catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404
34
34
  end
35
35
 
36
- it 'should allow versions that have been specified' do
36
+ it 'allows versions that have been specified' do
37
37
  subject.call('PATH_INFO' => '/v1/asoasd').last.should == 'v1'
38
38
  end
39
39
  end
@@ -1,16 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Grape::Middleware::Versioner do
4
+
4
5
  let(:klass) { Grape::Middleware::Versioner }
5
- it 'should recognize :path' do
6
+
7
+ it 'recognizes :path' do
6
8
  klass.using(:path).should == Grape::Middleware::Versioner::Path
7
9
  end
8
10
 
9
- it 'should recognize :header' do
11
+ it 'recognizes :header' do
10
12
  klass.using(:header).should == Grape::Middleware::Versioner::Header
11
13
  end
12
14
 
13
- it 'should recognize :param' do
15
+ it 'recognizes :param' do
14
16
  klass.using(:param).should == Grape::Middleware::Versioner::Param
15
17
  end
18
+
16
19
  end
@@ -4,19 +4,19 @@ describe Grape::Util::HashStack do
4
4
  let(:klass){ Grape::Util::HashStack }
5
5
 
6
6
  describe '#get' do
7
- it 'should find the first available key' do
7
+ it 'finds the first available key' do
8
8
  subject[:abc] = 123
9
9
  subject.push(:abc => 345)
10
10
  subject.get(:abc).should == 345
11
11
  end
12
12
 
13
- it 'should be nil if the key has not been set' do
13
+ it 'is nil if the key has not been set' do
14
14
  subject[:abc].should be_nil
15
15
  end
16
16
  end
17
17
 
18
18
  describe '#set' do
19
- it 'should set a value on the highest frame' do
19
+ it 'sets a value on the highest frame' do
20
20
  subject.push
21
21
  subject.set(:abc, 123)
22
22
  subject.stack.last[:abc].should == 123
@@ -24,31 +24,31 @@ describe Grape::Util::HashStack do
24
24
  end
25
25
 
26
26
  describe '#imbue' do
27
- it 'should push a new value onto the end of an array' do
27
+ it 'pushes a new value onto the end of an array' do
28
28
  subject[:abc] = []
29
29
  subject.imbue(:abc, [123])
30
30
  subject.imbue(:abc, [456])
31
31
  subject[:abc].should == [123, 456]
32
32
  end
33
33
 
34
- it 'should merge a hash that is passed' do
34
+ it 'merges a hash that is passed' do
35
35
  subject[:abc] = {:foo => 'bar'}
36
36
  subject.imbue(:abc, {:baz => 'wich'})
37
37
  subject[:abc].should == {:foo => 'bar', :baz => 'wich'}
38
38
  end
39
39
 
40
- it 'should set the value if not a hash or array' do
40
+ it 'sets the value if not a hash or array' do
41
41
  subject.imbue(:abc, 123)
42
42
  subject[:abc].should == 123
43
43
  end
44
44
 
45
- it 'should be able to imbue an array without explicit setting' do
45
+ it 'is able to imbue an array without explicit setting' do
46
46
  subject.imbue(:arr, [1])
47
47
  subject.imbue(:arr, [2])
48
48
  subject[:arr].should == [1,2]
49
49
  end
50
50
 
51
- it 'should be able to imbue a hash without explicit setting' do
51
+ it 'is able to imbue a hash without explicit setting' do
52
52
  subject.imbue(:hash, :foo => 'bar')
53
53
  subject.imbue(:hash, :baz => 'wich')
54
54
  subject[:hash].should == {:foo => 'bar', :baz => 'wich'}
@@ -56,16 +56,16 @@ describe Grape::Util::HashStack do
56
56
  end
57
57
 
58
58
  describe '#push' do
59
- it 'should return a HashStack' do
59
+ it 'returns a HashStack' do
60
60
  subject.push(klass.new).should be_kind_of(klass)
61
61
  end
62
62
 
63
- it 'should place the passed value on the top of the stack' do
63
+ it 'places the passed value on the top of the stack' do
64
64
  subject.push(:abc => 123)
65
65
  subject.stack.should == [{}, {:abc => 123}]
66
66
  end
67
67
 
68
- it 'should push an empty hash by default' do
68
+ it 'pushes an empty hash by default' do
69
69
  subject[:abc] = 123
70
70
  subject.push
71
71
  subject.stack.should == [{:abc => 123}, {}]
@@ -73,7 +73,7 @@ describe Grape::Util::HashStack do
73
73
  end
74
74
 
75
75
  describe '#pop' do
76
- it 'should remove and return the top frame' do
76
+ it 'removes and return the top frame' do
77
77
  subject.push(:abc => 123)
78
78
  subject.pop.should == {:abc => 123}
79
79
  subject.stack.size.should == 1
@@ -81,7 +81,7 @@ describe Grape::Util::HashStack do
81
81
  end
82
82
 
83
83
  describe '#peek' do
84
- it 'should return the top frame without removing it' do
84
+ it 'returns the top frame without removing it' do
85
85
  subject.push(:abc => 123)
86
86
  subject.peek.should == {:abc => 123}
87
87
  subject.stack.size.should == 2
@@ -89,40 +89,40 @@ describe Grape::Util::HashStack do
89
89
  end
90
90
 
91
91
  describe '#prepend' do
92
- it 'should return a HashStack' do
92
+ it 'returns a HashStack' do
93
93
  subject.prepend(klass.new).should be_kind_of(klass)
94
94
  end
95
95
 
96
- it "should prepend a HashStack's stack onto its own stack" do
96
+ it "prepends a HashStack's stack onto its own stack" do
97
97
  other = klass.new.push(:abc => 123)
98
98
  subject.prepend(other).stack.should == [{}, {:abc => 123}, {}]
99
99
  end
100
100
  end
101
101
 
102
102
  describe '#concat' do
103
- it 'should return a HashStack' do
103
+ it 'returns a HashStack' do
104
104
  subject.concat(klass.new).should be_kind_of(klass)
105
105
  end
106
106
 
107
- it "should append a HashStack's stack onto its own stack" do
107
+ it "appends a HashStack's stack onto its own stack" do
108
108
  other = klass.new.push(:abc => 123)
109
109
  subject.concat(other).stack.should == [{}, {}, {:abc => 123}]
110
110
  end
111
111
  end
112
112
 
113
113
  describe '#update' do
114
- it 'should merge! into the top frame' do
114
+ it 'merges! into the top frame' do
115
115
  subject.update(:abc => 123)
116
116
  subject.stack.should == [{:abc => 123}]
117
117
  end
118
118
 
119
- it 'should return a HashStack' do
119
+ it 'returns a HashStack' do
120
120
  subject.update(:abc => 123).should be_kind_of(klass)
121
121
  end
122
122
  end
123
123
 
124
124
  describe '#clone' do
125
- it 'should perform a deep copy' do
125
+ it 'performs a deep copy' do
126
126
  subject[:abc] = 123
127
127
  subject.push :def => 234
128
128
  clone = subject.clone
@@ -57,7 +57,7 @@ describe Grape::Validations::PresenceValidator do
57
57
  ValidationsSpec::PresenceValidatorSpec::API
58
58
  end
59
59
 
60
- it "does not validate for any params" do
60
+ it 'does not validate for any params' do
61
61
  get("/bacons")
62
62
  last_response.status.should == 200
63
63
  last_response.body.should == "All the bacon"
@@ -20,12 +20,12 @@ describe Grape::Validations::RegexpValidator do
20
20
  ValidationsSpec::RegexpValidatorSpec::API
21
21
  end
22
22
 
23
- it 'should refuse invalid input' do
23
+ it 'refuses invalid input' do
24
24
  get '/', :name => "invalid name"
25
25
  last_response.status.should == 400
26
26
  end
27
27
 
28
- it 'should accept valid input' do
28
+ it 'accepts valid input' do
29
29
  get '/', :name => "bob"
30
30
  last_response.status.should == 200
31
31
  end
@@ -85,7 +85,7 @@ describe Grape::Validations do
85
85
  last_response.body.should == 'custom: is not custom!'
86
86
  end
87
87
 
88
- it "skip validation when parameter isn't present" do
88
+ it "skips validation when parameter isn't present" do
89
89
  get '/optional_custom'
90
90
  last_response.status.should == 200
91
91
  last_response.body.should == 'optional with custom works!'
@@ -148,19 +148,19 @@ describe Grape::Validations do
148
148
  end
149
149
  end
150
150
 
151
- specify 'the parent namespace should use the validator' do
151
+ specify 'the parent namespace uses the validator' do
152
152
  get '/nested/one', { :custom => 'im wrong, validate me'}
153
153
  last_response.status.should == 400
154
154
  last_response.body.should == 'custom: is not custom!'
155
155
  end
156
156
 
157
- specify 'the nested namesapce should inherit the custom validator' do
157
+ specify 'the nested namesapce inherits the custom validator' do
158
158
  get '/nested/nested/two', { :custom => 'im wrong, validate me'}
159
159
  last_response.status.should == 400
160
160
  last_response.body.should == 'custom: is not custom!'
161
161
  end
162
162
 
163
- specify 'peer namesapces should not have the validator' do
163
+ specify 'peer namesapces does not have the validator' do
164
164
  get '/peer/one', { :custom => 'im not validated' }
165
165
  last_response.status.should == 200
166
166
  last_response.body.should == 'no validation required'
@@ -1,5 +1,5 @@
1
- shared_examples_for "versioning" do
2
- it 'should set the API version' do
1
+ shared_examples_for 'versioning' do
2
+ it 'sets the API version' do
3
3
  subject.version 'v1', macro_options
4
4
  subject.get :hello do
5
5
  "Version: #{request.env['api.version']}"
@@ -8,7 +8,7 @@ shared_examples_for "versioning" do
8
8
  last_response.body.should eql "Version: v1"
9
9
  end
10
10
 
11
- it 'should add the prefix before the API version' do
11
+ it 'adds the prefix before the API version' do
12
12
  subject.prefix 'api'
13
13
  subject.version 'v1', macro_options
14
14
  subject.get :hello do
@@ -18,7 +18,7 @@ shared_examples_for "versioning" do
18
18
  last_response.body.should eql "Version: v1"
19
19
  end
20
20
 
21
- it 'should be able to specify version as a nesting' do
21
+ it 'is able to specify version as a nesting' do
22
22
  subject.version 'v2', macro_options
23
23
  subject.get '/awesome' do
24
24
  "Radical"
@@ -41,7 +41,7 @@ shared_examples_for "versioning" do
41
41
  last_response.status.should eql 404
42
42
  end
43
43
 
44
- it 'should be able to specify multiple versions' do
44
+ it 'is able to specify multiple versions' do
45
45
  subject.version 'v1', 'v2', macro_options
46
46
  subject.get 'awesome' do
47
47
  "I exist"
@@ -55,23 +55,51 @@ shared_examples_for "versioning" do
55
55
  last_response.status.should eql 404
56
56
  end
57
57
 
58
- it 'should allow the same endpoint to be implemented for different versions' do
59
- subject.version 'v2', macro_options
60
- subject.get 'version' do
61
- request.env['api.version']
62
- end
58
+ context 'with different versions for the same endpoint' do
59
+ context 'without a prefix' do
60
+ it 'allows the same endpoint to be implemented' do
61
+ subject.version 'v2', macro_options
62
+ subject.get 'version' do
63
+ request.env['api.version']
64
+ end
63
65
 
64
- subject.version 'v1', macro_options do
65
- get 'version' do
66
- "version " + request.env['api.version']
66
+ subject.version 'v1', macro_options do
67
+ get 'version' do
68
+ "version " + request.env['api.version']
69
+ end
70
+ end
71
+
72
+ versioned_get '/version', 'v2', macro_options
73
+ last_response.status.should == 200
74
+ last_response.body.should == 'v2'
75
+ versioned_get '/version', 'v1', macro_options
76
+ last_response.status.should == 200
77
+ last_response.body.should == 'version v1'
67
78
  end
68
79
  end
69
80
 
70
- versioned_get '/version', 'v2', macro_options
71
- last_response.status.should == 200
72
- last_response.body.should == 'v2'
73
- versioned_get '/version', 'v1', macro_options
74
- last_response.status.should == 200
75
- last_response.body.should == 'version v1'
81
+ context 'with a prefix' do
82
+ it 'allows the same endpoint to be implemented' do
83
+ subject.prefix 'api'
84
+ subject.version 'v2', macro_options
85
+ subject.get 'version' do
86
+ request.env['api.version']
87
+ end
88
+
89
+ subject.version 'v1', macro_options do
90
+ get 'version' do
91
+ "version " + request.env['api.version']
92
+ end
93
+ end
94
+
95
+ versioned_get '/version', 'v1', macro_options.merge(:prefix => subject.prefix)
96
+ last_response.status.should == 200
97
+ last_response.body.should == 'version v1'
98
+
99
+ versioned_get '/version', 'v2', macro_options.merge(:prefix => subject.prefix)
100
+ last_response.status.should == 200
101
+ last_response.body.should == 'v2'
102
+ end
103
+ end
76
104
  end
77
- end
105
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-24 00:00:00.000000000 Z
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -258,10 +258,10 @@ executables: []
258
258
  extensions: []
259
259
  extra_rdoc_files: []
260
260
  files:
261
- - .document
262
261
  - .gitignore
263
262
  - .rspec
264
263
  - .travis.yml
264
+ - .yardopts
265
265
  - CHANGELOG.markdown
266
266
  - Gemfile
267
267
  - Guardfile
@@ -292,7 +292,6 @@ files:
292
292
  - lib/grape/middleware/error.rb
293
293
  - lib/grape/middleware/filter.rb
294
294
  - lib/grape/middleware/formatter.rb
295
- - lib/grape/middleware/prefixer.rb
296
295
  - lib/grape/middleware/versioner.rb
297
296
  - lib/grape/middleware/versioner/header.rb
298
297
  - lib/grape/middleware/versioner/param.rb
@@ -318,7 +317,6 @@ files:
318
317
  - spec/grape/middleware/error_spec.rb
319
318
  - spec/grape/middleware/exception_spec.rb
320
319
  - spec/grape/middleware/formatter_spec.rb
321
- - spec/grape/middleware/prefixer_spec.rb
322
320
  - spec/grape/middleware/versioner/header_spec.rb
323
321
  - spec/grape/middleware/versioner/param_spec.rb
324
322
  - spec/grape/middleware/versioner/path_spec.rb
@@ -347,7 +345,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
347
345
  version: '0'
348
346
  segments:
349
347
  - 0
350
- hash: 653936499
348
+ hash: -130218097
351
349
  required_rubygems_version: !ruby/object:Gem::Requirement
352
350
  none: false
353
351
  requirements:
@@ -356,7 +354,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
356
354
  version: '0'
357
355
  segments:
358
356
  - 0
359
- hash: 653936499
357
+ hash: -130218097
360
358
  requirements: []
361
359
  rubyforge_project: grape
362
360
  rubygems_version: 1.8.24