rest-in-peace 4.1.0 → 4.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rest_in_peace/api_call.rb +1 -5
- data/lib/rest_in_peace/template_sanitizer.rb +3 -3
- data/spec/rest_in_peace/template_sanitizer_spec.rb +119 -63
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
4.1.
|
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,
|
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,
|
8
|
+
def initialize(url_template, params, klass)
|
9
9
|
@url_template = url_template
|
10
10
|
@params = params.dup
|
11
|
-
@
|
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 ||= @
|
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
|
-
|
11
|
-
|
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
|
-
|
17
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
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
|
-
|
50
|
-
end
|
53
|
+
subject { template_sanitizer.leftover_params }
|
51
54
|
|
52
|
-
|
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
|
-
|
68
|
-
let(:
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
77
|
-
let(:
|
78
|
-
|
79
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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
|
-
|
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.
|
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-
|
12
|
+
date: 2015-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|