arstotzka 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +3 -0
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +6 -4
- data/README.md +61 -64
- data/config/yardstick.yml +14 -26
- data/lib/arstotzka/base.rb +27 -0
- data/lib/arstotzka/builder.rb +24 -83
- data/lib/arstotzka/class_methods.rb +3 -3
- data/lib/arstotzka/crawler.rb +33 -29
- data/lib/arstotzka/fetcher.rb +11 -23
- data/lib/arstotzka/options.rb +72 -0
- data/lib/arstotzka/reader.rb +32 -23
- data/lib/arstotzka/type_cast.rb +3 -3
- data/lib/arstotzka/version.rb +1 -1
- data/lib/arstotzka/wrapper.rb +18 -10
- data/lib/arstotzka.rb +4 -2
- data/scripts/check_readme.sh +6 -0
- data/spec/integration/yard/arstotzka/builder_spec.rb +11 -11
- data/spec/integration/yard/arstotzka/crawler_spec.rb +7 -7
- data/spec/integration/yard/arstotzka/fetcher_spec.rb +3 -3
- data/spec/integration/yard/arstotzka/reader_spec.rb +5 -5
- data/spec/integration/yard/arstotzka/wrapper_spec.rb +6 -6
- data/spec/lib/arstotzka/builder_spec.rb +7 -7
- data/spec/lib/arstotzka/crawler_spec.rb +28 -28
- data/spec/lib/arstotzka/fetcher_spec.rb +9 -9
- data/spec/lib/arstotzka/options_spec.rb +208 -0
- data/spec/lib/arstotzka/reader_spec.rb +15 -15
- data/spec/lib/arstotzka/wrapper_spec.rb +13 -13
- data/spec/lib/arstotzka_spec.rb +2 -2
- data/spec/support/models/arstotzka/dummy.rb +4 -4
- data/spec/support/models/collector.rb +3 -1
- data/spec/support/models/star_gazer.rb +1 -1
- metadata +7 -2
@@ -7,28 +7,28 @@ describe Arstotzka::Crawler do
|
|
7
7
|
described_class.new default_options.merge(options), &block
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:block)
|
11
|
-
let(:
|
12
|
-
let(:default_options) { {
|
13
|
-
let(:options)
|
14
|
-
let(:json_file)
|
15
|
-
let(:json)
|
16
|
-
let(:value)
|
10
|
+
let(:block) { proc { |v| v } }
|
11
|
+
let(:keys) { '' }
|
12
|
+
let(:default_options) { { keys: keys, case: :lower_camel } }
|
13
|
+
let(:options) { {} }
|
14
|
+
let(:json_file) { 'arstotzka.json' }
|
15
|
+
let(:json) { load_json_fixture_file(json_file) }
|
16
|
+
let(:value) { crawler.value(json) }
|
17
17
|
|
18
18
|
context 'when no block is given' do
|
19
19
|
subject(:crawler) do
|
20
20
|
described_class.new default_options.merge(options)
|
21
21
|
end
|
22
22
|
|
23
|
-
let(:
|
23
|
+
let(:keys) { %w[user name] }
|
24
24
|
|
25
25
|
it 'retrieves attribute from base json' do
|
26
26
|
expect(value).to eq(json['user']['name'])
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
context 'when parsing with a
|
31
|
-
let(:
|
30
|
+
context 'when parsing with a keys' do
|
31
|
+
let(:keys) { %w[user name] }
|
32
32
|
|
33
33
|
it 'retrieves attribute from base json' do
|
34
34
|
expect(value).to eq(json['user']['name'])
|
@@ -44,7 +44,7 @@ describe Arstotzka::Crawler do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
context 'when crawler finds a nil attribute' do
|
47
|
-
let(:
|
47
|
+
let(:keys) { %w[car model] }
|
48
48
|
|
49
49
|
it 'returns nil' do
|
50
50
|
expect(value).to be_nil
|
@@ -57,7 +57,7 @@ describe Arstotzka::Crawler do
|
|
57
57
|
|
58
58
|
context 'when there is an array of arrays' do
|
59
59
|
let(:json_file) { 'accounts.json' }
|
60
|
-
let(:
|
60
|
+
let(:keys) { %w[banks accounts balance] }
|
61
61
|
|
62
62
|
it 'returns the values as array of arrays' do
|
63
63
|
expect(value).to eq([[1000.0, 1500.0], [50.0, -500.0]])
|
@@ -90,7 +90,7 @@ describe Arstotzka::Crawler do
|
|
90
90
|
|
91
91
|
context 'when json is empty' do
|
92
92
|
let(:json) { nil }
|
93
|
-
let(:
|
93
|
+
let(:keys) { %w[car model] }
|
94
94
|
|
95
95
|
it 'returns nil' do
|
96
96
|
expect(value).to be_nil
|
@@ -101,8 +101,8 @@ describe Arstotzka::Crawler do
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
|
-
context 'with an snake case
|
105
|
-
let(:
|
104
|
+
context 'with an snake case keys' do
|
105
|
+
let(:keys) { ['has_money'] }
|
106
106
|
|
107
107
|
it 'returns camel cased value' do
|
108
108
|
expect(value).to eq(json['hasMoney'])
|
@@ -110,7 +110,7 @@ describe Arstotzka::Crawler do
|
|
110
110
|
end
|
111
111
|
|
112
112
|
context 'when dealing with json inside arrays' do
|
113
|
-
let(:
|
113
|
+
let(:keys) { %w[animals race species name] }
|
114
114
|
let(:expected) do
|
115
115
|
['European squid', 'Macaque monkey', 'Mexican redknee tarantula']
|
116
116
|
end
|
@@ -161,7 +161,7 @@ describe Arstotzka::Crawler do
|
|
161
161
|
context 'with default option' do
|
162
162
|
let(:default_value) { 'NotFound' }
|
163
163
|
let(:options) { { default: default_value } }
|
164
|
-
let(:
|
164
|
+
let(:keys) { %w[projects name] }
|
165
165
|
|
166
166
|
context 'when there is a key missing' do
|
167
167
|
it 'returns the default value' do
|
@@ -183,7 +183,7 @@ describe Arstotzka::Crawler do
|
|
183
183
|
|
184
184
|
context 'when the key is not missing but the value is nil' do
|
185
185
|
let(:json_file) { 'person.json' }
|
186
|
-
let(:
|
186
|
+
let(:keys) { %w[user name] }
|
187
187
|
|
188
188
|
it { expect(value).to be_nil }
|
189
189
|
|
@@ -202,7 +202,7 @@ describe Arstotzka::Crawler do
|
|
202
202
|
|
203
203
|
context 'when the key last key is missing but the value is nil' do
|
204
204
|
let(:json_file) { 'person.json' }
|
205
|
-
let(:
|
205
|
+
let(:keys) { %w[user nick_name] }
|
206
206
|
|
207
207
|
it 'returns the default value' do
|
208
208
|
expect(value).to eq(default_value)
|
@@ -223,8 +223,8 @@ describe Arstotzka::Crawler do
|
|
223
223
|
|
224
224
|
context 'when the node is missing but default has the same node' do
|
225
225
|
let(:default_value) { { node: { value: 1 } } }
|
226
|
-
let(:
|
227
|
-
let(:json)
|
226
|
+
let(:keys) { %w[node node node] }
|
227
|
+
let(:json) { {} }
|
228
228
|
|
229
229
|
it 'does not crawl through default value' do
|
230
230
|
expect(value).to eq(default_value)
|
@@ -234,8 +234,8 @@ describe Arstotzka::Crawler do
|
|
234
234
|
|
235
235
|
context 'when using a snake case' do
|
236
236
|
let(:json) { { snake_cased: 'snake', snakeCased: 'Camel' }.stringify_keys }
|
237
|
-
let(:
|
238
|
-
let(:options) { {
|
237
|
+
let(:keys) { ['snake_cased'] }
|
238
|
+
let(:options) { { case: :snake } }
|
239
239
|
|
240
240
|
it 'fetches from snake cased fields' do
|
241
241
|
expect(value).to eq('snake')
|
@@ -244,8 +244,8 @@ describe Arstotzka::Crawler do
|
|
244
244
|
|
245
245
|
context 'when using a upper camel case' do
|
246
246
|
let(:json) { { UpperCase: 'upper', upperCase: 'lower' }.stringify_keys }
|
247
|
-
let(:
|
248
|
-
let(:options) { {
|
247
|
+
let(:keys) { ['upper_case'] }
|
248
|
+
let(:options) { { case: :upper_camel } }
|
249
249
|
|
250
250
|
it 'fetches from upper camel cased fields' do
|
251
251
|
expect(value).to eq('upper')
|
@@ -254,14 +254,14 @@ describe Arstotzka::Crawler do
|
|
254
254
|
|
255
255
|
context 'when using a symbol keys' do
|
256
256
|
let(:json) { load_json_fixture_file('arstotzka.json').symbolize_keys }
|
257
|
-
let(:
|
257
|
+
let(:keys) { ['id'] }
|
258
258
|
|
259
259
|
it 'fetches from symbol keys' do
|
260
260
|
expect(value).to eq(json[:id])
|
261
261
|
end
|
262
262
|
|
263
263
|
context 'when crawler finds a nil attribute' do
|
264
|
-
let(:
|
264
|
+
let(:keys) { %w[car model] }
|
265
265
|
|
266
266
|
it 'returns nil' do
|
267
267
|
expect(value).to be_nil
|
@@ -274,7 +274,7 @@ describe Arstotzka::Crawler do
|
|
274
274
|
end
|
275
275
|
|
276
276
|
context 'when using key with false value' do
|
277
|
-
let(:
|
277
|
+
let(:keys) { ['has_money'] }
|
278
278
|
|
279
279
|
before do
|
280
280
|
json['hasMoney'] = false
|
@@ -7,10 +7,10 @@ describe Arstotzka::Fetcher do
|
|
7
7
|
described_class.new json, instance, options.merge(path: path)
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:path)
|
10
|
+
let(:path) { '' }
|
11
11
|
let(:instance) { Arstotzka::Fetcher::Dummy.new }
|
12
|
-
let(:json)
|
13
|
-
let(:value)
|
12
|
+
let(:json) { load_json_fixture_file('arstotzka.json') }
|
13
|
+
let(:value) { fetcher.fetch }
|
14
14
|
|
15
15
|
context 'when fetching with no options' do
|
16
16
|
let(:options) { {} }
|
@@ -74,19 +74,19 @@ describe Arstotzka::Fetcher do
|
|
74
74
|
|
75
75
|
describe 'after option' do
|
76
76
|
let(:instance) { MyParser.new(json) }
|
77
|
-
let(:json)
|
78
|
-
let(:options)
|
77
|
+
let(:json) { [100, 250, -25] }
|
78
|
+
let(:options) { { after: :sum } }
|
79
79
|
|
80
80
|
it 'applies after call ' do
|
81
81
|
expect(fetcher.fetch).to eq(325)
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
describe '
|
85
|
+
describe 'klass options' do
|
86
86
|
let(:path) { 'name' }
|
87
|
-
let(:name)
|
88
|
-
let(:json)
|
89
|
-
let(:options) { {
|
87
|
+
let(:name) { 'Robert' }
|
88
|
+
let(:json) { { name: name } }
|
89
|
+
let(:options) { { klass: wrapper } }
|
90
90
|
let(:wrapper) { Person }
|
91
91
|
|
92
92
|
it 'wraps the result in an object' do
|
@@ -0,0 +1,208 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Arstotzka::Options do
|
6
|
+
subject(:options) { described_class.new(options_hash) }
|
7
|
+
|
8
|
+
context 'when initializing without options' do
|
9
|
+
let(:options_hash) { {} }
|
10
|
+
|
11
|
+
describe '#after' do
|
12
|
+
it do
|
13
|
+
expect(options.after).to be_falsey
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#cached' do
|
18
|
+
it do
|
19
|
+
expect(options.cached).to be_falsey
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#case' do
|
24
|
+
it 'returns default case' do
|
25
|
+
expect(options.case).to eq(:lower_camel)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#compact' do
|
30
|
+
it do
|
31
|
+
expect(options.compact).to be_falsey
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#default' do
|
36
|
+
it do
|
37
|
+
expect(options.default).to be_nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#flatten' do
|
42
|
+
it do
|
43
|
+
expect(options.flatten).to be_falsey
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#full_path' do
|
48
|
+
it do
|
49
|
+
expect(options.full_path).to be_nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#json' do
|
54
|
+
it 'returns default json option' do
|
55
|
+
expect(options.json).to eq(:json)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#klass' do
|
60
|
+
it do
|
61
|
+
expect(options.klass).to be_nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#path' do
|
66
|
+
it do
|
67
|
+
expect(options.path).to be_nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#type' do
|
72
|
+
it 'returns none' do
|
73
|
+
expect(options.type).to eq(:none)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when initializing with options' do
|
79
|
+
let(:options_hash) do
|
80
|
+
{
|
81
|
+
after: :method_call,
|
82
|
+
cached: true,
|
83
|
+
case: :snake,
|
84
|
+
compact: true,
|
85
|
+
default: 10,
|
86
|
+
flatten: true,
|
87
|
+
full_path: 'key.sub.fetch',
|
88
|
+
json: :hash,
|
89
|
+
klass: Star,
|
90
|
+
path: 'key.sub',
|
91
|
+
type: :integer
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#cached' do
|
96
|
+
it do
|
97
|
+
expect(options.cached).to be_truthy
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe '#after' do
|
102
|
+
it 'returns method name' do
|
103
|
+
expect(options.after).to eq(:method_call)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#case' do
|
108
|
+
it 'returns defined snake case' do
|
109
|
+
expect(options.case).to eq(:snake)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#compact' do
|
114
|
+
it do
|
115
|
+
expect(options.compact).to be_truthy
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '#default' do
|
120
|
+
it 'returns defined default value' do
|
121
|
+
expect(options.default).to eq(10)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#flatten' do
|
126
|
+
it do
|
127
|
+
expect(options.flatten).to be_truthy
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe '#full_path' do
|
132
|
+
it 'returns defined full path' do
|
133
|
+
expect(options.full_path).to eq('key.sub.fetch')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe '#json' do
|
138
|
+
it 'returns defined json option' do
|
139
|
+
expect(options.json).to eq(:hash)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#klass' do
|
144
|
+
it 'returns defined class' do
|
145
|
+
expect(options.klass).to eq(Star)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe '#path' do
|
150
|
+
it 'returns defined path' do
|
151
|
+
expect(options.path).to eq('key.sub')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe '#type' do
|
156
|
+
it 'returns defined path' do
|
157
|
+
expect(options.type).to eq(:integer)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe '#klass' do
|
163
|
+
before do
|
164
|
+
allow_any_instance_of(described_class)
|
165
|
+
.to receive(:warn)
|
166
|
+
end
|
167
|
+
|
168
|
+
context 'when initializing with old class key' do
|
169
|
+
let(:options_hash) { { class: Star } }
|
170
|
+
|
171
|
+
it 'returns the configured class' do
|
172
|
+
expect(options.klass).to eq(Star)
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'raises a warn' do
|
176
|
+
expect(options).to have_received(:warn)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'when initializing with old class key and klass' do
|
181
|
+
let(:options_hash) { { class: Star, klass: Game } }
|
182
|
+
|
183
|
+
it 'returns the configured class' do
|
184
|
+
expect(options.klass).to eq(Game)
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'raises a warn' do
|
188
|
+
expect(options).to have_received(:warn)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
describe '#merge' do
|
194
|
+
let(:options_hash) { { json: :hash, default: 10 } }
|
195
|
+
|
196
|
+
it do
|
197
|
+
expect(options.merge(default: 10)).to be_a(described_class)
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'overrides values' do
|
201
|
+
expect(options.merge(default: 10).default).to eq(10)
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'keeps not overriten value' do
|
205
|
+
expect(options.merge(default: 10).json).to eq(:hash)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
@@ -24,16 +24,16 @@ end
|
|
24
24
|
|
25
25
|
describe Arstotzka::Reader do
|
26
26
|
subject(:reader) do
|
27
|
-
described_class.new(
|
27
|
+
described_class.new(keys: keys, case: case_type)
|
28
28
|
end
|
29
29
|
|
30
|
-
let(:
|
30
|
+
let(:keys) { %w[user full_name] }
|
31
31
|
let(:json_file) { 'complete_person.json' }
|
32
32
|
let(:full_json) { load_json_fixture_file(json_file) }
|
33
|
-
let(:json)
|
34
|
-
let(:sym_json)
|
33
|
+
let(:json) { full_json }
|
34
|
+
let(:sym_json) { json.symbolize_keys }
|
35
35
|
let(:case_type) { :snake }
|
36
|
-
let(:index)
|
36
|
+
let(:index) { 0 }
|
37
37
|
|
38
38
|
describe '#read' do
|
39
39
|
context 'when the key is found' do
|
@@ -41,12 +41,12 @@ describe Arstotzka::Reader do
|
|
41
41
|
|
42
42
|
it_behaves_like 'reader fetchin value'
|
43
43
|
|
44
|
-
context 'when the
|
45
|
-
let(:json)
|
44
|
+
context 'when the keys case is changed' do
|
45
|
+
let(:json) { full_json['user'] }
|
46
46
|
let(:index) { 1 }
|
47
47
|
|
48
48
|
context 'with snake_case type' do
|
49
|
-
let(:
|
49
|
+
let(:keys) { %w[user FullName] }
|
50
50
|
let(:expected) { json['full_name'] }
|
51
51
|
|
52
52
|
it_behaves_like 'reader fetchin value'
|
@@ -54,7 +54,7 @@ describe Arstotzka::Reader do
|
|
54
54
|
|
55
55
|
context 'with upper_camel type' do
|
56
56
|
let(:case_type) { :upper_camel }
|
57
|
-
let(:
|
57
|
+
let(:keys) { %w[user login_name] }
|
58
58
|
let(:expected) { json['LoginName'] }
|
59
59
|
|
60
60
|
it_behaves_like 'reader fetchin value'
|
@@ -62,7 +62,7 @@ describe Arstotzka::Reader do
|
|
62
62
|
|
63
63
|
context 'with lower_camel type' do
|
64
64
|
let(:case_type) { :lower_camel }
|
65
|
-
let(:
|
65
|
+
let(:keys) { %w[user birth_date] }
|
66
66
|
let(:expected) { json['birthDate'] }
|
67
67
|
|
68
68
|
it_behaves_like 'reader fetchin value'
|
@@ -72,7 +72,7 @@ describe Arstotzka::Reader do
|
|
72
72
|
context 'when key is found but value is null' do
|
73
73
|
let(:json) { full_json['user'] }
|
74
74
|
let(:index) { 1 }
|
75
|
-
let(:
|
75
|
+
let(:keys) { %w[user password_reminder] }
|
76
76
|
|
77
77
|
it do
|
78
78
|
expect(reader.read(json, index)).to be_nil
|
@@ -86,7 +86,7 @@ describe Arstotzka::Reader do
|
|
86
86
|
end
|
87
87
|
|
88
88
|
context 'when json has both string and symble' do
|
89
|
-
let(:
|
89
|
+
let(:keys) { %w[key] }
|
90
90
|
let(:json) { { key: 'symbol', 'key' => 'string' } }
|
91
91
|
|
92
92
|
it 'fetches the string key first' do
|
@@ -96,7 +96,7 @@ describe Arstotzka::Reader do
|
|
96
96
|
end
|
97
97
|
|
98
98
|
context 'when the key is missing' do
|
99
|
-
let(:
|
99
|
+
let(:keys) { %w[age] }
|
100
100
|
|
101
101
|
it do
|
102
102
|
expect do
|
@@ -107,13 +107,13 @@ describe Arstotzka::Reader do
|
|
107
107
|
end
|
108
108
|
|
109
109
|
describe 'ended?' do
|
110
|
-
context 'when index is within
|
110
|
+
context 'when index is within keys' do
|
111
111
|
let(:index) { 1 }
|
112
112
|
|
113
113
|
it { expect(reader).not_to be_ended(index) }
|
114
114
|
end
|
115
115
|
|
116
|
-
context 'when index is outside
|
116
|
+
context 'when index is outside keys' do
|
117
117
|
let(:index) { 2 }
|
118
118
|
|
119
119
|
it { expect(reader).to be_ended(index) }
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
describe Arstotzka::Wrapper do
|
6
6
|
let(:options) { {} }
|
7
7
|
let(:subject) { described_class.new options }
|
8
|
-
let(:hash)
|
8
|
+
let(:hash) { { a: 1 } }
|
9
9
|
|
10
10
|
describe '#wrap' do
|
11
11
|
let(:value) { hash }
|
@@ -25,8 +25,8 @@ describe Arstotzka::Wrapper do
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
context 'with
|
29
|
-
let(:options) { {
|
28
|
+
context 'with klass otpion' do
|
29
|
+
let(:options) { { klass: OpenStruct } }
|
30
30
|
|
31
31
|
it 'creates new instance from given class' do
|
32
32
|
expect(result).to be_a(OpenStruct)
|
@@ -50,10 +50,10 @@ describe Arstotzka::Wrapper do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
context 'with type otpion' do
|
53
|
-
let(:type)
|
54
|
-
let(:value)
|
53
|
+
let(:type) { :integer }
|
54
|
+
let(:value) { '1' }
|
55
55
|
let(:options) { { type: type } }
|
56
|
-
let(:cast)
|
56
|
+
let(:cast) { result }
|
57
57
|
|
58
58
|
it_behaves_like 'casts basic types'
|
59
59
|
|
@@ -75,8 +75,8 @@ describe Arstotzka::Wrapper do
|
|
75
75
|
expect(result).to be_nil
|
76
76
|
end
|
77
77
|
|
78
|
-
context 'when passing
|
79
|
-
let(:options) { { type: type,
|
78
|
+
context 'when passing klass parameter' do
|
79
|
+
let(:options) { { type: type, klass: Arstotzka::Wrapper::Dummy } }
|
80
80
|
|
81
81
|
it do
|
82
82
|
expect(result).to be_nil
|
@@ -92,8 +92,8 @@ describe Arstotzka::Wrapper do
|
|
92
92
|
float: NilClass,
|
93
93
|
string: String
|
94
94
|
|
95
|
-
context 'when passing
|
96
|
-
let(:options) { { type: type,
|
95
|
+
context 'when passing klass parameter' do
|
96
|
+
let(:options) { { type: type, klass: Arstotzka::Wrapper::Dummy } }
|
97
97
|
|
98
98
|
it_behaves_like 'a result that is type cast',
|
99
99
|
integer: NilClass,
|
@@ -102,10 +102,10 @@ describe Arstotzka::Wrapper do
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
context 'when passing
|
105
|
+
context 'when passing klass parameter' do
|
106
106
|
let(:value) { 1 }
|
107
|
-
let(:options) { { type: type,
|
108
|
-
let(:cast)
|
107
|
+
let(:options) { { type: type, klass: Arstotzka::Wrapper::Dummy } }
|
108
|
+
let(:cast) { result.value }
|
109
109
|
|
110
110
|
it_behaves_like 'casts basic types'
|
111
111
|
|
data/spec/lib/arstotzka_spec.rb
CHANGED
@@ -33,7 +33,7 @@ describe Arstotzka do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'when caching the value' do
|
36
|
-
let(:attribute)
|
36
|
+
let(:attribute) { :age }
|
37
37
|
let!(:old_value) { json['age'] }
|
38
38
|
|
39
39
|
before do
|
@@ -91,7 +91,7 @@ describe Arstotzka do
|
|
91
91
|
end
|
92
92
|
|
93
93
|
context 'when wrapping it with a class and caching' do
|
94
|
-
let(:attribute)
|
94
|
+
let(:attribute) { :old_house }
|
95
95
|
let!(:old_value) { json['oldHouse'] }
|
96
96
|
|
97
97
|
it 'returns an onject wrap' do
|
@@ -9,10 +9,10 @@ module Arstotzka
|
|
9
9
|
expose :name, path: 'user'
|
10
10
|
expose :father_name, full_path: 'father.name'
|
11
11
|
expose :age, cached: true
|
12
|
-
expose :house,
|
13
|
-
expose :old_house,
|
14
|
-
expose :games,
|
15
|
-
expose :games_filtered,
|
12
|
+
expose :house, klass: ::House
|
13
|
+
expose :old_house, klass: ::House, cached: true
|
14
|
+
expose :games, klass: ::Game
|
15
|
+
expose :games_filtered, klass: ::Game, after: :filter_games, full_path: 'games'
|
16
16
|
|
17
17
|
def initialize(json)
|
18
18
|
@json = json
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative './collector/game'
|
4
|
+
|
3
5
|
class Collector
|
4
6
|
include Arstotzka
|
5
7
|
|
@@ -14,7 +16,7 @@ class Collector
|
|
14
16
|
default: 'MissingName',
|
15
17
|
full_path: 'collections.cars.units.nick_name'
|
16
18
|
expose :finished_games, json: :hash,
|
17
|
-
flatten: true,
|
19
|
+
flatten: true, klass: Collector::Game,
|
18
20
|
after: :filter_finished, compact: true,
|
19
21
|
full_path: 'collections.games.titles'
|
20
22
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arstotzka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -203,15 +203,18 @@ files:
|
|
203
203
|
- config/yardstick.yml
|
204
204
|
- docker-compose.yml
|
205
205
|
- lib/arstotzka.rb
|
206
|
+
- lib/arstotzka/base.rb
|
206
207
|
- lib/arstotzka/builder.rb
|
207
208
|
- lib/arstotzka/class_methods.rb
|
208
209
|
- lib/arstotzka/crawler.rb
|
209
210
|
- lib/arstotzka/exception.rb
|
210
211
|
- lib/arstotzka/fetcher.rb
|
212
|
+
- lib/arstotzka/options.rb
|
211
213
|
- lib/arstotzka/reader.rb
|
212
214
|
- lib/arstotzka/type_cast.rb
|
213
215
|
- lib/arstotzka/version.rb
|
214
216
|
- lib/arstotzka/wrapper.rb
|
217
|
+
- scripts/check_readme.sh
|
215
218
|
- spec/fixtures/accounts.json
|
216
219
|
- spec/fixtures/accounts_missing.json
|
217
220
|
- spec/fixtures/arstotzka.json
|
@@ -230,6 +233,7 @@ files:
|
|
230
233
|
- spec/lib/arstotzka/builder_spec.rb
|
231
234
|
- spec/lib/arstotzka/crawler_spec.rb
|
232
235
|
- spec/lib/arstotzka/fetcher_spec.rb
|
236
|
+
- spec/lib/arstotzka/options_spec.rb
|
233
237
|
- spec/lib/arstotzka/reader_spec.rb
|
234
238
|
- spec/lib/arstotzka/wrapper_spec.rb
|
235
239
|
- spec/lib/arstotzka_spec.rb
|
@@ -298,6 +302,7 @@ test_files:
|
|
298
302
|
- spec/lib/arstotzka/builder_spec.rb
|
299
303
|
- spec/lib/arstotzka/crawler_spec.rb
|
300
304
|
- spec/lib/arstotzka/fetcher_spec.rb
|
305
|
+
- spec/lib/arstotzka/options_spec.rb
|
301
306
|
- spec/lib/arstotzka/reader_spec.rb
|
302
307
|
- spec/lib/arstotzka/wrapper_spec.rb
|
303
308
|
- spec/lib/arstotzka_spec.rb
|