grepg 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/grepg/parser.rb +29 -15
- data/lib/grepg/version.rb +1 -1
- data/spec/grepg/parser_spec.rb +36 -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: 5970896fb073a016c531753aee5a4ea8e23537c1
|
4
|
+
data.tar.gz: f2ab740989e19f697af5fe94bcb3b79fcb396925
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7632215dcd09df77023e986964e939d4d6637ab72cc929b54d30651a7fb35fb1ec51adbb30e490da8ce065fb91bd69ba4b3f6bc826df2d754b33bd7638c372fb
|
7
|
+
data.tar.gz: da479cce6948f4f90bc274785b0055d12e533e9847431c93f59b262779d03609d903c42e24a65b4b3aca4ba9443b277dcf06a8cca2073c592ba723d520ae9fec
|
data/lib/grepg/parser.rb
CHANGED
@@ -23,6 +23,11 @@ module GrepPage
|
|
23
23
|
:type => :string,
|
24
24
|
:required => false,
|
25
25
|
:short => "-s"
|
26
|
+
opt :search_operator,
|
27
|
+
"One of: OR, AND",
|
28
|
+
:type => :string,
|
29
|
+
:default => default_config['search_operator'],
|
30
|
+
:short => "-o"
|
26
31
|
opt :colorize,
|
27
32
|
"colorize output",
|
28
33
|
:type => :boolean,
|
@@ -57,7 +62,8 @@ Defaults:
|
|
57
62
|
@user = @opts[:user]
|
58
63
|
@topic = @opts[:topic]
|
59
64
|
@search_term = @opts[:search]
|
60
|
-
@
|
65
|
+
@search_operator = (@opts[:search_operator] || "and").upcase.to_sym
|
66
|
+
@colorize = @opts[:colorize] || true
|
61
67
|
end
|
62
68
|
|
63
69
|
def self.get_default_config
|
@@ -84,48 +90,56 @@ Defaults:
|
|
84
90
|
GrepPage::API.cheats(user, sheet_id)
|
85
91
|
end
|
86
92
|
|
87
|
-
def filter_cheats(cheats, search_term)
|
93
|
+
def filter_cheats(cheats, search_term, search_operator)
|
88
94
|
cheats.select do |cheat|
|
89
|
-
|
90
|
-
|
95
|
+
search_term.split(' ').map do |token|
|
96
|
+
text_to_search = [ cheat[:description],
|
97
|
+
cheat[:command]
|
98
|
+
].join(' ').downcase
|
99
|
+
text_to_search.include? token.downcase
|
100
|
+
end.reduce {|a,b| search_operator == :AND ? a && b : a || b}
|
91
101
|
end
|
92
102
|
end
|
93
103
|
|
94
|
-
def process_args
|
95
|
-
headers = ["User: #{user}"]
|
96
|
-
headers << "Topic: #{topic}" if topic
|
97
|
-
|
104
|
+
def process_args
|
105
|
+
headers = ["User: #{@user}"]
|
106
|
+
headers << "Topic: #{@topic}" if @topic
|
107
|
+
if(@search_term)
|
108
|
+
label = "Search-Term: #{@search_term}"
|
109
|
+
label += "(#{@search_operator})" if @search_term.split(' ').count > 1
|
110
|
+
headers << label
|
111
|
+
end
|
98
112
|
puts headers.join(", ")
|
99
113
|
|
100
114
|
begin
|
101
|
-
topics = get_all_topics(user)
|
115
|
+
topics = get_all_topics(@user)
|
102
116
|
rescue RestClient::ResourceNotFound
|
103
117
|
raise GrepPage::NotFoundError, "Unable to find user"
|
104
118
|
end
|
105
119
|
|
106
|
-
unless topic
|
120
|
+
unless @topic
|
107
121
|
# No topic specified so show all topics
|
108
122
|
puts "Available Topics => "
|
109
123
|
puts topics.map{|t| t[:name]}
|
110
124
|
return
|
111
125
|
end
|
112
126
|
|
113
|
-
topic = filter_topics(topics, topic)
|
127
|
+
topic = filter_topics(topics, @topic)
|
114
128
|
if topic.nil? || topic.empty?
|
115
129
|
puts "Can't find that topic. Choose one of the following"
|
116
130
|
puts topics.map{|t| t[:name]}
|
117
131
|
raise GrepPage::NotFoundError, "Unable to find topic"
|
118
132
|
end
|
119
133
|
|
120
|
-
cheats = get_cheats(user, topic[:id])
|
121
|
-
cheats = filter_cheats(cheats, search_term) if search_term
|
134
|
+
cheats = get_cheats(@user, topic[:id])
|
135
|
+
cheats = filter_cheats(cheats, @search_term, @search_operator) if @search_term
|
122
136
|
|
123
|
-
GrepPage::Formatter.cheat_rows(cheats, search_term, colorize)
|
137
|
+
GrepPage::Formatter.cheat_rows(cheats, @search_term, @colorize)
|
124
138
|
end
|
125
139
|
|
126
140
|
def run!
|
127
141
|
begin
|
128
|
-
process_args
|
142
|
+
process_args
|
129
143
|
rescue GrepPage::NotFoundError => ex
|
130
144
|
abort "Error: #{ex.message}"
|
131
145
|
end
|
data/lib/grepg/version.rb
CHANGED
data/spec/grepg/parser_spec.rb
CHANGED
@@ -2,19 +2,31 @@ require 'json'
|
|
2
2
|
require 'yaml'
|
3
3
|
require_relative '../../lib/grepg.rb'
|
4
4
|
|
5
|
+
# Integration level tests
|
5
6
|
describe GrepPage::Parser do
|
6
7
|
describe '.initialize' do
|
7
8
|
context "expected behavior" do
|
8
9
|
it "returns a set of cheats when username and topic are given" do
|
9
10
|
parser = GrepPage::Parser.new('-u kdavis -t css'.split(' '))
|
10
11
|
output = capture_stdout { parser.run! }
|
11
|
-
expect(output).to match(/
|
12
|
+
expect(output).to match(/palette/)
|
12
13
|
end
|
13
14
|
|
14
15
|
it "returns a set of cheats when username, topic and search term are given" do
|
15
16
|
parser = GrepPage::Parser.new('-u kdavis -t css -s colors'.split(' '))
|
16
17
|
output = capture_stdout { parser.run! }
|
17
|
-
expect(output).to match(/
|
18
|
+
expect(output).to match(/palette/)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns a set of cheats search term has multiple words" do
|
22
|
+
args = '-u kdavis -t css -o AND -s'.split(' ')
|
23
|
+
parser = GrepPage::Parser.new(args << "colors asbd")
|
24
|
+
output = capture_stdout { parser.run! }
|
25
|
+
expect(output).not_to match(/palette/)
|
26
|
+
args = '-u kdavis -t css -o OR -s'.split(' ')
|
27
|
+
expect(capture_stdout {
|
28
|
+
GrepPage::Parser.new(args << "colors asbd").run!
|
29
|
+
}).to match(/palette/)
|
18
30
|
end
|
19
31
|
|
20
32
|
# Test if we are seeing colors in the output
|
@@ -81,6 +93,28 @@ describe GrepPage::Parser do
|
|
81
93
|
expect(GrepPage::Parser.get_default_config).to eq(default_contents)
|
82
94
|
end
|
83
95
|
end
|
96
|
+
|
97
|
+
describe '#filter_cheats' do
|
98
|
+
let ( :parser ) { GrepPage::Parser.new('-u kdavis'.split(' ')) }
|
99
|
+
let ( :cheats ) {[{
|
100
|
+
description: "bootstrap styles $@ 123",
|
101
|
+
command: "command for bootstrap styles"
|
102
|
+
}]}
|
103
|
+
|
104
|
+
it('filters cheats based on search term') do
|
105
|
+
expect(parser.filter_cheats(cheats, 'boot style', :AND).count).to be > 0
|
106
|
+
expect(parser.filter_cheats(cheats, 'boot', :AND).count).to be > 0
|
107
|
+
# test special characters
|
108
|
+
expect(parser.filter_cheats(cheats, '$@', :AND).count).to be > 0
|
109
|
+
end
|
110
|
+
|
111
|
+
it('processes AND and OR for queries') do
|
112
|
+
# process OR
|
113
|
+
expect(parser.filter_cheats(cheats, 'boot garbage', :OR).count).to be > 0
|
114
|
+
# process AND
|
115
|
+
expect(parser.filter_cheats(cheats, 'boot garbage', :AND).count).to be 0
|
116
|
+
end
|
117
|
+
end
|
84
118
|
end
|
85
119
|
|
86
120
|
|