sinatra-browse 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +165 -0
- data/README.md +113 -0
- data/lib/sinatra/browse/format.rb +50 -0
- data/lib/sinatra/browse/route.rb +101 -0
- data/lib/sinatra/browse.rb +90 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 299a354c585ed02f7ae75c3b8ffbc0ce5554d439
|
4
|
+
data.tar.gz: 0ffaa6e1f4a3c0315f556ef89a42f548d4701ec0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb3763dcf26d9b9d6538f462e53d47289dfc347b7923131ac8545055f96d9346ccd41bdc30667b1b4dba95954c23915d1cf52ba519febf54d8b5b642f672d03f
|
7
|
+
data.tar.gz: 8ad00f11b3add8ad9f124f1f1f1b4a86d19ed8d07cba4cd713324062e61bbd891df278065c2a6107e2e2a1001d6a62b73ee96b72a3599481fc9b1f7fb4aac115
|
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# sinatra-browse
|
2
|
+
|
3
|
+
Self documenting parameter declaration framework for Sinatra.
|
4
|
+
|
5
|
+
## What problem do we solve?
|
6
|
+
|
7
|
+
Sinatra has tools to define `GET`, `POST`, `DELETE` etc. routes. It doesn't have any tools to define their parameters.
|
8
|
+
|
9
|
+
There exist several frameworks that generate documentation based on comment blocks above routes. The problem is you have to update these every time something is changed in the code. We have seen programmers forget this in too many projects.
|
10
|
+
|
11
|
+
## How do we solve it?
|
12
|
+
|
13
|
+
We believe in using actual code as documentation. Take a look at the following example.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require "sinatra/base"
|
17
|
+
require "sinatra/browse"
|
18
|
+
|
19
|
+
class App < Sinatra::Base
|
20
|
+
register Sinatra::Browse
|
21
|
+
|
22
|
+
description "Creates a new user"
|
23
|
+
param :display_name, :String, required: true
|
24
|
+
param :type, :String, in: ["admin", "moderator", "user"], default: user
|
25
|
+
param :age, :Integer
|
26
|
+
param :gender, :String, in: ["m", "w", "M", "W"], transform: :upcase
|
27
|
+
param :activated, :Boolean, default: false
|
28
|
+
param :email, :String, format: /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/
|
29
|
+
post "/users" do
|
30
|
+
# ... some code that creates a new user
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Here we have a clear list of what parameters are expected and how they are validated. Since this is code and not a comment block, it will always be up to date with the behaviour of our application.
|
36
|
+
|
37
|
+
**Question:** But we don't want to go look at source code just to get api documentation. What do we do?
|
38
|
+
|
39
|
+
Sinatra-browse automatically generates documentation from your parameter validations. Just surf to the following url in your browser. *http://<api_ip_address>:<api_port>/browse*
|
40
|
+
|
41
|
+
The syntax is inspired by the [sinatra-param](https://github.com/mattt/sinatra-param) and [thor](https://github.com/erikhuda/thor) projects.
|
42
|
+
|
43
|
+
## Parameter types
|
44
|
+
|
45
|
+
At the time of writing four parameter types are available.
|
46
|
+
|
47
|
+
* `:String`
|
48
|
+
* `:Integer`
|
49
|
+
* `:Float`
|
50
|
+
* `:Boolean` ["1/0", "true/false", "t/f", "yes/no", "y/n"]
|
51
|
+
|
52
|
+
## Default values
|
53
|
+
|
54
|
+
You can set default values in your declarations. These will be used when the parameter in question wasn't provided in the request. You can either set the default value or provide a proc to generate it.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
param :media_type, :String, default: "book"
|
58
|
+
param :year, :Integer, default: lambda { Time.now.year }
|
59
|
+
```
|
60
|
+
|
61
|
+
## Parameter validation
|
62
|
+
|
63
|
+
You can write some quick validation logic directly in the parameter declaration. If the validation fails, either a standard 400 error will be returned or a custom error block will execute if provided.
|
64
|
+
|
65
|
+
`required` Fails if this parameter is not provided in the request.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
param :you_must_include_me, :String, required: true
|
69
|
+
```
|
70
|
+
|
71
|
+
`depends_on` Some times there are parameters that are required by other parameters. Use `depends_on` to implement this. The example below will allow you to send *post_something* without any other parameters. If you send *user_name* though, you will be required to send *password* along with it.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
param :post_something, :String
|
75
|
+
param :user_name, :String, depends_on: :password
|
76
|
+
param :password, :String
|
77
|
+
```
|
78
|
+
|
79
|
+
`in` Fails if this parameter is not included in the values set by `in`. You can use anything that responds to *.member?* like an array or a range.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
param :single_digit, :Integer, in: 1..9
|
83
|
+
param :small_prime_number, :Integer, in: Prime.take(10)
|
84
|
+
param :order, :String, in: ["ascending", "descending"]
|
85
|
+
```
|
86
|
+
|
87
|
+
`format` This validation is only for parameters of type `:String`. You can pass a regular expression that the string provided must match to.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
param :alphanumeric, :String, format: /^[0-9A-Za-z]*$/
|
91
|
+
```
|
92
|
+
|
93
|
+
## Error handling
|
94
|
+
|
95
|
+
When a validation fails, a standard 400 error will be returned. You can override this and do your own error handling using `on_error`.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
param :lets_fail, :Integer, in: 1..9, on_error: proc { halt 404, "if you're not giving us a number between 1 and 9, we're going to pretend not to be here!" }
|
99
|
+
get 'example_route' do
|
100
|
+
# This is the scope that the on_error proc will be executed in.
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
If a request is made that fails validation on the *lets_fail* parameter, then the proc provided to `on_error` will be called **in the same scope as your route**. Therefore you have access to Sinatra keywords such as *halt*.
|
105
|
+
|
106
|
+
## Parameter transformation
|
107
|
+
|
108
|
+
You can use transform to execute a quick method on any prameter provided. Anything that will respond to *to_proc* will do.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
param :only_caps, :String, transform :upcase
|
112
|
+
param :power_of_two, :Integer, transform: proc { |n| n * n }
|
113
|
+
```
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module Sinatra::Browse
|
6
|
+
def self.format(f, browse_routes)
|
7
|
+
case f
|
8
|
+
when "kusohtml"
|
9
|
+
KusoHtml.new(browse_routes)
|
10
|
+
when "json"
|
11
|
+
JSON.new(browse_routes)
|
12
|
+
when "yaml"
|
13
|
+
YAML.new(browse_routes)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class BrowseFormat
|
18
|
+
def initialize(browse_routes)
|
19
|
+
@browse_routes = browse_routes
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class KusoHtml < BrowseFormat
|
24
|
+
def generate
|
25
|
+
output = ""
|
26
|
+
@browse_routes.each { |name, route|
|
27
|
+
output += "<h3>#{name}</h3>"
|
28
|
+
output += "<p>#{route.description}</p><ul>"
|
29
|
+
route.parameters.each { |param_key, param_value|
|
30
|
+
output += "<li>#{param_key} #{param_value.to_s}</li>"
|
31
|
+
}
|
32
|
+
output += "</ul>"
|
33
|
+
}
|
34
|
+
output
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class JSON < BrowseFormat
|
39
|
+
def generate
|
40
|
+
@browse_routes.values.map { |br| br.to_hash }.to_json
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class YAML < BrowseFormat
|
45
|
+
def generate
|
46
|
+
@browse_routes.values.map { |br| br.to_hash }.to_yaml
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Sinatra::Browse
|
4
|
+
class Route
|
5
|
+
attr_reader :parameters
|
6
|
+
attr_reader :name
|
7
|
+
attr_reader :match
|
8
|
+
attr_reader :description
|
9
|
+
|
10
|
+
class ValidationError < Exception; end
|
11
|
+
|
12
|
+
# This is here because we're using the name as the keys for the
|
13
|
+
# _browse_routes hash. We want to build it outside of this class for that.
|
14
|
+
def self.build_name(request_method, path_info)
|
15
|
+
"#{request_method} #{path_info}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(request_method, path_info, description, parameters = nil)
|
19
|
+
@name = build_name(request_method, path_info)
|
20
|
+
@match = build_match(request_method, path_info)
|
21
|
+
@description = description
|
22
|
+
@parameters = parameters || {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_hash
|
26
|
+
{name: @name, description: @description}.merge @parameters
|
27
|
+
end
|
28
|
+
|
29
|
+
def matches?(request_method, path_info)
|
30
|
+
!! (build_name(request_method,path_info) =~ @match)
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_parameter?(parameter)
|
34
|
+
@parameters.has_key?(parameter.to_sym)
|
35
|
+
end
|
36
|
+
|
37
|
+
def coerce_type(params)
|
38
|
+
@parameters.each { |k,v|
|
39
|
+
params[k] &&= case v[:type]
|
40
|
+
when :Boolean
|
41
|
+
cast_to_boolean(params[k])
|
42
|
+
else
|
43
|
+
send(v[:type], params[k])
|
44
|
+
end
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def set_defaults(params)
|
49
|
+
@parameters.each { |k,v|
|
50
|
+
unless params[k] || v[:default].nil?
|
51
|
+
params[k] = v[:default].is_a?(Proc) ? v[:default].call(params[k]) : v[:default]
|
52
|
+
end
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete_undefined(params)
|
57
|
+
params.delete_if { |i| !self.has_parameter?(i) }
|
58
|
+
end
|
59
|
+
|
60
|
+
def validate(params)
|
61
|
+
@parameters.each { |k,v|
|
62
|
+
return fail_validation v, :required if !params[k] && v[:required]
|
63
|
+
if params[k]
|
64
|
+
return fail_validation v, :depends_on if v[:depends_on] && !params[v[:depends_on]]
|
65
|
+
return fail_validation v, :in if v[:in] && !v[:in].member?(params[k])
|
66
|
+
return fail_validation v, :format if v[:type] == :String && v[:format] && !(params[k] =~ v[:format])
|
67
|
+
end
|
68
|
+
}
|
69
|
+
|
70
|
+
{success: true}
|
71
|
+
end
|
72
|
+
|
73
|
+
def transform(params)
|
74
|
+
@parameters.each { |k,v|
|
75
|
+
params[k] = v[:transform].to_proc.call(params[k]) if params[k] && v[:transform]
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
def cast_to_boolean(param)
|
81
|
+
case param
|
82
|
+
when "y", "yes", "t", "true", "1"
|
83
|
+
true
|
84
|
+
when "n", "no", "f", "false", "0"
|
85
|
+
false
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def fail_validation(parameter, reason)
|
90
|
+
{ success: false, reason: reason }.merge parameter
|
91
|
+
end
|
92
|
+
|
93
|
+
def build_name(request_method, path_info)
|
94
|
+
self.class.build_name(request_method, path_info)
|
95
|
+
end
|
96
|
+
|
97
|
+
def build_match(request_method, path_info)
|
98
|
+
/^#{request_method}\s\s#{path_info.gsub(/:[^\/]*/, '[^\/]*')}$/
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'sinatra/base'
|
4
|
+
|
5
|
+
Dir["#{File.dirname(__FILE__)}/browse/*.rb"].each {|f| require f }
|
6
|
+
|
7
|
+
module Sinatra::Browse
|
8
|
+
def param(name, type, options = {})
|
9
|
+
temp_browse_params[name] = options.merge({ type: type })
|
10
|
+
end
|
11
|
+
|
12
|
+
def parameter_options(parameter, options)
|
13
|
+
#TODO: Raise error when the parameter overridden doesn't exist
|
14
|
+
temp_browse_params[parameter].merge! options
|
15
|
+
end
|
16
|
+
alias :param_options :parameter_options
|
17
|
+
|
18
|
+
def temp_browse_params
|
19
|
+
@_temp_browse_params ||= reset_temp_params
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset_temp_params
|
23
|
+
@_temp_browse_params = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def browse_routes
|
27
|
+
@_browse_routes ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def browse_description
|
31
|
+
@_browse_description ||= ""
|
32
|
+
end
|
33
|
+
|
34
|
+
def describe(description)
|
35
|
+
@_browse_description = description
|
36
|
+
end
|
37
|
+
alias :desc :describe
|
38
|
+
|
39
|
+
def browse_routes_for(request_method, path_info)
|
40
|
+
browse_routes.values.find { |v| v.matches?(request_method, path_info) }
|
41
|
+
end
|
42
|
+
|
43
|
+
#TODO: Rename method... It doesn't sound like we'd be creating a new route object here
|
44
|
+
def set_browse_routes_for(request_method, path_info, description = browse_description, new_params = temp_browse_params)
|
45
|
+
new_route = Route.new(request_method, path_info, browse_description, new_params)
|
46
|
+
browse_routes[new_route.name] = new_route
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.registered(app)
|
50
|
+
@app = app
|
51
|
+
|
52
|
+
app.before do
|
53
|
+
browse_route = app.browse_routes_for(request.request_method, request.path_info)
|
54
|
+
|
55
|
+
if browse_route
|
56
|
+
#TODO: Optionally throw error for undefined params
|
57
|
+
browse_route.delete_undefined(params) #TODO: Make this optional per route and global
|
58
|
+
browse_route.coerce_type(params)
|
59
|
+
browse_route.set_defaults(params)
|
60
|
+
validation_result = browse_route.validate(params)
|
61
|
+
unless validation_result[:success]
|
62
|
+
if validation_result[:on_error].respond_to?(:to_proc)
|
63
|
+
instance_exec &validation_result[:on_error].to_proc
|
64
|
+
else
|
65
|
+
halt 400, {
|
66
|
+
error: "parameter validation failed",
|
67
|
+
parameter: validation_result[:name],
|
68
|
+
reason: validation_result[:reason]
|
69
|
+
}.to_json
|
70
|
+
end
|
71
|
+
end
|
72
|
+
browse_route.transform(params)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Create the (future) browsable api
|
77
|
+
app.param :format, :String, in: ["kusohtml", "json", "yaml"], default: "kusohtml"
|
78
|
+
app.get '/browse' do
|
79
|
+
Sinatra::Browse.format(params["format"], app.browse_routes).generate
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.route_added(verb, path, block)
|
84
|
+
@app.set_browse_routes_for(verb, path)
|
85
|
+
@app.reset_temp_params
|
86
|
+
@app.desc ""
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
module Sinatra; register(Browse) end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-browse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Axsh Co. LTD
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Parameter declaration framework and browsable API for Sinatra
|
14
|
+
email: dev@axsh.net
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sinatra/browse.rb
|
20
|
+
- lib/sinatra/browse/route.rb
|
21
|
+
- lib/sinatra/browse/format.rb
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
homepage: https://github.com/axsh/sinatra-browse
|
25
|
+
licenses:
|
26
|
+
- LGPLv3
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.0.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Parameter declaration framework and browsable API for Sinatra
|
48
|
+
test_files: []
|
49
|
+
has_rdoc:
|