rest-in-peace 1.4.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +75 -2
- data/VERSION +1 -1
- data/lib/rest_in_peace/active_model_api.rb +72 -0
- data/lib/rest_in_peace/definition_proxy/attributes_definitions.rb +20 -0
- data/lib/rest_in_peace/definition_proxy/collection_method_definitions.rb +1 -0
- data/lib/rest_in_peace/definition_proxy/resource_method_definitions.rb +7 -8
- data/lib/rest_in_peace/definition_proxy.rb +21 -0
- data/lib/rest_in_peace/faraday/raise_errors_middleware.rb +34 -0
- data/lib/rest_in_peace/faraday/ssl_config_creator.rb +66 -0
- data/lib/rest_in_peace/response_converter.rb +28 -12
- data/lib/rest_in_peace/template_sanitizer.rb +1 -1
- data/lib/rest_in_peace.rb +45 -9
- data/rest-in-peace.gemspec +5 -3
- data/spec/rest_in_peace/active_model_api_spec.rb +201 -0
- data/spec/rest_in_peace/definition_proxy/attributes_definitions_spec.rb +36 -0
- data/spec/rest_in_peace/definition_proxy/collection_method_definitions_spec.rb +7 -2
- data/spec/rest_in_peace/definition_proxy/resource_method_definitions_spec.rb +69 -13
- data/spec/rest_in_peace/definition_proxy_spec.rb +34 -1
- data/spec/rest_in_peace/response_converter_spec.rb +22 -5
- data/spec/rest_in_peace/template_sanitizer_spec.rb +12 -0
- data/spec/rest_in_peace_spec.rb +140 -7
- data/spec/spec_helper.rb +4 -1
- metadata +51 -18
- data/lib/rest_in_peace/ssl_config_creator.rb +0 -59
data/spec/rest_in_peace_spec.rb
CHANGED
@@ -2,13 +2,46 @@ require 'rest_in_peace'
|
|
2
2
|
|
3
3
|
describe RESTinPeace do
|
4
4
|
|
5
|
-
let(:struct) { Struct.new(:name) }
|
6
5
|
let(:extended_class) do
|
7
|
-
Class.new
|
6
|
+
Class.new do
|
8
7
|
include RESTinPeace
|
8
|
+
|
9
|
+
rest_in_peace do
|
10
|
+
attributes do
|
11
|
+
read :id, :name, :relation
|
12
|
+
write :my_array, :my_hash, :array_with_hash, :overridden_attribute
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def overridden_attribute
|
17
|
+
'something else'
|
18
|
+
end
|
19
|
+
|
20
|
+
def relation=(v)
|
21
|
+
@relation = v
|
22
|
+
end
|
23
|
+
|
24
|
+
def self_defined_method
|
25
|
+
puts 'yolo'
|
26
|
+
end
|
9
27
|
end
|
10
28
|
end
|
11
|
-
let(:
|
29
|
+
let(:my_array) { %w(element) }
|
30
|
+
let(:name) { 'test' }
|
31
|
+
let(:my_hash) { { element1: 'yolo' } }
|
32
|
+
let(:array_with_hash) { [my_hash.dup] }
|
33
|
+
let(:overridden_attribute) { 'initial value' }
|
34
|
+
let(:attributes) do
|
35
|
+
{
|
36
|
+
id: 1,
|
37
|
+
name: name,
|
38
|
+
relation: { id: 1234 },
|
39
|
+
my_array: my_array,
|
40
|
+
my_hash: my_hash,
|
41
|
+
array_with_hash: array_with_hash,
|
42
|
+
overridden_attribute: overridden_attribute,
|
43
|
+
}
|
44
|
+
end
|
12
45
|
let(:instance) { extended_class.new(attributes) }
|
13
46
|
|
14
47
|
describe '::api' do
|
@@ -37,19 +70,109 @@ describe RESTinPeace do
|
|
37
70
|
specify { expect(extended_class.rip_registry).to eq(collection: [], resource: []) }
|
38
71
|
end
|
39
72
|
|
73
|
+
describe '::rip_attributes' do
|
74
|
+
subject { extended_class }
|
75
|
+
specify { expect(extended_class).to respond_to(:rip_attributes) }
|
76
|
+
specify do
|
77
|
+
expect(extended_class.rip_attributes).to eq(
|
78
|
+
read: [:id, :name, :relation, :my_array, :my_hash, :array_with_hash, :overridden_attribute],
|
79
|
+
write: [:my_array, :my_hash, :array_with_hash, :overridden_attribute])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '::rip_namespace' do
|
84
|
+
subject { extended_class }
|
85
|
+
specify { expect(subject).to respond_to(:rip_namespace) }
|
86
|
+
specify { expect(subject).to respond_to(:rip_namespace=) }
|
87
|
+
it 'allows setting the namespace' do
|
88
|
+
expect { subject.rip_namespace = :blubb }.
|
89
|
+
to change { subject.rip_namespace }.from(nil).to(:blubb)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
40
93
|
describe '#api' do
|
41
94
|
subject { instance }
|
42
95
|
specify { expect(subject).to respond_to(:api).with(0).arguments }
|
43
96
|
end
|
44
97
|
|
45
98
|
describe '#to_h' do
|
99
|
+
subject { instance.to_h }
|
100
|
+
specify { expect(subject).to eq(attributes.merge(overridden_attribute: 'something else')) }
|
101
|
+
end
|
102
|
+
|
103
|
+
describe '#hash_for_updates' do
|
46
104
|
subject { instance }
|
47
|
-
specify { expect(subject).to respond_to(:
|
105
|
+
specify { expect(subject).to respond_to(:hash_for_updates).with(0).arguments }
|
106
|
+
|
107
|
+
context 'without a namspace defined' do
|
108
|
+
it 'adds id by default' do
|
109
|
+
expect(subject.hash_for_updates).to include(id: 1)
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'overridden getter' do
|
113
|
+
specify { expect(subject.hash_for_updates).to include(overridden_attribute: 'something else') }
|
114
|
+
end
|
115
|
+
|
116
|
+
context 'self defined methods' do
|
117
|
+
specify { expect(subject).to respond_to(:self_defined_method) }
|
118
|
+
specify { expect(subject.hash_for_updates).to_not include(:self_defined_method) }
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'hash' do
|
122
|
+
specify { expect(subject.hash_for_updates[:my_hash]).to eq(element1: 'yolo') }
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'with objects assigned' do
|
126
|
+
let(:my_hash) { double('OtherClass') }
|
127
|
+
it 'deeply calls hash_for_updates' do
|
128
|
+
expect(my_hash).to receive(:hash_for_updates).and_return({})
|
129
|
+
subject.hash_for_updates
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with a namspace defined' do
|
135
|
+
let(:extended_class) do
|
136
|
+
Class.new do
|
137
|
+
include RESTinPeace
|
138
|
+
|
139
|
+
rest_in_peace do
|
140
|
+
attributes do
|
141
|
+
read :id
|
142
|
+
write :name
|
143
|
+
end
|
144
|
+
|
145
|
+
namespace_attributes_with :blubb
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
specify { expect(subject.hash_for_updates).to eq(id: 1, blubb: { id: 1, name: 'test' }) }
|
151
|
+
end
|
48
152
|
end
|
49
153
|
|
50
154
|
describe '#initialize' do
|
51
155
|
subject { instance }
|
52
|
-
|
156
|
+
|
157
|
+
context 'read only attribute' do
|
158
|
+
specify { expect { subject }.to_not raise_error }
|
159
|
+
specify { expect(subject.name).to eq('test') }
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'write attribute' do
|
163
|
+
context 'via rip defined attribute' do
|
164
|
+
it 'uses the setter' do
|
165
|
+
expect_any_instance_of(extended_class).to receive(:my_array=)
|
166
|
+
subject
|
167
|
+
end
|
168
|
+
end
|
169
|
+
context 'self defined attribute' do
|
170
|
+
it 'uses the setter' do
|
171
|
+
expect_any_instance_of(extended_class).to receive(:relation=)
|
172
|
+
subject
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
53
176
|
|
54
177
|
context 'unknown params' do
|
55
178
|
let(:attributes) { { name: 'test42', email: 'yolo@example.org' } }
|
@@ -61,14 +184,24 @@ describe RESTinPeace do
|
|
61
184
|
let(:attributes) { {} }
|
62
185
|
specify { expect(subject.name).to eq(nil) }
|
63
186
|
end
|
187
|
+
|
188
|
+
context 'read only param' do
|
189
|
+
let(:attributes) { { id: 123 } }
|
190
|
+
specify { expect(subject.id).to eq(123) }
|
191
|
+
end
|
64
192
|
end
|
65
193
|
|
66
194
|
describe '#update_attributes' do
|
67
|
-
let(:new_attributes) { { name: 'yoloswag' } }
|
195
|
+
let(:new_attributes) { { name: 'yoloswag', my_array: ['yoloswag'] } }
|
68
196
|
subject { instance }
|
69
197
|
specify do
|
70
198
|
expect { subject.update_attributes(new_attributes) }.
|
71
|
-
to change(instance, :
|
199
|
+
to change(instance, :my_array).from(attributes[:my_array]).to(new_attributes[:my_array])
|
200
|
+
end
|
201
|
+
|
202
|
+
specify do
|
203
|
+
expect { subject.update_attributes(new_attributes) }.
|
204
|
+
to_not change(instance, :name).from(attributes[:name])
|
72
205
|
end
|
73
206
|
end
|
74
207
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'simplecov'
|
2
2
|
SimpleCov.start
|
3
3
|
|
4
|
+
require 'i18n'
|
5
|
+
I18n.enforce_available_locales = true
|
6
|
+
|
4
7
|
RSpec.configure do |config|
|
5
8
|
# These two settings work together to allow you to limit a spec run
|
6
9
|
# to individual examples or groups you care about by tagging them with
|
@@ -22,7 +25,7 @@ RSpec.configure do |config|
|
|
22
25
|
# Print the 10 slowest examples and example groups at the
|
23
26
|
# end of the spec run, to help surface which specs are running
|
24
27
|
# particularly slow.
|
25
|
-
config.profile_examples = 10
|
28
|
+
# config.profile_examples = 10
|
26
29
|
|
27
30
|
# Run specs in random order to surface order dependencies. If you find an
|
28
31
|
# order dependency and want to debug it, you can fix the order by providing
|
metadata
CHANGED
@@ -1,20 +1,38 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest-in-peace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Raffael Schmid
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>'
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - <=
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ! '>'
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - <=
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.1'
|
14
33
|
- !ruby/object:Gem::Dependency
|
15
34
|
name: rake
|
16
35
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
36
|
requirements:
|
19
37
|
- - ~>
|
20
38
|
- !ruby/object:Gem::Version
|
@@ -22,7 +40,6 @@ dependencies:
|
|
22
40
|
type: :development
|
23
41
|
prerelease: false
|
24
42
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
43
|
requirements:
|
27
44
|
- - ~>
|
28
45
|
- !ruby/object:Gem::Version
|
@@ -30,7 +47,6 @@ dependencies:
|
|
30
47
|
- !ruby/object:Gem::Dependency
|
31
48
|
name: rspec
|
32
49
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
50
|
requirements:
|
35
51
|
- - ~>
|
36
52
|
- !ruby/object:Gem::Version
|
@@ -38,7 +54,6 @@ dependencies:
|
|
38
54
|
type: :development
|
39
55
|
prerelease: false
|
40
56
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
57
|
requirements:
|
43
58
|
- - ~>
|
44
59
|
- !ruby/object:Gem::Version
|
@@ -46,49 +61,61 @@ dependencies:
|
|
46
61
|
- !ruby/object:Gem::Dependency
|
47
62
|
name: guard
|
48
63
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
64
|
requirements:
|
51
65
|
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '2.6'
|
68
|
+
- - ! '>='
|
52
69
|
- !ruby/object:Gem::Version
|
53
70
|
version: 2.6.1
|
54
71
|
type: :development
|
55
72
|
prerelease: false
|
56
73
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
74
|
requirements:
|
59
75
|
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.6'
|
78
|
+
- - ! '>='
|
60
79
|
- !ruby/object:Gem::Version
|
61
80
|
version: 2.6.1
|
62
81
|
- !ruby/object:Gem::Dependency
|
63
82
|
name: guard-rspec
|
64
83
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
84
|
requirements:
|
67
85
|
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '4.2'
|
88
|
+
- - ! '>='
|
68
89
|
- !ruby/object:Gem::Version
|
69
90
|
version: 4.2.0
|
70
91
|
type: :development
|
71
92
|
prerelease: false
|
72
93
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
94
|
requirements:
|
75
95
|
- - ~>
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '4.2'
|
98
|
+
- - ! '>='
|
76
99
|
- !ruby/object:Gem::Version
|
77
100
|
version: 4.2.0
|
78
101
|
- !ruby/object:Gem::Dependency
|
79
102
|
name: simplecov
|
80
103
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
104
|
requirements:
|
83
105
|
- - ~>
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0.8'
|
108
|
+
- - ! '>='
|
84
109
|
- !ruby/object:Gem::Version
|
85
110
|
version: 0.8.2
|
86
111
|
type: :development
|
87
112
|
prerelease: false
|
88
113
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
114
|
requirements:
|
91
115
|
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.8'
|
118
|
+
- - ! '>='
|
92
119
|
- !ruby/object:Gem::Version
|
93
120
|
version: 0.8.2
|
94
121
|
description: Let your api REST in peace.
|
@@ -113,16 +140,21 @@ files:
|
|
113
140
|
- images/rest_in_peace.gif
|
114
141
|
- lib/rest-in-peace.rb
|
115
142
|
- lib/rest_in_peace.rb
|
143
|
+
- lib/rest_in_peace/active_model_api.rb
|
116
144
|
- lib/rest_in_peace/api_call.rb
|
117
145
|
- lib/rest_in_peace/definition_proxy.rb
|
146
|
+
- lib/rest_in_peace/definition_proxy/attributes_definitions.rb
|
118
147
|
- lib/rest_in_peace/definition_proxy/collection_method_definitions.rb
|
119
148
|
- lib/rest_in_peace/definition_proxy/resource_method_definitions.rb
|
120
149
|
- lib/rest_in_peace/errors.rb
|
150
|
+
- lib/rest_in_peace/faraday/raise_errors_middleware.rb
|
151
|
+
- lib/rest_in_peace/faraday/ssl_config_creator.rb
|
121
152
|
- lib/rest_in_peace/response_converter.rb
|
122
|
-
- lib/rest_in_peace/ssl_config_creator.rb
|
123
153
|
- lib/rest_in_peace/template_sanitizer.rb
|
124
154
|
- rest-in-peace.gemspec
|
155
|
+
- spec/rest_in_peace/active_model_api_spec.rb
|
125
156
|
- spec/rest_in_peace/api_call_spec.rb
|
157
|
+
- spec/rest_in_peace/definition_proxy/attributes_definitions_spec.rb
|
126
158
|
- spec/rest_in_peace/definition_proxy/collection_method_definitions_spec.rb
|
127
159
|
- spec/rest_in_peace/definition_proxy/resource_method_definitions_spec.rb
|
128
160
|
- spec/rest_in_peace/definition_proxy_spec.rb
|
@@ -133,30 +165,31 @@ files:
|
|
133
165
|
homepage: http://github.com/ninech/
|
134
166
|
licenses:
|
135
167
|
- MIT
|
168
|
+
metadata: {}
|
136
169
|
post_install_message:
|
137
170
|
rdoc_options: []
|
138
171
|
require_paths:
|
139
172
|
- lib
|
140
173
|
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
-
none: false
|
142
174
|
requirements:
|
143
175
|
- - ! '>='
|
144
176
|
- !ruby/object:Gem::Version
|
145
177
|
version: '0'
|
146
178
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
-
none: false
|
148
179
|
requirements:
|
149
180
|
- - ! '>='
|
150
181
|
- !ruby/object:Gem::Version
|
151
182
|
version: '0'
|
152
183
|
requirements: []
|
153
184
|
rubyforge_project:
|
154
|
-
rubygems_version:
|
185
|
+
rubygems_version: 2.4.1
|
155
186
|
signing_key:
|
156
|
-
specification_version:
|
187
|
+
specification_version: 4
|
157
188
|
summary: REST in peace
|
158
189
|
test_files:
|
190
|
+
- spec/rest_in_peace/active_model_api_spec.rb
|
159
191
|
- spec/rest_in_peace/api_call_spec.rb
|
192
|
+
- spec/rest_in_peace/definition_proxy/attributes_definitions_spec.rb
|
160
193
|
- spec/rest_in_peace/definition_proxy/collection_method_definitions_spec.rb
|
161
194
|
- spec/rest_in_peace/definition_proxy/resource_method_definitions_spec.rb
|
162
195
|
- spec/rest_in_peace/definition_proxy_spec.rb
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'openssl'
|
2
|
-
|
3
|
-
module RESTinPeace
|
4
|
-
class SSLConfigCreator
|
5
|
-
class MissingParam < Exception; end
|
6
|
-
|
7
|
-
def initialize(config, verify = :peer)
|
8
|
-
@config = config
|
9
|
-
@verify = verify
|
10
|
-
|
11
|
-
raise MissingParam, 'Specify :ca_cert in ssl options' unless @config[:ca_cert]
|
12
|
-
raise MissingParam, 'Specify :client_key in ssl options' unless @config[:client_key]
|
13
|
-
raise MissingParam, 'Specify :client_cert in ssl options' unless @config[:client_cert]
|
14
|
-
end
|
15
|
-
|
16
|
-
def faraday_options
|
17
|
-
{client_cert: client_cert, client_key: client_key, ca_file: ca_cert_path, verify_mode: verify_mode}
|
18
|
-
end
|
19
|
-
|
20
|
-
def client_cert
|
21
|
-
OpenSSL::X509::Certificate.new(open_file(client_cert_path))
|
22
|
-
end
|
23
|
-
|
24
|
-
def client_cert_path
|
25
|
-
path(@config[:client_cert])
|
26
|
-
end
|
27
|
-
|
28
|
-
def client_key
|
29
|
-
OpenSSL::PKey::RSA.new(open_file(client_key_path))
|
30
|
-
end
|
31
|
-
|
32
|
-
def client_key_path
|
33
|
-
path(@config[:client_key])
|
34
|
-
end
|
35
|
-
|
36
|
-
def ca_cert_path
|
37
|
-
path(@config[:ca_cert])
|
38
|
-
end
|
39
|
-
|
40
|
-
def verify_mode
|
41
|
-
case @verify
|
42
|
-
when :peer
|
43
|
-
OpenSSL::SSL::VERIFY_PEER
|
44
|
-
else
|
45
|
-
raise "Unknown verify variant '#{@verify}'"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def open_file(file)
|
52
|
-
File.open(file)
|
53
|
-
end
|
54
|
-
|
55
|
-
def path(file)
|
56
|
-
File.join(file)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|