yqr 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '029359abb01e97e427165fd1f866c67d88efabb8'
4
+ data.tar.gz: ebfd5dc34c4d990dfbfa5c012af87d8fe57edd84
5
+ SHA512:
6
+ metadata.gz: cffe33948129a9ff0137e5e14df6dd1953726ba1b8e3030552efbbf93fe8721e806fd03b24c2e6d6da41bba08acb4e9bf9e34e26da7de40b4f58da8479f2521e
7
+ data.tar.gz: 872669c0458b90009562d5736d5e8929a41980a107131b24c0f21926b444391b23fe7428ba66a33fef40934ba39099d3f9b10025715de1cd498cd6099c60808d
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.ruby-version
11
+ /log/*.log
12
+ /vendor/bundler/
13
+
@@ -0,0 +1,14 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.10
5
+ - 2.3.7
6
+ - 2.4.4
7
+ - 2.5.1
8
+ before_install: gem install bundler --no-document
9
+ cache: bundler
10
+ script:
11
+ - bundle exec rake spec
12
+ - echo "gem 'hashie'" >> Gemfile
13
+ - bundle
14
+ - bundle exec rake spec_with_hashie
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at metalels86@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yqr.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017,2018 metalels
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.
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 metalels
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.
@@ -0,0 +1,94 @@
1
+ [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](MIT-LICENSE)
2
+ [![Build Status](https://travis-ci.org/metalels/yqr.svg?branch=master)](https://travis-ci.org/metalels/yqr)
3
+
4
+ # Yqr
5
+ Yaml query executer is written in ruby.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yqr'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yqr
22
+
23
+ ## Installation (Option)
24
+
25
+ By using [Hashie gem](https://rubygems.org/gems/hashie/) together, even simpler access becomes possible.
26
+
27
+ ## Usage
28
+
29
+ ```
30
+ $ cat example1.yaml
31
+ ---
32
+ cat:
33
+ - mike
34
+ - buchi
35
+ dog:
36
+ - pochi
37
+ - koro
38
+
39
+ $ yqr --file example1.yaml [dog][0]
40
+ pochi
41
+
42
+ $ cat example2.yaml
43
+ ---
44
+ - name: mike
45
+ kind: cat
46
+ - name: pochi
47
+ kind: dog
48
+ - name: buchi
49
+ kind: cat
50
+ - name: koro
51
+ kind: dog
52
+
53
+ $ yqr ".find{|a| a[kind] == 'dog'}[name]" < example2.yaml
54
+ pochi
55
+
56
+ $ cat example2.yaml | bundle exec yqr ".select{|a| a[kind] == 'cat'}.last[name]
57
+ buchi
58
+ ```
59
+
60
+ ## Usage (Output-type)
61
+
62
+ ```
63
+ # Default output type is yaml
64
+ $ yqr --file example4.yaml "[cat].first"
65
+ ---
66
+ :name: mike
67
+ :sex: male
68
+
69
+ # Raw output type (Object.to_s)
70
+ $ yqr --file example4.yaml --raw "[cat].first"
71
+ {:name=>"mike", :sex=>"male"}
72
+
73
+ # Json output type
74
+ $ yqr --file example4.yaml --json "[cat].first"
75
+ {"name":"mike","sex":"male"}
76
+ ```
77
+
78
+ ### Enable Hashie access
79
+
80
+ if you install [Hashie gem](https://rubygems.org/gems/hashie/), you can also use query as:
81
+
82
+ ```
83
+ $ yqr --file example4.yaml ".cat.first"
84
+ ---
85
+ :name: mike
86
+ :sex: male
87
+
88
+ $ yqr --file example4.yaml --raw ".cat.first"
89
+ #<Hashie::Mash name="mike" sex="male">
90
+ ```
91
+
92
+ ## Contributing
93
+ git-flow.
94
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
3
+
4
+ task :spec do
5
+ require "./spec/yqr_spec.rb"
6
+ end
7
+
8
+ task :spec_with_hashie do
9
+ require "./spec/yqr_hashie_spec.rb"
10
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yqr"
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ #require "irb"
14
+ #IRB.start(__FILE__)
15
+ require "pry"
16
+ Pry.start
@@ -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
data/exe/yqr ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require 'yqr'
3
+ require 'optparse'
4
+
5
+ Version = Yqr::VERSION
6
+
7
+ usage = <<-EOS
8
+ YAML query executer.
9
+
10
+ usage: yqr [options] --file YAML_FILE [options] QUERY
11
+ or can also use Input-redirect
12
+ yqr [options] QUERY < YAML_FILE
13
+ or can also use Pipe
14
+ cat YAML_FILE | yqr [options] QUERY
15
+ options:
16
+ EOS
17
+
18
+ OptionParser.new do |opt|
19
+ opt.banner = usage
20
+ opt.on('--file [VALUE]', 'Input Yaml file.') {|v| Yqr.options[:file] = v}
21
+ opt.on('--nosymborize', 'Disable auto symborize keys.') {|v| Yqr.options[:symborize] = false}
22
+ opt.on('--json', 'Output Json format [low priority]') {|v| Yqr.options[:json] = v}
23
+ opt.on('--raw', 'Output raw(to_s) format [high priority]') {|v| Yqr.options[:raw] = v}
24
+ opt.on('--debug', 'Enable debug print') {|v| Yqr.options[:debug] = v}
25
+ args = opt.parse!(ARGV)
26
+ if args.length != 1
27
+ puts opt
28
+ exit
29
+ end
30
+ Yqr.options[:query] = args.pop
31
+ unless Yqr.options[:file]
32
+ bodies = []
33
+ while line = STDIN.gets
34
+ bodies << line
35
+ end
36
+
37
+ Yqr.options[:body] = bodies.join("\n")
38
+ end
39
+ end
40
+
41
+
42
+ puts Yqr.exec_with_format
@@ -0,0 +1,180 @@
1
+ require "yaml"
2
+ require "yqr/version"
3
+
4
+ begin
5
+ require "hashie"
6
+ rescue LoadError
7
+ # do nothing
8
+ end
9
+
10
+ module Yqr
11
+ @yaml = nil
12
+ @options = {debug: false, json: false, symborize: true, raw: false}
13
+
14
+ class << self
15
+ def yaml
16
+ @yaml
17
+ end
18
+
19
+ def options
20
+ @options
21
+ end
22
+
23
+ def load_yaml
24
+ if @options[:file]
25
+ load_file
26
+ else
27
+ load_body
28
+ end
29
+ symbolize_all_keys @yaml
30
+ end
31
+
32
+ def load_body
33
+ @yaml = if enable_symborize && Module.constants.include?(:Hashie)
34
+ load_body_use_hashie @options[:body]
35
+ else
36
+ YAML.load @options[:body]
37
+ end
38
+ end
39
+
40
+ def load_file
41
+ @yaml = if enable_symborize && Module.constants.include?(:Hashie)
42
+ load_yaml_use_hashie @options[:file]
43
+ else
44
+ YAML.load_file @options[:file]
45
+ end
46
+ end
47
+
48
+ def load_body_use_hashie(body)
49
+ tmp_file_path = "/tmp/yqr_tmp.#{Time.now.to_i}"
50
+ File.write tmp_file_path, body
51
+ load_yaml_use_hashie tmp_file_path, true
52
+ end
53
+
54
+ def load_yaml_use_hashie(path, delete=false)
55
+ begin
56
+ Hashie::Mash.load path
57
+ rescue => ex
58
+ if debug
59
+ puts ex.message
60
+ end
61
+ YAML.load_file path
62
+ ensure
63
+ if delete && File.exist?(path)
64
+ File.delete path
65
+ end
66
+ end
67
+ end
68
+
69
+ def symbolize_all_keys(obj)
70
+ return obj unless enable_symborize
71
+ case obj.class.to_s
72
+ when 'Hash'
73
+ if Module.constants.include?(:Hashie)
74
+ obj = Hashie::Mash.new(obj)
75
+ else
76
+ obj.keys.each do |key|
77
+ obj[(key.to_sym rescue key) || key] = symbolize_all_keys(obj.delete(key))
78
+ end
79
+ end
80
+ obj
81
+ when 'Array'
82
+ obj.map! do |elem|
83
+ symbolize_all_keys elem
84
+ end
85
+ obj
86
+ else
87
+ obj
88
+ end
89
+ end
90
+
91
+ def result_decode(obj)
92
+ return obj unless enable_symborize
93
+ case obj.class.to_s
94
+ when 'Hash'
95
+ obj.keys.each do |key|
96
+ obj[(key.to_sym rescue key) || key] = result_decode(obj.delete(key))
97
+ end
98
+ obj
99
+ when 'Array'
100
+ obj.map! do |elem|
101
+ result_decode elem
102
+ end
103
+ obj
104
+ when 'Hashie::Mash'
105
+ result_decode obj.to_h
106
+ else
107
+ obj
108
+ end
109
+ end
110
+
111
+ def exec
112
+ if debug
113
+ puts "==== Query Info ===="
114
+ puts %Q{self.yaml#{parse_query}}
115
+ puts "==== Yaml Info ====="
116
+ puts @yaml
117
+ puts "===================="
118
+ end
119
+
120
+ case parse_query
121
+ when /\A(\w|\d)/
122
+ raise "<QUERY> format is invalid."
123
+ end
124
+
125
+ eval %Q{self.yaml#{parse_query}}
126
+ rescue => ex
127
+ STDERR.puts "Error was happen."
128
+ STDERR.print ex.message
129
+ end
130
+
131
+ def exec_with_format
132
+ load_yaml
133
+ obj = exec
134
+ if debug
135
+ puts "==== Object Info ===="
136
+ puts obj.class
137
+ puts obj
138
+ puts "====================="
139
+ end
140
+
141
+ if @options[:raw]
142
+ return raw_output_formatter(obj)
143
+ end
144
+
145
+ obj = result_decode obj
146
+
147
+ case obj.class.to_s
148
+ when "Array", "Hash"
149
+ if @options[:json]
150
+ require 'json'
151
+ obj.to_json
152
+ else
153
+ obj.to_yaml
154
+ end
155
+ else
156
+ obj
157
+ end
158
+ end
159
+
160
+ def raw_output_formatter(obj)
161
+ obj.inspect
162
+ end
163
+
164
+ def parse_query
165
+ if enable_symborize
166
+ @options[:query].gsub(/\[(\w*)\]/, '[:\1]').gsub(/\[:(\d*)\]/, '[\1]')
167
+ else
168
+ @options[:query]
169
+ end
170
+ end
171
+
172
+ def enable_symborize
173
+ @options[:symborize]
174
+ end
175
+
176
+ def debug
177
+ @options[:debug]
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,3 @@
1
+ module Yqr
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yqr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yqr"
8
+ spec.version = Yqr::VERSION
9
+ spec.authors = ["metalels"]
10
+ spec.email = ["metalels86@gmail.com"]
11
+
12
+ spec.summary = %q{YAML Query is written in ruby.}
13
+ spec.description = %q{YAML Query is written in ruby.}
14
+ spec.homepage = "https://github.com/metalels/yqr"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "pry"
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yqr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - metalels
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-03 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: YAML Query is written in ruby.
56
+ email:
57
+ - metalels86@gmail.com
58
+ executables:
59
+ - yqr
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - MIT-LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - exe/yqr
74
+ - lib/yqr.rb
75
+ - lib/yqr/version.rb
76
+ - yqr.gemspec
77
+ homepage: https://github.com/metalels/yqr
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: YAML Query is written in ruby.
101
+ test_files: []