zn 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 346238f45cbf0b747669a91e1f2f9f6976be1e71
4
- data.tar.gz: f9545545d62a6a03d096a49bcdf2363f1f2d9d72
3
+ metadata.gz: 169966541807052462e0dcc386a5a2946ee19c27
4
+ data.tar.gz: a9c201bb92ec31de84a59bcf20a552a839feb014
5
5
  SHA512:
6
- metadata.gz: 2f5309ccf119b54a98d134104565157fe103c33333c6193722f015eead15a422042cc7648bdd3f0fa6136cb7b45cc11b7f7a61ad6ed191f1a7480abbb5eebef1
7
- data.tar.gz: 8dc69e5f0c5dd463220a90f9fb7d1a6bab86c138b296720c807d6a3e181ec0a161930914144631bf4d0e13ea13080d1b63344b9a25338f6c6adcd4ef54eefea5
6
+ metadata.gz: 55f9ee7f95c5be03460fe30bd20ddac9677188200f6b41aec2b6f8ca17715119e3847c109f933eddb0b4bf5f4f3b1097408b75676ad7c68ab848096706e0cbbd
7
+ data.tar.gz: 82dd515ec234cffc7a43f7a49577c9975c0fb53e699906a3e2da4005c68ad1ff9e495d7a3083789b7640fa6d3e220508ef96833d34932ae011a2c1467f01ef9d
data/Dockerfile ADDED
@@ -0,0 +1,17 @@
1
+ FROM ruby:2.4
2
+
3
+ MAINTAINER Latheesh <latheeshp@gmail.com>
4
+
5
+ RUN apt-get update && apt-get install -yqq git bash make gcc vim
6
+
7
+ RUN { echo 'install: --no-document'; echo 'update: --no-document'; } >> /etc/gemrc
8
+
9
+ WORKDIR /app
10
+
11
+ COPY . /app
12
+
13
+ RUN bin/setup
14
+ RUN bin/install
15
+
16
+ WORKDIR /app
17
+ ENTRYPOINT ["bash"]
data/README.md CHANGED
@@ -1,39 +1,117 @@
1
1
  # Zn
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zn`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Zn is a tool to search and correlate data sources (JSON files for now).
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
7
+ ### Local
10
8
 
11
- ```ruby
12
- gem 'zn'
9
+ ```
10
+ $ git clone https://github.com/lpadukana/zn
11
+ $ bin/setup
12
+ $ bin/install
13
13
  ```
14
14
 
15
- And then execute:
16
-
17
- $ bundle
15
+ ### Docker
18
16
 
19
- Or install it yourself as:
17
+ ```
18
+ $ docker run -it latheeshp/zn:latest
19
+ ```
20
20
 
21
- $ gem install zn
21
+ Note: For latest changes, please use `exe/zn` instead of depending on the installed version.
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ This tool depends on an index to understand the relationship between datasets. An example is available at `spec/fixtures/starwars/index.yml`. In this case, this file defines how `people.json`, `planets.json` and `robots.json` are related.
26
+
27
+ ### Search Examples
28
+
29
+ ```sh
30
+ $ zn search \
31
+ --config-file=spec/fixtures/starwars/index.yml \
32
+ --dataset robots \
33
+ --key id \
34
+ --value r2
35
+ {"id":"r2","name":"R2-D2","owner_id":"obi"}
36
+
37
+ # config file can be set using environment variables
38
+ $ export ZN_CONFIG_FILE=spec/fixtures/starwars/index.yml
39
+
40
+ $ zn search --dataset people --key name --value Kenobi
41
+ {"id":"obi","name":"Obi Wan Kenobi","planet_id":"ste"}
42
+ ```
43
+
44
+ ### Search and Associate Example
45
+
46
+ ```sh
47
+ $ export ZN_CONFIG_FILE=spec/fixtures/starwars/index.yml
48
+
49
+ $ zn search --dataset robots --key id --value r2 | zn associate --dataset robots
50
+ {"id":"r2","name":"R2-D2","owner_id":"obi","owner":{"id":"obi","name":"Obi Wan Kenobi","planet_id":"ste"}}
51
+
52
+ $ zn search --dataset people --key name --value Kenobi | zn associate --dataset people
53
+ {"id":"obi","name":"Obi Wan Kenobi","planet_id":"ste","robots":[{"id":"r2","name":"R2-D2","owner_id":"obi"},{"id":"c3po","name":"C-3PO","owner_id":"obi"}],"home_planet":{"id":"ste","name":"Stewjon"}}
54
+ ```
55
+
56
+ ### Search, Associate and Pretty-print Example
57
+
58
+ ```sh
59
+ $ export ZN_CONFIG_FILE=spec/fixtures/starwars/index.yml
60
+
61
+ $ zn search --dataset people --key name --value Kenobi | zn associate --dataset people | zn prettify-json
62
+ {
63
+ "id": "obi",
64
+ "name": "Obi Wan Kenobi",
65
+ "planet_id": "ste",
66
+ "robots": [
67
+ {
68
+ "id": "r2",
69
+ "name": "R2-D2",
70
+ "owner_id": "obi"
71
+ },
72
+ {
73
+ "id": "c3po",
74
+ "name": "C-3PO",
75
+ "owner_id": "obi"
76
+ }
77
+ ],
78
+ "home_planet": {
79
+ "id": "ste",
80
+ "name": "Stewjon"
81
+ }
82
+ }
83
+ ```
84
+
85
+ ## Tests
86
+
87
+ ### Rspec
88
+
89
+ ```sh
90
+ $ bin/rspec-test
91
+ ```
92
+
93
+ ### Style Checks
94
+
95
+ ```sh
96
+ $ bin/style-check
97
+ ```
98
+
99
+ ### Smoke Tests
100
+
101
+ ```sh
102
+ $ bin/smoke-test
103
+ ```
26
104
 
27
105
  ## Development
28
106
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
108
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+ To install this gem onto your local machine, run `bin/install`. To release a new version, update the version number in `version.rb`, and then run `bin/release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
110
 
33
111
  ## Contributing
34
112
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/lpadukana/zn.
113
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/lpadukana/zn>.
36
114
 
37
115
  ## License
38
116
 
39
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
117
+ Available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/bin/install ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euxo pipefail
4
+
5
+ bundle exec rake install
data/bin/release ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euxo pipefail
4
+
5
+ bundle exec rake release
data/bin/rspec-test ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euxo pipefail
4
+
5
+ bundle exec rspec
data/bin/smoke-test ADDED
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+
5
+ # Smoke-tests
6
+
7
+ mkdir -p /tmp/zn
8
+
9
+ fail() {
10
+ echo -e "\\033[31mFAIL: $*\\033[0m"
11
+ exit 1
12
+ }
13
+
14
+ run () {
15
+ echo -e ">>> \\033[1m$*\\033[0m"
16
+ sh -c "$@" > >(tee /tmp/zn/stdout) 2> >(tee /tmp/zn/stderr >&2)
17
+ echo ""
18
+ }
19
+
20
+ check_output() {
21
+ if ! grep -q "${*?}" /tmp/zn/stdout; then
22
+ fail "Expected output: $*"
23
+ fi
24
+ }
25
+
26
+ # Exercise help
27
+ run "exe/zn --help"
28
+ check_output "Search the text in values of the given key"
29
+ check_output "Find associated entities"
30
+ check_output "List keys in a dataset"
31
+ check_output "List known datasets"
32
+ check_output "Pretty print JSON"
33
+ check_output "Usage:"
34
+ check_output "Parameters:"
35
+ check_output "Subcommands:"
36
+ check_output "Options:"
37
+
38
+ run "exe/zn search \
39
+ --config-file=spec/fixtures/starwars/index.yml \
40
+ --dataset robots \
41
+ --key id \
42
+ --value r2"
43
+ check_output '{"id":"r2","name":"R2-D2","owner_id":"obi"}'
44
+
45
+ run "exe/zn search \
46
+ --config-file=spec/fixtures/starwars/index.yml \
47
+ --dataset robots \
48
+ --key id \
49
+ --value r2 |\
50
+ exe/zn associate \
51
+ --config-file=spec/fixtures/starwars/index.yml \
52
+ --dataset robots"
53
+ check_output '{"id":"r2","name":"R2-D2","owner_id":"obi","owner":{"id":"obi","name":"Obi Wan Kenobi","planet_id":"ste"}}'
54
+
55
+ run "exe/zn search \
56
+ --config-file=spec/fixtures/starwars/index.yml \
57
+ --dataset robots \
58
+ --key id \
59
+ --value r2 |\
60
+ exe/zn associate \
61
+ --config-file=spec/fixtures/starwars/index.yml \
62
+ --dataset robots |\
63
+ exe/zn prettify-json"
64
+ check_output '{
65
+ "id": "r2",
66
+ "name": "R2-D2",
67
+ "owner_id": "obi",
68
+ "owner": {
69
+ "id": "obi",
70
+ "name": "Obi Wan Kenobi",
71
+ "planet_id": "ste"
72
+ }
73
+ }'
74
+
75
+ run "exe/zn search \
76
+ --config-file=spec/fixtures/starwars/index.yml \
77
+ --dataset people \
78
+ --key name \
79
+ --value Obi |\
80
+ exe/zn associate \
81
+ --config-file=spec/fixtures/starwars/index.yml \
82
+ --dataset people |\
83
+ exe/zn prettify-json"
84
+ check_output '{
85
+ "id": "obi",
86
+ "name": "Obi Wan Kenobi",
87
+ "planet_id": "ste",
88
+ "robots": \[
89
+ {
90
+ "id": "r2",
91
+ "name": "R2-D2",
92
+ "owner_id": "obi"
93
+ },
94
+ {
95
+ "id": "c3po",
96
+ "name": "C-3PO",
97
+ "owner_id": "obi"
98
+ }
99
+ \],
100
+ "home_planet": {
101
+ "id": "ste",
102
+ "name": "Stewjon"
103
+ }
104
+ }'
105
+
106
+ run 'exe/zn search \
107
+ --config-file=spec/fixtures/starwars/index.yml \
108
+ --dataset planets \
109
+ --key name \
110
+ --value "Outer Rim" |\
111
+ exe/zn associate \
112
+ --config-file=spec/fixtures/starwars/index.yml \
113
+ --dataset planets |\
114
+ exe/zn prettify-json'
115
+ check_output '{
116
+ "id": "ou-3453",
117
+ "name": "Outer Rim 3453",
118
+ "residents": \[
119
+ {
120
+ "id": "rev",
121
+ "name": "Darth Revan",
122
+ "planet_id": "ou-3453"
123
+ }
124
+ \]
125
+ }'
126
+
127
+ run 'zn list-keys \
128
+ --config-file spec/fixtures/starwars/index.yml \
129
+ --dataset robots'
130
+ check_output 'id
131
+ name
132
+ owner_id'
133
+
134
+ run 'zn list-datasets \
135
+ --config-file spec/fixtures/starwars/index.yml'
136
+ check_output 'people
137
+ planets
138
+ robots'
139
+
140
+ echo -e "\\033[32mSUCCESS\\033[0m"
data/bin/test CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  set -euxo pipefail
4
4
 
5
- bin/style-check && bundle exec rspec
5
+ bin/style-check && bin/rspec-test
data/lib/zn/cli.rb CHANGED
@@ -59,15 +59,16 @@ module Zn
59
59
  required: true, environment_variable: 'ZN_CONFIG_FILE'
60
60
 
61
61
  def execute
62
- puts search_assembly(config_file).list_keys(dataset: dataset)
62
+ puts search_assembly(config_file).dataset_keys(dataset: dataset)
63
63
  end
64
64
  end
65
65
 
66
66
  subcommand 'list-datasets', 'List known datasets' do
67
- option ['--config-file', '-c'], 'CONFIG_FILE', 'Config that defines datasets', required: true
67
+ option ['--config-file', '-c'], 'CONFIG_FILE', 'Config that defines datasets',
68
+ required: true, environment_variable: 'ZN_CONFIG_FILE'
68
69
 
69
70
  def execute
70
- puts search_assembly(config_file).list_datasets
71
+ puts search_assembly(config_file).dataset_names
71
72
  end
72
73
  end
73
74
 
data/lib/zn/dataset.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'forwardable'
4
+ require 'set'
4
5
 
5
6
  module Zn
6
7
  # Holds repo, associators and performs aggregate operations
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'multi_json'
4
- require 'jq/extend'
5
4
 
6
5
  module Zn
7
6
  module Repos
@@ -29,25 +28,31 @@ module Zn
29
28
  value = params.fetch(:value)
30
29
  exact = params.fetch(:exact)
31
30
 
32
- query = exact ? search_exact_query(key, value) : search_query(key, value)
33
-
34
- all.jq(query).map do |result|
35
- yield result
31
+ if exact
32
+ search_exact(key, value) { |result| yield result }
33
+ else
34
+ search_for_inclusion(key, value) { |result| yield result }
36
35
  end
37
36
  end
38
37
 
39
38
  private
40
39
 
41
- def search_query(key, sub_string)
42
- if sub_string.to_s.empty?
43
- ".[] | select(.#{key}|tostring == \"\")"
44
- else
45
- ".[] | select(.#{key}|tostring | contains(\"#{sub_string}\"))"
40
+ def search_exact(key, value)
41
+ return enum_for(:search_exact, key, value) unless block_given?
42
+
43
+ all.each do |element|
44
+ yield element if element[key].to_s == value.to_s
46
45
  end
47
46
  end
48
47
 
49
- def search_exact_query(key, value)
50
- ".[] | select(.#{key}|tostring == \"#{value}\")"
48
+ def search_for_inclusion(key, value)
49
+ return enum_for(:search_for_inclusion, key, value) unless block_given?
50
+
51
+ return [].each if value.empty?
52
+
53
+ all.each do |element|
54
+ yield element if element[key].to_s.include?(value.to_s)
55
+ end
51
56
  end
52
57
  end
53
58
  end
data/lib/zn/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zn
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
data/zn.gemspec CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ['lib']
29
29
 
30
30
  spec.add_dependency 'multi_json', '~> 1.12'
31
- spec.add_dependency 'ruby-jq', '~> 0.1'
31
+ spec.add_dependency 'clamp', '~> 1.1'
32
32
 
33
33
  spec.add_development_dependency 'bundler', '~> 1.15'
34
34
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Latheesh Padukana
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-13 00:00:00.000000000 Z
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.12'
27
27
  - !ruby/object:Gem::Dependency
28
- name: ruby-jq
28
+ name: clamp
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.1'
33
+ version: '1.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.1'
40
+ version: '1.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -163,12 +163,17 @@ files:
163
163
  - ".rubocop.yml"
164
164
  - ".ruby-version"
165
165
  - ".travis.yml"
166
+ - Dockerfile
166
167
  - Gemfile
167
168
  - LICENSE.txt
168
169
  - README.md
169
170
  - Rakefile
170
171
  - bin/console
172
+ - bin/install
173
+ - bin/release
174
+ - bin/rspec-test
171
175
  - bin/setup
176
+ - bin/smoke-test
172
177
  - bin/style-check
173
178
  - bin/test
174
179
  - exe/zn