sinatra-api 1.0.0

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/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format doc
3
+ --require spec_helper
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --protected -m markdown -r README.md
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2013 Algol Labs, LLC. <dev@algollabs.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to use,
6
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Sinatra::API
2
+
3
+ API parameter enforcing, coercion, and parameter-resource automatic resolution.
4
+
5
+ ## License
6
+
7
+ This gem is licensed under the MIT license. Copyright Algol Labs, LLC. 2013
@@ -0,0 +1,43 @@
1
+ # Copyright (c) 2013 Algol Labs, LLC. <dev@algollabs.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a
4
+ # copy of this software and associated documentation files (the "Software"),
5
+ # to deal in the Software without restriction, including without limitation
6
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ # and/or sell copies of the Software, and to permit persons to whom the
8
+ # Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ #
21
+
22
+ module Sinatra
23
+ module API
24
+ module Callbacks
25
+ attr_accessor :api_callbacks
26
+
27
+ def self.extended(base)
28
+ base.api_callbacks = {}
29
+ end
30
+
31
+ def on(event, &callback)
32
+ (self.api_callbacks[event.to_sym] ||= []) << callback
33
+ end
34
+
35
+ def trigger(event, *args)
36
+ callbacks = self.api_callbacks[event.to_sym] || []
37
+ callbacks.each do |callback|
38
+ callback.call(*args)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,235 @@
1
+ # Copyright (c) 2013 Algol Labs, LLC. <dev@algollabs.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a
4
+ # copy of this software and associated documentation files (the "Software"),
5
+ # to deal in the Software without restriction, including without limitation
6
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ # and/or sell copies of the Software, and to permit persons to whom the
8
+ # Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ #
21
+
22
+ module Sinatra
23
+ # TODO: accept nested parameters
24
+ module API
25
+ public
26
+
27
+ # attr_accessor :resource_aliases
28
+
29
+ module Helpers
30
+ def api_call?
31
+ (request.accept || '').to_s.include?('json') ||
32
+ (request.content_type||'').to_s.include?('json')
33
+ end
34
+
35
+ # Define the required API arguments map. Any item defined
36
+ # not found in the supplied parameters of the API call will
37
+ # result in a 400 RC with a proper message marking the missing
38
+ # field.
39
+ #
40
+ # The map is a Hash of parameter keys and optional validator blocks.
41
+ #
42
+ # @example A map of required API call arguments
43
+ # api_required!({ title: nil, user_id: nil })
44
+ #
45
+ # Each entry can be optionally mapped to a validation proc that will
46
+ # be invoked *if* the field was supplied. The proc will be passed
47
+ # the value of the field.
48
+ #
49
+ # If the value is invalid and you need to suspend the request, you
50
+ # must return a String object with an appropriate error message.
51
+ #
52
+ # @example Rejecting a title if it's rude
53
+ # api_required!({
54
+ # :title => lambda { |t| return "Don't be rude" if t && t =~ /rude/ }
55
+ # })
56
+ #
57
+ # @note
58
+ # The supplied value passed to validation blocks is not pre-processed,
59
+ # so you must make sure that you check for nils or bad values in validator blocks!
60
+ def api_required!(args, h = params)
61
+ args.each_pair { |name, cnd|
62
+ if cnd.is_a?(Hash)
63
+ api_required!(cnd, h[name])
64
+ next
65
+ end
66
+
67
+ parse_api_argument(h, name, cnd, :required)
68
+ }
69
+ end
70
+
71
+ # Same as #api_required! except that fields defined in this map
72
+ # are optional and will be used only if they're supplied.
73
+ #
74
+ # @see #api_required!
75
+ def api_optional!(args, h = params)
76
+ args.each_pair { |name, cnd|
77
+ if cnd.is_a?(Hash)
78
+ api_optional!(cnd, h[name])
79
+ next
80
+ end
81
+
82
+ parse_api_argument(h, name, cnd, :optional)
83
+ }
84
+ end
85
+
86
+ # Consumes supplied parameters with the given keys from the API
87
+ # parameter map, and yields the consumed values for processing by
88
+ # the supplied block (if any).
89
+ #
90
+ # This is useful if:
91
+ # 1. a certain parameter does not correspond to a model attribute
92
+ # and needs to be renamed, or is used in a validation context
93
+ # 2. the data needs special treatment
94
+ # 3. the data needs to be (re)formatted
95
+ #
96
+ def api_consume!(keys)
97
+ out = nil
98
+
99
+ keys = [ keys ] unless keys.is_a?(Array)
100
+ keys.each do |k|
101
+ if val = @api[:required].delete(k.to_sym)
102
+ out = val
103
+ out = yield(val) if block_given?
104
+ end
105
+
106
+ if val = @api[:optional].delete(k.to_sym)
107
+ out = val
108
+ out = yield(val) if block_given?
109
+ end
110
+ end
111
+
112
+ out
113
+ end
114
+
115
+ def api_transform!(key, &handler)
116
+ if val = @api[:required][key.to_sym]
117
+ @api[:required][key.to_sym] = yield(val) if block_given?
118
+ end
119
+
120
+ if val = @api[:optional][key.to_sym]
121
+ @api[:optional][key.to_sym] = yield(val) if block_given?
122
+ end
123
+ end
124
+
125
+ def api_has_param?(key)
126
+ @api[:optional].has_key?(key)
127
+ end
128
+
129
+ def api_param(key)
130
+ @api[:optional][key.to_sym] || @api[:required][key.to_sym]
131
+ end
132
+
133
+ # Returns a Hash of the *supplied* request parameters. Rejects
134
+ # any parameter that was not defined in the REQUIRED or OPTIONAL
135
+ # maps (or was consumed).
136
+ #
137
+ # @param Hash q A Hash of attributes to merge with the parameters,
138
+ # useful for defining defaults
139
+ def api_params(q = {})
140
+ @api[:optional].deep_merge(@api[:required]).deep_merge(q)
141
+ end
142
+
143
+ def api_clear!()
144
+ @api = { required: {}, optional: {} }
145
+ end
146
+
147
+ alias_method :api_reset!, :api_clear!
148
+
149
+ # Attempt to locate a resource based on an ID supplied in a request parameter.
150
+ #
151
+ # If the param map contains a resource id (ie, :folder_id),
152
+ # we attempt to locate and expose it to the route.
153
+ #
154
+ # A 404 is raised if:
155
+ # 1. the scope is missing (@space for folder, @space or @folder for page)
156
+ # 2. the resource couldn't be identified in its scope (@space or @folder)
157
+ #
158
+ # If the resources were located, they're accessible using @folder or @page.
159
+ #
160
+ # The route can be halted using the :requires => [] condition when it expects
161
+ # a resource.
162
+ #
163
+ # @example using :requires to reject a request with an invalid @page
164
+ # get '/folders/:folder_id/pages/:page_id', :requires => [ :page ] do
165
+ # @page.show # page is good
166
+ # @folder.show # so is its folder
167
+ # end
168
+ #
169
+ def __api_locate_resource(r, container = nil)
170
+
171
+ resource_id = params[r + '_id'].to_i
172
+ rklass = r.camelize
173
+
174
+ collection = case
175
+ when container.nil?; eval "#{ResourcePrefix}#{rklass}"
176
+ else; container.send("#{r.to_plural}")
177
+ end
178
+
179
+ puts "locating resource #{r} with id #{resource_id} from #{collection} [#{container}]"
180
+
181
+ resource = collection.get(resource_id)
182
+
183
+ if !resource
184
+ m = "No such resource: #{rklass}##{resource_id}"
185
+ if container
186
+ m << " in #{container.class.name.to_s}##{container.id}"
187
+ end
188
+
189
+ halt 404, m
190
+ end
191
+
192
+ if respond_to?(:can?)
193
+ unless can? :access, resource
194
+ halt 403, "You do not have access to this #{rklass} resource."
195
+ end
196
+ end
197
+
198
+ instance_variable_set('@'+r, resource)
199
+
200
+ API.trigger :resource_located, resource, r
201
+
202
+ API.aliases_for(r).each { |resource_alias|
203
+ instance_variable_set('@'+resource_alias, resource)
204
+ puts "API: resource #{rklass} exported to @#{resource_alias}"
205
+ }
206
+
207
+ resource
208
+ end
209
+
210
+ private
211
+
212
+ def parse_api_argument(h = params, name, cnd, type)
213
+ cnd ||= lambda { |*_| true }
214
+ name = name.to_s
215
+
216
+ unless [:required, :optional].include?(type)
217
+ raise ArgumentError, 'API Argument type must be either :required or :optional'
218
+ end
219
+
220
+ if !h.has_key?(name)
221
+ if type == :required
222
+ halt 400, "Missing required parameter :#{name}"
223
+ end
224
+ else
225
+ if cnd.respond_to?(:call)
226
+ errmsg = cnd.call(h[name])
227
+ halt 400, { :"#{name}" => errmsg } if errmsg && errmsg.is_a?(String)
228
+ end
229
+
230
+ @api[type][name.to_sym] = h[name]
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,36 @@
1
+ module Sinatra
2
+ module API
3
+ module ResourceAliases
4
+ attr_accessor :resource_aliases
5
+
6
+ def self.extended(base)
7
+ base.resource_aliases = {}
8
+ base.on :resource_located do |resource, name|
9
+ base.aliases_for(name).each do |resource_alias|
10
+ instance_variable_set("@#{resource_alias}", resource)
11
+ end
12
+ end
13
+ end
14
+
15
+ def alias_resource(original, resource_alias)
16
+ key, resource_alias = original.to_sym, resource_alias.to_s
17
+
18
+ self.resource_aliases[key] ||= []
19
+
20
+ return if self.resource_aliases[key].include?(resource_alias)
21
+ return if key.to_s == resource_alias
22
+
23
+ self.resource_aliases[key] << resource_alias
24
+ logger.debug "API resource #{original} is now aliased as #{resource_alias}"
25
+ end
26
+
27
+ def aliases_for(resource)
28
+ self.resource_aliases[resource.to_sym] || []
29
+ end
30
+
31
+ def reset_aliases!
32
+ self.resource_aliases = {}
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ # Copyright (c) 2013 Algol Labs, LLC. <dev@algollabs.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a
4
+ # copy of this software and associated documentation files (the "Software"),
5
+ # to deal in the Software without restriction, including without limitation
6
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ # and/or sell copies of the Software, and to permit persons to whom the
8
+ # Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ #
21
+
22
+ module Sinatra
23
+ module API
24
+ VERSION = "1.0.0"
25
+ end
26
+ end
@@ -0,0 +1,93 @@
1
+ # Copyright (c) 2013 Algol Labs, LLC. <dev@algollabs.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a
4
+ # copy of this software and associated documentation files (the "Software"),
5
+ # to deal in the Software without restriction, including without limitation
6
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ # and/or sell copies of the Software, and to permit persons to whom the
8
+ # Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
22
+
23
+ gem_root = File.join(File.expand_path(File.dirname(__FILE__)), '..', '..')
24
+
25
+ require 'json'
26
+ require 'sinatra/base'
27
+ require 'active_support/core_ext/hash'
28
+ require 'active_support/core_ext/string'
29
+ require 'sinatra/api/version'
30
+ require 'sinatra/api/callbacks'
31
+ require 'sinatra/api/helpers'
32
+ require 'sinatra/api/resource_aliases'
33
+
34
+ module Sinatra
35
+ module API
36
+ extend Callbacks
37
+ extend ResourceAliases
38
+
39
+ class << self
40
+ attr_accessor :logger
41
+ end
42
+
43
+ ResourcePrefix = '::'
44
+
45
+ # Parse a JSON construct from a string stream.
46
+ #
47
+ # Override this to use a custom JSON parser, if necessary.
48
+ #
49
+ # @param [String] stream The raw JSON stream.
50
+ #
51
+ # @return [Hash] A Hash of the parsed JSON.
52
+ def parse_json(stream)
53
+ ::JSON.parse(stream)
54
+ end
55
+
56
+ def self.registered(app)
57
+ self.logger = ActiveSupport::Logger.new(STDOUT)
58
+
59
+ app.helpers Helpers
60
+ app.before do
61
+ @api = { required: {}, optional: {} }
62
+ @parent_resource = nil
63
+
64
+ if api_call?
65
+ request.body.rewind
66
+ body = request.body.read.to_s || ''
67
+
68
+ unless body.empty?
69
+ begin
70
+ params.merge!(parse_json(body))
71
+ rescue ::JSON::ParserError => e
72
+ logger.warn e.message
73
+ logger.warn e.backtrace
74
+
75
+ halt 400, "Malformed JSON content"
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ app.set(:requires) do |*resources|
82
+ condition do
83
+ @required = resources.collect { |r| r.to_s }
84
+ @required.each do |r|
85
+ @parent_resource = __api_locate_resource(r, @parent_resource)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ register API
93
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(%W[#{File.dirname(__FILE__)} lib sinatra api version])
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'sinatra-api'
5
+ s.summary = 'Helpers for building RESTful APIs in Sinatra.'
6
+ s.version = Sinatra::API::VERSION
7
+ s.date = Time.now.strftime('%Y-%m-%d')
8
+ s.authors = [ 'Ahmad Amireh' ]
9
+ s.email = 'ahmad@algollabs.com'
10
+ s.homepage = 'https://github.com/amireh/sinatra-api'
11
+ s.files = Dir.glob("{lib,spec}/**/*.rb") +
12
+ [ 'LICENSE', 'README.md', '.rspec', '.yardopts', __FILE__ ]
13
+ s.has_rdoc = 'yard'
14
+ s.license = 'MIT'
15
+
16
+ s.required_ruby_version = '>= 1.9.3'
17
+
18
+ s.add_dependency 'json'
19
+ s.add_dependency 'sinatra'
20
+ s.add_dependency 'activesupport'
21
+
22
+ s.add_development_dependency 'rspec'
23
+ s.add_development_dependency 'rack-test'
24
+ s.add_development_dependency 'yard', '>= 0.8.0'
25
+ s.add_development_dependency 'gem-release', '>= 0.6.0'
26
+ end
@@ -0,0 +1,28 @@
1
+ class ModelAdapter
2
+ end
3
+
4
+ class CollectionAdapter
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def self.get(key)
10
+ return @model.new
11
+ end
12
+ end
13
+
14
+ class Item
15
+ def self.get(id)
16
+ return {} if id == 1
17
+ end
18
+
19
+ def sub_items
20
+ CollectionAdapter.new(SubItem)
21
+ end
22
+ end
23
+
24
+ class SubItem < ModelAdapter
25
+ def item
26
+ Item.new
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ module Router
2
+ class << self
3
+ def puts(*args)
4
+ super(*args) if $VERBOSE
5
+ end
6
+
7
+ # Locates routes defined for any verb containing the provided token.
8
+ #
9
+ # @param [String] token the token the route should contain
10
+ # @return [Array<Hash, Fixnum>] a map of all the verb routes, and the count of located routes
11
+ def routes_for(token)
12
+ all_routes = {}
13
+ count = 0
14
+ Sinatra::Application.routes.each do |verb_routes|
15
+ verb, routes = verb_routes[0], verb_routes[1]
16
+ all_routes[verb] ||= []
17
+ routes.each_with_index do |route, i|
18
+ route_regex = route.first.source
19
+ if route_regex.to_s.include?(token)
20
+ all_routes[verb] << route
21
+ count += 1
22
+
23
+ puts "Route located: #{verb} -> #{route_regex.to_s}"
24
+ end
25
+ end
26
+ all_routes[verb].uniq!
27
+ end
28
+ [ all_routes, count ]
29
+ end
30
+
31
+ def purge(token)
32
+ routes, nr_routes = *routes_for(token)
33
+
34
+ # puts "cleaning up #{nr_routes} routes"
35
+
36
+ routes.each_pair do |verb, vroutes|
37
+ vroutes.each do |r| delete_route(verb, r) end
38
+ end
39
+
40
+ yield(nr_routes) if block_given?
41
+
42
+ nr_routes
43
+ end
44
+
45
+ protected
46
+
47
+ def delete_route(verb, r)
48
+ verb_routes = Sinatra::Application.routes.select { |v| v == verb }.first
49
+
50
+ unless verb_routes
51
+ raise "Couldn't find routes for verb #{verb}, that's impossible"
52
+ end
53
+
54
+ unless verb_routes[1].delete(r)
55
+ raise "Route '#{r}' not found for verb #{verb}"
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,92 @@
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
@@ -0,0 +1,32 @@
1
+ describe Sinatra::API::ResourceAliases do
2
+ include_examples 'integration specs'
3
+ include_examples 'aliasing specs'
4
+
5
+ context 'processing aliases' do
6
+ before :each do
7
+ app.class_eval do
8
+ Sinatra::API::alias_resource :item, :item_alias
9
+ end
10
+ end
11
+
12
+ it 'should export a resource as an alias' do
13
+ app.get '/items/:item_id', requires: [ :item ] do
14
+ @item_alias.to_json
15
+ end
16
+
17
+ get '/items/1'
18
+ last_response.status.should == 200
19
+ last_response.body.should == {}.to_json
20
+ end
21
+
22
+ it 'should not affect the original resource' do
23
+ app.get '/items/:item_id', requires: [ :item ] do
24
+ @item.to_json
25
+ end
26
+
27
+ get '/items/1'
28
+ last_response.status.should == 200
29
+ last_response.body.should == {}.to_json
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..')
2
+
3
+ ENV['RACK_ENV'] = 'test'
4
+
5
+ require 'lib/sinatra/api'
6
+ require 'rspec'
7
+ require 'rack/test'
8
+
9
+ class SinatraAPITestApp < Sinatra::Base
10
+ register Sinatra::API
11
+ end
12
+
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
+ RSpec.configure do |config|
20
+ Thread.abort_on_exception = true
21
+
22
+ config.treat_symbols_as_metadata_keys_with_true_values = true
23
+ config.run_all_when_everything_filtered = true
24
+ 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
+ config.order = 'random'
31
+
32
+ include Rack::Test::Methods
33
+
34
+ def app
35
+ Sinatra::Application
36
+ end
37
+ end
38
+
39
+ Dir["./spec/{helpers,support}/**/*.rb"].sort.each { |f| require f }
@@ -0,0 +1,9 @@
1
+ shared_examples_for 'aliasing specs' do
2
+ before :each do
3
+ Sinatra::API.reset_aliases!
4
+
5
+ app.class_eval do
6
+ Sinatra::API.reset_aliases!
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ shared_examples_for "integration specs" do
2
+ before :each do
3
+ Router.purge('/')
4
+ end
5
+ end
@@ -0,0 +1,99 @@
1
+ describe "Helpers" do
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
99
+ end
@@ -0,0 +1,43 @@
1
+ describe Sinatra::API::ResourceAliases do
2
+ include_examples 'aliasing specs'
3
+
4
+ describe 'instance methods' do
5
+ context '#alias_resource' do
6
+ it 'should define a resource alias' do
7
+ Sinatra::API.alias_resource :item, :item_alias
8
+ Sinatra::API.resource_aliases[:item].should == [ 'item_alias' ]
9
+ end
10
+
11
+ it 'should define multiple resource aliases' do
12
+ Sinatra::API.alias_resource :item, :first_alias
13
+ Sinatra::API.alias_resource :item, :second_alias
14
+ Sinatra::API.resource_aliases[:item].should == %w[ first_alias second_alias ]
15
+ end
16
+
17
+ it 'should ignore duplicate aliases' do
18
+ Sinatra::API.alias_resource :item, :item_alias
19
+ Sinatra::API.alias_resource :item, :item_alias
20
+ Sinatra::API.resource_aliases[:item].should == [ 'item_alias' ]
21
+ end
22
+
23
+ it 'should ignore a meaningless alias' do
24
+ Sinatra::API.alias_resource :item, :item
25
+ Sinatra::API.resource_aliases[:item].should == []
26
+ end
27
+ end
28
+
29
+ context '#aliases_for' do
30
+ it 'should locate a resource alias' do
31
+ Sinatra::API.alias_resource :item, :item_alias
32
+ Sinatra::API.aliases_for(:item).should == [ 'item_alias' ]
33
+ end
34
+ end
35
+ end
36
+
37
+ context 'processing aliases' do
38
+ it 'should export a resource as an alias' do
39
+ Sinatra::API.alias_resource :item, :item_alias
40
+ Sinatra::API.trigger(:resource_located, Item.new, 'item')
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ahmad Amireh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sinatra
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: yard
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 0.8.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 0.8.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: gem-release
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.6.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.6.0
126
+ description:
127
+ email: ahmad@algollabs.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - lib/sinatra/api.rb
133
+ - lib/sinatra/api/callbacks.rb
134
+ - lib/sinatra/api/helpers.rb
135
+ - lib/sinatra/api/resource_aliases.rb
136
+ - lib/sinatra/api/version.rb
137
+ - spec/unit/callbacks_spec.rb
138
+ - spec/unit/resource_aliases_spec.rb
139
+ - spec/integration/helpers_spec.rb
140
+ - spec/integration/resource_aliases_spec.rb
141
+ - spec/support/integration_specs.rb
142
+ - spec/support/aliasing_specs.rb
143
+ - spec/helpers/fixtures.rb
144
+ - spec/helpers/router.rb
145
+ - spec/spec_helper.rb
146
+ - LICENSE
147
+ - README.md
148
+ - .rspec
149
+ - .yardopts
150
+ - sinatra-api.gemspec
151
+ homepage: https://github.com/amireh/sinatra-api
152
+ licenses:
153
+ - MIT
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: 1.9.3
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 1.8.23
173
+ signing_key:
174
+ specification_version: 3
175
+ summary: Helpers for building RESTful APIs in Sinatra.
176
+ test_files: []
177
+ has_rdoc: yard