krackle 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +2 -0
- data/bin/krackle +5 -0
- data/krackle.gemspec +23 -0
- data/lib/krackle.rb +78 -0
- data/lib/krackle/version.rb +3 -0
- data/spec/fixtures/profile.yml +23 -0
- data/spec/lib/krackle_spec.rb +47 -0
- data/spec/spec_helper.rb +93 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 935cd8156c0a62d0d94d393c2cba5441004c9af5
|
4
|
+
data.tar.gz: f2cac3c129115a7f6ed77ad772007865816dc7c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8b0ff6f60a1454ea79306cf1c08f7020903a54247fbfe3355e3858f6455278ed28eab0d0129f56a6757d5be5cce8db999b78bcdbdf911010219d84268ffe17e8
|
7
|
+
data.tar.gz: 476d9f5b5a511ef14e3a2d5c504bd847e9811f581ddee3675736486ae21cdb7c370c875865cb000d8127a25df5bfaef4769df9835b042a13bdf79a0d1edc6662
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Brad Gessler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Krackle
|
2
|
+
|
3
|
+
CLI for querying and flattening YAML data.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'krackle'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install krackle
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You have a YAML file and want to pull a key or many out:
|
24
|
+
|
25
|
+
```yaml
|
26
|
+
first_name: Brad
|
27
|
+
last_name: Gessler
|
28
|
+
location:
|
29
|
+
state: CA
|
30
|
+
city: San Francisco
|
31
|
+
tax_brackets:
|
32
|
+
- high
|
33
|
+
- really high
|
34
|
+
- my eyes are bleeding high
|
35
|
+
projects:
|
36
|
+
- name: Firehose.io
|
37
|
+
url: https://github.com/firehose/gem
|
38
|
+
contributors:
|
39
|
+
- Brad Gessler
|
40
|
+
- Zach Zolton
|
41
|
+
- name: Krackle
|
42
|
+
url: https://github.com/bradgessler/krackle
|
43
|
+
contributors:
|
44
|
+
- Brad Gessler
|
45
|
+
- Ronald McDonald
|
46
|
+
- first_name: Billy
|
47
|
+
last_name: Goat
|
48
|
+
- name: Nada
|
49
|
+
```
|
50
|
+
|
51
|
+
What do you do? Krackle it!
|
52
|
+
|
53
|
+
```sh
|
54
|
+
$ curl https://raw.githubusercontent.com/bradgessler/krackle/master/spec/fixtures/profile.yml > profile.yml
|
55
|
+
$ cat profile.yml | krackle first_name
|
56
|
+
Brad
|
57
|
+
$ cat profile.yml | krackle location.state
|
58
|
+
CA
|
59
|
+
$ cat profile.yml | krackle location.tax_brackets[1]
|
60
|
+
really high
|
61
|
+
$ cat profile.yml | krackle projects[].contributors[0]
|
62
|
+
Brad Gessler
|
63
|
+
Brad Gessler
|
64
|
+
$ cat profile.yml | krackle projects[0].contributors[]
|
65
|
+
Brad Gessler
|
66
|
+
Zach Zolton
|
67
|
+
$ cat profile.yml | krackle projects[0].contributors[0]
|
68
|
+
Brad Gessler
|
69
|
+
```
|
70
|
+
|
71
|
+
Now you can query deep into YAML without cracking open a Ruby session.
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it ( https://github.com/bradgessler/krackle/fork )
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/krackle
ADDED
data/krackle.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'krackle/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "krackle"
|
8
|
+
spec.version = Krackle::VERSION
|
9
|
+
spec.authors = ["Brad Gessler"]
|
10
|
+
spec.email = ["bradgessler@gmail.com"]
|
11
|
+
spec.summary = %q{Query semi-structure data from the command line.}
|
12
|
+
spec.homepage = "https://github.com/bradgessler/krackle"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", ">= 3.0.0"
|
23
|
+
end
|
data/lib/krackle.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require "krackle/version"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
module Krackle
|
5
|
+
class Engine
|
6
|
+
def initialize(hash)
|
7
|
+
@hash = hash
|
8
|
+
end
|
9
|
+
|
10
|
+
def query(expression)
|
11
|
+
Parser.new(Tokenizer.new(expression).tokenize).parse(@hash)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Tokenizer
|
16
|
+
def initialize(str)
|
17
|
+
@str = str
|
18
|
+
end
|
19
|
+
|
20
|
+
def tokenize
|
21
|
+
scanner = StringScanner.new(@str)
|
22
|
+
tokens = []
|
23
|
+
until scanner.empty?
|
24
|
+
case
|
25
|
+
when match = scanner.scan(/\[(\d+)?\]/)
|
26
|
+
tokens << [:COLLECTION, (scanner[1] ? scanner[1].to_i : nil)]
|
27
|
+
when match =scanner.scan(/\.?(\w+)/)
|
28
|
+
tokens << [:KEY, scanner[1]]
|
29
|
+
else
|
30
|
+
raise "Say what? <#{scanner.peek(10).inspect}> at pos #{scanner.pos}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
tokens
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Parser
|
38
|
+
def initialize(tokens)
|
39
|
+
@tokens = tokens
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse(hash)
|
43
|
+
nodes = Array[hash]
|
44
|
+
# p tokenize
|
45
|
+
|
46
|
+
@tokens.each do |(token, value)|
|
47
|
+
# p [token, value], nodes, "-"*30
|
48
|
+
case token
|
49
|
+
when :KEY
|
50
|
+
nodes = nodes.map{ |node| node[value] }.compact
|
51
|
+
when :COLLECTION
|
52
|
+
if value
|
53
|
+
nodes = nodes.map{ |node| node[value] }.compact
|
54
|
+
else
|
55
|
+
nodes = nodes.flatten.compact
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
nodes
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class CLI
|
65
|
+
def initialize(args=ARGV)
|
66
|
+
@expression, @path = args
|
67
|
+
end
|
68
|
+
|
69
|
+
def run
|
70
|
+
io = if @path
|
71
|
+
File.open(@path, 'r')
|
72
|
+
else
|
73
|
+
$stdin.tty? ? raise("Specify a file path") : $stdin
|
74
|
+
end
|
75
|
+
Engine.new(YAML.load(io.read)).query(@expression).join("\n")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
first_name: Brad
|
2
|
+
last_name: Gessler
|
3
|
+
location:
|
4
|
+
state: CA
|
5
|
+
city: San Francisco
|
6
|
+
tax_brackets:
|
7
|
+
- high
|
8
|
+
- really high
|
9
|
+
- my eyes are bleeding high
|
10
|
+
projects:
|
11
|
+
- name: Firehose.io
|
12
|
+
url: https://github.com/firehose/gem
|
13
|
+
contributors:
|
14
|
+
- Brad Gessler
|
15
|
+
- Zach Zolton
|
16
|
+
- name: Krackle
|
17
|
+
url: https://github.com/bradgessler/krackle
|
18
|
+
contributors:
|
19
|
+
- Brad Gessler
|
20
|
+
- Ronald McDonald
|
21
|
+
- first_name: Billy
|
22
|
+
last_name: Goat
|
23
|
+
- name: Nada
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
describe Krackle::Engine do
|
5
|
+
subject { Krackle::Engine.new(hash) }
|
6
|
+
let(:hash) { YAML.load_file('./spec/fixtures/profile.yml') }
|
7
|
+
|
8
|
+
context "queries" do
|
9
|
+
it "values from a list" do
|
10
|
+
expect(subject.query("projects[].name")).to eql(%w[Firehose.io Krackle Nada])
|
11
|
+
end
|
12
|
+
|
13
|
+
it "item in a list in a list" do
|
14
|
+
expect(subject.query("projects[].contributors[].last_name")).to eql(["Goat"])
|
15
|
+
end
|
16
|
+
|
17
|
+
it "nth item in a list in a list" do
|
18
|
+
expect(subject.query("projects[].contributors[0]")).to eql(["Brad Gessler", "Brad Gessler"])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "nth item in an nth list in an nth list" do
|
22
|
+
expect(subject.query("projects[0].contributors[0]")).to eql(["Brad Gessler"])
|
23
|
+
end
|
24
|
+
|
25
|
+
it "list" do
|
26
|
+
expect(subject.query("projects[]").size).to eql(3)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "key" do
|
30
|
+
expect(subject.query("first_name")).to eql(["Brad"])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "deep key" do
|
34
|
+
expect(subject.query("location.state")).to eql(["CA"])
|
35
|
+
end
|
36
|
+
|
37
|
+
it "deep key list nth item" do
|
38
|
+
expect(subject.query("location.tax_brackets[1]")).to eql(["really high"])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe Krackle::CLI do
|
44
|
+
it "parses cli" do
|
45
|
+
expect(Krackle::CLI.new(["projects[].contributors[0]", "spec/fixtures/profile.yml"]).run).to eql("Brad Gessler\nBrad Gessler")
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "krackle"
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
6
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
7
|
+
# files.
|
8
|
+
#
|
9
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
10
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
11
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
12
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
13
|
+
# a separate helper file that requires the additional dependencies and performs
|
14
|
+
# the additional setup, and require it from the spec files that actually need
|
15
|
+
# it.
|
16
|
+
#
|
17
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
18
|
+
# users commonly want.
|
19
|
+
#
|
20
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
21
|
+
RSpec.configure do |config|
|
22
|
+
# rspec-expectations config goes here. You can use an alternate
|
23
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
24
|
+
# assertions if you prefer.
|
25
|
+
config.expect_with :rspec do |expectations|
|
26
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
27
|
+
# and `failure_message` of custom matchers include text for helper methods
|
28
|
+
# defined using `chain`, e.g.:
|
29
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
30
|
+
# # => "be bigger than 2 and smaller than 4"
|
31
|
+
# ...rather than:
|
32
|
+
# # => "be bigger than 2"
|
33
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
34
|
+
end
|
35
|
+
|
36
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
37
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
38
|
+
config.mock_with :rspec do |mocks|
|
39
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
40
|
+
# a real object. This is generally recommended, and will default to
|
41
|
+
# `true` in RSpec 4.
|
42
|
+
mocks.verify_partial_doubles = true
|
43
|
+
end
|
44
|
+
|
45
|
+
# The settings below are suggested to provide a good initial experience
|
46
|
+
# with RSpec, but feel free to customize to your heart's content.
|
47
|
+
=begin
|
48
|
+
# These two settings work together to allow you to limit a spec run
|
49
|
+
# to individual examples or groups you care about by tagging them with
|
50
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
51
|
+
# get run.
|
52
|
+
config.filter_run :focus
|
53
|
+
config.run_all_when_everything_filtered = true
|
54
|
+
|
55
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
56
|
+
# recommended. For more details, see:
|
57
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
58
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
59
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
60
|
+
config.disable_monkey_patching!
|
61
|
+
|
62
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
63
|
+
# be too noisy due to issues in dependencies.
|
64
|
+
config.warnings = true
|
65
|
+
|
66
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
67
|
+
# file, and it's useful to allow more verbose output when running an
|
68
|
+
# individual spec file.
|
69
|
+
if config.files_to_run.one?
|
70
|
+
# Use the documentation formatter for detailed output,
|
71
|
+
# unless a formatter has already been configured
|
72
|
+
# (e.g. via a command-line flag).
|
73
|
+
config.default_formatter = 'doc'
|
74
|
+
end
|
75
|
+
|
76
|
+
# Print the 10 slowest examples and example groups at the
|
77
|
+
# end of the spec run, to help surface which specs are running
|
78
|
+
# particularly slow.
|
79
|
+
config.profile_examples = 10
|
80
|
+
|
81
|
+
# Run specs in random order to surface order dependencies. If you find an
|
82
|
+
# order dependency and want to debug it, you can fix the order by providing
|
83
|
+
# the seed, which is printed after each run.
|
84
|
+
# --seed 1234
|
85
|
+
config.order = :random
|
86
|
+
|
87
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
88
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
89
|
+
# test failures related to randomization by passing the same `--seed` value
|
90
|
+
# as the one that triggered the failure.
|
91
|
+
Kernel.srand config.seed
|
92
|
+
=end
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: krackle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Gessler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-09 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- bradgessler@gmail.com
|
58
|
+
executables:
|
59
|
+
- krackle
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/krackle
|
70
|
+
- krackle.gemspec
|
71
|
+
- lib/krackle.rb
|
72
|
+
- lib/krackle/version.rb
|
73
|
+
- spec/fixtures/profile.yml
|
74
|
+
- spec/lib/krackle_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: https://github.com/bradgessler/krackle
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Query semi-structure data from the command line.
|
100
|
+
test_files:
|
101
|
+
- spec/fixtures/profile.yml
|
102
|
+
- spec/lib/krackle_spec.rb
|
103
|
+
- spec/spec_helper.rb
|