query_params 1.1.1 → 1.2.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.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/lib/query_params/condition.rb +6 -6
- data/lib/query_params/version.rb +1 -1
- data/lib/query_params.rb +22 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e8bb9564113b072437e39a807cad77db2a80021
|
|
4
|
+
data.tar.gz: b8504a4728fddb28797cddb23467726b886e520d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 67337a313336cb63b9ffa83db118a1bebc0172979539255b510c38c4de43be176e779619655417fa2d4482eed5686958f9346a57e7b05131d7f0e303a68df6c8
|
|
7
|
+
data.tar.gz: 6c70590522a3198f742ecd016fb02359394490c78c1109d26e3a7dfb18ab97388d10f1dd58e863ccf37e9aa1d8c71d00729265e97d2a60c540ad9a21575e6d48
|
data/README.md
CHANGED
|
@@ -27,8 +27,13 @@ Or install it yourself as:
|
|
|
27
27
|
## Usage
|
|
28
28
|
|
|
29
29
|
```ruby
|
|
30
|
-
|
|
30
|
+
# Complete URI
|
|
31
|
+
URI::QueryParams.build_uri(base_uri: "http://domain.com/search", q: "Mark", filters: ["age <= 18", "type = 1"])
|
|
31
32
|
# => "http://domain.com/search?q=Mark&filters=age::le(18)|type::eq(1)"
|
|
33
|
+
|
|
34
|
+
# Only query params
|
|
35
|
+
URI::QueryParams.filters(filters: ["age <= 18", "type = 1"])
|
|
36
|
+
# => "age::le(18)|type::eq(1)"
|
|
32
37
|
```
|
|
33
38
|
|
|
34
39
|
## Contributing
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
require "query_params"
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module Condition
|
|
4
4
|
|
|
5
5
|
OPERATORS = { "=" => "eq", ">=" => "ge", "<=" => "le" }
|
|
6
6
|
|
|
7
|
-
def
|
|
7
|
+
def set_conditions(conditions)
|
|
8
8
|
|
|
9
9
|
if conditions.kind_of?(Array)
|
|
10
10
|
conditions.each do |condition|
|
|
11
|
-
set_query(
|
|
11
|
+
set_query(condition)
|
|
12
12
|
end
|
|
13
13
|
else
|
|
14
|
-
set_query(
|
|
14
|
+
set_query(conditions)
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
def
|
|
18
|
+
def set_query(condition)
|
|
19
19
|
tokens = condition.split(" ")
|
|
20
20
|
|
|
21
21
|
raise(ArgumentError, "Invalid condition: #{condition}. Send operator separate for space.") if tokens.size < 3
|
|
@@ -26,7 +26,7 @@ class Condition
|
|
|
26
26
|
|
|
27
27
|
raise(ArgumentError, "Invalid operator. Accepted tokens: #{OPERATORS.values}") if OPERATORS[operator].nil?
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
self.send(OPERATORS[operator], field, value)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
end
|
data/lib/query_params/version.rb
CHANGED
data/lib/query_params.rb
CHANGED
|
@@ -6,11 +6,17 @@ module URI
|
|
|
6
6
|
class QueryParams
|
|
7
7
|
|
|
8
8
|
include Filter
|
|
9
|
+
include Condition
|
|
9
10
|
|
|
10
11
|
def initialize(base_uri = "")
|
|
11
12
|
@base_uri = base_uri.nil? ? "" : base_uri
|
|
12
13
|
@params = []
|
|
13
14
|
@query = ""
|
|
15
|
+
@filter_param_name = "filters"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def set_filter_param_name(name = @filter_param_name)
|
|
19
|
+
@filter_param_name = name
|
|
14
20
|
end
|
|
15
21
|
|
|
16
22
|
def build_uri()
|
|
@@ -22,16 +28,30 @@ module URI
|
|
|
22
28
|
uri += "#{@query}"
|
|
23
29
|
uri += "&" unless @params.empty?
|
|
24
30
|
end
|
|
25
|
-
uri += "
|
|
31
|
+
uri += "#{@filter_param_name}=#{build_filters()}" unless @params.empty?
|
|
26
32
|
uri = "" if uri.length == 1
|
|
27
33
|
"#{@base_uri}#{uri}"
|
|
28
34
|
end
|
|
29
35
|
|
|
30
36
|
def self.build_uri(options = {})
|
|
31
37
|
queryParams = QueryParams.new(options[:base_uri])
|
|
32
|
-
|
|
38
|
+
queryParams.set_conditions(options[:filters]) if options[:filters]
|
|
33
39
|
queryParams.full_text_search(options[:q]) if options[:q]
|
|
40
|
+
queryParams.filter_param_name(options[:filter_param_name]) if options[:filter_param_name]
|
|
34
41
|
queryParams.build_uri()
|
|
35
42
|
end
|
|
43
|
+
|
|
44
|
+
def self.filters(filters)
|
|
45
|
+
if filters.nil? || filters.empty?
|
|
46
|
+
raise(ArgumentError, "Missing required parameter filters. Example: ['age >= 18']")
|
|
47
|
+
end
|
|
48
|
+
queryParams = QueryParams.new()
|
|
49
|
+
queryParams.set_conditions(filters)
|
|
50
|
+
queryParams.build_filters()
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def build_filters()
|
|
54
|
+
ERB::Util.url_encode(@params.join("|"))
|
|
55
|
+
end
|
|
36
56
|
end
|
|
37
57
|
end
|