arstotzka 1.2.1 → 1.2.2

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
  SHA256:
3
- metadata.gz: fcdfefb20e55716afccbdb1db14cb24c1b23f5c3609e0cdaccfb897e6f949c32
4
- data.tar.gz: f374f9f717e7f3b867d40270bc531eb3c22e54b9060f232e5984157c4039e612
3
+ metadata.gz: b1fd5f8ae512abca296de96373bd5f07d48b3172091437ccf50c5e47a2c5797a
4
+ data.tar.gz: 46ff041da66faf1bffe3fb0df64b71d268dd6e95dc2e9c43be2583eda005a924
5
5
  SHA512:
6
- metadata.gz: 9c2b0cce20c80bdfdc92bfd3dbb0eeff17585e7c2373daa5cfb960bf9356b54feb5d5c9ea66353a52f9b6400720e3fdcc2d27529f5456b9b2c6f0f50a066adc7
7
- data.tar.gz: 2996ef066a6d98673f10961f82962e9d570fc23a34882d3a5d280efdb93677425eac4a52b0786551c324d04b296333b0c551603a5fd8923b99e61f81229ccf8e
6
+ metadata.gz: 51f92382c369a3fdc44eaa02915f8837a3760f36b8a80fcb7b315ad2a11646cd9578e02a165d32eeebeb0114341865c540b8a5ba3cfd8025fc75382dca6aa63c
7
+ data.tar.gz: a2d26c467a2c768927a8cf18ade4682792522f5fb4e90332b8fd41e08b13516d5022a95b30da20f658f37924b2467494fe7cd8bb1109d092476279a7f1b77a94
@@ -17,3 +17,7 @@ RSpec/AlignLeftLetBrace:
17
17
  Style/ClassAndModuleChildren:
18
18
  Exclude:
19
19
  - spec/integration/**/*.rb
20
+
21
+ Style/ClassVars:
22
+ Exclude:
23
+ - spec/support/models/arstotzka/fetcher/class_variable.rb
data/README.md CHANGED
@@ -38,7 +38,7 @@ gem 'arstotzka'
38
38
 
39
39
  Yard Documentation
40
40
  -------------------
41
- https://www.rubydoc.info/gems/arstotzka/1.2.1
41
+ https://www.rubydoc.info/gems/arstotzka/1.2.2
42
42
 
43
43
  Getting Started
44
44
  ---------------
@@ -23,7 +23,6 @@ rules:
23
23
  - Arstotzka::Crawler#options
24
24
  - Arstotzka::Fetcher#instance
25
25
  - Arstotzka::Fetcher#options
26
- - Arstotzka::Fetcher#hash
27
26
  - Arstotzka::FetcherBuilder#options
28
27
  - Arstotzka::MethodBuilder#attr_names
29
28
  - Arstotzka::MethodBuilder#options
@@ -38,7 +37,6 @@ rules:
38
37
  - Arstotzka::Crawler#options
39
38
  - Arstotzka::Fetcher#instance
40
39
  - Arstotzka::Fetcher#options
41
- - Arstotzka::Fetcher#hash
42
40
  - Arstotzka::FetcherBuilder#options
43
41
  - Arstotzka::MethodBuilder#attr_names
44
42
  - Arstotzka::MethodBuilder#options
@@ -93,9 +93,23 @@ module Arstotzka
93
93
  delegate :instance, :after, :flatten, to: :options
94
94
  delegate :wrap, to: :wrapper
95
95
 
96
+ # @private
97
+ #
98
+ # Retrieves the hash to be crawled from the instance
99
+ #
100
+ # @return [Hash]
101
+ # rubocop:disable Metrics/AbcSize
96
102
  def hash
97
- @hash ||= instance.send(:eval, options.json.to_s)
103
+ @hash ||= case options.json.to_s
104
+ when /^@@.*/
105
+ instance.class.class_variable_get(options.json)
106
+ when /^@.*/
107
+ then instance.instance_variable_get(options.json)
108
+ else
109
+ instance.send(options.json.to_s)
110
+ end
98
111
  end
112
+ # rubocop:enable Metrics/AbcSize
99
113
 
100
114
  # @private
101
115
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Arstotzka
4
- VERSION = '1.2.1'
4
+ VERSION = '1.2.2'
5
5
  end
@@ -7,13 +7,13 @@ describe Arstotzka::Fetcher do
7
7
  described_class.new options
8
8
  end
9
9
 
10
- let(:options) { Arstotzka::Options.new(options_hash) }
10
+ let(:options) { Arstotzka::Options.new(options_hash.merge(instance: instance)) }
11
11
  let(:instance) { Arstotzka::Fetcher::Dummy.new(json) }
12
12
  let(:json) { load_json_fixture_file('arstotzka.json') }
13
13
  let(:value) { fetcher.fetch }
14
14
 
15
15
  context 'when fetching with no options' do
16
- let(:options_hash) { { instance: instance, key: key } }
16
+ let(:options_hash) { { key: key } }
17
17
  let(:key) { 'id' }
18
18
 
19
19
  it 'retrieves attribute from base json' do
@@ -50,7 +50,7 @@ describe Arstotzka::Fetcher do
50
50
 
51
51
  context 'when flatten option is true' do
52
52
  let(:options_hash) do
53
- { instance: instance, flatten: true, key: :value }
53
+ { flatten: true, key: :value }
54
54
  end
55
55
 
56
56
  it 'returns the fetched value flattened' do
@@ -68,7 +68,7 @@ describe Arstotzka::Fetcher do
68
68
 
69
69
  context 'when flatten option is false' do
70
70
  let(:options_hash) do
71
- { instance: instance, flatten: false, key: :value }
71
+ { flatten: false, key: :value }
72
72
  end
73
73
 
74
74
  it 'returns the fetched value non flattened' do
@@ -82,7 +82,7 @@ describe Arstotzka::Fetcher do
82
82
  let(:json) { { value: [100, 250, -25] } }
83
83
 
84
84
  let(:options_hash) do
85
- { instance: instance, after: :sum, key: :value }
85
+ { after: :sum, key: :value }
86
86
  end
87
87
 
88
88
  it 'applies after call ' do
@@ -97,7 +97,7 @@ describe Arstotzka::Fetcher do
97
97
  let(:wrapper) { Person }
98
98
 
99
99
  let(:options_hash) do
100
- { instance: instance, klass: wrapper, path: path }
100
+ { klass: wrapper, path: path }
101
101
  end
102
102
 
103
103
  it 'wraps the result in an object' do
@@ -115,7 +115,6 @@ describe Arstotzka::Fetcher do
115
115
 
116
116
  let(:options_hash) do
117
117
  {
118
- instance: instance,
119
118
  full_path: full_path,
120
119
  after_each: :create_person,
121
120
  json: :@hash,
@@ -142,4 +141,40 @@ describe Arstotzka::Fetcher do
142
141
  expect(fetcher.fetch).to all(be_a(Person))
143
142
  end
144
143
  end
144
+
145
+ describe 'json options' do
146
+ context 'when json is a method' do
147
+ let(:instance) { described_class::HashJson.new(hash) }
148
+ let(:hash) { { name: 'John Doe' } }
149
+ let(:options_hash) { { key: :name, json: :hash } }
150
+
151
+ it 'fetches hash from given method' do
152
+ expect(fetcher.fetch).to eq('John Doe')
153
+ end
154
+ end
155
+
156
+ context 'when json is an instance variable' do
157
+ let(:instance) { described_class::NoAccessor.new(hash) }
158
+ let(:hash) { { name: 'John Doe' } }
159
+ let(:options_hash) { { key: :name, json: :@hash } }
160
+
161
+ it 'fetches hash from given method' do
162
+ expect(fetcher.fetch).to eq('John Doe')
163
+ end
164
+ end
165
+
166
+ context 'when json is an instance variable' do
167
+ let(:instance) { described_class::ClassVariable.new }
168
+ let(:hash) { { name: 'John Doe' } }
169
+ let(:options_hash) { { key: :name, json: :@@json } }
170
+
171
+ before do
172
+ described_class::ClassVariable.json = hash
173
+ end
174
+
175
+ it 'fetches hash from given method' do
176
+ expect(fetcher.fetch).to eq('John Doe')
177
+ end
178
+ end
179
+ end
145
180
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arstotzka
4
+ class Fetcher
5
+ class ClassVariable
6
+ def self.json=(json)
7
+ @@json = json
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arstotzka
4
+ class Fetcher
5
+ class HashJson
6
+ def initialize(hash)
7
+ @hash = hash
8
+ end
9
+
10
+ private
11
+
12
+ attr_reader :hash
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arstotzka
4
+ class Fetcher
5
+ class NoAccessor
6
+ def initialize(hash)
7
+ @hash = hash
8
+ end
9
+ end
10
+ end
11
+ end
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.2.1
4
+ version: 1.2.2
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-24 00:00:00.000000000 Z
11
+ date: 2019-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -247,7 +247,10 @@ files:
247
247
  - spec/support/models/account.rb
248
248
  - spec/support/models/application.rb
249
249
  - spec/support/models/arstotzka/dummy.rb
250
+ - spec/support/models/arstotzka/fetcher/class_variable.rb
250
251
  - spec/support/models/arstotzka/fetcher/dummy.rb
252
+ - spec/support/models/arstotzka/fetcher/hash_json.rb
253
+ - spec/support/models/arstotzka/fetcher/no_accessor.rb
251
254
  - spec/support/models/arstotzka/type_cast.rb
252
255
  - spec/support/models/arstotzka/wrapper/dummy.rb
253
256
  - spec/support/models/bar.rb
@@ -327,7 +330,10 @@ test_files:
327
330
  - spec/support/models/account.rb
328
331
  - spec/support/models/application.rb
329
332
  - spec/support/models/arstotzka/dummy.rb
333
+ - spec/support/models/arstotzka/fetcher/class_variable.rb
330
334
  - spec/support/models/arstotzka/fetcher/dummy.rb
335
+ - spec/support/models/arstotzka/fetcher/hash_json.rb
336
+ - spec/support/models/arstotzka/fetcher/no_accessor.rb
331
337
  - spec/support/models/arstotzka/type_cast.rb
332
338
  - spec/support/models/arstotzka/wrapper/dummy.rb
333
339
  - spec/support/models/bar.rb