rql_parser 0.0.1alpha

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: ca4cc9992b3d422eb1a6036033ea2662ad0b84d7a9cb62684840c6da18aea7e5
4
+ data.tar.gz: f3f7d4c0710d05aa5ed811d81e0ee2a2a676fdf66caa0e6a06f109484a9f39de
5
+ SHA512:
6
+ metadata.gz: a2092da64d7ccff15725dd25ce1df65cd028d820fd87af560bdbce900f638404806be31c4e68325c0e66779a76392ee397ddc257866cae7ead05ef58c326cdb6
7
+ data.tar.gz: e07ee60668d20e024ae694c0bfb9e09367cd662521eb353810a750592b9352bb306ee92d0a7de7040d1c7202ec7634b8e4446677ca8d8156b93a0016c5b3e6eb
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at Aliaksei.Kharkou@itechart-group.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rql_parser.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Aliaksei Kharkou
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,43 @@
1
+ # RqlParser
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rql_parser`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rql_parser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rql_parser
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rql_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the RqlParser project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rql_parser/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rql_parser"
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
@@ -0,0 +1,11 @@
1
+ class BaseInteraction < ActiveInteraction::Base
2
+ private
3
+
4
+ def perform(interaction)
5
+ if interaction.valid?
6
+ interaction.result
7
+ else
8
+ errors.merge!(interaction.errors)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,44 @@
1
+ module RqlParser
2
+ class Format < BaseInteraction
3
+ string :rql
4
+
5
+ RQL_FORMAT = /\A[0-9A-Za-z_()|&,+\-!=<> ]+\z/.freeze
6
+
7
+ validates :rql, format: { with: RQL_FORMAT }
8
+
9
+ def execute
10
+ res = remove_whitespace(rql)
11
+ replace_shorthands(res)
12
+ end
13
+
14
+ private
15
+
16
+ def remove_whitespace(rql)
17
+ if /[0-9a-zA-Z] [0-9a-zA-Z(]/.match?(rql)
18
+ errors.add(:rql_parser, 'has invalid whitespace')
19
+ else
20
+ rql.delete(' ')
21
+ end
22
+ end
23
+
24
+ def replace_shorthands(rql)
25
+ res = without_shorthands(rql)
26
+ if /[!<>=]/.match?(res)
27
+ errors.add(:rql_parser, 'has invalid shorthands')
28
+ else
29
+ res
30
+ end
31
+ end
32
+
33
+ def without_shorthands(rql)
34
+ rql.gsub(/([0-9A-Za-z_]+)=(eq|ne|lt|le|gt|ge)=([0-9A-Za-z_]+)/,
35
+ '\2(\1,\3)')
36
+ .gsub(/([0-9A-Za-z_]+)!=([0-9A-Za-z_]+)/, 'ne(\1,\2)')
37
+ .gsub(/([0-9A-Za-z_]+)<([0-9A-Za-z_]+)/, 'lt(\1,\2)')
38
+ .gsub(/([0-9A-Za-z_]+)<=([0-9A-Za-z_]+)/, 'le(\1,\2)')
39
+ .gsub(/([0-9A-Za-z_]+)>([0-9A-Za-z_]+)/, 'gt(\1,\2)')
40
+ .gsub(/([0-9A-Za-z_]+)>=([0-9A-Za-z_]+)/, 'ge(\1,\2)')
41
+ .gsub(/([0-9A-Za-z_]+)=([0-9A-Za-z_]+)/, 'eq(\1,\2)')
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RqlParser
4
+ #
5
+ # some doc
6
+ class FromParams < BaseInteraction
7
+ hash :params, strip: false
8
+
9
+ def execute
10
+ perform RqlParser::Parse.run(rql: to_rql(params))
11
+ end
12
+
13
+ private
14
+
15
+ def to_rql(params)
16
+ params.except(:controller, :action).map do |k, v|
17
+ [k, v].compact.join('=')
18
+ end.join('&')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RqlParser
4
+ class Parse < BaseInteraction
5
+ string :rql
6
+
7
+ def execute
8
+ formatted = perform(Format.run(inputs))
9
+ expression(formatted) || errors.add(:rql) unless errors.any?
10
+ end
11
+
12
+ private
13
+
14
+ def expression(str)
15
+ group(str) || or_expression(str)
16
+ end
17
+
18
+ def group(str)
19
+ res = false
20
+ if /\A\(.+\)\z/.match?(str)
21
+ res = or_expression(str[1..-2])
22
+ if res
23
+ res = { type: :group, args: res } if res[:args].size > 1
24
+ end
25
+ end
26
+ res
27
+ end
28
+
29
+ def or_expression(str)
30
+ res = []
31
+ split = str.split(/[|;]/)
32
+ temp = ''
33
+ while split.any?
34
+ temp += split.shift
35
+ temp_and_exp = and_expression(temp)
36
+ if temp_and_exp
37
+ res.push(temp_and_exp)
38
+ temp = ''
39
+ else
40
+ temp += ';'
41
+ end
42
+ end
43
+ return false if temp.present?
44
+
45
+ res.size > 1 ? { type: :function, identifier: 'or', args: res } : res.first
46
+ end
47
+
48
+ def and_expression(str)
49
+ res = []
50
+ split = str.split(/,/)
51
+ temp = ''
52
+ while split.any?
53
+ temp += split.shift
54
+ temp_and_exp = function(temp) || group(temp) || and_strict(temp)
55
+ if temp_and_exp
56
+ res.push(temp_and_exp)
57
+ temp = ''
58
+ else
59
+ temp += ','
60
+ end
61
+ end
62
+ return false if temp.present?
63
+
64
+ res.size > 1 ? { type: :function, identifier: 'and', args: res } : res.first
65
+ end
66
+
67
+ def and_strict(str)
68
+ res = []
69
+ split = str.split(/&/)
70
+ temp = ''
71
+ while split.any?
72
+ temp += split.shift
73
+ temp_and_exp = function(temp) || group(temp)
74
+ if temp_and_exp
75
+ res.push(temp_and_exp)
76
+ temp = ''
77
+ else
78
+ temp += ','
79
+ end
80
+ end
81
+ return false if temp.present?
82
+
83
+ res.size > 1 ? { type: :function, identifier: 'and', args: res } : res.first
84
+ end
85
+
86
+ def function(str)
87
+ res = false
88
+ if /\A[a-z]+\(.+\)\z/.match?(str)
89
+ identifier = str.match(/\A([a-z]+)/)[1]
90
+ args = str.match(/\A[a-z]+\((.+)\)\z/)[1]
91
+ temp_args = args(args)
92
+ if temp_args
93
+ res = { type: :function, identifier: identifier, args: temp_args }
94
+ else
95
+ false
96
+ end
97
+ end
98
+ res
99
+ end
100
+
101
+ def args(str)
102
+ res = []
103
+ split = str.split(',')
104
+ temp = ''
105
+ while split.any?
106
+ temp += split.shift
107
+ temp_arg = arg(temp)
108
+ if temp_arg
109
+ res.push(temp_arg)
110
+ temp = ''
111
+ else
112
+ temp += ','
113
+ end
114
+ end
115
+ return false if temp.present?
116
+
117
+ res
118
+ end
119
+
120
+ def arg(str)
121
+ expression(str) || array_of_values(str) || value(str)
122
+ end
123
+
124
+ def array_of_values(str)
125
+ return false unless /\A\(.+\)\z/.match?(str)
126
+
127
+ res = []
128
+ split = str[1..-2].split(',')
129
+ temp = ''
130
+ while split.any?
131
+ temp += split.shift
132
+ temp_arg = value(temp)
133
+ if temp_arg
134
+ res.push(temp_arg)
135
+ temp = ''
136
+ else
137
+ temp += ','
138
+ end
139
+ end
140
+ return false if temp.present?
141
+
142
+ { arg_array: res }
143
+ end
144
+
145
+ def value(str)
146
+ if str.match?(/\A[_0-9a-zA-Z]+\z/)
147
+ { arg: str }
148
+ else
149
+ false
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,21 @@
1
+ exp = group | or_exp ;
2
+
3
+ group = '(', or_exp, ')' ;
4
+
5
+ or_exp = and_exp, { or_delim, and_exp } ;
6
+
7
+ or_delim = '|', ';' ;
8
+
9
+ and_exp = (function | and_strict | group), { ',', (function | and_strict | group) } ;
10
+
11
+ function = identifier, '(', [arg, { ',', arg }], ')' ;
12
+
13
+ and_strict = (function | group), { '&', (function | group) } ;
14
+
15
+ identifier = /[a-z]+/
16
+
17
+ arg = and_exp | array_of_values | value ;
18
+
19
+ value = /[_0-9a-zA-Z]+/ ;
20
+
21
+ array_of_values = '(', value, { ',', value }, ')' ;
@@ -0,0 +1,3 @@
1
+ module RqlParser
2
+ VERSION = "0.0.1alpha"
3
+ end
data/lib/rql_parser.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "rql_parser/version"
2
+
3
+ module RqlParser
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rql_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rql_parser"
8
+ spec.version = RqlParser::VERSION
9
+ spec.authors = ["Aliaksei Kharkou"]
10
+ spec.email = ["Aliaksei.Kharkou@itechart-group.com"]
11
+
12
+ spec.summary = %q{RqlParser is a gem for using RQL as valid params}
13
+ spec.description = %q{RqlParser is a gem for using RQL as valid params}
14
+ spec.homepage = "https://github.com/deorigosa-work/rql-parser"
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["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/deorigosa-work/rql-parser"
22
+ spec.metadata["changelog_uri"] = "https://github.com/deorigosa-work/rql-parser/CHANGELOG.md"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "active_interaction", "~> 3.6.0"
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rql_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1alpha
5
+ platform: ruby
6
+ authors:
7
+ - Aliaksei Kharkou
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_interaction
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.6.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: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
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
+ description: RqlParser is a gem for using RQL as valid params
56
+ email:
57
+ - Aliaksei.Kharkou@itechart-group.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/base_interaction.rb
71
+ - lib/rql_parser.rb
72
+ - lib/rql_parser/format.rb
73
+ - lib/rql_parser/from_params.rb
74
+ - lib/rql_parser/parse.rb
75
+ - lib/rql_parser/rules.ebnf
76
+ - lib/rql_parser/version.rb
77
+ - rql_parser.gemspec
78
+ homepage: https://github.com/deorigosa-work/rql-parser
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ homepage_uri: https://github.com/deorigosa-work/rql-parser
83
+ source_code_uri: https://github.com/deorigosa-work/rql-parser
84
+ changelog_uri: https://github.com/deorigosa-work/rql-parser/CHANGELOG.md
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">"
97
+ - !ruby/object:Gem::Version
98
+ version: 1.3.1
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.7.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: RqlParser is a gem for using RQL as valid params
105
+ test_files: []