grepg 0.0.7 → 0.0.8

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: d4461f02d7f064ba3c1b94e4c9d9e52585bfcc2a
4
- data.tar.gz: facace3baba5687fecd162bede531d71bb87fbba
3
+ metadata.gz: 5970896fb073a016c531753aee5a4ea8e23537c1
4
+ data.tar.gz: f2ab740989e19f697af5fe94bcb3b79fcb396925
5
5
  SHA512:
6
- metadata.gz: 3a5f2b0cac9fce61381180a5c17a9674819a3580377a601e2190eb9dc978c656827a6629bce6ce634a2e6e7dcb1623a2ff8f7f05688ce29bc988f14c55a5a940
7
- data.tar.gz: 05a78e200bede2dfafce4a7680c73c099e245172592c253d073bc593247a8dba21dd00bf766e29c438c4f5c3c6dafb4a2e3df1bd7cf5a6147f5c894809a3bc24
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
- @colorize = @opts[:colorize]
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
- (cheat[:description].downcase[search_term.downcase] ||
90
- cheat[:command].downcase[search_term.downcase]) != nil
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(user, topic, search_term, colorize)
95
- headers = ["User: #{user}"]
96
- headers << "Topic: #{topic}" if topic
97
- headers << "Search-Term: #{search_term}" if search_term
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(@user, @topic, @search_term, @colorize)
142
+ process_args
129
143
  rescue GrepPage::NotFoundError => ex
130
144
  abort "Error: #{ex.message}"
131
145
  end
data/lib/grepg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module GrepPage
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -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(/colors/)
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(/colors/)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grepg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yash Ranadive