sinatra-browse 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 299a354c585ed02f7ae75c3b8ffbc0ce5554d439
4
- data.tar.gz: 0ffaa6e1f4a3c0315f556ef89a42f548d4701ec0
3
+ metadata.gz: eb3a18b58acc57c6a80b56bf6a0e08d96e2b04e0
4
+ data.tar.gz: 87150a5f51798b69ac97540940caec5ba16a5f9d
5
5
  SHA512:
6
- metadata.gz: bb3763dcf26d9b9d6538f462e53d47289dfc347b7923131ac8545055f96d9346ccd41bdc30667b1b4dba95954c23915d1cf52ba519febf54d8b5b642f672d03f
7
- data.tar.gz: 8ad00f11b3add8ad9f124f1f1f1b4a86d19ed8d07cba4cd713324062e61bbd891df278065c2a6107e2e2a1001d6a62b73ee96b72a3599481fc9b1f7fb4aac115
6
+ metadata.gz: 64b8e196681b25a346f8f0cfe135e220a9ebd1b38da76c50481491be880e9817d2384b293e588da7f4155bb0f7131cff9cef974516146bc230452775b8c285e7
7
+ data.tar.gz: 004f30d10ea627e10adce8b34a2115c820ff2b0934bbc3af47bb2a9d3116e154d04fd2d92a6d671047aaa4961674e42adf636e194706072d03c43581b8a431bf
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # sinatra-browse
2
2
 
3
- Self documenting parameter declaration framework for Sinatra.
3
+ Parameter declaration framework and browsable API for Sinatra.
4
4
 
5
5
  ## What problem do we solve?
6
6
 
@@ -10,6 +10,8 @@ There exist several frameworks that generate documentation based on comment bloc
10
10
 
11
11
  ## How do we solve it?
12
12
 
13
+ **Parameter Declaration**
14
+
13
15
  We believe in using actual code as documentation. Take a look at the following example.
14
16
 
15
17
  ```ruby
@@ -21,7 +23,7 @@ class App < Sinatra::Base
21
23
 
22
24
  description "Creates a new user"
23
25
  param :display_name, :String, required: true
24
- param :type, :String, in: ["admin", "moderator", "user"], default: user
26
+ param :type, :String, in: ["admin", "moderator", "user"], default: "user"
25
27
  param :age, :Integer
26
28
  param :gender, :String, in: ["m", "w", "M", "W"], transform: :upcase
27
29
  param :activated, :Boolean, default: false
@@ -34,11 +36,15 @@ end
34
36
 
35
37
  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
38
 
37
- **Question:** But we don't want to go look at source code just to get api documentation. What do we do?
39
+ The syntax is inspired by the [sinatra-param](https://github.com/mattt/sinatra-param) and [thor](https://github.com/erikhuda/thor) projects.
38
40
 
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*
41
+ **Browsable API**
40
42
 
41
- The syntax is inspired by the [sinatra-param](https://github.com/mattt/sinatra-param) and [thor](https://github.com/erikhuda/thor) projects.
43
+ Sinatra-browse allows you to surf to your API. This works as documentation and allows you to send requests and see their responses directly in your browser.
44
+
45
+ http://<api_ip_address>:<api_port>/browse
46
+
47
+ *Remark:* This is still work in progress. Right now the page only shows some simple documentation.
42
48
 
43
49
  ## Parameter types
44
50
 
@@ -108,6 +114,15 @@ If a request is made that fails validation on the *lets_fail* parameter, then th
108
114
  You can use transform to execute a quick method on any prameter provided. Anything that will respond to *to_proc* will do.
109
115
 
110
116
  ```ruby
111
- param :only_caps, :String, transform :upcase
117
+ param :only_caps, :String, transform: :upcase
112
118
  param :power_of_two, :Integer, transform: proc { |n| n * n }
113
119
  ```
120
+ ## Removing undefined parameters
121
+
122
+ By default sinatra-browse removes all parameters that weren't defined. You can disable this behaviour with the following line.
123
+
124
+ disable :remove_undefined_parameters
125
+
126
+ You can also set a `system_parameters` variable to allow for a select few parameters that aren't removed. By default this is set to *[ "splat", "captures" ]*.
127
+
128
+ set system_parameters: [ "id", "username", "password" ]
@@ -0,0 +1,49 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Sinatra::Browse
4
+ def ValidationError < Exception; end
5
+
6
+ class Validation
7
+ attr_reader :fail_message
8
+ end
9
+
10
+ class In < Validation
11
+ def fail_message(value, accepted)
12
+ "Invalid value: '#{value}'. Must be a member of #{accepted.inspect}."
13
+ end
14
+
15
+ def validate(value)
16
+ end
17
+
18
+ def fail(value, accepted)
19
+ raise ValidationError, fail_message(value, accepted)
20
+ end
21
+ end
22
+
23
+ class Parameter
24
+ attr_reader :value
25
+
26
+ def initialize(value)
27
+ @value = coerce(value)
28
+ end
29
+
30
+ def validate()
31
+ end
32
+ end
33
+
34
+ class BPString < Parameter
35
+ def coerce(v); String(v) end
36
+ end
37
+
38
+ class BPBoolean < Parameter
39
+ def coerce(v)
40
+ case v
41
+ when "y", "yes", "t", "true", "1"
42
+ true
43
+ when "n", "no", "f", "false", "0"
44
+ false
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -53,8 +53,8 @@
53
53
  }
54
54
  end
55
55
 
56
- def delete_undefined(params)
57
- params.delete_if { |i| !self.has_parameter?(i) }
56
+ def delete_undefined(params, system_params)
57
+ params.delete_if { |i| !(self.has_parameter?(i) || system_params.member?(i)) }
58
58
  end
59
59
 
60
60
  def validate(params)
@@ -49,12 +49,20 @@ module Sinatra::Browse
49
49
  def self.registered(app)
50
50
  @app = app
51
51
 
52
+ app.enable :remove_undefined_parameters
53
+ app.set system_parameters: ["splat", "captures"]
54
+
52
55
  app.before do
53
56
  browse_route = app.browse_routes_for(request.request_method, request.path_info)
54
57
 
55
58
  if browse_route
56
59
  #TODO: Optionally throw error for undefined params
57
- browse_route.delete_undefined(params) #TODO: Make this optional per route and global
60
+ #TODO: Make undefined parameter deletion optional per route
61
+
62
+ if settings.remove_undefined_parameters
63
+ browse_route.delete_undefined(params, settings.system_parameters)
64
+ end
65
+
58
66
  browse_route.coerce_type(params)
59
67
  browse_route.set_defaults(params)
60
68
  validation_result = browse_route.validate(params)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-browse
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Axsh Co. LTD
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-28 00:00:00.000000000 Z
11
+ date: 2014-04-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Parameter declaration framework and browsable API for Sinatra
14
14
  email: dev@axsh.net
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/sinatra/browse.rb
20
20
  - lib/sinatra/browse/route.rb
21
+ - lib/sinatra/browse/parameter.rb
21
22
  - lib/sinatra/browse/format.rb
22
23
  - LICENSE
23
24
  - README.md