ddy_remote_resource 0.4.5 → 0.4.6

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: 81623623abfc2c02eca8ba86097669fe5228e691
4
- data.tar.gz: d6d96b4787ab3c8fbbc9489025da6b8fa680c3b0
3
+ metadata.gz: fd27f10265b4c4567775dc03f968ec908257802e
4
+ data.tar.gz: 5abcb50998d29ad84675de80012dfc20f3b836b2
5
5
  SHA512:
6
- metadata.gz: 5891d80cd394f2682b5df1f1962ac676d42966d50153637a9250c0e096ae3ddce3dd1fffd979224561669c6e02bfda14ddf2da50bd1ef86575e416c024e32257
7
- data.tar.gz: 93d0f8606a690a56b8fe2f319beac338735810772ba4af8d6b9179947799dd12d06cdb6d645fdc1b1399c13575413d25ec9a4880ed0986bf647254231648321e
6
+ metadata.gz: 27c071103e04b51b9eef007bf57b802147c0bed726d000c547ea1561ed7e1d941733c366eb5c4a847efe4e7d2c96c43ae3170c9e006d820d362037a03a4eccee
7
+ data.tar.gz: 67bf285bdfd25e82f43c7f6e8c68fbfdfd9113117487e249d6f6cedb9a01b30578d2a980d6613dd0d7d205d662f575776ddf2f3761e84b903e9d5bb6ebdc6e3f
data/README.md CHANGED
@@ -39,7 +39,7 @@ You can set a few options for the `RemoteResource` enabled class.
39
39
 
40
40
  #### Base URL options (`base_url`)
41
41
 
42
- The `base_url` is constructed from the `.site`, `.version`, `.path_prefix`, `.path_postfix`, `.collection`, and `.collection_name` options. The `.collection_name` is automatically constructed from the relative class name.
42
+ The `base_url` is constructed from the `.site`, `.version`, `.path_prefix`, `.path_postfix`, `.collection`, `.collection_prefix`, and `.collection_name` options. The `.collection_name` is automatically constructed from the relative class name.
43
43
 
44
44
  We will use the `ContactPerson` class for these examples, with the `.collection_name` of `'contact_person'`:
45
45
 
@@ -59,6 +59,10 @@ We will use the `ContactPerson` class for these examples, with the `.collection_
59
59
  * *Default:* `false`
60
60
  * *Example:* `.collection = true`
61
61
  * *`base_url`:* `https://www.myapp.com/contact_persons`
62
+ * `.collection_prefix`: This sets the prefix for the collection, before `collection_name` that is used to construct the `base_url`. The prefix variable has to be set via connection_options' key `collection_options`.
63
+ * *Default:* ``
64
+ * *Example:* `.collection_prefix = "/companies/:company_id"` and connection_options `collection_options: { company_id: 2 }`
65
+ * *`base_url`:* `https://www.myapp.com/companies/2/contact_persons`
62
66
  * `.collection_name`: This sets the `collection_name` that is used to construct the `base_url`.
63
67
  * *Example:* `.collection_name = "company"`
64
68
  * *`base_url`:* `https://www.myapp.com/company`
@@ -3,7 +3,7 @@ module RemoteResource
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- class_attribute :site, :version, :path_prefix, :path_postfix, :collection, :collection_name, instance_accessor: false
6
+ class_attribute :site, :version, :path_prefix, :path_postfix, :collection_prefix, :collection, :collection_name, instance_accessor: false
7
7
 
8
8
  self.collection = false
9
9
  end
@@ -16,7 +16,17 @@ module RemoteResource
16
16
 
17
17
  id = "/#{id}" if id.present?
18
18
 
19
- "#{site}#{version.presence}#{path_prefix.presence}/#{url_safe_relative_name}#{id}#{path_postfix.presence}"
19
+ "#{site}#{version.presence}#{path_prefix.presence}#{collection_prefix}/#{url_safe_relative_name}#{id}#{path_postfix.presence}"
20
+ end
21
+
22
+ def collection_prefix
23
+ prefix = connection_options.fetch(:collection_prefix, resource_klass.collection_prefix)
24
+
25
+ if prefix.present?
26
+ 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) }
29
+ end
20
30
  end
21
31
 
22
32
  def url_safe_relative_name
@@ -1,3 +1,3 @@
1
1
  module RemoteResource
2
- VERSION = '0.4.5'
2
+ VERSION = '0.4.6'
3
3
  end
@@ -95,6 +95,67 @@ describe RemoteResource::UrlNamingDetermination do
95
95
  end
96
96
  end
97
97
 
98
+ context 'collection_prefix' do
99
+ context 'when the connection_options contain a collection_prefix' do
100
+ let(:connection_options) do
101
+ { collection_prefix: '/parent/:parent_id', collection_options: { parent_id: 696 } }
102
+ end
103
+
104
+ it 'uses the collection_prefix of the connection_options' do
105
+ expect(url_naming_determination.base_url).to eql 'http://www.foobar.com/parent/696/url_naming_determination_dummy'
106
+ end
107
+
108
+ context 'when collection_options is NOT present' do
109
+ let(:connection_options) do
110
+ { collection_prefix: '/parent/:parent_id' }
111
+ end
112
+
113
+ it 'raises an exception' do
114
+ expect { url_naming_determination.base_url }.to raise_error(KeyError)
115
+ end
116
+ end
117
+
118
+ context 'when collection_prefix variable is not set in the collection_options' do
119
+ let(:connection_options) do
120
+ { collection_prefix: '/parent/:parent_id', collection_options: { other_id: 696 } }
121
+ end
122
+
123
+ it 'raises an exception' do
124
+ expect { url_naming_determination.base_url }.to raise_error(KeyError)
125
+ end
126
+ end
127
+ end
128
+
129
+ context 'when the connection_options do NOT contain a collection_prefix' do
130
+ context 'and the resource_klass contains a collection_prefix' do
131
+ before { dummy_class.collection_prefix = '/parent/:parent_id' }
132
+ after { dummy_class.collection_prefix = nil }
133
+
134
+ context 'when connection_options includes collection_options with key parent_id' do
135
+ let(:connection_options) do
136
+ { collection_options: { parent_id: 696 } }
137
+ end
138
+
139
+ it 'uses the collection_prefix of the resource_klass' do
140
+ expect(url_naming_determination.base_url).to eql 'http://www.foobar.com/parent/696/url_naming_determination_dummy'
141
+ end
142
+ end
143
+
144
+ 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)
147
+ end
148
+ end
149
+ end
150
+
151
+ context 'and the resource_klass does NOT contain a collection_prefix' do
152
+ it 'does NOT use the collection_prefix' do
153
+ expect(url_naming_determination.base_url).to eql 'http://www.foobar.com/url_naming_determination_dummy'
154
+ end
155
+ end
156
+ end
157
+ end
158
+
98
159
  context 'id' do
99
160
  context 'when an id is specified' do
100
161
  it 'uses that id in the base url' do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe RemoteResource::VERSION do
4
4
 
5
- it { should eql '0.4.5' }
5
+ it { should eql '0.4.6' }
6
6
  end
7
7
 
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddy_remote_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan van der Pas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-07 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler