sinatra-api 1.0.2 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.yardopts +1 -1
- data/lib/sinatra/api.rb +50 -27
- data/lib/sinatra/api/callbacks.rb +29 -0
- data/lib/sinatra/api/config.rb +42 -0
- data/lib/sinatra/api/error_handler.rb +54 -0
- data/lib/sinatra/api/helpers.rb +1 -0
- data/lib/sinatra/api/parameter_validator.rb +90 -0
- data/lib/sinatra/api/parameter_validators/float_validator.rb +17 -0
- data/lib/sinatra/api/parameter_validators/integer_validator.rb +17 -0
- data/lib/sinatra/api/parameter_validators/string_validator.rb +17 -0
- data/lib/sinatra/api/parameters.rb +62 -20
- data/lib/sinatra/api/resource_aliases.rb +1 -1
- data/lib/sinatra/api/resources.rb +22 -3
- data/lib/sinatra/api/version.rb +1 -1
- data/spec/helpers/api_calls.rb +33 -0
- data/spec/helpers/rack_test_api_response.rb +45 -0
- data/spec/helpers/rspec_api_response_matchers.rb +135 -0
- data/spec/integration/parameter_validators/float_validator_spec.rb +85 -0
- data/spec/integration/parameter_validators/integer_validator_spec.rb +66 -0
- data/spec/integration/parameter_validators/string_validator_spec.rb +25 -0
- data/spec/integration/parameter_validators_spec.rb +3 -0
- data/spec/integration/parameters_spec.rb +144 -0
- data/spec/integration/resources_spec.rb +17 -0
- data/spec/spec_helper.rb +5 -11
- data/spec/unit/callbacks_spec.rb +1 -98
- metadata +16 -2
- data/spec/integration/helpers_spec.rb +0 -92
@@ -0,0 +1,144 @@
|
|
1
|
+
describe Sinatra::API::Parameters do
|
2
|
+
include_examples 'integration specs'
|
3
|
+
|
4
|
+
context 'defining parameters' do
|
5
|
+
context 'required parameters' do
|
6
|
+
it 'should define a required parameter using hash style' do
|
7
|
+
app.get '/' do
|
8
|
+
api_required!({
|
9
|
+
id: nil
|
10
|
+
})
|
11
|
+
end
|
12
|
+
|
13
|
+
get '/'
|
14
|
+
last_response.status.should == 400
|
15
|
+
last_response.body.should match(/Missing required parameter :id/)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should define required parameters using list style' do
|
19
|
+
app.post '/' do
|
20
|
+
api_required!([ :id, :name ])
|
21
|
+
end
|
22
|
+
|
23
|
+
post '/'
|
24
|
+
last_response.status.should == 400
|
25
|
+
last_response.body.should match(/missing required parameter :id/i)
|
26
|
+
|
27
|
+
post '/', { id: 10 }.to_json
|
28
|
+
last_response.status.should == 400
|
29
|
+
last_response.body.should match(/missing required parameter :name/i)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should define a single required parameter' do
|
33
|
+
app.get '/' do
|
34
|
+
api_parameter! :id, required: true
|
35
|
+
end
|
36
|
+
|
37
|
+
get '/'
|
38
|
+
last_response.status.should == 400
|
39
|
+
last_response.body.should match(/Missing required parameter :id/)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'optional parameters' do
|
44
|
+
it 'should define an optional parameter using hash style' do
|
45
|
+
app.get '/' do
|
46
|
+
api_optional!({
|
47
|
+
id: nil
|
48
|
+
})
|
49
|
+
end
|
50
|
+
|
51
|
+
get '/'
|
52
|
+
last_response.status.should == 200
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should define a single required parameter' do
|
56
|
+
app.get '/' do
|
57
|
+
api_parameter! :id
|
58
|
+
end
|
59
|
+
|
60
|
+
get '/'
|
61
|
+
last_response.status.should == 200
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should define optional parameters using list style' do
|
65
|
+
app.post '/' do
|
66
|
+
api_optional! [ :id, :name ]
|
67
|
+
api_params.to_json
|
68
|
+
end
|
69
|
+
|
70
|
+
post '/', { id: 5, name: 'test' }.to_json
|
71
|
+
last_response.status.should == 200
|
72
|
+
last_response.body.should == { id: 5, name: 'test' }.to_json
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should reject a request missing a required parameter" do
|
78
|
+
app.get '/' do
|
79
|
+
api_required! [ :id ]
|
80
|
+
end
|
81
|
+
|
82
|
+
get '/'
|
83
|
+
last_response.status.should == 400
|
84
|
+
last_response.body.should match(/Missing required parameter :id/)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should accept a request satisfying required parameters" do
|
88
|
+
app.get '/' do
|
89
|
+
api_required! [ :id ]
|
90
|
+
end
|
91
|
+
|
92
|
+
get '/', { id: 5 }
|
93
|
+
last_response.status.should == 200
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should accept a request not satisfying optional parameters" do
|
97
|
+
app.get '/' do
|
98
|
+
api_required! [ :id ]
|
99
|
+
api_optional! [ :name ]
|
100
|
+
end
|
101
|
+
|
102
|
+
get '/', { id: 5 }
|
103
|
+
last_response.status.should == 200
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should apply parameter conditions" do
|
107
|
+
app.get '/' do
|
108
|
+
api_optional!({
|
109
|
+
name: lambda { |v|
|
110
|
+
unless (v || '').match /ahmad/
|
111
|
+
"Unexpected name."
|
112
|
+
end
|
113
|
+
}
|
114
|
+
})
|
115
|
+
end
|
116
|
+
|
117
|
+
get '/', { name: 'foobar' }
|
118
|
+
last_response.status.should == 400
|
119
|
+
last_response.body.should match(/Unexpected name/)
|
120
|
+
|
121
|
+
get '/', { name: 'ahmad' }
|
122
|
+
last_response.status.should == 200
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should pick parameters" do
|
126
|
+
app.get '/' do
|
127
|
+
api_optional!({
|
128
|
+
name: nil
|
129
|
+
})
|
130
|
+
|
131
|
+
api_params.to_json
|
132
|
+
end
|
133
|
+
|
134
|
+
get '/', {
|
135
|
+
name: 'foobar',
|
136
|
+
some: 'thing'
|
137
|
+
}
|
138
|
+
|
139
|
+
last_response.body.should == {
|
140
|
+
name: 'foobar'
|
141
|
+
}.to_json
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
describe Sinatra::API::Resources do
|
2
|
+
include_examples 'integration specs'
|
3
|
+
|
4
|
+
it 'should locate a resource' do
|
5
|
+
app.get '/items/:item_id', requires: [ :item ] do
|
6
|
+
@item.to_json
|
7
|
+
end
|
8
|
+
|
9
|
+
get '/items/1'
|
10
|
+
last_response.status.should == 200
|
11
|
+
last_response.body.should == {}.to_json
|
12
|
+
|
13
|
+
get '/items/2'
|
14
|
+
last_response.status.should == 404
|
15
|
+
last_response.body.should match /No such resource/
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,25 +8,19 @@ require 'rack/test'
|
|
8
8
|
|
9
9
|
class SinatraAPITestApp < Sinatra::Base
|
10
10
|
register Sinatra::API
|
11
|
+
Sinatra::API.configure({
|
12
|
+
verbose: false,
|
13
|
+
with_errors: true
|
14
|
+
})
|
15
|
+
|
11
16
|
end
|
12
17
|
|
13
|
-
# This file was generated by the `rspec --init` command. Conventionally, all
|
14
|
-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
15
|
-
# Require this file using `require "spec_helper"` to ensure that it is only
|
16
|
-
# loaded once.
|
17
|
-
#
|
18
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
18
|
RSpec.configure do |config|
|
20
19
|
Thread.abort_on_exception = true
|
21
20
|
|
22
21
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
23
22
|
config.run_all_when_everything_filtered = true
|
24
23
|
config.filter_run :focus => true
|
25
|
-
|
26
|
-
# Run specs in random order to surface order dependencies. If you find an
|
27
|
-
# order dependency and want to debug it, you can fix the order by providing
|
28
|
-
# the seed, which is printed after each run.
|
29
|
-
# --seed 1234
|
30
24
|
config.order = 'random'
|
31
25
|
|
32
26
|
include Rack::Test::Methods
|
data/spec/unit/callbacks_spec.rb
CHANGED
@@ -1,99 +1,2 @@
|
|
1
|
-
describe
|
2
|
-
before :each do
|
3
|
-
Router.purge('/')
|
4
|
-
end
|
5
|
-
|
6
|
-
it "should reject a request missing a required parameter" do
|
7
|
-
app.get '/' do
|
8
|
-
api_required!({
|
9
|
-
id: nil
|
10
|
-
})
|
11
|
-
end
|
12
|
-
|
13
|
-
get '/'
|
14
|
-
last_response.status.should == 400
|
15
|
-
last_response.body.should match(/Missing required parameter :id/)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should accept a request satisfying required parameters" do
|
19
|
-
app.get '/' do
|
20
|
-
api_required!({
|
21
|
-
id: nil
|
22
|
-
})
|
23
|
-
end
|
24
|
-
|
25
|
-
get '/', { id: 5 }
|
26
|
-
last_response.status.should == 200
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should accept a request not satisfying optional parameters" do
|
30
|
-
app.get '/' do
|
31
|
-
api_required!({
|
32
|
-
id: nil
|
33
|
-
})
|
34
|
-
api_optional!({
|
35
|
-
name: nil
|
36
|
-
})
|
37
|
-
end
|
38
|
-
|
39
|
-
get '/', { id: 5 }
|
40
|
-
last_response.status.should == 200
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should apply parameter conditions" do
|
44
|
-
app.get '/' do
|
45
|
-
api_optional!({
|
46
|
-
name: lambda { |v|
|
47
|
-
unless (v || '').match /ahmad/
|
48
|
-
"Unexpected name."
|
49
|
-
end
|
50
|
-
}
|
51
|
-
})
|
52
|
-
end
|
53
|
-
|
54
|
-
get '/', { name: 'foobar' }
|
55
|
-
last_response.status.should == 400
|
56
|
-
last_response.body.should match(/Unexpected name/)
|
57
|
-
|
58
|
-
get '/', { name: 'ahmad' }
|
59
|
-
last_response.status.should == 200
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should pick parameters" do
|
63
|
-
app.get '/' do
|
64
|
-
api_optional!({
|
65
|
-
name: nil
|
66
|
-
})
|
67
|
-
|
68
|
-
api_params.to_json
|
69
|
-
end
|
70
|
-
|
71
|
-
get '/', {
|
72
|
-
name: 'foobar',
|
73
|
-
some: 'thing'
|
74
|
-
}
|
75
|
-
|
76
|
-
last_response.body.should == {
|
77
|
-
name: 'foobar'
|
78
|
-
}.to_json
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should locate a resource" do
|
82
|
-
app.get '/items/:item_id', requires: [ :item ] do
|
83
|
-
@item.to_json
|
84
|
-
end
|
85
|
-
|
86
|
-
get '/items/1'
|
87
|
-
last_response.status.should == 200
|
88
|
-
last_response.body.should == {}.to_json
|
89
|
-
|
90
|
-
get '/items/2'
|
91
|
-
last_response.status.should == 404
|
92
|
-
last_response.body.should match /No such resource/
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should define a resource alias" do
|
96
|
-
Sinatra::API.alias_resource :item, :item_alias
|
97
|
-
Sinatra::API.aliases_for(:item).should == [ 'item_alias' ]
|
98
|
-
end
|
1
|
+
describe Sinatra::API::Callbacks do
|
99
2
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -130,21 +130,35 @@ extensions: []
|
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
132
|
- lib/sinatra/api.rb
|
133
|
+
- lib/sinatra/api/config.rb
|
133
134
|
- lib/sinatra/api/callbacks.rb
|
134
135
|
- lib/sinatra/api/helpers.rb
|
135
136
|
- lib/sinatra/api/parameters.rb
|
137
|
+
- lib/sinatra/api/parameter_validator.rb
|
136
138
|
- lib/sinatra/api/resource_aliases.rb
|
137
139
|
- lib/sinatra/api/resources.rb
|
140
|
+
- lib/sinatra/api/error_handler.rb
|
138
141
|
- lib/sinatra/api/version.rb
|
142
|
+
- lib/sinatra/api/parameter_validators/string_validator.rb
|
143
|
+
- lib/sinatra/api/parameter_validators/float_validator.rb
|
144
|
+
- lib/sinatra/api/parameter_validators/integer_validator.rb
|
139
145
|
- spec/unit/callbacks_spec.rb
|
140
146
|
- spec/unit/resource_aliases_spec.rb
|
147
|
+
- spec/integration/parameters_spec.rb
|
148
|
+
- spec/integration/parameter_validators_spec.rb
|
141
149
|
- spec/integration/api_spec.rb
|
142
|
-
- spec/integration/
|
150
|
+
- spec/integration/resources_spec.rb
|
143
151
|
- spec/integration/resource_aliases_spec.rb
|
152
|
+
- spec/integration/parameter_validators/integer_validator_spec.rb
|
153
|
+
- spec/integration/parameter_validators/float_validator_spec.rb
|
154
|
+
- spec/integration/parameter_validators/string_validator_spec.rb
|
144
155
|
- spec/support/integration_specs.rb
|
145
156
|
- spec/support/aliasing_specs.rb
|
146
157
|
- spec/helpers/fixtures.rb
|
158
|
+
- spec/helpers/api_calls.rb
|
147
159
|
- spec/helpers/router.rb
|
160
|
+
- spec/helpers/rack_test_api_response.rb
|
161
|
+
- spec/helpers/rspec_api_response_matchers.rb
|
148
162
|
- spec/spec_helper.rb
|
149
163
|
- LICENSE
|
150
164
|
- README.md
|
@@ -1,92 +0,0 @@
|
|
1
|
-
describe Sinatra::API::Helpers do
|
2
|
-
include_examples 'integration specs'
|
3
|
-
|
4
|
-
it "should reject a request missing a required parameter" do
|
5
|
-
app.get '/' do
|
6
|
-
api_required!({
|
7
|
-
id: nil
|
8
|
-
})
|
9
|
-
end
|
10
|
-
|
11
|
-
get '/'
|
12
|
-
last_response.status.should == 400
|
13
|
-
last_response.body.should match(/Missing required parameter :id/)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should accept a request satisfying required parameters" do
|
17
|
-
app.get '/' do
|
18
|
-
api_required!({
|
19
|
-
id: nil
|
20
|
-
})
|
21
|
-
end
|
22
|
-
|
23
|
-
get '/', { id: 5 }
|
24
|
-
last_response.status.should == 200
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should accept a request not satisfying optional parameters" do
|
28
|
-
app.get '/' do
|
29
|
-
api_required!({
|
30
|
-
id: nil
|
31
|
-
})
|
32
|
-
api_optional!({
|
33
|
-
name: nil
|
34
|
-
})
|
35
|
-
end
|
36
|
-
|
37
|
-
get '/', { id: 5 }
|
38
|
-
last_response.status.should == 200
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should apply parameter conditions" do
|
42
|
-
app.get '/' do
|
43
|
-
api_optional!({
|
44
|
-
name: lambda { |v|
|
45
|
-
unless (v || '').match /ahmad/
|
46
|
-
"Unexpected name."
|
47
|
-
end
|
48
|
-
}
|
49
|
-
})
|
50
|
-
end
|
51
|
-
|
52
|
-
get '/', { name: 'foobar' }
|
53
|
-
last_response.status.should == 400
|
54
|
-
last_response.body.should match(/Unexpected name/)
|
55
|
-
|
56
|
-
get '/', { name: 'ahmad' }
|
57
|
-
last_response.status.should == 200
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should pick parameters" do
|
61
|
-
app.get '/' do
|
62
|
-
api_optional!({
|
63
|
-
name: nil
|
64
|
-
})
|
65
|
-
|
66
|
-
api_params.to_json
|
67
|
-
end
|
68
|
-
|
69
|
-
get '/', {
|
70
|
-
name: 'foobar',
|
71
|
-
some: 'thing'
|
72
|
-
}
|
73
|
-
|
74
|
-
last_response.body.should == {
|
75
|
-
name: 'foobar'
|
76
|
-
}.to_json
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should locate a resource" do
|
80
|
-
app.get '/items/:item_id', requires: [ :item ] do
|
81
|
-
@item.to_json
|
82
|
-
end
|
83
|
-
|
84
|
-
get '/items/1'
|
85
|
-
last_response.status.should == 200
|
86
|
-
last_response.body.should == {}.to_json
|
87
|
-
|
88
|
-
get '/items/2'
|
89
|
-
last_response.status.should == 404
|
90
|
-
last_response.body.should match /No such resource/
|
91
|
-
end
|
92
|
-
end
|