clean_query_params 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --profile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clean_query_params.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stuart Liston
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,77 @@
1
+ # CleanQueryParams
2
+
3
+ A small, concise DSL for describing query parameters, specifically for sorting, paging and querying on index actions.
4
+ I aim to make [the specs](https://github.com/stuliston/clean_query_params/blob/master/spec/clean_query_params_spec.rb) as clear a demonstration as possible,
5
+ but here's a quick test case as an example:
6
+
7
+ Given a */users* endpoint, you can create a params object like this:
8
+
9
+ ```ruby
10
+ class UserParameters
11
+ include CleanQueryParams
12
+
13
+ defaults sort_by: 'last_name',
14
+ sort_dir: 'ASC',
15
+ page_number: 1,
16
+ page_size: 20
17
+
18
+ queryable_by :first_name, :last_name, :age
19
+ end
20
+
21
+ it "gives you a query params object with pagination, sorting and an extracted query hash" do
22
+
23
+ raw_params = {
24
+ page_number: 2,
25
+ page_size: 15,
26
+ first_name: 'Stu',
27
+ role: 'admin'
28
+ }
29
+
30
+ user_params = UserParameters.new(raw_params)
31
+
32
+ # specified above as UserParameters default
33
+ expect(user_params.sort_by).to eq 'last_name'
34
+
35
+ # sensible global default
36
+ expect(user_params.sort_dir).to eq 'ASC'
37
+
38
+ # defined in raw params
39
+ expect(user_params.page_number).to eq 2
40
+
41
+ # defined in raw params
42
+ expect(user_params.page_size).to eq 15
43
+
44
+ # only key/values specified above in queryable_by
45
+ expect(user_params.query).to eq({ first_name: 'Stu' })
46
+ end
47
+ ```
48
+
49
+ I'm planning to write a railtie to make integration with rails controllers seamless.
50
+
51
+ More examples to follow...
52
+
53
+ ## Installation
54
+
55
+ Add this line to your application's Gemfile:
56
+
57
+ gem 'clean_query_params'
58
+
59
+ And then execute:
60
+
61
+ $ bundle
62
+
63
+ Or install it yourself as:
64
+
65
+ $ gem install clean_query_params
66
+
67
+ ## Usage
68
+
69
+ TODO: Write usage instructions here
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clean_query_params/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clean_query_params"
8
+ spec.version = CleanQueryParams::VERSION
9
+ spec.authors = ["Stuart Liston"]
10
+ spec.email = ["stuart.liston@gmail.com"]
11
+ spec.description = %q{Gives you a concise DSL for describing query parameters}
12
+ spec.summary = %q{Aims to validate and default a raw params hash (e.g. in a Rails controller) into something you can confidently query your datasource against.}
13
+ spec.homepage = "http://github.com/stuliston/clean_query_params"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+
26
+ spec.add_runtime_dependency "i18n"
27
+ spec.add_runtime_dependency "active_support"
28
+ end
@@ -0,0 +1,3 @@
1
+ module CleanQueryParams
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require "clean_query_params/version"
2
+ require 'active_support/core_ext'
3
+
4
+ module CleanQueryParams
5
+
6
+ # Hmmmm - probably too presumptuous.
7
+ # Make this confugurable at the app-level?
8
+ GLOBAL_DEFAULTS = {
9
+ page_number: 1,
10
+ page_size: 20,
11
+ sort_by: 'id',
12
+ sort_dir: 'ASC'
13
+ }
14
+
15
+ def initialize(params = {})
16
+ params.keep_if { |k,v| v.present? }
17
+ @params = defaults.merge(params)
18
+ end
19
+
20
+ def query
21
+ params.slice(*valid_query_keys)
22
+ end
23
+
24
+ def sort_by
25
+ params[:sort_by]
26
+ end
27
+
28
+ def sort_dir
29
+ params[:sort_dir].upcase
30
+ end
31
+
32
+ def page_number
33
+ params[:page_number]
34
+ end
35
+
36
+ def page_size
37
+ params[:page_size]
38
+ end
39
+
40
+ # Define and initialise the class-level _defaults Hash
41
+ # and _valid_query_keys Set.
42
+ #
43
+ # Extend host class with macros when we're included.
44
+ #
45
+ def self.included(host_class)
46
+ host_class.class_attribute :_defaults
47
+ host_class._defaults = Hash.new
48
+
49
+ host_class.class_attribute :_valid_query_keys
50
+ host_class._valid_query_keys = Set.new
51
+
52
+ host_class.extend(Dsl)
53
+ end
54
+
55
+ # Module containing macros (class methods).
56
+ #
57
+ module Dsl
58
+
59
+ def defaults(options = {})
60
+ self._defaults = options
61
+ end
62
+
63
+ def queryable_by(*keys)
64
+ self._valid_query_keys = Set.new(keys)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ attr_reader :params
71
+
72
+ def defaults
73
+ GLOBAL_DEFAULTS.merge(self.class._defaults)
74
+ end
75
+
76
+ def valid_query_keys
77
+ self.class._valid_query_keys
78
+ end
79
+
80
+ end
@@ -0,0 +1,139 @@
1
+ require 'clean_query_params'
2
+ require 'pry'
3
+
4
+ describe CleanQueryParams do
5
+
6
+ describe "full example for documentation" do
7
+
8
+ class UserParameters
9
+ include CleanQueryParams
10
+
11
+ defaults sort_by: 'last_name',
12
+ sort_dir: 'ASC',
13
+ page_number: 1,
14
+ page_size: 20
15
+
16
+ queryable_by :first_name, :last_name, :age
17
+ end
18
+
19
+ it "gives you a query params object with pagination, sorting and an extracted query hash" do
20
+
21
+ raw_params = {
22
+ page_number: 2,
23
+ page_size: 15,
24
+ first_name: 'Stu',
25
+ role: 'admin'
26
+ }
27
+
28
+ user_params = UserParameters.new(raw_params)
29
+
30
+ # specified above as UserParameters default
31
+ expect(user_params.sort_by).to eq 'last_name'
32
+
33
+ # sensible global default
34
+ expect(user_params.sort_dir).to eq 'ASC'
35
+
36
+ # defined in raw params
37
+ expect(user_params.page_number).to eq 2
38
+
39
+ # defined in raw params
40
+ expect(user_params.page_size).to eq 15
41
+
42
+ # only key/values specified above in queryable_by
43
+ expect(user_params.query).to eq({ first_name: 'Stu' })
44
+ end
45
+
46
+ end
47
+
48
+ describe "public API" do
49
+
50
+ context "when a class mixes us in" do
51
+
52
+ class ImageParameters
53
+ include CleanQueryParams
54
+ end
55
+
56
+ let(:image_params) { ImageParameters.new }
57
+
58
+ it "responds to #sort_by" do
59
+ expect(image_params).to respond_to :sort_by
60
+ end
61
+
62
+ it "responds to #sort_dir" do
63
+ expect(image_params).to respond_to :sort_dir
64
+ end
65
+
66
+ it "responds to #page_number" do
67
+ expect(image_params).to respond_to :page_number
68
+ end
69
+
70
+ it "responds to #page_size" do
71
+ expect(image_params).to respond_to :page_size
72
+ end
73
+
74
+ it "responds to #query" do
75
+ expect(image_params).to respond_to :query
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ describe ".defaults" do
83
+
84
+ class PostParameters
85
+ include CleanQueryParams
86
+ defaults page_size: 10, sort_by: 'created_at'
87
+ end
88
+
89
+ let(:post_params) { PostParameters.new(params) }
90
+
91
+ context "when the params don't specify a value" do
92
+ let(:params) { {} }
93
+
94
+ it "exposes the default" do
95
+ expect(post_params.page_size).to eq 10
96
+ expect(post_params.sort_by).to eq 'created_at'
97
+ end
98
+ end
99
+
100
+ context "when the params specify a value" do
101
+ let(:params) { {page_size: 50} }
102
+
103
+ it "exposes the specified value" do
104
+ expect(post_params.page_size).to eq 50
105
+ end
106
+ end
107
+
108
+ context "when nobody specifies a value" do
109
+
110
+ class CarParameters
111
+ include CleanQueryParams
112
+ end
113
+
114
+ let(:params) { {} }
115
+
116
+ it "exposes a global default" do
117
+ car_params = CarParameters.new(params)
118
+ expect(car_params.page_size).to eq CleanQueryParams::GLOBAL_DEFAULTS[:page_size]
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ describe ".queryable_by" do
125
+
126
+ class PlaceParameters
127
+ include CleanQueryParams
128
+ queryable_by :name, :address
129
+ end
130
+
131
+ it "specifies what keys can exist in #query" do
132
+ params = { name: 'Mamasita', admin: 'DANGER', address: 'Melbourne' }
133
+ place_params = PlaceParameters.new(params)
134
+ expect(place_params.query).to eq({ name: 'Mamasita', address: 'Melbourne' })
135
+ end
136
+
137
+ end
138
+
139
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clean_query_params
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Stuart Liston
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ type: :development
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
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: rspec
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
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: pry
64
+ type: :development
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
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: i18n
80
+ type: :runtime
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
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: active_support
96
+ type: :runtime
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Gives you a concise DSL for describing query parameters
111
+ email:
112
+ - stuart.liston@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - clean_query_params.gemspec
124
+ - lib/clean_query_params.rb
125
+ - lib/clean_query_params/version.rb
126
+ - spec/clean_query_params_spec.rb
127
+ homepage: http://github.com/stuliston/clean_query_params
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.23
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Aims to validate and default a raw params hash (e.g. in a Rails controller)
152
+ into something you can confidently query your datasource against.
153
+ test_files:
154
+ - spec/clean_query_params_spec.rb