json2yaml 0.0.2 → 0.0.3

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: e46194deb17804a4272e56ed0bd0b3b07886d9fe
4
- data.tar.gz: 03533791357b43d1abcedeadbc6072bda1334921
3
+ metadata.gz: f0c939abcc658d8470f0099f083631effed64548
4
+ data.tar.gz: 130fd19abd47da39896cd0c0564386cb572729be
5
5
  SHA512:
6
- metadata.gz: 2cb1e19c2b1eb75b24a9066deecae9d73695d6f979861680a0a962abe318ee43c4f5f8d2318427510415c5675460698d969f468d3fe9d4cc5d1c2690f07e5f16
7
- data.tar.gz: dd940815742d9a40863d8c93307190cb132ddfd8f7bd44cd146db27203609064fc8a6e150a3438460d22ced74b9e3862a398dda1cf3fb797d0b6ff00920f3f35
6
+ metadata.gz: e7134526de4f9ab1e2d47a3b316807e6b3f19e110cc698bfb5e016663c1bf7fe5df199229a8114ecbf82db19de488ed59b12e739c292b110dd3cd6c78586ffa2
7
+ data.tar.gz: 9177b778c9cff036305864acde21a862a28477c0d54f16813fe0594f6323c34235adc3ca2d3b7341f53d00e85214c53d0e520e34a4adf867e5fc4b51cfc431e3
data/README.md CHANGED
@@ -13,6 +13,8 @@ Convert between JSON and YAML in CLI
13
13
 
14
14
  Now, you can use `json2yaml` and `yaml2json`
15
15
 
16
+ ### Read from STDIN
17
+
16
18
  ```
17
19
  $ echo '{ "a" : 123 }' | json2yaml
18
20
  #=>
@@ -29,6 +31,14 @@ $ cat bar.yml | yaml2json > bar.json
29
31
 
30
32
  ```
31
33
 
34
+ ### Read from files
35
+
36
+ ```
37
+ $ json2yaml log1.json log2.json > log.yaml
38
+
39
+ $ yaml2json log1.yaml log2.yaml > log.json
40
+ ```
41
+
32
42
  ## Contributing
33
43
 
34
44
  1. Fork it
@@ -3,6 +3,6 @@
3
3
 
4
4
  require 'json2yaml'
5
5
 
6
- Json2yaml.to_yaml(STDIN).each do |yaml|
6
+ Json2yaml.to_yaml(STDIN, ARGV).each do |yaml|
7
7
  print yaml
8
8
  end
@@ -3,6 +3,6 @@
3
3
 
4
4
  require 'json2yaml'
5
5
 
6
- Json2yaml.to_json(STDIN).each do |json|
6
+ Json2yaml.to_json(STDIN, ARGV).each do |json|
7
7
  print json
8
8
  end
@@ -3,18 +3,26 @@ require 'yajl/json_gem'
3
3
  require 'yaml'
4
4
 
5
5
  module Json2yaml
6
- def self.to_yaml(json_stream)
6
+ def self.to_yaml(json_stream, argv)
7
+ inputs = argv.empty? ? [json_stream] : argv.map{|file| open(file) }
8
+
7
9
  Enumerator.new do |yielder|
8
- Yajl::Parser.new.parse(json_stream) do |d|
9
- yielder.yield d.to_yaml
10
+ inputs.each do |input|
11
+ Yajl::Parser.new.parse(input) do |d|
12
+ yielder.yield d.to_yaml
13
+ end
10
14
  end
11
15
  end
12
16
  end
13
17
 
14
- def self.to_json(yaml_stream)
18
+ def self.to_json(yaml_stream, argv)
19
+ inputs = argv.empty? ? [yaml_stream] : argv.map{|file| open(file) }
20
+
15
21
  Enumerator.new do |yielder|
16
- YAML.load_stream(yaml_stream) do |d|
17
- yielder.yield d.to_json + "\n"
22
+ inputs.each do |input|
23
+ YAML.load_stream(input) do |d|
24
+ yielder.yield d.to_json + "\n"
25
+ end
18
26
  end
19
27
  end
20
28
  end
@@ -1,3 +1,3 @@
1
1
  module Json2yaml
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,2 @@
1
+ {"foo":"bar","foobar":"baz"}
2
+ {"qux":"quux"}
@@ -0,0 +1,5 @@
1
+ ---
2
+ foo: bar
3
+ foobar: baz
4
+ ---
5
+ qux: quux
@@ -0,0 +1 @@
1
+ [1,true,false,null]
@@ -0,0 +1,4 @@
1
+ - 1
2
+ - true
3
+ - false
4
+ -
@@ -1,17 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Json2yaml do
4
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), 'fixtures')
5
+
4
6
  context "with single data" do
5
7
  let(:json_str) { "{\"a\":\"b\"}\n" }
6
8
  let(:yaml_str) { "---\na: b\n" }
7
9
  let(:json_io) { StringIO.new(json_str, "r") }
8
10
  let(:yaml_io) { StringIO.new(yaml_str, "r") }
9
11
  context '.to_json' do
10
- it { expect(Json2yaml.to_yaml(json_io).reduce(&:+)).to eq(yaml_str) }
12
+ it { expect(Json2yaml.to_yaml(json_io, []).reduce(&:+)).to eq(yaml_str) }
11
13
  end
12
14
 
13
15
  context '.to_yaml' do
14
- it { expect(Json2yaml.to_json(yaml_io).reduce(&:+)).to eq(json_str) }
16
+ it { expect(Json2yaml.to_json(yaml_io, []).reduce(&:+)).to eq(json_str) }
15
17
  end
16
18
  end
17
19
 
@@ -21,11 +23,43 @@ describe Json2yaml do
21
23
  let(:json_io) { StringIO.new(json_str, "r") }
22
24
  let(:yaml_io) { StringIO.new(yaml_str, "r") }
23
25
  context '.to_json' do
24
- it { expect(Json2yaml.to_yaml(json_io).reduce(&:+)).to eq(yaml_str) }
26
+ it { expect(Json2yaml.to_yaml(json_io, []).reduce(&:+)).to eq(yaml_str) }
25
27
  end
26
28
 
27
29
  context '.to_yaml' do
28
- it { expect(Json2yaml.to_json(yaml_io).reduce(&:+)).to eq(json_str) }
30
+ it { expect(Json2yaml.to_json(yaml_io, []).reduce(&:+)).to eq(json_str) }
31
+ end
32
+ end
33
+
34
+ context "load by arguments" do
35
+ let(:test1_json_as_yaml) { "---\nfoo: bar\nfoobar: baz\n---\nqux: quux\n" }
36
+ let(:test2_json_as_yaml) { "---\n- 1\n- true\n- false\n- \n" }
37
+ let(:test1_yaml_as_json) { "{\"foo\":\"bar\",\"foobar\":\"baz\"}\n{\"qux\":\"quux\"}\n" }
38
+ let(:test2_yaml_as_json) { "[1,true,false,null]\n" }
39
+ let(:empty_io) { StringIO.new }
40
+
41
+ context "with single argument" do
42
+ context '.to_json' do
43
+ let(:argv) { [File.join(FIXTURES_DIR, 'test1.yaml')] }
44
+ it { expect(Json2yaml.to_json(empty_io, argv).reduce(&:+)).to eq(test1_yaml_as_json) }
45
+ end
46
+
47
+ context '.to_yaml' do
48
+ let(:argv) { [File.join(FIXTURES_DIR, 'test1.json')] }
49
+ it { expect(Json2yaml.to_yaml(empty_io, argv).reduce(&:+)).to eq(test1_json_as_yaml) }
50
+ end
51
+ end
52
+
53
+ context "with multiple arguments" do
54
+ context '.to_json' do
55
+ let(:argv) { [File.join(FIXTURES_DIR, 'test1.yaml'), File.join(FIXTURES_DIR, 'test2.yaml')] }
56
+ it { expect(Json2yaml.to_json(empty_io, argv).reduce(&:+)).to eq(test1_yaml_as_json + test2_yaml_as_json) }
57
+ end
58
+
59
+ context '.to_yaml' do
60
+ let(:argv) { [File.join(FIXTURES_DIR, 'test1.json'), File.join(FIXTURES_DIR, 'test2.json')] }
61
+ it { expect(Json2yaml.to_yaml(empty_io, argv).reduce(&:+)).to eq(test1_json_as_yaml + test2_json_as_yaml) }
62
+ end
29
63
  end
30
64
  end
31
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json2yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - fukayatsu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-09 00:00:00.000000000 Z
11
+ date: 2014-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yajl-ruby
@@ -88,6 +88,10 @@ files:
88
88
  - json2yaml.gemspec
89
89
  - lib/json2yaml.rb
90
90
  - lib/json2yaml/version.rb
91
+ - spec/fixtures/test1.json
92
+ - spec/fixtures/test1.yaml
93
+ - spec/fixtures/test2.json
94
+ - spec/fixtures/test2.yaml
91
95
  - spec/json2yaml_spec.rb
92
96
  - spec/spec_helper.rb
93
97
  homepage: https://github.com/fukayatsu/json2yaml
@@ -115,6 +119,10 @@ signing_key:
115
119
  specification_version: 4
116
120
  summary: Convert between JSON and YAML in CLI
117
121
  test_files:
122
+ - spec/fixtures/test1.json
123
+ - spec/fixtures/test1.yaml
124
+ - spec/fixtures/test2.json
125
+ - spec/fixtures/test2.yaml
118
126
  - spec/json2yaml_spec.rb
119
127
  - spec/spec_helper.rb
120
128
  has_rdoc: