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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e4760d86c9799b2c819f9122447add095fc4b55
4
- data.tar.gz: 0f895652f78e9638d645fc22fbf233ec651d05f8
3
+ metadata.gz: 4e8bb9564113b072437e39a807cad77db2a80021
4
+ data.tar.gz: b8504a4728fddb28797cddb23467726b886e520d
5
5
  SHA512:
6
- metadata.gz: cbb53f374616cda74e535c17ebd6443e484754d2825329a10a81a04f9ee456287a439bb44ccad8d5ce2c80d30a53db166f6a96a845ac888a8904c0c096103215
7
- data.tar.gz: 80dc0c7f0f4ed7cd87fdbab76ae5b158c6dcf5d03319cbc68ff51226ca7e7f7e84a5e3c5ccc1164bc5670a83331af48082c2a1675f970a946c52fb3e7a193609
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
- QueryParams.build_uri(base_uri: "http://domain.com/search", q: "Mark", filters: ["age <= 18", "type = 1"])
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
- class Condition
3
+ module Condition
4
4
 
5
5
  OPERATORS = { "=" => "eq", ">=" => "ge", "<=" => "le" }
6
6
 
7
- def self.build_uri(queryParams, conditions)
7
+ def set_conditions(conditions)
8
8
 
9
9
  if conditions.kind_of?(Array)
10
10
  conditions.each do |condition|
11
- set_query(queryParams, condition)
11
+ set_query(condition)
12
12
  end
13
13
  else
14
- set_query(queryParams, conditions)
14
+ set_query(conditions)
15
15
  end
16
16
  end
17
17
 
18
- def self.set_query(queryParams, condition)
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
- queryParams.send(OPERATORS[operator], field, value)
29
+ self.send(OPERATORS[operator], field, value)
30
30
  end
31
31
 
32
32
  end
@@ -1,3 +1,3 @@
1
1
  module QueryParams
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.1"
3
3
  end
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 += "filters=#{ERB::Util.url_encode(@params.join("|"))}" unless @params.empty?
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
- Condition.build_uri(queryParams, options[:filters]) if options[:filters]
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Souza