xxx2json 0.1.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: 4a0bd23e7e9bcb8945991de0680040f091df7d95
4
+ data.tar.gz: 836fa6208b05c97ab2be262bf5cb05d60d73efc6
5
+ SHA512:
6
+ metadata.gz: 47a96b0e2cbe8d3f60c6cd2e927358ccd9769c83f647dc5245e1d2115802be9d940481372bd02a29e487238da7e130df71cdcca33faa4789a2c3a942f2195aee
7
+ data.tar.gz: 98fb56302a9876edc3d8b1ca858b47b7b36c4d799e8f29def6efff8450f3f32c89256f8f7042508f657288199a315ca73bb59300e1370ff63338a8301a9d7ef2
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
@@ -0,0 +1,8 @@
1
+ Gemfile.lock
2
+ doc/
3
+ pkg/
4
+ **/*.gem
5
+ .yardoc/
6
+ .bundle/
7
+ vendor/bundle/
8
+ .tags
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - ruby-head
8
+
9
+ os:
10
+ - linux
11
+ - osx
12
+
13
+ gemfile:
14
+ - Gemfile
15
+
16
+ script: 'bundle exec rake spec'
@@ -0,0 +1 @@
1
+ --markup markdown --title "xxx2json Documentation" --protected
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2014-12-29
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'kramdown'
7
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Hiroshi Toyama
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # xxx2json [![Build Status](https://secure.travis-ci.org/toyama0919/xxx2json.png?branch=master)](http://travis-ci.org/toyama0919/xxx2json)
2
+
3
+ other format to json
4
+
5
+ ## Examples(LTSV)
6
+
7
+ $ echo 'test:hoge\ttest2:hoge' | xxx2json ltsv
8
+ #=> [
9
+ #=> {
10
+ #=> "test": "hoge",
11
+ #=> "test2": "hoge"
12
+ #=> }
13
+ #=> ]
14
+
15
+ ## Support Format
16
+
17
+ * ltsv
18
+ * csv
19
+ * tsv
20
+ * yaml
21
+ * rss
22
+ * regexp
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ gem 'xxx2json'
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install xxx2json
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new [Pull Request](../../pull/new/master)
45
+
46
+ ## Information
47
+
48
+ * [Homepage](https://github.com/toyama0919/xxx2json)
49
+ * [Issues](https://github.com/toyama0919/xxx2json/issues)
50
+ * [Documentation](http://rubydoc.info/gems/xxx2json/frames)
51
+ * [Email](mailto:toyama0919@gmail.com)
52
+
53
+ ## Copyright
54
+
55
+ Copyright (c) 2014 Hiroshi Toyama
56
+
57
+ See [LICENSE.txt](../LICENSE.txt) for details.
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler'
7
+ rescue LoadError => e
8
+ warn e.message
9
+ warn "Run `gem install bundler` to install Bundler."
10
+ exit -1
11
+ end
12
+
13
+ begin
14
+ Bundler.setup(:development)
15
+ rescue Bundler::BundlerError => e
16
+ warn e.message
17
+ warn "Run `bundle install` to install missing gems."
18
+ exit e.status_code
19
+ end
20
+
21
+ require 'rake'
22
+
23
+ require 'rubygems/tasks'
24
+ Gem::Tasks.new
25
+
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new
28
+
29
+ task :test => :spec
30
+ task :default => :spec
31
+
32
+ require 'yard'
33
+ YARD::Rake::YardocTask.new
34
+ task :doc => :yard
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'xxx2json'
5
+
6
+ Xxx2json::CLI.start
@@ -0,0 +1,3 @@
1
+ require 'xxx2json/version'
2
+ require 'xxx2json/core'
3
+ require 'xxx2json/cli'
@@ -0,0 +1,71 @@
1
+ require 'json'
2
+ require 'thor'
3
+ require 'yaml'
4
+ require 'ltsv'
5
+
6
+ module Xxx2json
7
+ class CLI < Thor
8
+
9
+ class_option :file, type: :string, desc: 'input file. default is STDIN'
10
+ def initialize(args = [], options = {}, config = {})
11
+ super(args, options, config)
12
+ @global_options = config[:shell].base.options
13
+ if @global_options['file'].nil?
14
+ return unless File.pipe?(STDIN)
15
+ @buffer = STDIN.read
16
+ else
17
+ @buffer = File.read(@global_options['file'])
18
+ end
19
+ @core = Core.new
20
+ end
21
+
22
+ desc "csv", "csv to json"
23
+ option :headers, type: :array, default: nil, desc: 'header'
24
+ def csv
25
+ puts_json @core.convert_csv(@buffer, ',', options['headers'])
26
+ end
27
+
28
+ desc "space", "space delimiter to json"
29
+ option :headers, type: :array, default: nil, desc: 'header'
30
+ def space
31
+ puts_json @core.convert_csv(@buffer, ' ', options['headers'])
32
+ end
33
+
34
+ desc "tsv", "tsv to json"
35
+ option :headers, type: :array, desc: 'headers'
36
+ def tsv
37
+ puts_json @core.convert_csv(@buffer, "\t", options['headers'])
38
+ end
39
+
40
+ desc "yaml", "yaml to json"
41
+ def yaml
42
+ puts_json YAML.load(@buffer)
43
+ end
44
+
45
+ desc "ltsv", "ltsv to json"
46
+ def ltsv
47
+ puts_json LTSV.parse(@buffer)
48
+ end
49
+
50
+ desc "regexp", "say hello to NAME"
51
+ option :format, type: :string, required: true, desc: 'header'
52
+ def regexp
53
+ puts_json @core.convert_regexp(@buffer, options['format'])
54
+ end
55
+
56
+ desc "rss", "rss to json"
57
+ def rss
58
+ puts_json @core.convert_rss(@buffer)
59
+ end
60
+
61
+ desc "version", "show version"
62
+ def version
63
+ puts VERSION
64
+ end
65
+
66
+ private
67
+ def puts_json(data)
68
+ puts JSON.pretty_generate(data)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,54 @@
1
+ require 'fluent/config'
2
+ require 'fluent/engine'
3
+ require 'fluent/parser'
4
+ require 'csv'
5
+ require 'crack/xml'
6
+
7
+
8
+ module Xxx2json
9
+ class Core
10
+
11
+ def initialize
12
+ end
13
+
14
+ def convert_csv(buffer, col_sep, headers)
15
+ CSV.parse(buffer, col_sep: col_sep, headers: headers.nil?).map do |row|
16
+ headers.nil? ? row.to_hash : Hash[*headers.zip(row).flatten]
17
+ end
18
+ end
19
+
20
+ def convert_regexp(buffer, format)
21
+ format = format.gsub(/^\/(.+)\/$/, '\1')
22
+ parser = Fluent::TextParser::RegexpParser.new(Regexp.new(format))
23
+ results = []
24
+ begin
25
+ buffer.lines.each_with_index do |line, i|
26
+ line = line.strip
27
+ time, parsed = parser.call(line)
28
+ if parsed.nil?
29
+ STDERR.puts "format error! line #{i + 1} [#{line}]"
30
+ end
31
+ results << parsed unless parsed.nil?
32
+ end
33
+ rescue ArgumentError, RegexpError => e
34
+ STDERR.puts "\n#{e.message}\n#{e.backtrace.join("\n")}"
35
+ parsed_time = parsed = nil
36
+ end
37
+ results
38
+ end
39
+
40
+ def convert_rss(buffer)
41
+ rss = Crack::XML.parse(buffer)
42
+ if rss.key?('rss')
43
+ display = rss['rss']['channel']['item']
44
+ elsif rss.key?('rdf:RDF')
45
+ display = rss['rdf:RDF']['item']
46
+ elsif rss.key?('root')
47
+ display = rss['root']['articles']['article']
48
+ elsif rss.key?('feed')
49
+ display = rss['feed']['entry']
50
+ end
51
+ display
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ module Xxx2json
2
+ # xxx2json version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'xxx2json'
3
+
4
+ describe Xxx2json::CLI do
5
+ before do
6
+ end
7
+
8
+ it "should stdout sample" do
9
+ output = capture_stdout do
10
+ Xxx2json::CLI.start(['help'])
11
+ end
12
+ output.should_not nil
13
+ end
14
+
15
+ it "include" do
16
+ output = capture_stdout do
17
+ Xxx2json::CLI.start(['help', 'sample'])
18
+ end
19
+ output.should include('--fields')
20
+ end
21
+
22
+ after do
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'xxx2json'
3
+
4
+ describe Xxx2json::Core do
5
+ before do
6
+ @core = Core.new
7
+ end
8
+
9
+ it "core not nil" do
10
+ @core.should_not nil
11
+ end
12
+
13
+ it "private method" do
14
+ @core.send(:sample, []).should == []
15
+ end
16
+
17
+ after do
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec'
2
+ require 'xxx2json/version'
3
+
4
+ include Xxx2json
5
+
6
+ def capture_stdout
7
+ out = StringIO.new
8
+ $stdout = out
9
+ yield
10
+ return out.string
11
+ ensure
12
+ $stdout = STDOUT
13
+ end
14
+
15
+ def capture_stderr
16
+ out = StringIO.new
17
+ $stderr = out
18
+ yield
19
+ return out.string
20
+ ensure
21
+ $stderr = STDERR
22
+ end
23
+
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'xxx2json'
3
+
4
+ describe Xxx2json do
5
+ it "should have a VERSION constant" do
6
+ subject.const_get('VERSION').should_not be_empty
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/xxx2json/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "xxx2json"
7
+ gem.version = Xxx2json::VERSION
8
+ gem.summary = %q{other format to json}
9
+ gem.description = %q{other format to json}
10
+ gem.license = "MIT"
11
+ gem.authors = ["Hiroshi Toyama"]
12
+ gem.email = "toyama0919@gmail.com"
13
+ gem.homepage = "https://github.com/toyama0919/xxx2json"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'thor'
21
+ gem.add_dependency 'fluentd'
22
+ gem.add_dependency 'crack'
23
+ gem.add_dependency 'ltsv'
24
+
25
+ gem.add_development_dependency 'bundler', '~> 1.7.2'
26
+ gem.add_development_dependency 'pry', '~> 0.10.1'
27
+ gem.add_development_dependency 'rake', '~> 10.3.2'
28
+ gem.add_development_dependency 'rspec', '~> 2.4'
29
+ gem.add_development_dependency 'rubocop', '~> 0.24.1'
30
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
31
+ gem.add_development_dependency 'yard', '~> 0.8'
32
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xxx2json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroshi Toyama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fluentd
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: crack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ltsv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.7.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.7.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 10.3.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 10.3.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.4'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.24.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.24.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubygems-tasks
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.2'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.2'
153
+ - !ruby/object:Gem::Dependency
154
+ name: yard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.8'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.8'
167
+ description: other format to json
168
+ email: toyama0919@gmail.com
169
+ executables:
170
+ - xxx2json
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".document"
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".travis.yml"
178
+ - ".yardopts"
179
+ - ChangeLog.md
180
+ - Gemfile
181
+ - LICENSE.txt
182
+ - README.md
183
+ - Rakefile
184
+ - bin/xxx2json
185
+ - lib/xxx2json.rb
186
+ - lib/xxx2json/cli.rb
187
+ - lib/xxx2json/core.rb
188
+ - lib/xxx2json/version.rb
189
+ - spec/cli_spec.rb
190
+ - spec/core_spec.rb
191
+ - spec/spec_helper.rb
192
+ - spec/xxx2json_spec.rb
193
+ - xxx2json.gemspec
194
+ homepage: https://github.com/toyama0919/xxx2json
195
+ licenses:
196
+ - MIT
197
+ metadata: {}
198
+ post_install_message:
199
+ rdoc_options: []
200
+ require_paths:
201
+ - lib
202
+ required_ruby_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ required_rubygems_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ requirements: []
213
+ rubyforge_project:
214
+ rubygems_version: 2.4.5
215
+ signing_key:
216
+ specification_version: 4
217
+ summary: other format to json
218
+ test_files:
219
+ - spec/cli_spec.rb
220
+ - spec/core_spec.rb
221
+ - spec/spec_helper.rb
222
+ - spec/xxx2json_spec.rb
223
+ has_rdoc: