Class: Jsonar::CLI
- Inherits:
-
Object
- Object
- Jsonar::CLI
- Defined in:
- lib/jsonar/cli.rb
Class Method Summary collapse
- .get_query ⇒ Object
- .run(args = []) ⇒ Object
- .search(index, query) ⇒ Object
- .show_results(result) ⇒ Object
Class Method Details
.get_query ⇒ Object
35 36 37 38 39 |
# File 'lib/jsonar/cli.rb', line 35 def self.get_query puts 'What are you looking for? (press Ctrl-c to exit)' query = STDIN.gets query && query.chomp || query end |
.run(args = []) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jsonar/cli.rb', line 6 def self.run(args = []) begin index = Jsonar::Indexer.from_files(args) rescue ArgumentError puts 'Please specify a JSON file to search in' puts 'Usage: jsonar [FILE]...' return rescue Errno::ENOENT => e puts e. return rescue JSON::ParserError => e puts 'JSON file is invalid' puts e. return end loop do query = get_query results = search(index, query) show_results(results) end end |
.search(index, query) ⇒ Object
41 42 43 |
# File 'lib/jsonar/cli.rb', line 41 def self.search(index, query) index[query] end |
.show_results(result) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/jsonar/cli.rb', line 45 def self.show_results(result) if result ap result else puts 'No matching record found.' end end |