query_params 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
+ SHA1:
3
+ metadata.gz: 8323efe71cd09abe2c93f83f4fc3762d64bfc11b
4
+ data.tar.gz: 9ee2064b4be6ad854034b82083825457496ae213
5
+ SHA512:
6
+ metadata.gz: 4f37f39162c02d07e290e94b6bb5ef64cc2deb496f7bf2cb4b59bdda3108fe33c4bb90b4fc1b926751d21d56e3dd153ca4920e04ffc40635b93b61e95b2c95a2
7
+ data.tar.gz: 316c656c9e7d73bf2c93b5bfcb2e4482052c82408da8bb0ddeecba10484b625bc592251efe61e81706692ed5794a2d5e52074e3b97746be1f228e9da853da0e9
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in query_params.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 William Souza
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,40 @@
1
+ # Query Params
2
+
3
+ Format URL parameters like a query.
4
+ It allows you to send operators and the same parameter twice on the same request.
5
+
6
+ For example:
7
+ ```
8
+ http://domain.com/search?filters=age::ge(18)|age::le(25)
9
+ ```
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'query-params'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install query-params
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ puts QueryParams.build_uri(base_uri: "http://domain.com/search", q: "Mark", conditions: ["age <= 18", "type = 1"])
31
+ # http://domain.com/search?q=Mark&filters=age::le(18)|type::eq(1)
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/willmiranda/query-params/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "query_params"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ require "query_params"
2
+
3
+ class Condition
4
+
5
+ OPERATORS = { "=" => "eq", ">=" => "ge", "<=" => "le" }
6
+
7
+ def self.build_uri(queryParams, conditions)
8
+
9
+ if conditions.kind_of?(Array)
10
+ conditions.each do |condition|
11
+ set_query(queryParams, condition)
12
+ end
13
+ else
14
+ set_query(queryParams, conditions)
15
+ end
16
+ end
17
+
18
+ def self.set_query(queryParams, condition)
19
+ tokens = condition.split(" ")
20
+
21
+ raise(ArgumentError, "Invalid condition: #{condition}. Send operator separate for space.") if tokens.size < 3
22
+
23
+ field = tokens[0].strip
24
+ operator = tokens[1].strip
25
+ value = tokens[2..tokens.size].join(" ").strip.gsub(/['"]/,"")
26
+
27
+ raise(ArgumentError, "Invalid operator. Accepted tokens: #{OPERATORS.values}") if OPERATORS[operator].nil?
28
+
29
+ queryParams.send(OPERATORS[operator], field, value)
30
+ end
31
+
32
+ end
@@ -0,0 +1,47 @@
1
+ module Filter
2
+
3
+ def full_text_search(query)
4
+ @query = "q=#{ERB::Util.url_encode(query.strip)}"
5
+ end
6
+
7
+ def q(query)
8
+ full_text_search(query)
9
+ end
10
+
11
+ def equal(key, value)
12
+ if value.kind_of?(Array)
13
+ @params.push("#{key}::in(#{value.join(",")})")
14
+ else
15
+ @params.push("#{key}::eq(#{value})")
16
+ end
17
+ end
18
+
19
+ def eq(key, value)
20
+ equal(key, value)
21
+ end
22
+
23
+ def greater_and_equal(key, value)
24
+ @params.push("#{key}::ge(#{value})")
25
+ end
26
+
27
+ def ge(key, value)
28
+ greater_and_equal(key, value)
29
+ end
30
+
31
+ def less_and_equal(key, value)
32
+ @params.push("#{key}::le(#{value})")
33
+ end
34
+
35
+ def le(key, value)
36
+ less_and_equal(key, value)
37
+ end
38
+
39
+ def between(key, range_ini, range_end)
40
+ @params.push("#{key}::bt(#{range_ini},#{range_end})")
41
+ end
42
+
43
+ def bt(key, range_ini, range_end)
44
+ between(key, range_ini, range_end)
45
+ end
46
+
47
+ end
@@ -0,0 +1,3 @@
1
+ module QueryParams
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ require "query_params/version"
2
+ require "query_params/filter"
3
+ require "query_params/condition"
4
+
5
+ class QueryParams
6
+
7
+ include Filter
8
+
9
+ def initialize(base_uri = "")
10
+ @base_uri = base_uri.nil? ? "" : base_uri
11
+ @params = []
12
+ @query = ""
13
+ end
14
+
15
+ def build_uri()
16
+ uri = ""
17
+ unless @base_uri.empty?
18
+ uri += uri.include?('?') ? "&" : "?"
19
+ end
20
+ unless @query.empty?
21
+ uri += "#{@query}"
22
+ uri += "&" unless @params.empty?
23
+ end
24
+ uri += "filters=#{ERB::Util.url_encode(@params.join("|"))}" unless @params.empty?
25
+ uri = "" if uri.length == 1
26
+ "#{@base_uri}#{uri}"
27
+ end
28
+
29
+ def self.build_uri(options = {})
30
+ queryParams = QueryParams.new(options[:base_uri])
31
+ Condition.build_uri(queryParams, options[:conditions]) if options[:conditions]
32
+ queryParams.full_text_search(options[:q]) if options[:q]
33
+ queryParams.build_uri()
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'query_params/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "query_params"
8
+ spec.version = QueryParams::VERSION
9
+ spec.authors = ["William Souza"]
10
+ spec.email = ["willmiranda@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
14
+ end
15
+
16
+ spec.summary = %q{Format URL parameters like a query.}
17
+ spec.description = %q{It allows you to send operators and the same parameter twice on the same request.}
18
+ spec.homepage = "https://github.com/willmiranda/query-params"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.8"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency 'rspec'
29
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - William Souza
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: It allows you to send operators and the same parameter twice on the same
56
+ request.
57
+ email:
58
+ - willmiranda@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/query_params.rb
72
+ - lib/query_params/condition.rb
73
+ - lib/query_params/filter.rb
74
+ - lib/query_params/version.rb
75
+ - query_params.gemspec
76
+ - tasks/rspec.rake
77
+ homepage: https://github.com/willmiranda/query-params
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Format URL parameters like a query.
102
+ test_files: []