query_filters 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6aee43e678bb4ae2a21c29cc93d3931b1f7f8cd8519a8b46560796c4c8f106c0
4
+ data.tar.gz: c266c3f356d6ffe661c196ad021e08248c49c4f22dbbc6056ec909dc4d86864b
5
+ SHA512:
6
+ metadata.gz: 5b3bb31bb4f553d91b2d1c968312890cf7b57a96533257d8875f9cf615f74e2ed2f3df9aff89706d2949b351665adfd352b9d7595db642b38210b85f3f6fa083
7
+ data.tar.gz: 64c810e03fd7556422261404fa4e209dcd2a7f3779466640073ab1fd026e0d834a88681b65b23869c1a1250cfb865839fc7de14270d7b65e4a5feef10852ea33
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ gems.locked
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Sergey Besedin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # QueryFilters
2
+
3
+ Hello there! You can use this tiny library to parse pretty query strings to pretty hashes or arrays.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'query_filters'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install query_filters
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ QueryFilters.parse('date_gteq=20-11-2018,date_lteq=20-12-2018')
25
+ ```
26
+
27
+ Produces:
28
+
29
+ ```
30
+ { date_gteq: '20-11-2018', date_lteq: '20-12-2018' }
31
+ ```
32
+
33
+ ### Nested params
34
+
35
+ ```
36
+ QueryFilters.parse('date_gteq=20-11-2018,date_lteq=20-12-2018,posts:title=what about dogs,posts:order=title_desc')
37
+ ```
38
+
39
+ Produces:
40
+
41
+ ```
42
+ {
43
+ date_gteq: '20-11-2018',
44
+ date_lteq: '20-12-2018',
45
+ posts: { title: 'what about dogs', order: 'date_desc' }
46
+ }
47
+ ```
48
+
49
+ ### Configuration
50
+
51
+ You can configure parser through options:
52
+
53
+ ```
54
+ QueryFilters.parse('posts/title=what about dogs;posts/order=title_desc',
55
+ nesting_separator: '/',
56
+ assignment_separator: ':',
57
+ filters_separator: ';',
58
+ symbolize_keys: false
59
+ )
60
+ ```
61
+
62
+ Produces:
63
+
64
+ ```
65
+ {"posts"=>{"title"=>"what about dogs", "order"=>"title_desc"}}
66
+ ```
67
+
68
+ Default options are:
69
+
70
+ * nesting_separator `:`
71
+ * assignment_separator `=`
72
+ * filters_separator `,`
73
+ * symbolize_keys `true`
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kressh/query_filters.
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "query_filters"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/gems.rb ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in query_filters.gemspec
4
+ gemspec
@@ -0,0 +1,12 @@
1
+ require 'query_filters/version'
2
+
3
+ require 'query_filters/parser'
4
+ require 'query_filters/query'
5
+
6
+ module QueryFilters
7
+ class ParseError < StandardError; end
8
+
9
+ def self.parse(query, options = {})
10
+ Query.new(query, options).to_hash
11
+ end
12
+ end
@@ -0,0 +1,61 @@
1
+ require 'active_support/core_ext/hash/deep_merge'
2
+ require 'forwardable'
3
+
4
+ module QueryFilters
5
+ class Parser
6
+ extend ::Forwardable
7
+
8
+ DEFAULT_OPTIONS = {
9
+ nesting_separator: ':',
10
+ assignment_separator: '=',
11
+ filters_separator: ',',
12
+ symbolize_keys: true
13
+ }.freeze
14
+
15
+ attr_reader :query, :options, :filters
16
+
17
+ def_delegator :query, :query_string
18
+
19
+ def initialize(query, options = {})
20
+ @query = query
21
+ @options = DEFAULT_OPTIONS.merge(options)
22
+ parse
23
+ end
24
+
25
+ def parse
26
+ @filters = query_string.split(options[:filters_separator]).map do |filter|
27
+ parse_filter(filter)
28
+ end
29
+ self
30
+ end
31
+
32
+ def to_hash
33
+ filters_hash = {}
34
+ filters.each { |filter| filters_hash.deep_merge!(filter) }
35
+ filters_hash
36
+ end
37
+
38
+ private
39
+
40
+ def parse_filter(filter)
41
+ key, value = split_filter(filter)
42
+ keys = key.split(options[:nesting_separator])
43
+
44
+ keys = keys.map(&:to_sym) if options[:symbolize_keys]
45
+
46
+ nested_hash_from_array(keys + [value])
47
+ end
48
+
49
+ def split_filter(filter)
50
+ filter.split(options[:assignment_separator])
51
+ end
52
+
53
+ def nested_hash_from_array(array)
54
+ if array.size == 1
55
+ array.first
56
+ else
57
+ { array.shift => nested_hash_from_array(array) }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,20 @@
1
+ require 'forwardable'
2
+
3
+ module QueryFilters
4
+ class Query
5
+ extend ::Forwardable
6
+
7
+ attr_accessor :query_string, :options
8
+
9
+ def_delegators :parser, :to_hash, :parse
10
+
11
+ def initialize(query_string, options = {})
12
+ @options = options
13
+ @query_string = query_string
14
+ end
15
+
16
+ def parser
17
+ Parser.new(self, options)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module QueryFilters
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'query_filters/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'query_filters'
8
+ spec.version = QueryFilters::VERSION
9
+ spec.authors = ['Sergey Besedin']
10
+ spec.email = ['kr3ssh@pm.me']
11
+
12
+ spec.summary = 'Parse filters (or something else) from pretty human readable query string to hash'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/kressh/query_filters.git'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+
22
+ spec.metadata['homepage_uri'] = spec.homepage
23
+ spec.metadata['source_code_uri'] = spec.homepage
24
+ spec.metadata['changelog_uri'] = 'https://github.com/kressh/query_filters/master/tree/CHANGELOG.md'
25
+ else
26
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
27
+ 'public gem pushes.'
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = 'exe'
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.add_dependency 'activesupport', '>= 4.0'
40
+
41
+ spec.add_development_dependency 'bundler', '~> 2.0'
42
+ spec.add_development_dependency 'rake', '~> 10.0'
43
+ spec.add_development_dependency 'minitest'
44
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_filters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Besedin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Parse filters (or something else) from pretty human readable query string
70
+ to hash
71
+ email:
72
+ - kr3ssh@pm.me
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - gems.rb
84
+ - lib/query_filters.rb
85
+ - lib/query_filters/parser.rb
86
+ - lib/query_filters/query.rb
87
+ - lib/query_filters/version.rb
88
+ - query_filters.gemspec
89
+ homepage: https://github.com/kressh/query_filters.git
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ allowed_push_host: https://rubygems.org
94
+ homepage_uri: https://github.com/kressh/query_filters.git
95
+ source_code_uri: https://github.com/kressh/query_filters.git
96
+ changelog_uri: https://github.com/kressh/query_filters/master/tree/CHANGELOG.md
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Parse filters (or something else) from pretty human readable query string
116
+ to hash
117
+ test_files: []