api-filters 0.0.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 +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +2 -0
- data/api-filters.gemspec +24 -0
- data/lib/api/filters.rb +36 -0
- data/lib/api/filters/between.rb +23 -0
- data/lib/api/filters/condition.rb +63 -0
- data/lib/api/filters/equal_to.rb +20 -0
- data/lib/api/filters/greater_equal_than.rb +15 -0
- data/lib/api/filters/greater_than.rb +15 -0
- data/lib/api/filters/in.rb +24 -0
- data/lib/api/filters/json.rb +11 -0
- data/lib/api/filters/lower_equal_than.rb +15 -0
- data/lib/api/filters/lower_than.rb +15 -0
- data/lib/api/filters/query.rb +24 -0
- data/lib/api/filters/version.rb +5 -0
- data/spec/api/filters/between_spec.rb +16 -0
- data/spec/api/filters/equal_to_spec.rb +45 -0
- data/spec/api/filters/greater_equal_than_spec.rb +41 -0
- data/spec/api/filters/greater_than_spec.rb +42 -0
- data/spec/api/filters/in_spec.rb +36 -0
- data/spec/api/filters/lower_equal_than_spec.rb +41 -0
- data/spec/api/filters/lower_than_spec.rb +41 -0
- data/spec/api/filters/query_spec.rb +9 -0
- data/spec/api/filters_spec.rb +24 -0
- data/spec/spec_helper.rb +11 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c52275d7c4a43af25a276c6aeede46e026808eb3
|
4
|
+
data.tar.gz: 2189467fd2c7bf0ea0cfba50ee7097dbefac5b7f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e15beab9ce8291a10c640202fc04cbb8d27643a0d48c6f24668e04fb510e1597957044ced0fe51c83d66322966c6a7d9ff6c480bfc213ec56bf9c83358edd6ff
|
7
|
+
data.tar.gz: a2f934133bbac06e2d86fcd714aed29eb4d41cba1af5bc1141aeadeb6a83f31bbfdbe170fcf123b45b665d70d5794b1655a18c9dee76fadee1d9e43b254cb509
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
api-filters
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.1
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Breno Oliveira
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Api::Filters
|
2
|
+
|
3
|
+
This gem is based on Moip API experience for specific reports. This GEM convert a URI parameters em SQL Query to execute in your Database.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'api-filters'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install api-filters
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
### Supported clauses
|
24
|
+
- In (in)
|
25
|
+
- Greater Equal Than (ge)
|
26
|
+
- Greater Than (gt)
|
27
|
+
- Lower Equal Than (le)
|
28
|
+
- Lower Than (lt)
|
29
|
+
- Between (bt)
|
30
|
+
|
31
|
+
filters=status::in(WAITING)|paymentMethod::in(CREDIT_CARD)|amount::ge(100)|amount::le(30000)
|
32
|
+
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it ( https://github.com/moip/api-filters/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
data/api-filters.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'api/filters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "api-filters"
|
8
|
+
spec.version = Api::Filters::VERSION
|
9
|
+
spec.authors = ["Breno Oliveira"]
|
10
|
+
spec.email = ["breno.oliveira@moip.com.br"]
|
11
|
+
spec.summary = %q{This GEM provide another way to use api filters }
|
12
|
+
spec.description = %q{This GEM provide another way to use api filters }
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency 'json'
|
24
|
+
end
|
data/lib/api/filters.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "json"
|
2
|
+
require "api/filters/version"
|
3
|
+
|
4
|
+
require "api/filters/condition"
|
5
|
+
require "api/filters/between"
|
6
|
+
require "api/filters/equal_to"
|
7
|
+
require "api/filters/greater_equal_than"
|
8
|
+
require "api/filters/greater_than"
|
9
|
+
require "api/filters/in"
|
10
|
+
require "api/filters/lower_equal_than"
|
11
|
+
require "api/filters/lower_than"
|
12
|
+
require "api/filters/query"
|
13
|
+
|
14
|
+
module Api
|
15
|
+
module Filters
|
16
|
+
|
17
|
+
def self.parse(string)
|
18
|
+
if string
|
19
|
+
filters = Hash.new
|
20
|
+
filters[string.split("|").map { |f| f.split("::") }.each { |f| filters[f.first] = f.last }]
|
21
|
+
return filters
|
22
|
+
end
|
23
|
+
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.condition(field, value)
|
28
|
+
[Between, EqualTo, GreaterEqualThan, GreaterThan, In, LowerEqualThan, LowerThan].each do |condition|
|
29
|
+
if condition.handle? value
|
30
|
+
return condition.new(field, value).elastic_query if condition.new(field, value).respond_to? :elastic_query
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Api
|
2
|
+
module Filters
|
3
|
+
class Between
|
4
|
+
include Api::Filters::Condition
|
5
|
+
|
6
|
+
condition_name "bt"
|
7
|
+
|
8
|
+
def build_param
|
9
|
+
{ @field => extract_param }
|
10
|
+
end
|
11
|
+
|
12
|
+
def elastic_query
|
13
|
+
param = @condition.match(/\((.*),(.*)\)/)
|
14
|
+
{ range: { :"#{@field}" => { gte: "#{param[1]}", lte: "#{param[2]}" } } }
|
15
|
+
end
|
16
|
+
|
17
|
+
def extract_param
|
18
|
+
query_param = @condition.match(/\((.*),(.*)\)/)
|
19
|
+
query_param[1]..query_param[2]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Api
|
2
|
+
module Filters
|
3
|
+
module Condition
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def condition_name(condition_name = nil)
|
12
|
+
@condition_name = condition_name if condition_name
|
13
|
+
@condition_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def condition_symbol(condition_symbol = nil)
|
17
|
+
@condition_symbol = condition_symbol if condition_symbol
|
18
|
+
@condition_symbol
|
19
|
+
end
|
20
|
+
|
21
|
+
def handle?(string)
|
22
|
+
string =~ /^#{condition_name}\(.*\)$/
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(field, condition)
|
28
|
+
@field, @condition = field, condition
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_param
|
32
|
+
{ "#{@field} #{self.class.condition_symbol} ?" => extract_param }
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
def extract_param
|
37
|
+
query_param = @condition.match(/\((.*)\)/)[1]
|
38
|
+
get_value(query_param)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_value(query_param)
|
42
|
+
extract_amount(query_param) || extract_int(query_param) || query_param
|
43
|
+
end
|
44
|
+
|
45
|
+
def extract_amount(value)
|
46
|
+
(value.to_f/100.0) if @field == "total_value"
|
47
|
+
end
|
48
|
+
|
49
|
+
def extract_int(value)
|
50
|
+
value.to_i if !!(value =~ /^[-+]?[0-9]+$/)
|
51
|
+
end
|
52
|
+
|
53
|
+
# def extract_status(value)
|
54
|
+
# Array(value).map { |status| ReportsWorkers::Utils::Status.parse(status) }.flatten if @field == "status"
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# def extract_payment_method(value)
|
58
|
+
# Array(value).map { |paymentMethod| ReportsWorkers::Utils::PaymentMethod.parse(paymentMethod) }.flatten if @field == "payment_type"
|
59
|
+
# end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Api
|
2
|
+
module Filters
|
3
|
+
class EqualTo
|
4
|
+
include Api::Filters::Condition
|
5
|
+
|
6
|
+
condition_name "eq"
|
7
|
+
condition_symbol "="
|
8
|
+
|
9
|
+
def build_param
|
10
|
+
return super unless ["status", "paymentMethod"].include? @field
|
11
|
+
{ "#{@field}" => extract_param }
|
12
|
+
end
|
13
|
+
|
14
|
+
def elastic_query
|
15
|
+
{ match: { :"#{@field}" => extract_param } }
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Api
|
2
|
+
module Filters
|
3
|
+
class In
|
4
|
+
include Api::Filters::Condition
|
5
|
+
|
6
|
+
condition_name "in"
|
7
|
+
|
8
|
+
def build_param
|
9
|
+
{ @field => extract_param }
|
10
|
+
end
|
11
|
+
|
12
|
+
def elastic_query
|
13
|
+
{ terms: { :"#{@field}" => extract_param } }
|
14
|
+
end
|
15
|
+
|
16
|
+
def extract_param
|
17
|
+
query_param = @condition.match(/\((.*)\)/)
|
18
|
+
query_param[1].split(",").map do |value|
|
19
|
+
get_value(value)
|
20
|
+
end.flatten.uniq
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Api
|
2
|
+
module Filters
|
3
|
+
class Query
|
4
|
+
|
5
|
+
def initialize(condition = nil)
|
6
|
+
@condition = condition
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_param
|
10
|
+
q = %(%#{@condition}%)
|
11
|
+
q.downcase!
|
12
|
+
[ "LOWER(buyer_name) LIKE ? " \
|
13
|
+
"OR LOWER(buyer_email) LIKE ? " \
|
14
|
+
"OR LOWER(reason) LIKE ? " \
|
15
|
+
"OR LOWER(owner_id) LIKE ? " \
|
16
|
+
"OR payment_used_id LIKE ? " \
|
17
|
+
"OR LOWER(order_external_id) LIKE ? " \
|
18
|
+
"OR LOWER(payment_external_id) LIKE ? " \
|
19
|
+
"OR buyer_cep LIKE ?",
|
20
|
+
q, q, q, q, q, q, q, q ] unless @condition.nil?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Api::Filters::Between do
|
4
|
+
describe "#build_param" do
|
5
|
+
|
6
|
+
context "quando informado between corretamente" do
|
7
|
+
it "retorna um range" do
|
8
|
+
expect(described_class.new("payment_date", "bt(2012-01-01,2012-02-01)").build_param).to eq({ "payment_date" => "2012-01-01".."2012-02-01" })
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context ".elastic_query" do
|
13
|
+
it { expect(described_class.new("payment_date", "bt(2012-01-01,2012-02-01)").elastic_query).to eq({ range: { payment_date: { gte: "2012-01-01", lte: "2012-02-01" } } }) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::EqualTo do
|
4
|
+
|
5
|
+
describe "#build_param" do
|
6
|
+
|
7
|
+
def build_param(field, condition)
|
8
|
+
described_class.new(field, condition).build_param
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses um inteiro" do
|
12
|
+
expect(build_param("amount", "eq(1000)")).to eq({ "amount = ?" => 1000 })
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses uma string" do
|
16
|
+
expect(build_param("amount", "eq(string)")).to eq({ "amount = ?" => "string" })
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parses uma data" do
|
20
|
+
expect(build_param("date", "eq(2013-01-01)")).to eq({ "date = ?" => "2013-01-01" })
|
21
|
+
end
|
22
|
+
|
23
|
+
# it "parses um status" do
|
24
|
+
# expect(build_param("status", "eq(WAITING)")).to eq({ "status" => [2, 3, 6]})
|
25
|
+
# end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".elastic_query" do
|
30
|
+
it { expect(described_class.new("date", "eq(2013-01-01)").elastic_query).to eq({ match: { date: "2013-01-01" } }) }
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".handle?" do
|
34
|
+
|
35
|
+
it "return true quando eq" do
|
36
|
+
expect(described_class.handle?("eq(1000)")).to be_truthy
|
37
|
+
end
|
38
|
+
|
39
|
+
it "return false quando nao eh um eq valido" do
|
40
|
+
expect(described_class.handle?("bt(10,20)")).to be_falsey
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::GreaterEqualThan do
|
4
|
+
|
5
|
+
describe "#build_param" do
|
6
|
+
|
7
|
+
def build_param(field, condition)
|
8
|
+
described_class.new(field, condition).build_param
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses um inteiro" do
|
12
|
+
expect(build_param("amount", "ge(1000)")).to eq({ "amount >= ?" => 1000 })
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses uma string" do
|
16
|
+
expect(build_param("amount", "ge(string)")).to eq({ "amount >= ?" => "string" })
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parses uma data" do
|
20
|
+
expect(build_param("date", "ge(2013-01-01)")).to eq({ "date >= ?" => "2013-01-01" })
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".elastic_query" do
|
26
|
+
it { expect(described_class.new("amount", "ge(20)").elastic_query).to eq({ range: { amount: { gte: 20 } } }) }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".handle?" do
|
30
|
+
|
31
|
+
it "return true quando gt" do
|
32
|
+
expect(described_class.handle?("ge(1000)")).to be_truthy
|
33
|
+
end
|
34
|
+
|
35
|
+
it "return false quando nao é um gt válido" do
|
36
|
+
expect(described_class.handle?("bt(10,20)")).to be_falsey
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::GreaterThan do
|
4
|
+
|
5
|
+
describe "#build_param" do
|
6
|
+
|
7
|
+
def build_param(field, condition)
|
8
|
+
described_class.new(field, condition).build_param
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses um inteiro" do
|
12
|
+
expect(build_param("amount", "gt(1000)")).to eq({ "amount > ?" => 1000 })
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses uma string" do
|
16
|
+
expect(build_param("amount", "gt(string)")).to eq({ "amount > ?" => "string" })
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parses uma data" do
|
20
|
+
expect(build_param("date", "gt(2013-01-01)")).to eq({ "date > ?" => "2013-01-01" })
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".elastic_query" do
|
26
|
+
it { expect(described_class.new("amount", "gt(20)").elastic_query).to eq({ range: { amount: { gt: 20 } } }) }
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
describe ".handle?" do
|
31
|
+
|
32
|
+
it "return true quando gt" do
|
33
|
+
expect(described_class.handle?("gt(1000)")).to be_truthy
|
34
|
+
end
|
35
|
+
|
36
|
+
it "return false quando nao é um gt válido" do
|
37
|
+
expect(described_class.handle?("bt(10,20)")).to be_falsey
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::In do
|
4
|
+
|
5
|
+
describe "#build_params" do
|
6
|
+
|
7
|
+
# it "parses 1 inteiro" do
|
8
|
+
# expect(described_class.new("status", "in(1)").build_param).to eq({ "status" => ["PAID"]})
|
9
|
+
# end
|
10
|
+
|
11
|
+
# it "parses 3 inteiros" do
|
12
|
+
# expect(described_class.new("status", "in(1, 2, 3)").build_param).to eq({ "status" => ["PAID", "WAITING"]})
|
13
|
+
# end
|
14
|
+
|
15
|
+
it "parses 3 string" do
|
16
|
+
expect(described_class.new("createdAt", "in(2010-01-01,2011-01-01,2012-01-01)").build_param).to eq({ "createdAt" => ["2010-01-01","2011-01-01","2012-01-01"]})
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".elastic_query" do
|
20
|
+
it { expect(described_class.new("createdAt", "in(2010-01-01,2011-01-01,2012-01-01)").elastic_query).to eq({terms: { createdAt: ["2010-01-01", "2011-01-01", "2012-01-01"] }}) }
|
21
|
+
end
|
22
|
+
|
23
|
+
# context "quando é informado status" do
|
24
|
+
#
|
25
|
+
# it "parses 1 status de pedido para array de status de pagamento" do
|
26
|
+
# expect(described_class.new("status", "in(PAID)").build_param).to eq({ "status" => [1,4]})
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# it "parses mais de 1 status de pedido para array de status de pagamento" do
|
30
|
+
# expect(described_class.new("status", "in(PAID,NOT_PAID,WAITING)").build_param).to eq({ "status" => [1,4,5,2,3,6]})
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::LowerEqualThan do
|
4
|
+
|
5
|
+
describe "#build_param" do
|
6
|
+
|
7
|
+
def build_param(field, condition)
|
8
|
+
described_class.new(field, condition).build_param
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses um inteiro" do
|
12
|
+
expect(build_param("amount", "le(1000)")).to eq({ "amount <= ?" => 1000 })
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses uma string" do
|
16
|
+
expect(build_param("amount", "le(string)")).to eq({ "amount <= ?" => "string" })
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parses uma data" do
|
20
|
+
expect(build_param("date", "le(2013-01-01)")).to eq({ "date <= ?" => "2013-01-01" })
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".elastic_query" do
|
26
|
+
it { expect(described_class.new("amount", "le(20)").elastic_query).to eq({ range: { amount: { lte: 20 } } }) }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".handle?" do
|
30
|
+
|
31
|
+
it "return true quando le" do
|
32
|
+
expect(described_class.handle?("le(1000)")).to be_truthy
|
33
|
+
end
|
34
|
+
|
35
|
+
it "return false quando nao é um le válido" do
|
36
|
+
expect(described_class.handle?("bt(10,20)")).to be_falsey
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::LowerThan do
|
4
|
+
|
5
|
+
describe "#build_param" do
|
6
|
+
|
7
|
+
def build_param(field, condition)
|
8
|
+
described_class.new(field, condition).build_param
|
9
|
+
end
|
10
|
+
|
11
|
+
it "parses um inteiro" do
|
12
|
+
expect(build_param("amount", "lt(1000)")).to eq({ "amount < ?" => 1000 })
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses uma string" do
|
16
|
+
expect(build_param("amount", "lt(string)")).to eq({ "amount < ?" => "string" })
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parses uma data" do
|
20
|
+
expect(build_param("date", "lt(2013-01-01)")).to eq({ "date < ?" => "2013-01-01" })
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".elastic_query" do
|
26
|
+
it { expect(described_class.new("amount", "lt(20)").elastic_query).to eq({ range: { amount: { lt: 20 } } }) }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".handle?" do
|
30
|
+
|
31
|
+
it "return true quando lt" do
|
32
|
+
expect(described_class.handle?("lt(1000)")).to be_truthy
|
33
|
+
end
|
34
|
+
|
35
|
+
it "return false quando nao é um lt válido" do
|
36
|
+
expect(described_class.handle?("bt(10,20)")).to be_falsey
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters::Query do
|
4
|
+
describe "#build_param" do
|
5
|
+
it "retorna a query com or" do
|
6
|
+
expect(described_class.new("teste").build_param).to eq(["LOWER(buyer_name) LIKE ? OR LOWER(buyer_email) LIKE ? OR LOWER(reason) LIKE ? OR LOWER(owner_id) LIKE ? OR payment_used_id LIKE ? OR LOWER(order_external_id) LIKE ? OR LOWER(payment_external_id) LIKE ? OR buyer_cep LIKE ?", "%teste%","%teste%","%teste%","%teste%","%teste%","%teste%","%teste%","%teste%"])
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Api::Filters do
|
4
|
+
|
5
|
+
describe ".parse" do
|
6
|
+
|
7
|
+
it "apenas 1 filtro" do
|
8
|
+
expect(described_class.parse("buyerName::Joao da Silva")).to eql({ "buyerName" => "Joao da Silva" })
|
9
|
+
end
|
10
|
+
|
11
|
+
it "mais de 1 filtro" do
|
12
|
+
expect(described_class.parse("buyerName::João da Silva|buyerEmail::buyer@domain.com")).to eq({
|
13
|
+
"buyerName" => "João da Silva",
|
14
|
+
"buyerEmail" => "buyer@domain.com"
|
15
|
+
})
|
16
|
+
end
|
17
|
+
|
18
|
+
it "nil" do
|
19
|
+
expect(described_class.parse(nil)).to eq({})
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api-filters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Breno Oliveira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-20 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: 'This GEM provide another way to use api filters '
|
56
|
+
email:
|
57
|
+
- breno.oliveira@moip.com.br
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".ruby-gemset"
|
65
|
+
- ".ruby-version"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- api-filters.gemspec
|
71
|
+
- lib/api/filters.rb
|
72
|
+
- lib/api/filters/between.rb
|
73
|
+
- lib/api/filters/condition.rb
|
74
|
+
- lib/api/filters/equal_to.rb
|
75
|
+
- lib/api/filters/greater_equal_than.rb
|
76
|
+
- lib/api/filters/greater_than.rb
|
77
|
+
- lib/api/filters/in.rb
|
78
|
+
- lib/api/filters/json.rb
|
79
|
+
- lib/api/filters/lower_equal_than.rb
|
80
|
+
- lib/api/filters/lower_than.rb
|
81
|
+
- lib/api/filters/query.rb
|
82
|
+
- lib/api/filters/version.rb
|
83
|
+
- spec/api/filters/between_spec.rb
|
84
|
+
- spec/api/filters/equal_to_spec.rb
|
85
|
+
- spec/api/filters/greater_equal_than_spec.rb
|
86
|
+
- spec/api/filters/greater_than_spec.rb
|
87
|
+
- spec/api/filters/in_spec.rb
|
88
|
+
- spec/api/filters/lower_equal_than_spec.rb
|
89
|
+
- spec/api/filters/lower_than_spec.rb
|
90
|
+
- spec/api/filters/query_spec.rb
|
91
|
+
- spec/api/filters_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: ''
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
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
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.2.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: This GEM provide another way to use api filters
|
117
|
+
test_files:
|
118
|
+
- spec/api/filters/between_spec.rb
|
119
|
+
- spec/api/filters/equal_to_spec.rb
|
120
|
+
- spec/api/filters/greater_equal_than_spec.rb
|
121
|
+
- spec/api/filters/greater_than_spec.rb
|
122
|
+
- spec/api/filters/in_spec.rb
|
123
|
+
- spec/api/filters/lower_equal_than_spec.rb
|
124
|
+
- spec/api/filters/lower_than_spec.rb
|
125
|
+
- spec/api/filters/query_spec.rb
|
126
|
+
- spec/api/filters_spec.rb
|
127
|
+
- spec/spec_helper.rb
|