rest-in-peace 4.1.0 → 4.1.1

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.1.0
1
+ 4.1.1
@@ -44,12 +44,8 @@ module RESTinPeace
44
44
  sanitizer.leftover_params
45
45
  end
46
46
 
47
- def attributes
48
- @attributes = @klass.to_h if @klass.respond_to?(:to_h)
49
- end
50
-
51
47
  def sanitizer
52
- @sanitizer ||= RESTinPeace::TemplateSanitizer.new(@url_template, @params, attributes)
48
+ @sanitizer ||= RESTinPeace::TemplateSanitizer.new(@url_template, @params, @klass)
53
49
  end
54
50
 
55
51
  def convert_response(response)
@@ -5,10 +5,10 @@ module RESTinPeace
5
5
 
6
6
  class IncompleteParams < RESTinPeace::DefaultError; end
7
7
 
8
- def initialize(url_template, params, attributes)
8
+ def initialize(url_template, params, klass)
9
9
  @url_template = url_template
10
10
  @params = params.dup
11
- @attributes = attributes
11
+ @klass = klass
12
12
  @url = nil
13
13
  end
14
14
 
@@ -17,7 +17,7 @@ module RESTinPeace
17
17
  @url = @url_template.dup
18
18
  tokens.each do |token|
19
19
  param = @params.delete(token.to_sym)
20
- param ||= @attributes[token.to_sym]
20
+ param ||= @klass.send(token) if @klass.respond_to?(token)
21
21
  raise IncompleteParams, "Unknown parameter for token :#{token} found" unless param
22
22
  @url.sub!(%r{:#{token}}, param.to_s)
23
23
  end
@@ -1,90 +1,146 @@
1
1
  require 'rest_in_peace/template_sanitizer'
2
+ require 'ostruct'
2
3
 
3
4
  describe RESTinPeace::TemplateSanitizer do
4
- let(:template_sanitizer) { RESTinPeace::TemplateSanitizer.new(url_template, params, attributes) }
5
5
  let(:attributes) { {} }
6
+ let(:template_sanitizer) { RESTinPeace::TemplateSanitizer.new(url_template, params, klass) }
6
7
 
7
- describe '#url' do
8
- subject { template_sanitizer.url }
9
8
 
10
- context 'single token' do
11
- let(:params) { { id: 1 } }
12
- let(:url_template) { '/a/:id' }
13
- specify { expect(subject).to eq('/a/1') }
14
- end
9
+ context 'with class' do
10
+ let(:klass) { OpenStruct }
15
11
 
16
- context 'multiple token' do
17
- let(:params) { { id: 2, a_id: 1 } }
18
- let(:url_template) { '/a/:a_id/b/:id' }
19
- specify { expect(subject).to eq('/a/1/b/2') }
20
- end
12
+ describe '#url' do
13
+ subject { template_sanitizer.url }
21
14
 
22
- context 'tokens with substrings' do
23
- let(:params) { { element: 'asd', element_id: 1 } }
24
- let(:url_template) { '/a/:element/b/:element_id' }
25
- specify { expect(subject).to eq('/a/asd/b/1') }
26
- end
15
+ context 'single token' do
16
+ let(:params) { { id: 1 } }
17
+ let(:url_template) { '/a/:id' }
18
+ specify { expect(subject).to eq('/a/1') }
19
+ end
20
+
21
+ context 'multiple token' do
22
+ let(:params) { { id: 2, a_id: 1 } }
23
+ let(:url_template) { '/a/:a_id/b/:id' }
24
+ specify { expect(subject).to eq('/a/1/b/2') }
25
+ end
27
26
 
28
- context 'tokens with substrings, reverse order' do
29
- let(:params) { { element: 'asd', element_id: 1 } }
30
- let(:url_template) { '/a/:element_id/b/:element' }
31
- specify { expect(subject).to eq('/a/1/b/asd') }
27
+ context 'incomplete params' do
28
+ let(:params) { { id: 1 } }
29
+ let(:url_template) { '/a/:a_id/b/:id' }
30
+ specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
31
+ end
32
32
  end
33
33
 
34
- context 'incomplete params' do
34
+ describe '#tokens' do
35
35
  let(:params) { {} }
36
- let(:url_template) { '/a/:id' }
37
- specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
38
- end
39
36
 
40
- context 'immutability of the url template' do
41
- let(:params) { { id: 1 } }
42
- let(:url_template) { '/a/:id' }
43
- specify { expect { subject }.to_not change { url_template } }
37
+ context 'single token' do
38
+ let(:url_template) { '/a/:id' }
39
+ subject { template_sanitizer.tokens }
40
+ specify { expect(subject).to eq(%w(id)) }
41
+ end
42
+
43
+ context 'multiple tokens' do
44
+ let(:url_template) { '/a/:a_id/b/:id' }
45
+ subject { template_sanitizer.tokens }
46
+ specify { expect(subject).to eq(%w(a_id id)) }
47
+ end
44
48
  end
45
49
 
46
- context 'immutability of the params' do
47
- let(:params) { { id: 1 } }
50
+ describe '#leftover_params' do
51
+ let(:params) { { id: 1, name: 'test' } }
48
52
  let(:url_template) { '/a/:id' }
49
- specify { expect { subject }.to_not change { params } }
50
- end
53
+ subject { template_sanitizer.leftover_params }
51
54
 
52
- context 'tokens from attributes' do
53
- let(:params) { { id: 1 } }
54
- let(:attributes) { { a_id: 2 } }
55
- let(:url_template) { '/a/:a_id/b/:id' }
56
- specify { expect(subject).to eq('/a/2/b/1') }
57
- end
58
-
59
- context 'incomplete params and attributes' do
60
- let(:params) { { id: 1 } }
61
- let(:attributes) { {} }
62
- let(:url_template) { '/a/:a_id/b/:id' }
63
- specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
55
+ specify { expect(subject).to eq({name: 'test'}) }
64
56
  end
65
57
  end
66
58
 
67
- describe '#tokens' do
68
- let(:params) { {} }
69
-
70
- context 'single token' do
71
- let(:url_template) { '/a/:id' }
72
- subject { template_sanitizer.tokens }
73
- specify { expect(subject).to eq(%w(id)) }
59
+ context 'with object' do
60
+ let(:klass) { OpenStruct.new(attributes) }
61
+
62
+ describe '#url' do
63
+ subject { template_sanitizer.url }
64
+
65
+ context 'single token' do
66
+ let(:params) { { id: 1 } }
67
+ let(:url_template) { '/a/:id' }
68
+ specify { expect(subject).to eq('/a/1') }
69
+ end
70
+
71
+ context 'multiple token' do
72
+ let(:params) { { id: 2, a_id: 1 } }
73
+ let(:url_template) { '/a/:a_id/b/:id' }
74
+ specify { expect(subject).to eq('/a/1/b/2') }
75
+ end
76
+
77
+ context 'tokens with substrings' do
78
+ let(:params) { { element: 'asd', element_id: 1 } }
79
+ let(:url_template) { '/a/:element/b/:element_id' }
80
+ specify { expect(subject).to eq('/a/asd/b/1') }
81
+ end
82
+
83
+ context 'tokens with substrings, reverse order' do
84
+ let(:params) { { element: 'asd', element_id: 1 } }
85
+ let(:url_template) { '/a/:element_id/b/:element' }
86
+ specify { expect(subject).to eq('/a/1/b/asd') }
87
+ end
88
+
89
+ context 'incomplete params' do
90
+ let(:params) { {} }
91
+ let(:url_template) { '/a/:id' }
92
+ specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
93
+ end
94
+
95
+ context 'immutability of the url template' do
96
+ let(:params) { { id: 1 } }
97
+ let(:url_template) { '/a/:id' }
98
+ specify { expect { subject }.to_not change { url_template } }
99
+ end
100
+
101
+ context 'immutability of the params' do
102
+ let(:params) { { id: 1 } }
103
+ let(:url_template) { '/a/:id' }
104
+ specify { expect { subject }.to_not change { params } }
105
+ end
106
+
107
+ context 'tokens from attributes' do
108
+ let(:params) { { id: 1 } }
109
+ let(:attributes) { { a_id: 2 } }
110
+ let(:url_template) { '/a/:a_id/b/:id' }
111
+ specify { expect(subject).to eq('/a/2/b/1') }
112
+ end
113
+
114
+ context 'incomplete params and attributes' do
115
+ let(:params) { { id: 1 } }
116
+ let(:attributes) { {} }
117
+ let(:url_template) { '/a/:a_id/b/:id' }
118
+ specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
119
+ end
74
120
  end
75
121
 
76
- context 'multiple tokens' do
77
- let(:url_template) { '/a/:a_id/b/:id' }
78
- subject { template_sanitizer.tokens }
79
- specify { expect(subject).to eq(%w(a_id id)) }
122
+ describe '#tokens' do
123
+ let(:params) { {} }
124
+
125
+ context 'single token' do
126
+ let(:url_template) { '/a/:id' }
127
+ subject { template_sanitizer.tokens }
128
+ specify { expect(subject).to eq(%w(id)) }
129
+ end
130
+
131
+ context 'multiple tokens' do
132
+ let(:url_template) { '/a/:a_id/b/:id' }
133
+ subject { template_sanitizer.tokens }
134
+ specify { expect(subject).to eq(%w(a_id id)) }
135
+ end
80
136
  end
81
- end
82
137
 
83
- describe '#leftover_params' do
84
- let(:params) { { id: 1, name: 'test' } }
85
- let(:url_template) { '/a/:id' }
86
- subject { template_sanitizer.leftover_params }
138
+ describe '#leftover_params' do
139
+ let(:params) { { id: 1, name: 'test' } }
140
+ let(:url_template) { '/a/:id' }
141
+ subject { template_sanitizer.leftover_params }
87
142
 
88
- specify { expect(subject).to eq({name: 'test'}) }
143
+ specify { expect(subject).to eq({name: 'test'}) }
144
+ end
89
145
  end
90
146
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-in-peace
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-05 00:00:00.000000000 Z
12
+ date: 2015-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel