active_json_cli 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b1843965e895a4c12245c8d83919ced4ae7a4531878850efc648d2ed131bbe9f
4
+ data.tar.gz: '0068f99a699e3081963061aeb0ed2af9dc9cca1a099984ba5b0f9a4f785b5d4e'
5
+ SHA512:
6
+ metadata.gz: 634388d572a8e98fce84283e930554a9874abd9a1d6b1b7d6236e120ee8af9640f9eb5adf44e3a5b9c5c2368931734b41e5139a303eeb6e15e6bb0111203c0ef
7
+ data.tar.gz: a238c6996b978cb3abe2238057ad262f94c86d4fa46eaa515f3d74f1471064c7a4ebe3956ac92fd37fa43ea21c3a49962378fa2a00591ddc0103167642a75120
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.0-dev
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in active_json.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_json_cli (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.2)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ pry-doc (0.13.5)
16
+ pry (~> 0.11)
17
+ yard (~> 0.9.11)
18
+ rake (10.5.0)
19
+ rspec (3.8.0)
20
+ rspec-core (~> 3.8.0)
21
+ rspec-expectations (~> 3.8.0)
22
+ rspec-mocks (~> 3.8.0)
23
+ rspec-core (3.8.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-expectations (3.8.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-mocks (3.8.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-support (3.8.0)
32
+ yard (0.9.16)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ active_json_cli!
39
+ bundler (~> 1.17)
40
+ pry
41
+ pry-doc
42
+ rake (~> 10.0)
43
+ rspec (~> 3.2)
44
+
45
+ BUNDLED WITH
46
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Micah
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # ActiveJson CLI
2
+
3
+ Format, query, and pluck data from JSON response files from the command line.
4
+
5
+ ## Installation
6
+
7
+ $ gem install active_json_cli
8
+
9
+ ## Usage
10
+
11
+ ###### example.json
12
+
13
+
14
+ ```json
15
+ [
16
+ {
17
+ "drink_name": "latte",
18
+ "prices": { "small": 3.5, "medium": 4.0, "large": 4.5 },
19
+ }
20
+ ]
21
+ ```
22
+
23
+ ##### Command breakdown
24
+ $ `gem command` + `json_path` + `query filter(s)`
25
+
26
+ #### Filtering:
27
+
28
+ Filter JSON content with the `where` keyword followed by an attribute comparison filter (`==` `!=` `<=` `>=` `<` `>`).
29
+
30
+ For example running the following command...
31
+
32
+ $ bin/active_json example.json where: 'drink_name == "latte"'
33
+
34
+ ...will return all entries whose `drink_name` keyword is "latte"
35
+
36
+ If the JSON contains nested content you are able to query it as well:
37
+
38
+ $ bin/active_json example.json where: 'prices.small >= 3.5'
39
+
40
+ You are able chain any number of filters:
41
+
42
+ $ bin/active_json example.json where: 'drink_name == "latte", prices.small <= 3.5'
43
+
44
+ You can also compare attributes with one another:
45
+
46
+ $ bin/active_json example.json where: 'prices.small >= prices.large'
47
+
48
+ Running the command without a `where` filter will return all JSON entries.
49
+
50
+ #### Plucking:
51
+
52
+ If you would rather return a particular attribute rather than the whole entry you can use the `pluck` keyword to only return a specified attribute. Running...
53
+
54
+ $ bin/active_json example.json where: 'drink_name == "latte"' pluck: prices.small
55
+
56
+ ...will return the `prices.small` attribute of all the entries whose `drink_name` keyword is "latte"
57
+
58
+ Likewise running the `pluck` command without a `where` filter will return the specified attributes of all JSON entries.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
Binary file
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'active_json_cli'
8
+ spec.version = ActiveJson::VERSION
9
+ spec.authors = ['Micah']
10
+ spec.email = ['micah.boyd@protonmail.com']
11
+
12
+ spec.summary = 'Query JSON files from the command line'
13
+ spec.homepage = 'https://github.com/micahboyd/active_json_cli'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ # spec.metadata["homepage_uri"] = spec.homepage
22
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+ else
25
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
26
+ 'public gem pushes.'
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = 'bin'
35
+ spec.executables << 'active_json'
36
+ spec.require_paths = ['lib']
37
+
38
+ spec.add_development_dependency 'bundler', '~> 1.17'
39
+ spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.2'
41
+ spec.add_development_dependency 'pry'
42
+ spec.add_development_dependency 'pry-doc'
43
+ end
data/bin/active_json ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(__FILE__, '..', '..', 'lib')
4
+
5
+ require 'cli'
6
+ require 'json'
7
+
8
+ json_path, *args = ARGV
9
+
10
+ json = IO.read(json_path)
11
+
12
+ filters, attribute =
13
+ %w[where: pluck:].map { |v| i = args.index(v); args[i+1] if i }
14
+
15
+ query_result = ActiveJson::CLI.select(json, where: filters, pluck: attribute)
16
+ pretty_result = JSON.pretty_generate(query_result)
17
+
18
+ STDOUT.write(pretty_result)
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'active_json'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require 'pry'
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,91 @@
1
+ module ActiveJson
2
+ module Filter
3
+ extend self
4
+
5
+ VALID_OPERATORS = %w[== != <= >= < >].freeze
6
+
7
+ def new(args)
8
+ attributes = process_args(args)
9
+ values = attributes.map(&parse_values)
10
+ filter_lambda(values)
11
+ end
12
+
13
+ private
14
+
15
+ def filter_lambda(filter)
16
+ -> (data) do
17
+ extracted_data, *operation = filter.clone.map! do |attribute|
18
+ attribute.is_a?(Array) ? reduce_data(attribute, data) : attribute
19
+ end
20
+ extracted_data&.send(*operation) unless operation.include?(nil)
21
+ end
22
+ end
23
+
24
+ # --------- HELPERS ---------
25
+
26
+ def process_args(args)
27
+ string_quote = args['"'] || args["'"]
28
+ if string_quote
29
+ split_with_strings(args, string_quote)
30
+ else
31
+ args.split
32
+ end
33
+ end
34
+
35
+ def split_with_strings(args, quote)
36
+ string_start = args.index(quote)
37
+ string_end = args.rindex(quote)
38
+ string = args[string_start..string_end]
39
+ args.split.each.with_index(-1).with_object([]) do |(arg, i), obj|
40
+ string[arg] && string[obj[i]] ? obj[i] << " #{arg}" : obj << arg
41
+ end
42
+ end
43
+
44
+ def parse_values
45
+ -> (value) do
46
+ case value
47
+ when string then value[1..-2]
48
+ when float then value.to_f
49
+ when integer then value.to_i
50
+ when operator then value.to_sym
51
+ when chain then parse_chain(value)
52
+ else [[:[], value.to_sym]]
53
+ end
54
+ end
55
+ end
56
+
57
+ def string
58
+ -> (value) { %w[' "].any? { |q| value.start_with?(q) && value.end_with?(q) } }
59
+ end
60
+
61
+ def float
62
+ -> (value) { value.to_f.to_s == value }
63
+ end
64
+
65
+ def integer
66
+ -> (value) { value.to_i.to_s == value }
67
+ end
68
+
69
+ def operator
70
+ -> (value) { VALID_OPERATORS.include?(value) }
71
+ end
72
+
73
+ def chain
74
+ -> (value) { value['.'] }
75
+ end
76
+
77
+ def parse_chain(value)
78
+ value.gsub('.', ' [] ')
79
+ .split
80
+ .prepend(:[])
81
+ .map(&:to_sym)
82
+ .each_slice(2)
83
+ .to_a
84
+ end
85
+
86
+ def reduce_data(pairs, data)
87
+ pairs.reduce(data) { |d, pair| d.send(*pair) }
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,22 @@
1
+ module ActiveJson
2
+ module Pluck
3
+ extend self
4
+
5
+ def new(pluck)
6
+ attributes = split_nested(pluck)
7
+ -> (hash) do
8
+ attributes ? attributes.reduce(hash, &dig_attribute) : hash
9
+ end
10
+ end
11
+
12
+ def dig_attribute
13
+ -> (hash, attribute) { hash.dig(attribute) if hash }
14
+ end
15
+
16
+ def split_nested(pluck)
17
+ return unless pluck
18
+ pluck.split('.').map(&:to_sym)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_json/filter'
2
+
3
+ module ActiveJson
4
+ module Query
5
+ extend self
6
+
7
+ def select(data, where:, pluck: nil)
8
+ result = data.select(&apply_filters(where))
9
+ pluck ? result.map(&pluck_attributes(pluck)).compact : result
10
+ end
11
+
12
+ def reject(data, where:, pluck: nil)
13
+ result = data.reject(&apply_filters(where))
14
+ pluck ? result.map(&pluck_attributes(pluck)).compact : result
15
+ end
16
+
17
+ private
18
+
19
+ def apply_filters(filters)
20
+ -> (record) do
21
+ filters.all? { |filter| filter.call(record) }
22
+ end
23
+ end
24
+
25
+ def pluck_attributes(pluck)
26
+ -> (record) { pluck.call(record) }
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveJson
2
+ VERSION = '0.1.1'
3
+ end
data/lib/cli.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'lib'
2
+ require 'json'
3
+
4
+ module ActiveJson
5
+ module CLI
6
+ class << self
7
+
8
+ def select(json, where:, pluck: nil)
9
+ data = parse_json(json)
10
+ filter, pluck = build_query(where, pluck)
11
+ Query.select(data, where: filter, pluck: pluck)
12
+ end
13
+
14
+ def reject(json, where:, pluck: nil)
15
+ data = parse_json(json)
16
+ filter, pluck = build_query(where, pluck)
17
+ Query.reject(data, where: filter, pluck: pluck)
18
+ end
19
+
20
+ private
21
+
22
+ def build_query(where, pluck)
23
+ [
24
+ where&.split(',')&.map(&filter) || [],
25
+ Pluck.new(pluck)
26
+ ]
27
+ end
28
+
29
+ def parse_json(json)
30
+ JSON.parse(json, symbolize_names: true)
31
+ end
32
+
33
+ def filter
34
+ -> (attrs) { Filter.new(attrs.strip) }
35
+ end
36
+ end
37
+
38
+ end
39
+ end
data/lib/lib.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'active_json/version'
2
+ require 'active_json/query'
3
+ require 'active_json/filter'
4
+ require 'active_json/pluck'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_json_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Micah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-doc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - micah.boyd@protonmail.com
86
+ executables:
87
+ - active_json
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - active_json_cli-0.1.0.gem
101
+ - active_json_cli.gemspec
102
+ - bin/active_json
103
+ - bin/console
104
+ - bin/setup
105
+ - lib/active_json/filter.rb
106
+ - lib/active_json/pluck.rb
107
+ - lib/active_json/query.rb
108
+ - lib/active_json/version.rb
109
+ - lib/cli.rb
110
+ - lib/lib.rb
111
+ homepage: https://github.com/micahboyd/active_json_cli
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.0.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Query JSON files from the command line
134
+ test_files: []