jsonar 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c45310b7d167aa22850ae59d36b5050e8b33b4ef
4
+ data.tar.gz: e0841d57267d6848a6bb5556f68329197f3d0a64
5
+ SHA512:
6
+ metadata.gz: e792d26c6200c87302639ea059725c731b076ccb24af4573d1552e83ff252fb7c2cbc002ad3a93a1084ca0eaa22744eeacedf387929403efc932a1a674ae145e
7
+ data.tar.gz: 49925f2330016e636e471098cae1d51544c0ed02e780fc02e7e5274e3b93e70ec6a3496d37d0b62ff39ba68dafa7a8b47595e6b7564a9bf2ffeb1ecba0be2472
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'jsonar'
3
+ Jsonar::CLI.run(ARGV)
@@ -0,0 +1,5 @@
1
+ module Jsonar
2
+ require 'jsonar/cli'
3
+ require 'jsonar/indexer'
4
+ require 'jsonar/version'
5
+ end
@@ -0,0 +1,53 @@
1
+ require 'jsonar/indexer'
2
+ require 'awesome_print'
3
+
4
+ module Jsonar
5
+ class CLI
6
+ def self.run(args = [])
7
+ begin
8
+ index = Jsonar::Indexer.from_files(args)
9
+ rescue ArgumentError
10
+ puts 'Please specify a JSON file to search in'
11
+ puts 'Usage: jsonar [FILE]...'
12
+ return
13
+ rescue Errno::ENOENT => e
14
+ puts e.message
15
+ return
16
+ rescue JSON::ParserError => e
17
+ puts 'JSON file is invalid'
18
+ puts e.message
19
+ return
20
+ end
21
+
22
+ loop do
23
+ query = get_query
24
+ results = search(index, query)
25
+ show_results(results)
26
+ end
27
+ end
28
+
29
+ private_class_method
30
+
31
+ trap('INT') do
32
+ exit
33
+ end
34
+
35
+ def self.get_query
36
+ puts 'What are you looking for? (press Ctrl-c to exit)'
37
+ query = STDIN.gets
38
+ query && query.chomp || query
39
+ end
40
+
41
+ def self.search(index, query)
42
+ index[query]
43
+ end
44
+
45
+ def self.show_results(result)
46
+ if result
47
+ ap result
48
+ else
49
+ puts 'No matching record found.'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,47 @@
1
+ require 'json'
2
+ require 'set'
3
+
4
+ module Jsonar
5
+ class Indexer
6
+ def self.from_files(files = [])
7
+ raise ArgumentError if files.empty?
8
+ index = {}
9
+ files.each do |file|
10
+ index = Jsonar::Indexer.from_string(File.read(file), index)
11
+ end
12
+ index
13
+ end
14
+
15
+ private_class_method
16
+
17
+ def self.from_string(json, index = {})
18
+ input = JSON.parse(json)
19
+
20
+ # assume the json input is always an array at the top level
21
+ input.each do |item|
22
+ index_fields(item, item, index)
23
+ end
24
+
25
+ index
26
+ end
27
+
28
+ def self.index_fields(root, input, index)
29
+ if input.is_a? Array
30
+ input.each do |item|
31
+ index_fields(root, item, index)
32
+ end
33
+ elsif input.is_a? Hash
34
+ input.each_value do |v|
35
+ index_fields(root, v, index)
36
+ end
37
+ else
38
+ input = input.to_s
39
+ input = 'null' if input.empty?
40
+ index[input] = index[input] || Set.new
41
+ index[input] << root
42
+ end
43
+
44
+ index
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Jsonar
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Shioju
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ description: Command-line tool for searching in JSON files
42
+ email: shioju12@gmail.com
43
+ executables:
44
+ - jsonar
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/jsonar
49
+ - lib/jsonar.rb
50
+ - lib/jsonar/cli.rb
51
+ - lib/jsonar/indexer.rb
52
+ - lib/jsonar/version.rb
53
+ homepage: http://rubygems.org/gems/jsonar
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.4.5.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Command-line tool for searching in JSON files
77
+ test_files: []