frodo 0.12.4 → 0.12.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6171a906fa516145d1ea9bbff61f771cdbf9b936ceb815071ac93390ab9775c2
4
- data.tar.gz: 0f2c9b9d8d3c0311edd87b8a624ca8a6c807662a2522b3acfeffcdb29abb8b8b
3
+ metadata.gz: be94d246c0478f5b6e1f7b8816a9b2558c8f4e71649d50ce9722ae33dc8496a1
4
+ data.tar.gz: b09e077ea5a40039b571601c8c6a1e3576649dca6d81925c8df83a9842b35902
5
5
  SHA512:
6
- metadata.gz: be855e21a2371911d083c079059cd79473826f310334e11b75bda47d9cc59d883da3649587b308bec88b1bb2a0af779cec8bc29acc5bae3532ecf84049fc8be3
7
- data.tar.gz: 1de688ddb8d1695c59d29d42f92d14e1a86572d4cc2e0fd16268fe7f1a7a4bd8e56e145265aeb3b000ca4053f5ff4ac74328b20db31740053e93f03375e2fa43
6
+ metadata.gz: be646abf7839a1e9a726725691cffab8d41de9fd317664295b5bde64387b6e1fd434faef311bc6c1754fbc6f123e833ba4e806c605d3240799e0b7bce56b5440
7
+ data.tar.gz: adf218a868e0464583e32ebf1fc9b3e229e375ec89ec1344e2b4f7b4e05acc7716b9464dc17a28c392264934419a607320b46b037828faa20d3942805a06f27c
@@ -69,9 +69,9 @@ module Frodo
69
69
  end
70
70
 
71
71
  body = api_get(url_chunk).body
72
-
73
72
  # if manual query as a string we detect the set on the response
74
73
  entity_set = body['@odata.context'].split('#')[-1] if entity_set.nil?
74
+
75
75
  build_entity(entity_set, body)
76
76
  end
77
77
 
@@ -109,13 +109,12 @@ module Frodo
109
109
  # Returns the primary key value of the newly created entity.
110
110
  # Raises exceptions if an error is returned from Dynamics.
111
111
  def create!(entity_set_name, attrs)
112
- entity_set = service[entity_set_name]
113
- url_chunk = entity_set_to_url_chunk(entity_set)
114
- url = api_post(url_chunk, attrs).headers['odata-entityid']
112
+ url = api_post(entity_set_name, attrs).headers['odata-entityid']
115
113
  id_match = url.match(/\((.+)\)/)
116
114
  if id_match.nil?
117
115
  raise Frodo::Error.new "entity url not in expected format: #{url.inspect}"
118
116
  end
117
+
119
118
  return id_match[1]
120
119
  end
121
120
  alias insert! create!
@@ -150,12 +149,10 @@ module Frodo
150
149
  #
151
150
  # Returns true if the entity was successfully updated.
152
151
  # Raises an exception if an error is returned from Dynamics.
153
- def update!(entity_set, attrs, additional_headers={})
154
- entity = service[entity_set].new_entity(attrs)
155
- url_chunk = to_url_chunk(entity)
156
-
157
- raise ArgumentError, 'ID field missing from provided attributes' if entity.is_new?
152
+ def update!(entity_set_name, primary_key, attrs, additional_headers={})
153
+ raise ArgumentError, 'ID field missing from provided attributes' unless attrs.has_key?(primary_key)
158
154
 
155
+ url_chunk = "#{entity_set_name}(#{attrs[primary_key]})"
159
156
  api_patch url_chunk, attrs do |req|
160
157
  req.headers.merge!(additional_headers)
161
158
  end
@@ -208,7 +205,7 @@ module Frodo
208
205
  # Returns the Entity record.
209
206
  def find(entity_set, id)
210
207
  query = service[entity_set].query
211
- url_chunk = query.find(id)
208
+ url_chunk = query.find(id, entity_set)
212
209
 
213
210
  body = api_get(url_chunk).body
214
211
  build_entity(entity_set, body)
@@ -225,7 +222,7 @@ module Frodo
225
222
  query = service[entity_set].query
226
223
 
227
224
  fields.each{|field| query.select(field)}
228
- url_chunk = query.find(id)
225
+ url_chunk = query.find(id, entity_set)
229
226
 
230
227
  body = api_get(url_chunk).body
231
228
  build_entity(entity_set, body)
@@ -269,8 +266,11 @@ module Frodo
269
266
  end
270
267
 
271
268
  def build_entity(entity_set, data)
272
- entity_options = service[entity_set].entity_options
273
- single_entity?(data) ? parse_entity(data, entity_options) : parse_entities(data, entity_options)
269
+ if service.with_metadata?
270
+ entity_options = service[entity_set].entity_options
271
+ return single_entity?(data) ? parse_entity(data, entity_options) : parse_entities(data, entity_options)
272
+ end
273
+ single_entity?(data) ? data : data['value']
274
274
  end
275
275
 
276
276
  def single_entity?(body)
@@ -293,9 +293,7 @@ module Frodo
293
293
  entity.is_new? ? set : "#{set}(#{primary_key})"
294
294
  end
295
295
 
296
- def entity_set_to_url_chunk(entity_set)
297
- return entity_set.name
298
- end
296
+
299
297
 
300
298
  # Internal: Errors that should be rescued from in non-bang methods
301
299
  def exceptions
@@ -30,12 +30,7 @@ module Frodo
30
30
  # @param key [to_s] primary key to lookup
31
31
  # @return the path and querystring [String]
32
32
  def find(key)
33
- entity = @entity_set.new_entity
34
- key_property = entity.get_property(entity.primary_key)
35
- key_property.value = key
36
-
37
- pathname = "#{entity_set.name}(#{key_property.url_value})"
38
-
33
+ pathname = "#{entity_set.name}(#{key})"
39
34
  select_criteria = if list_criteria(:select)
40
35
  list_criteria(:select).map { |k, v| "#{k}=#{v}" }.join('&')
41
36
  end
@@ -70,7 +70,11 @@ module Frodo
70
70
  # @param entity_set_name [to_s] the name of the EntitySet desired
71
71
  # @return [Frodo::EntitySet] an Frodo::EntitySet to query
72
72
  def [](entity_set_name)
73
- entity_container[entity_set_name]
73
+ if with_metadata?
74
+ entity_container[entity_set_name]
75
+ else
76
+ EntitySet.new(name: entity_set_name)
77
+ end
74
78
  end
75
79
 
76
80
  # Returns the default namespace, that is, the namespace of the schema
@@ -168,6 +172,10 @@ module Frodo
168
172
  end
169
173
  end
170
174
 
175
+ def with_metadata?
176
+ !options.key?(:with_metadata) || options[:with_metadata]
177
+ end
178
+
171
179
  private
172
180
 
173
181
  def default_options
@@ -177,7 +185,7 @@ module Frodo
177
185
  end
178
186
 
179
187
  def default_logger
180
- Frodo.configuration.logger if Frodo.log?
188
+ Frodo.configuration.logger if Frodo.log?
181
189
  end
182
190
 
183
191
  def read_metadata
@@ -1,3 +1,3 @@
1
1
  module Frodo
2
- VERSION = '0.12.4'
2
+ VERSION = '0.12.5'
3
3
  end
@@ -26,6 +26,7 @@ describe Frodo::Client do
26
26
  to_return(body: File.new('spec/fixtures/files/metadata.xml'), status: 200)
27
27
 
28
28
  service = client.service
29
+ allow(service).to receive(:with_metadata?).and_return(true)
29
30
 
30
31
  expect(service['Products']).to_not be_nil
31
32
  expect(service['Products'].name).to eql('Products')
@@ -6,22 +6,23 @@ require 'json'
6
6
  describe Frodo::Concerns::API do
7
7
  let(:klass) do
8
8
  context = self
9
- Class.new {
9
+ Class.new do
10
10
  include Frodo::Concerns::Base
11
11
  include Frodo::Concerns::Connection
12
12
 
13
13
  include context.described_class
14
- }
14
+ end
15
15
  end
16
16
 
17
17
  let(:client) { klass.new }
18
- let(:connection) {
19
- Faraday.new('http://frodo.com', {}) do |conn|
18
+ let(:connection_uri) { 'http://frodo.com' }
19
+ let(:connection) do
20
+ Faraday.new(connection_uri, {}) do |conn|
20
21
  conn.request :json
21
22
  conn.response :json
22
23
  conn.adapter Faraday.default_adapter
23
24
  end
24
- }
25
+ end
25
26
 
26
27
  subject { client }
27
28
 
@@ -58,7 +59,7 @@ describe Frodo::Concerns::API do
58
59
  let(:url_chunk) { "leads?$filter=firstname eq 'yo'" }
59
60
  let(:path) { url_chunk }
60
61
  let(:entity_name) { 'entity' }
61
- let(:context) { "serviceRoot/$metadata##{entity_name}"}
62
+ let(:context) { "serviceRoot/$metadata##{entity_name}" }
62
63
  let(:body) { { '@odata.context' => context } }
63
64
 
64
65
  context 'url_chunk provided' do
@@ -105,7 +106,7 @@ describe Frodo::Concerns::API do
105
106
  it 'raises other errors' do
106
107
  allow(client).to receive(:create!).with(entity_type, attributes).and_raise(StandardError)
107
108
 
108
- expect{ subject }.to raise_error(StandardError)
109
+ expect { subject }.to raise_error(StandardError)
109
110
  end
110
111
  end
111
112
 
@@ -116,27 +117,14 @@ describe Frodo::Concerns::API do
116
117
  let(:attributes) { {} }
117
118
  let(:options) { attributes }
118
119
  let(:headers) { { 'odata-entityid' => url } }
119
- let(:entity_set_name) { 'things' }
120
120
 
121
+ before { stub_request(verb, "#{connection_uri}/#{entity_type}").to_return(body: body.to_json, headers: headers) }
121
122
  subject { client.create!(entity_type, attributes) }
122
123
 
123
- before do
124
- allow(client).to receive(:service).and_return(service)
125
- allow(service).to receive(:[]).with(entity_type).and_return(entity_set)
126
- allow(entity_set).to receive(:name).and_return(entity_set_name)
127
- allow(client).to receive(:entity_set_to_url_chunk).with(entity_set).and_return(path)
128
- end
129
-
130
124
  it 'posts entity_set info and returns resulting id' do
131
125
  expect(subject).to eq(id)
132
126
  end
133
127
 
134
- it 'raises errors that occur' do
135
- allow(client).to receive(:service).and_raise(StandardError)
136
-
137
- expect{ subject }.to raise_error(StandardError)
138
- end
139
-
140
128
  context 'alias .insert!' do
141
129
  subject { client.insert!(entity_type, attributes) }
142
130
 
@@ -145,38 +133,42 @@ describe Frodo::Concerns::API do
145
133
  end
146
134
 
147
135
  describe '.update' do
148
- let(:attributes) { {} }
136
+ let(:attributes) { { 'typeid' => 'some_id' } }
137
+ let(:primary_key) { 'typeid' }
149
138
 
150
- subject { client.update(entity_type, attributes) }
139
+ before { stub_request(verb, "#{connection_uri}/#{entity_type}(some_id)").to_return(body: body.to_json, headers: headers) }
140
+ subject { client.update(entity_type, primary_key, attributes) }
151
141
 
152
142
  it 'calls .create! and returns the result' do
153
- allow(client).to receive(:update!).with(entity_type, attributes).and_return(true)
143
+ expect(client).to receive(:update!).with(entity_type, primary_key, attributes).and_return(true)
154
144
 
155
145
  expect(subject).to be(true)
156
146
  end
157
147
 
158
148
  it 'returns false for expected exceptions' do
159
- allow(client).to receive(:update!).with(entity_type, attributes).and_raise(client_error)
149
+ allow(client).to receive(:update!).with(entity_type, primary_key, attributes).and_raise(client_error)
160
150
 
161
151
  expect(subject).to eq(false)
162
152
  end
163
153
 
164
154
  it 'raises other errors' do
165
- allow(client).to receive(:update!).with(entity_type, attributes).and_raise(StandardError)
155
+ allow(client).to receive(:update!).with(entity_type, primary_key, attributes).and_raise(StandardError)
166
156
 
167
- expect{ subject }.to raise_error(StandardError)
157
+ expect { subject }.to raise_error(StandardError)
168
158
  end
169
159
  end
170
160
 
171
161
  describe '.update!' do
172
162
  let(:verb) { :patch }
173
- let(:attributes) { {} }
163
+ let(:attributes) { { 'typeid' => 'some_id' } }
174
164
  let(:options) { attributes }
175
165
  let(:is_new) { false }
166
+ let(:primary_key) { 'typeid' }
176
167
 
177
- subject { client.update!(entity_type, attributes) }
168
+ subject { client.update!(entity_type, primary_key, attributes) }
178
169
 
179
170
  before do
171
+ stub_request(verb, "#{connection_uri}/#{entity_type}(some_id)").to_return(body: body.to_json, headers: headers)
180
172
  allow(client).to receive(:service).and_return(service)
181
173
  allow(service).to receive(:[]).with(entity_type).and_return(entity_set)
182
174
  allow(entity_set).to receive(:new_entity).with(attributes).and_return(entity)
@@ -189,37 +181,40 @@ describe Frodo::Concerns::API do
189
181
  end
190
182
 
191
183
  it 'raises errors that occur' do
192
- allow(client).to receive(:service).and_raise(StandardError)
184
+ allow(client).to receive(:api_patch).and_raise(StandardError)
193
185
 
194
- expect{ subject }.to raise_error(StandardError)
186
+ expect { subject }.to raise_error(StandardError)
195
187
  end
196
188
 
197
189
  context 'new entity (ie. has no id)' do
198
190
  let(:is_new) { true }
191
+ let(:attributes) { {} }
199
192
 
200
193
  it 'raises ArgumentError' do
201
- expect{ subject }.to raise_error(ArgumentError)
194
+ expect { subject }.to raise_error(ArgumentError)
202
195
  end
203
196
  end
204
197
 
205
198
  context 'with @odata.bind properties' do
206
- let(:attributes) {{
199
+ let(:attributes) do
200
+ {
207
201
  'ownerid@odata.bind': '/systemusers(12345)'
208
- }}
202
+ }
203
+ end
209
204
  it 'calls .update! with unmodified attributes' do
210
- expect(client).to receive(:update!).with(entity_type, attributes).and_return(true)
205
+ expect(client).to receive(:update!).with(entity_type, primary_key, attributes).and_return(true)
211
206
  expect(subject).to be(true)
212
207
  end
213
208
  end
214
209
 
215
210
  context 'with additional headers' do
216
- let(:additional_header) { {"header" => '1' } }
211
+ let(:additional_header) { { 'header' => '1' } }
217
212
 
218
213
  before do
219
214
  stub_request(verb, uri).to_return(body: body.to_json, headers: headers.merge!(additional_header))
220
215
  end
221
216
 
222
- subject { client.update!(entity_type, attributes, additional_header) }
217
+ subject { client.update!(entity_type, primary_key, attributes, additional_header) }
223
218
 
224
219
  it 'should update' do
225
220
  expect(subject).to be(true)
@@ -227,7 +222,7 @@ describe Frodo::Concerns::API do
227
222
 
228
223
  it 'sets headers on the built request object' do
229
224
  expect_any_instance_of(Faraday::Builder).to receive(:build_response)
230
- .with(anything(), have_attributes(headers: hash_including(additional_header)))
225
+ .with(anything, have_attributes(headers: hash_including(additional_header)))
231
226
 
232
227
  subject
233
228
  end
@@ -235,7 +230,6 @@ describe Frodo::Concerns::API do
235
230
  end
236
231
 
237
232
  describe '.destroy' do
238
-
239
233
  subject { client.destroy(entity_type, id) }
240
234
 
241
235
  it 'calls .create! and returns true' do
@@ -253,7 +247,7 @@ describe Frodo::Concerns::API do
253
247
  it 'raises other errors' do
254
248
  allow(client).to receive(:destroy!).with(entity_type, id).and_raise(StandardError)
255
249
 
256
- expect{ subject }.to raise_error(StandardError)
250
+ expect { subject }.to raise_error(StandardError)
257
251
  end
258
252
  end
259
253
 
@@ -279,14 +273,13 @@ describe Frodo::Concerns::API do
279
273
  end
280
274
 
281
275
  describe '.find' do
282
-
283
- subject { client.find(entity_type, id)}
276
+ subject { client.find(entity_type, id) }
284
277
 
285
278
  it 'returns found entity_set' do
286
279
  allow(client).to receive(:service).and_return(service)
287
280
  allow(service).to receive(:[]).with(entity_type).and_return(entity_set)
288
281
  allow(entity_set).to receive(:query).and_return(query)
289
- allow(query).to receive(:find).with(id).and_return(path)
282
+ allow(query).to receive(:find).with(id, entity_type).and_return(path)
290
283
  allow(client).to receive(:build_entity).with(entity_type, body).and_return(entity)
291
284
 
292
285
  expect(subject).to eq(entity)
@@ -329,7 +322,6 @@ describe Frodo::Concerns::API do
329
322
  subject { client.count(query) }
330
323
 
331
324
  context 'provided a Frodo::Query' do
332
-
333
325
  it 'uses query object to build count query and returns count' do
334
326
  allow(query).to receive(:is_a?).with(Frodo::Query.class).and_return(true)
335
327
  allow(query).to receive(:include_count)
@@ -341,7 +333,7 @@ describe Frodo::Concerns::API do
341
333
  it 'raises any error that occurs' do
342
334
  allow(query).to receive(:is_a?).with(Frodo::Query.class).and_raise(StandardError)
343
335
 
344
- expect{ subject }.to raise_error(StandardError)
336
+ expect { subject }.to raise_error(StandardError)
345
337
  end
346
338
  end
347
339
 
@@ -362,7 +354,7 @@ describe Frodo::Concerns::API do
362
354
  it 'raises any error that occurs' do
363
355
  allow(client).to receive(:service).and_raise(StandardError)
364
356
 
365
- expect{ subject }.to raise_error(StandardError)
357
+ expect { subject }.to raise_error(StandardError)
366
358
  end
367
359
  end
368
360
  end
@@ -370,7 +362,6 @@ describe Frodo::Concerns::API do
370
362
  # private methods
371
363
 
372
364
  describe '.api_path' do
373
-
374
365
  subject { client.send(:api_path, path) }
375
366
 
376
367
  context 'base_path is defined' do
@@ -393,24 +384,43 @@ describe Frodo::Concerns::API do
393
384
 
394
385
  before do
395
386
  allow(client).to receive(:service).and_return(service)
387
+ allow(service).to receive(:with_metadata?).and_return(true)
396
388
  allow(service).to receive(:[]).with(entity_type).and_return(entity_set)
397
389
  allow(entity_set).to receive(:entity_options).and_return(options)
398
390
  end
391
+ context 'without metadata' do
392
+ before { allow(service).to receive(:with_metadata?).and_return(false) }
399
393
 
400
- it 'parses single entity' do
401
- allow(client).to receive(:single_entity?).with(data).and_return(true)
402
-
403
- expect(client).to receive(:parse_entity).with(data, options)
404
- subject
405
- end
394
+ it 'parses single entity' do
395
+ allow(client).to receive(:single_entity?).with(data).and_return(true)
396
+ expect(subject).to eq(data)
397
+ end
406
398
 
407
- context 'multiple entities' do
408
- it 'parses multiple entities' do
409
- allow(client).to receive(:single_entity?).with(data).and_return(false)
399
+ context 'multiple entities' do
400
+ let(:data) { { 'odata' => 'test', 'value' => 'data!!!' } }
401
+ it 'parses multiple entities' do
402
+ allow(client).to receive(:single_entity?).with(data).and_return(false)
410
403
 
411
- expect(client).to receive(:parse_entities).with(data, options)
404
+ expect(subject).to eq(data['value'])
405
+ end
406
+ end
407
+ end
408
+ context 'with metadata' do
409
+ before { allow(service).to receive(:with_metadata?).and_return(true) }
410
+ it 'parses single entity' do
411
+ expect(client).to receive(:single_entity?).with(data).and_return(true)
412
+ expect(client).to receive(:parse_entity).with(data, options)
412
413
  subject
413
414
  end
415
+
416
+ context 'multiple entities' do
417
+ let(:data) { { 'odata' => 'test', 'value' => 'data!!!' } }
418
+ it 'parses multiple entities' do
419
+ expect(client).to receive(:single_entity?).with(data).and_return(false)
420
+ expect(client).to receive(:parse_entities).with(data, options)
421
+ subject
422
+ end
423
+ end
414
424
  end
415
425
  end
416
426
 
@@ -442,9 +452,9 @@ describe Frodo::Concerns::API do
442
452
  end
443
453
 
444
454
  describe '.parse_entities' do
445
- let(:entity_data) { "data!!!" }
455
+ let(:entity_data) { 'data!!!' }
446
456
  let(:data_value) { [entity_data, entity_data] }
447
- let(:data) { { 'value' => data_value } }
457
+ let(:data) { { 'value' => data_value } }
448
458
 
449
459
  subject { client.send(:parse_entities, data, options) }
450
460
 
@@ -455,9 +465,9 @@ describe Frodo::Concerns::API do
455
465
  end
456
466
 
457
467
  describe '.to_url_chunk' do
458
- let(:primary_key) { "I am the key!" }
459
- let(:property) { double() }
460
- let(:set) { "Who am I?" }
468
+ let(:primary_key) { 'I am the key!' }
469
+ let(:property) { double }
470
+ let(:set) { 'Who am I?' }
461
471
 
462
472
  subject { client.send(:to_url_chunk, entity) }
463
473
 
@@ -130,7 +130,40 @@ describe Frodo::Service do
130
130
  describe '#[]' do
131
131
  let(:entity_sets) { subject.entity_sets.keys.map { |name| subject[name] } }
132
132
  it { expect(entity_sets).to all(be_a(Frodo::EntitySet)) }
133
- it { expect {subject['Nonexistant']}.to raise_error(ArgumentError) }
133
+ it { expect { subject['Nonexistant'] }.to raise_error(ArgumentError) }
134
+ context 'when with_metadata returns false' do
135
+ before { allow(subject).to receive(:with_metadata?).and_return(false) }
136
+ it 'returns empty EntitySet' do
137
+ expect(subject['Nonexistant']).to be_a(Frodo::EntitySet)
138
+ end
139
+ end
140
+ end
141
+
142
+ describe '#with_metadata?' do
143
+ let(:options){{
144
+ name: 'ODataDemo',
145
+ metadata_file: metadata_file
146
+ }}
147
+ let(:subject) { Frodo::Service.new(service_url, options) }
148
+
149
+ it { expect(subject.with_metadata?).to be true}
150
+
151
+ context "when with_metadata true" do
152
+ let(:options){{
153
+ name: 'ODataDemo',
154
+ metadata_file: metadata_file,
155
+ with_metadata: true
156
+ }}
157
+ it { expect(subject.with_metadata?).to be true}
158
+ end
159
+ context "when with_metadata true" do
160
+ let(:options){{
161
+ name: 'ODataDemo',
162
+ metadata_file: metadata_file,
163
+ with_metadata: true
164
+ }}
165
+ it { expect(subject.with_metadata?).to be true}
166
+ end
134
167
  end
135
168
 
136
169
  describe '#get_property_type' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frodo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.4
4
+ version: 0.12.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Pinault
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-15 00:00:00.000000000 Z
11
+ date: 2019-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri