ddy_remote_resource 0.4.6 → 0.4.7

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: fd27f10265b4c4567775dc03f968ec908257802e
4
- data.tar.gz: 5abcb50998d29ad84675de80012dfc20f3b836b2
3
+ metadata.gz: ee1e160b0d79e1f23df3b5f2005f26440bd443c5
4
+ data.tar.gz: 85fb186045fd88dadeccd07f349c7695c4338df9
5
5
  SHA512:
6
- metadata.gz: 27c071103e04b51b9eef007bf57b802147c0bed726d000c547ea1561ed7e1d941733c366eb5c4a847efe4e7d2c96c43ae3170c9e006d820d362037a03a4eccee
7
- data.tar.gz: 67bf285bdfd25e82f43c7f6e8c68fbfdfd9113117487e249d6f6cedb9a01b30578d2a980d6613dd0d7d205d662f575776ddf2f3761e84b903e9d5bb6ebdc6e3f
6
+ metadata.gz: 6e50af7d6a5c7049e2067ea0521c8f74030eedbf74644ff254d831b797c923c87c6e2f74c7e968cf2706ac65d86036a9e5ad212eed20d32e79b916cb34e8681a
7
+ data.tar.gz: 88f99d3f9c0d5b136095d0baf364017b821b8e8108019d642e8d73c24bc80b5188e5fe470b0236b3e63750a794b2a0b50cf82001c2026f74fda31a029e71b84a
@@ -2,7 +2,7 @@ module RemoteResource
2
2
  module Base
3
3
  extend ActiveSupport::Concern
4
4
 
5
- OPTIONS = [:base_url, :site, :headers, :version, :path_prefix, :path_postfix, :content_type, :collection, :collection_name, :root_element]
5
+ OPTIONS = [:base_url, :site, :headers, :version, :path_prefix, :path_postfix, :collection_prefix, :content_type, :collection, :collection_name, :root_element]
6
6
 
7
7
  included do
8
8
  include Virtus.model
@@ -44,7 +44,7 @@ module RemoteResource
44
44
 
45
45
  def determined_request_url
46
46
  id = attributes[:id].presence
47
- base_url = original_connection_options[:base_url].presence || determined_url_naming.base_url(id)
47
+ base_url = original_connection_options[:base_url].presence || determined_url_naming.base_url(id, check_collection_options: true)
48
48
  content_type = connection_options[:content_type]
49
49
 
50
50
  "#{base_url}#{content_type}"
@@ -1,5 +1,6 @@
1
1
  module RemoteResource
2
2
  class UrlNamingDetermination
3
+ CollectionOptionKeyError = Class.new(StandardError)
3
4
 
4
5
  attr_reader :resource_klass, :connection_options
5
6
 
@@ -8,7 +9,7 @@ module RemoteResource
8
9
  @connection_options = connection_options
9
10
  end
10
11
 
11
- def base_url(id = nil)
12
+ def base_url(id = nil, check_collection_options: false)
12
13
  site = connection_options.fetch(:site, resource_klass.site)
13
14
  version = connection_options.fetch(:version, resource_klass.version)
14
15
  path_prefix = connection_options.fetch(:path_prefix, resource_klass.path_prefix)
@@ -16,16 +17,24 @@ module RemoteResource
16
17
 
17
18
  id = "/#{id}" if id.present?
18
19
 
19
- "#{site}#{version.presence}#{path_prefix.presence}#{collection_prefix}/#{url_safe_relative_name}#{id}#{path_postfix.presence}"
20
+ "#{site}#{version.presence}#{path_prefix.presence}#{collection_prefix(check_collection_options)}/#{url_safe_relative_name}#{id}#{path_postfix.presence}"
20
21
  end
21
22
 
22
- def collection_prefix
23
+ def collection_prefix(check_collection_options)
23
24
  prefix = connection_options.fetch(:collection_prefix, resource_klass.collection_prefix)
24
25
 
25
26
  if prefix.present?
26
27
  prefix = "/#{prefix}" unless prefix.chr == '/'
27
- collection_options = connection_options.fetch(:collection_options).with_indifferent_access
28
- prefix.gsub(/:\w+/) { |key| URI.parser.escape(collection_options.fetch(key[1..-1]).to_s) }
28
+ collection_options = connection_options.fetch(:collection_options, {}).with_indifferent_access
29
+
30
+ prefix.gsub(/:\w+/) do |key|
31
+ value = collection_options.fetch(key[1..-1], nil)
32
+ if value.nil?
33
+ raise(CollectionOptionKeyError, "`collection_prefix` variable `#{key}` is missing from `collection_options`") if check_collection_options
34
+ value = key
35
+ end
36
+ URI.parser.escape(value.to_s)
37
+ end
29
38
  end
30
39
  end
31
40
 
@@ -1,3 +1,3 @@
1
1
  module RemoteResource
2
- VERSION = '0.4.6'
2
+ VERSION = '0.4.7'
3
3
  end
@@ -23,7 +23,7 @@ describe RemoteResource::Base do
23
23
  specify { expect(described_class.const_defined?('RemoteResource::Querying::PersistenceMethods')).to be_truthy }
24
24
 
25
25
  describe 'OPTIONS' do
26
- let(:options) { [:base_url, :site, :headers, :version, :path_prefix, :path_postfix, :content_type, :collection, :collection_name, :root_element] }
26
+ let(:options) { [:base_url, :site, :headers, :version, :path_prefix, :path_postfix, :collection_prefix, :content_type, :collection, :collection_name, :root_element] }
27
27
 
28
28
  specify { expect(described_class::OPTIONS).to eql options }
29
29
  end
@@ -6,15 +6,16 @@ describe RemoteResource::ConnectionOptions do
6
6
  class ConnectionOptionsDummy
7
7
  include RemoteResource::Base
8
8
 
9
- self.site = 'https://foobar.com'
10
- self.content_type = ''
11
- self.extra_headers = { "X-Locale" => "nl" }
12
- self.version = '/v1'
13
- self.path_prefix = '/prefix'
14
- self.path_postfix = '/postfix'
15
- self.content_type = '.json'
16
- self.collection = true
17
- self.root_element = :test_dummy
9
+ self.site = 'https://foobar.com'
10
+ self.content_type = ''
11
+ self.extra_headers = { "X-Locale" => "nl" }
12
+ self.version = '/v1'
13
+ self.path_prefix = '/prefix'
14
+ self.path_postfix = '/postfix'
15
+ self.content_type = '.json'
16
+ self.collection_prefix = '/parent/:parent_id'
17
+ self.collection = true
18
+ self.root_element = :test_dummy
18
19
 
19
20
  end
20
21
  end
@@ -77,16 +78,17 @@ describe RemoteResource::ConnectionOptions do
77
78
  describe '#to_hash' do
78
79
  let(:connection_options_hash) do
79
80
  {
80
- base_url: 'https://foobar.com/v1/prefix/connection_options_dummies/postfix',
81
- site: 'https://foobar.com',
82
- headers: { "Accept" => "application/json", "X-Locale" => "nl" },
83
- version: '/v1',
84
- path_prefix: '/prefix',
85
- path_postfix: '/postfix',
86
- content_type: '.json',
87
- collection: true,
88
- collection_name: nil,
89
- root_element: :test_dummy
81
+ base_url: 'https://foobar.com/v1/prefix/parent/:parent_id/connection_options_dummies/postfix',
82
+ site: 'https://foobar.com',
83
+ headers: { "Accept" => "application/json", "X-Locale" => "nl" },
84
+ version: '/v1',
85
+ path_prefix: '/prefix',
86
+ path_postfix: '/postfix',
87
+ content_type: '.json',
88
+ collection_prefix: '/parent/:parent_id',
89
+ collection: true,
90
+ collection_name: nil,
91
+ root_element: :test_dummy
90
92
  }
91
93
  end
92
94
 
@@ -11,6 +11,10 @@ describe RemoteResource::Request do
11
11
  attr_accessor :name
12
12
 
13
13
  end
14
+
15
+ class RequestDummyWithCollectionPrefix < RequestDummy
16
+ self.collection_prefix = '/parent/:parent_id'
17
+ end
14
18
  end
15
19
 
16
20
  let(:dummy_class) { RemoteResource::RequestDummy }
@@ -334,6 +338,24 @@ describe RemoteResource::Request do
334
338
  expect(request.determined_request_url).to eql 'http://www.foobar.com/request_dummy.json'
335
339
  end
336
340
  end
341
+
342
+ context 'collection_prefix' do
343
+ let(:dummy_class) { RemoteResource::RequestDummyWithCollectionPrefix }
344
+
345
+ context 'when connection_options does include collection_options' do
346
+ let(:connection_options) do
347
+ { collection_options: { parent_id: 23 } }
348
+ end
349
+
350
+ it { expect(request.determined_request_url).to eql 'http://www.foobar.com/parent/23/request_dummy_with_collection_prefix.json' }
351
+ end
352
+
353
+ context 'when connection_options does NOT include collection_options' do
354
+ it 'raises error' do
355
+ expect{ request.determined_request_url }.to raise_error(RemoteResource::UrlNamingDetermination::CollectionOptionKeyError)
356
+ end
357
+ end
358
+ end
337
359
  end
338
360
 
339
361
  describe '#determined_params' do
@@ -110,8 +110,14 @@ describe RemoteResource::UrlNamingDetermination do
110
110
  { collection_prefix: '/parent/:parent_id' }
111
111
  end
112
112
 
113
- it 'raises an exception' do
114
- expect { url_naming_determination.base_url }.to raise_error(KeyError)
113
+ it 'returns base_url with variable' do
114
+ expect(url_naming_determination.base_url).to eql 'http://www.foobar.com/parent/:parent_id/url_naming_determination_dummy'
115
+ end
116
+
117
+ context 'when check_collection_options is true' do
118
+ it 'raises an exception' do
119
+ expect { url_naming_determination.base_url(nil, check_collection_options: true) }.to raise_error(RemoteResource::UrlNamingDetermination::CollectionOptionKeyError)
120
+ end
115
121
  end
116
122
  end
117
123
 
@@ -120,8 +126,14 @@ describe RemoteResource::UrlNamingDetermination do
120
126
  { collection_prefix: '/parent/:parent_id', collection_options: { other_id: 696 } }
121
127
  end
122
128
 
123
- it 'raises an exception' do
124
- expect { url_naming_determination.base_url }.to raise_error(KeyError)
129
+ it 'returns base_url with variable' do
130
+ expect(url_naming_determination.base_url).to eql 'http://www.foobar.com/parent/:parent_id/url_naming_determination_dummy'
131
+ end
132
+
133
+ context 'when check_collection_options is true' do
134
+ it 'raises an exception' do
135
+ expect { url_naming_determination.base_url(nil, check_collection_options: true) }.to raise_error(RemoteResource::UrlNamingDetermination::CollectionOptionKeyError)
136
+ end
125
137
  end
126
138
  end
127
139
  end
@@ -142,8 +154,14 @@ describe RemoteResource::UrlNamingDetermination do
142
154
  end
143
155
 
144
156
  context 'when connection_options does NOT include collection_options' do
145
- it 'uses the collection_prefix of the resource_klass' do
146
- expect { url_naming_determination.base_url }.to raise_error(KeyError)
157
+ it 'returns base_url with variable' do
158
+ expect(url_naming_determination.base_url).to eql 'http://www.foobar.com/parent/:parent_id/url_naming_determination_dummy'
159
+ end
160
+
161
+ context 'when check_collection_options is true' do
162
+ it 'raises an exception' do
163
+ expect { url_naming_determination.base_url(nil, check_collection_options: true) }.to raise_error(RemoteResource::UrlNamingDetermination::CollectionOptionKeyError)
164
+ end
147
165
  end
148
166
  end
149
167
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe RemoteResource::VERSION do
4
4
 
5
- it { should eql '0.4.6' }
5
+ it { should eql '0.4.7' }
6
6
  end
7
7
 
8
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddy_remote_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan van der Pas