yq 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/yq.rb +13 -4
  3. data/lib/yq/version.rb +1 -1
  4. data/spec/yq_spec.rb +43 -0
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 845c9c93cfd039439ebe95ff5a78c35de7e2a105
4
- data.tar.gz: 6f90bc3c178b9a6ba941b1204ef908155b45dba1
3
+ metadata.gz: e0df4607bd133443c36744c12e135d7f57ec02f2
4
+ data.tar.gz: 3141a098d8867570ccb20dc1c401763a52a63e11
5
5
  SHA512:
6
- metadata.gz: dd385c1dfd908e5496cab3748c450bc46dd90d2c7e1a1c7da40de28a8658ff38120bc141ac04fbee7b367fbf89899b88b089994ce5657091842d725b4ae01c55
7
- data.tar.gz: 0e6eab0698fd851e6b461aca496f4303bbd50e657febd930bfa57a6d8a6d3daee10ce1d973335169bcd8b851ff0adfff3865ef5a5b0647d7e88a082b32ef0c59
6
+ metadata.gz: 4ad934af4fbcf11be95bd2b39b122381811307c16c4345701c75d889d8e22fe59c5589e5667a127f51dd8de85eeaa9c10831ad80d0eb3bf8ad84f4912e29d1c9
7
+ data.tar.gz: d9e4567ac0c06ca1e9b396563bcc8ac054f7dc237f84246aef625e210797f2a7e48d90db9a74d30224038088b481f37f0c303c94c7f8d51299db98a48bda487e
data/lib/yq.rb CHANGED
@@ -73,7 +73,7 @@ module Yq
73
73
 
74
74
  def self.json_to_hash(json)
75
75
  JSON.parse(json)
76
- rescue JSON::ParserError => e
76
+ rescue JSON::ParserError
77
77
  LOGGER.debug "Non JSON output from jq. Interpreting."
78
78
  interpret_non_json_output(json)
79
79
  end
@@ -83,9 +83,18 @@ module Yq
83
83
  end
84
84
 
85
85
  def self.interpret_non_json_output(string)
86
- string.split("\n").map do |line|
87
- obj = JSON.parse(%Q[{ "value": #{line} }])
88
- obj["value"]
86
+ regex = /(^\{.+?^\}|^\[.+?\]|^".+?")/m
87
+ matches = string.scan(regex)
88
+
89
+ without_the_wrapping_array = matches.map(&:first)
90
+ without_the_wrapping_array.map do |line|
91
+ begin
92
+ JSON.parse(line)
93
+ rescue JSON::ParserError
94
+ LOGGER.debug "Assuming #{line} is a string."
95
+ obj = JSON.parse(%Q[{ "value": #{line} }])
96
+ obj["value"]
97
+ end
89
98
  end
90
99
  end
91
100
  end
data/lib/yq/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yq
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/spec/yq_spec.rb CHANGED
@@ -32,6 +32,44 @@ EOF
32
32
  - foo
33
33
  - bar
34
34
  - baz
35
+ EOF
36
+
37
+ let(:jq_non_json_response_2) {<<EOF}
38
+ {
39
+ "foo": "bar",
40
+ "baz": {
41
+ "blah": 10240,
42
+ "asdf": "qwer"
43
+ },
44
+ "bing": "bo.ng"
45
+ }
46
+ {
47
+ "foo": "far",
48
+ "bing": "ba.ng"
49
+ }
50
+ [1,2,3]
51
+ [4,5,6]
52
+ "asdf"
53
+ "qwer"
54
+ EOF
55
+
56
+ let(:yaml_from_non_json_response_2) {<<EOF}
57
+ ---
58
+ - foo: bar
59
+ baz:
60
+ blah: 10240
61
+ asdf: qwer
62
+ bing: bo.ng
63
+ - foo: far
64
+ bing: ba.ng
65
+ - - 1
66
+ - 2
67
+ - 3
68
+ - - 4
69
+ - 5
70
+ - 6
71
+ - asdf
72
+ - qwer
35
73
  EOF
36
74
 
37
75
  describe '.search' do
@@ -77,6 +115,11 @@ EOF
77
115
  subject { described_class.json_to_yaml(jq_non_json_response) }
78
116
  it {is_expected.to match(yaml_from_non_json_response)}
79
117
  end
118
+
119
+ context 'non-json response 2' do
120
+ subject { described_class.json_to_yaml(jq_non_json_response_2) }
121
+ it {is_expected.to match(yaml_from_non_json_response_2)}
122
+ end
80
123
  end
81
124
 
82
125
  describe '.search_yaml' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Park