sinatra-muster 0.0.1
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/.gitignore +18 -0
- data/.rspec +3 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +45 -0
- data/Rakefile +11 -0
- data/lib/sinatra/muster.rb +96 -0
- data/lib/sinatra/muster/version.rb +5 -0
- data/sinatra-muster.gemspec +28 -0
- data/spec/sinatra/muster/helpers_spec.rb +155 -0
- data/spec/spec_helper.rb +27 -0
- metadata +137 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--protected
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Christopher H. Laco
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Sinatra::Muster
|
2
|
+
|
3
|
+
Sinatra helpers for working with Muster query string parsing strategies
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sinatra-muster'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sinatra-muster
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'sinatra/base'
|
22
|
+
require 'sinatra/muster'
|
23
|
+
|
24
|
+
class MyApp < Sinatra::Base
|
25
|
+
register Sinatra::Muster
|
26
|
+
|
27
|
+
use Muster::Rack, Muster::Strategies::Hash
|
28
|
+
|
29
|
+
# GET /?select=id,name,password
|
30
|
+
get '/' do
|
31
|
+
query_filter :select, :only => ['id', 'name', 'email']
|
32
|
+
|
33
|
+
selected = query[:select] #=> ['id', 'name']
|
34
|
+
|
35
|
+
Users.all(:fields => selected)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'muster'
|
2
|
+
require 'sinatra/base'
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
module Muster
|
6
|
+
|
7
|
+
# Sinatra helpers for working with Muster query string parsing strategies
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# require 'sinatra/base'
|
12
|
+
# require 'sinatra/muster'
|
13
|
+
#
|
14
|
+
# class MyApp < Sinatra::Base
|
15
|
+
# register Sinatra::Muster
|
16
|
+
#
|
17
|
+
# use Muster::Rack, Muster::Strategies::Hash
|
18
|
+
#
|
19
|
+
# # GET /?select=id,name,password
|
20
|
+
# get '/' do
|
21
|
+
# query_filter :select, :only => ['id', 'name', 'email']
|
22
|
+
#
|
23
|
+
# selected = query[:select] #=> ['id', 'name']
|
24
|
+
#
|
25
|
+
# Users.all(:fields => selected)
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
module Helpers
|
29
|
+
|
30
|
+
# Add a filter to be applied to the data in {#query} results
|
31
|
+
#
|
32
|
+
# @param key [String,Symbol] the key of the values in {#query} to filter
|
33
|
+
# @param [optional, Hash] options the options available for this filter
|
34
|
+
# @option options [optional] :only when specified, only return the matching values
|
35
|
+
# If you specify a single value, a single value will be returned
|
36
|
+
# If you specify an Array of values, an Array will be returned, even if only one value matches
|
37
|
+
# @option options [optional] :except return all values except the ones given here
|
38
|
+
# If the raw data value is a single value, a single value will be returned
|
39
|
+
# If the raw data value is an Array, and array will be returned, even if all values are excluded
|
40
|
+
# If nothing was excluded, the raw value is returned as-is
|
41
|
+
#
|
42
|
+
# If you pass a scalar value instead of a Hash into options, it will be treated as the default, just like
|
43
|
+
# Hash#fetch does.
|
44
|
+
#
|
45
|
+
# If you pass nothing into the options argument, it will return all values if the key exists or raise
|
46
|
+
# a KeyError like Hash#fetch.
|
47
|
+
#
|
48
|
+
# @return [void]
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
#
|
52
|
+
# results.add_filter(:select, :only => [:id, :name])
|
53
|
+
# results.add_filter(:select, :except => [:id])
|
54
|
+
# results.add_filter(:page, 1)
|
55
|
+
def query_filter( key, *options )
|
56
|
+
muster_query.add_filter( key, *options )
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns the parsed and filtered query options from Muster::Rack
|
60
|
+
#
|
61
|
+
# @return [Muster::Results]
|
62
|
+
#
|
63
|
+
# @example
|
64
|
+
#
|
65
|
+
# # GET /?select=id,name,password
|
66
|
+
# get '/' do
|
67
|
+
# query_filter :select, :only => ['id', 'name', 'email']
|
68
|
+
#
|
69
|
+
# selected = query[:select] #=> ['id', 'name']
|
70
|
+
#
|
71
|
+
# Users.all(:fields => selected)
|
72
|
+
# end
|
73
|
+
def query
|
74
|
+
muster_query.filtered
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def muster_query
|
80
|
+
request.env['muster.query']
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
# Registers the helpers with the current Sinatra application
|
86
|
+
#
|
87
|
+
# @param app [Sinatra::Base] The Sinatra application instance
|
88
|
+
#
|
89
|
+
# @return [void]
|
90
|
+
def self.registered(app)
|
91
|
+
app.helpers Sinatra::Muster::Helpers
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
helpers Sinatra::Muster::Helpers
|
96
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sinatra/muster/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sinatra-muster"
|
8
|
+
gem.version = Sinatra::Muster::VERSION
|
9
|
+
gem.authors = ["Christopher H. Laco"]
|
10
|
+
gem.email = ["claco@chrislaco.com"]
|
11
|
+
gem.description = %q{Extension and Helpers for using Muster in Sinatra applications.}
|
12
|
+
gem.summary = %q{Sinatra Muster Exception}
|
13
|
+
gem.homepage = "https://github.com/claco/sinatra-muster"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'muster', '>= 0.0.4'
|
21
|
+
gem.add_dependency 'sinatra'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rspec', '~> 2.11.0'
|
24
|
+
gem.add_development_dependency 'redcarpet', '~> 2.1'
|
25
|
+
gem.add_development_dependency 'simplecov'
|
26
|
+
gem.add_development_dependency 'yard', '~> 0.8.2'
|
27
|
+
gem.add_development_dependency 'rack-test'
|
28
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sinatra::Muster::Helpers do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
subject(:app) do
|
7
|
+
modular_app(&action)
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'with no filters' do
|
11
|
+
let(:action) do
|
12
|
+
lambda do
|
13
|
+
query.inspect
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns all parsed values' do
|
18
|
+
get '/?select=id,name,password'
|
19
|
+
|
20
|
+
last_response.body.should == '{"select"=>["id", "name", "password"]}'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with :only filtering' do
|
25
|
+
context 'with no matching values' do
|
26
|
+
context 'and a scalar filter' do
|
27
|
+
let(:action) do
|
28
|
+
lambda do
|
29
|
+
query_filter :select, :only => 'name'
|
30
|
+
query.inspect
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns a nil' do
|
35
|
+
get '/?select=id,password'
|
36
|
+
|
37
|
+
last_response.body.should == '{"select"=>nil}'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'and an Array filter' do
|
42
|
+
let(:action) do
|
43
|
+
lambda do
|
44
|
+
query_filter :select, :only => ['name']
|
45
|
+
query.inspect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'returns an empty Array' do
|
50
|
+
get '/?select=id,password'
|
51
|
+
|
52
|
+
last_response.body.should == '{"select"=>[]}'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'with a matching value' do
|
58
|
+
context 'with a scalar filter' do
|
59
|
+
let(:action) do
|
60
|
+
lambda do
|
61
|
+
query_filter :select, :only => 'name'
|
62
|
+
query.inspect
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'returns filtered scalar value' do
|
67
|
+
get '/?select=id,name,password'
|
68
|
+
|
69
|
+
last_response.body.should == '{"select"=>"name"}'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with an Array filter' do
|
74
|
+
let(:action) do
|
75
|
+
lambda do
|
76
|
+
query_filter :select, :only => ['name']
|
77
|
+
query.inspect
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'returns filtered scalar value' do
|
82
|
+
get '/?select=id,name,password'
|
83
|
+
|
84
|
+
last_response.body.should == '{"select"=>["name"]}'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'with :except filtering' do
|
91
|
+
context 'with no remaining values' do
|
92
|
+
context 'and a scalar query' do
|
93
|
+
let(:action) do
|
94
|
+
lambda do
|
95
|
+
query_filter :select, :except => 'name'
|
96
|
+
query.inspect
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'returns a nil' do
|
101
|
+
get '/?select=name'
|
102
|
+
|
103
|
+
last_response.body.should == '{"select"=>nil}'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'and an Array query' do
|
108
|
+
let(:action) do
|
109
|
+
lambda do
|
110
|
+
query_filter :select, :except => ['id','name']
|
111
|
+
query.inspect
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'returns an empty nil' do
|
116
|
+
get '/?select=id,name'
|
117
|
+
|
118
|
+
last_response.body.should == '{"select"=>[]}'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'with remaining values' do
|
124
|
+
context 'with a scalar query value' do
|
125
|
+
let(:action) do
|
126
|
+
lambda do
|
127
|
+
query_filter :select, :except => 'id'
|
128
|
+
query.inspect
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'returns filtered scalar value' do
|
133
|
+
get '/?select=name'
|
134
|
+
|
135
|
+
last_response.body.should == '{"select"=>"name"}'
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'with an Array query value' do
|
140
|
+
let(:action) do
|
141
|
+
lambda do
|
142
|
+
query_filter :select, :except => ['password']
|
143
|
+
query.inspect
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'returns filtered Arrau value' do
|
148
|
+
get '/?select=id,name,password'
|
149
|
+
|
150
|
+
last_response.body.should == '{"select"=>["id", "name"]}'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'rack/test'
|
7
|
+
require 'sinatra/muster'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
end
|
14
|
+
|
15
|
+
def mock_app(base=Sinatra::Base, &block)
|
16
|
+
Sinatra.new(base, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def modular_app(&block)
|
20
|
+
mock_app do
|
21
|
+
register Sinatra::Muster
|
22
|
+
|
23
|
+
use Muster::Rack, Muster::Strategies::Hash
|
24
|
+
|
25
|
+
get '/', &block
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-muster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christopher H. Laco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: muster
|
16
|
+
requirement: &70190314709080 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.4
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70190314709080
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sinatra
|
27
|
+
requirement: &70190314708620 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70190314708620
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70190314708020 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.11.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70190314708020
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: redcarpet
|
49
|
+
requirement: &70190314707500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.1'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70190314707500
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: simplecov
|
60
|
+
requirement: &70190314707060 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70190314707060
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: &70190314706520 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.8.2
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70190314706520
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rack-test
|
82
|
+
requirement: &70190314706080 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70190314706080
|
91
|
+
description: Extension and Helpers for using Muster in Sinatra applications.
|
92
|
+
email:
|
93
|
+
- claco@chrislaco.com
|
94
|
+
executables: []
|
95
|
+
extensions: []
|
96
|
+
extra_rdoc_files: []
|
97
|
+
files:
|
98
|
+
- .gitignore
|
99
|
+
- .rspec
|
100
|
+
- .yardopts
|
101
|
+
- Gemfile
|
102
|
+
- LICENSE
|
103
|
+
- README.md
|
104
|
+
- Rakefile
|
105
|
+
- lib/sinatra/muster.rb
|
106
|
+
- lib/sinatra/muster/version.rb
|
107
|
+
- sinatra-muster.gemspec
|
108
|
+
- spec/sinatra/muster/helpers_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
homepage: https://github.com/claco/sinatra-muster
|
111
|
+
licenses: []
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.16
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: Sinatra Muster Exception
|
134
|
+
test_files:
|
135
|
+
- spec/sinatra/muster/helpers_spec.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
has_rdoc:
|