smooth_operator 1.10.9 → 1.10.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTYxNDdmNGJiNTIzZWU5YTc2MzlmYTkzYTJjOTZjOTcyZWU5YjMwMg==
4
+ MjkwOGZiOWM3NGYzODQ5MzY0ZGI4OTEwZWNhNWI5MzgyZmE4Y2E4Zg==
5
5
  data.tar.gz: !binary |-
6
- YjlhYzNiNWUwNDdiYjUyOTMyNDY2YjhmYmM3YmE0YzNlM2I2MTBjZQ==
6
+ NzFiOWZiZWY4N2QwZWFmNzU5ODRhYTcyMjk0NGMwNjg0ODgzNDgxOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTQwNzAxYTExZDA0ZjE3YWMwNDJhMDljZjRkMWU3OTFlNzgzNWU4ODliN2Ez
10
- MzI2NjUyZDMyYjdlYjJmYThkOGUzN2NlYjkxMTc1ODNiMGUyNmJiNzdkMjdl
11
- OTVhYjAwNDlhZTliNDA4ODE1ODM1NWY5NzIwYTExN2MyOWZjNTQ=
9
+ YTNiMjUwODdkN2NhMDljMjUxNGZkZTlkMzExMzM1ZTg5NzUyN2ExMzZlYTBk
10
+ MzE5OGIyZGYyMWZmNWYwOWY4OTU5YjBiNjNjMTE4MzdmMmE1M2Y2MWI3ZTFh
11
+ Y2I1YmY2YTUxMTZmMmNlOTRlMGU1N2FlNzVjNWVmMmEzYjdiODQ=
12
12
  data.tar.gz: !binary |-
13
- YTc1ODM3MmZlNmFjOGM0ZjYzNjc3ZTA0MmE1NGRlNzkyOWQzM2NiNmM5MTc2
14
- YjE0ZDg5YjcxNWY1ODk4ODE5ZjNkZWMyOWVmNWZlNzE5MTRiMTdmOTY1MmYz
15
- MTVlMDBhZGQyNjg1ZDNjMGJjM2I1YmE4MmY5OWFkZGNiNDg2Zjk=
13
+ MmExYzVkNDQ0YmEyZGJjZGMxYzljOWMwMjJmMjBiMGI3MDE1ZDU5ZjVlOTI0
14
+ ZDQ4ODA5NWIwZjcxYTY4M2Y0OGI2MzU2OTk5OWUxYzI4NzQ4MDI1YTY4ODNm
15
+ N2IzMmMzMTU2ODgwNmRiOGYwMjNkNTI3MDlmNDZiY2VkYWM0ZDA=
@@ -13,7 +13,7 @@ module SmoothOperator
13
13
 
14
14
  @connection, @operator_options, @options = strip_options(options)
15
15
 
16
- @relative_path = build_relative_path(relative_path, options)
16
+ @relative_path = resource_path(relative_path, options)
17
17
 
18
18
  @endpoint_user = options[:endpoint_user] || @operator_class.endpoint_user
19
19
 
@@ -39,7 +39,7 @@ module SmoothOperator
39
39
  [connection, operator_options, options]
40
40
  end
41
41
 
42
- def build_relative_path(relative_path, options)
42
+ def resource_path(relative_path, options)
43
43
  table_name = options[:table_name] || @operator_class.table_name
44
44
 
45
45
  if Helpers.present?(table_name)
@@ -32,7 +32,7 @@ module SmoothOperator
32
32
  def reload(relative_path = nil, data = {}, options = {})
33
33
  raise 'UnknownPath' if !respond_to?(:id) || Helpers.blank?(id)
34
34
 
35
- relative_path, options = build_relative_path(relative_path, options)
35
+ relative_path, options = resource_path(relative_path, options)
36
36
 
37
37
  success = {}
38
38
 
@@ -70,7 +70,7 @@ module SmoothOperator
70
70
  def destroy(relative_path = nil, data = {}, options = {})
71
71
  return false unless persisted?
72
72
 
73
- relative_path, options = build_relative_path(relative_path, options)
73
+ relative_path, options = resource_path(relative_path, options)
74
74
 
75
75
  success = {}
76
76
 
@@ -103,7 +103,7 @@ module SmoothOperator
103
103
  end
104
104
 
105
105
  def update(relative_path, data, options)
106
- relative_path, options = build_relative_path(relative_path, options)
106
+ relative_path, options = resource_path(relative_path, options)
107
107
 
108
108
  success = {}
109
109
 
@@ -117,24 +117,20 @@ module SmoothOperator
117
117
 
118
118
  private ##################### PRIVATE ####################
119
119
 
120
- def build_relative_path(relative_path, options)
120
+ def resource_path(relative_path, options)
121
121
  if Helpers.blank?(relative_path)
122
- if parent_object.nil?
122
+ if parent_object.nil? || options[:ignore_parent] == true
123
123
  relative_path = id.to_s
124
124
  else
125
125
  options ||= {}
126
126
  options[:table_name] = ''
127
- relative_path = "#{parent_object.send(:rest_relative_path)}/#{table_name}/#{id}"
127
+ relative_path = "#{parent_object.table_name}/#{parent_object.id}/#{table_name}/#{id}"
128
128
  end
129
129
  end
130
130
 
131
131
  [relative_path, options]
132
132
  end
133
133
 
134
- def rest_relative_path
135
- "#{table_name}/#{id}"
136
- end
137
-
138
134
  def make_remote_call(http_verb, relative_path, data, options)
139
135
  data ||= {}
140
136
  data.merge!(extra_params || {})
@@ -16,19 +16,43 @@ module SmoothOperator
16
16
 
17
17
 
18
18
  def success?
19
- http_status.between?(200, 299)
19
+ http_status.between?(200, 299) || http_status == 304
20
20
  end
21
21
 
22
22
  alias :ok? :success?
23
23
 
24
+ def not_success?
25
+ !success?
26
+ end
27
+
28
+ alias :not_ok? :not_success?
29
+
24
30
  def failure?
25
31
  http_status.between?(400, 499)
26
32
  end
27
33
 
34
+ alias :failed? :failure?
35
+
28
36
  def error?
29
37
  http_status.between?(500, 599) || http_status == 0
30
38
  end
31
39
 
40
+ def not_found?
41
+ http_status == 404
42
+ end
43
+
44
+ def not_processed?
45
+ http_status == 422
46
+ end
47
+
48
+ def timeout?
49
+ false
50
+ end
51
+
52
+ def connection_failed?
53
+ false
54
+ end
55
+
32
56
  def parsed_response
33
57
  return nil if body.nil?
34
58
 
@@ -42,7 +66,11 @@ module SmoothOperator
42
66
  end
43
67
 
44
68
  def status
45
- error? ? nil : success?
69
+ return nil if error?
70
+
71
+ return true if success?
72
+
73
+ not_processed? ? false : nil
46
74
  end
47
75
 
48
76
  def objects
@@ -5,10 +5,14 @@ module SmoothOperator
5
5
  class ConnectionFailed < Base
6
6
 
7
7
  def initialize(response)
8
- @response = response
8
+ super
9
9
  @http_status = 0
10
10
  end
11
11
 
12
+ def connection_failed?
13
+ true
14
+ end
15
+
12
16
  end
13
17
 
14
18
  end
@@ -5,10 +5,14 @@ module SmoothOperator
5
5
  class Timeout < Base
6
6
 
7
7
  def initialize(response)
8
- @response = response
8
+ super
9
9
  @http_status = 0
10
10
  end
11
11
 
12
+ def timeout?
13
+ true
14
+ end
15
+
12
16
  end
13
17
 
14
18
  end
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "1.10.9"
2
+ VERSION = "1.10.11"
3
3
  end
@@ -123,16 +123,31 @@ describe SmoothOperator::Persistence, helpers: :persistence do
123
123
  end
124
124
 
125
125
  context "when calling #reload on a nested object" do
126
- before do
127
- subject.id = 1
128
- subject.reload
129
- subject.posts.first.reload
126
+
127
+ context "without the option 'ignore_parent: true'" do
128
+ before do
129
+ subject.id = 1
130
+ subject.reload
131
+ subject.posts.first.reload
132
+ end
133
+
134
+ it "it should fetch server data, with NESTED REST url" do
135
+ expect(subject.posts.first.attributes).to eq({ id: 1, body: 'from_nested_url' })
136
+ end
130
137
  end
131
138
 
132
- it "it should fetch server data, with the correct nested REST url" do
133
- # binding.pry
134
- expect(subject.posts.first.attributes).to eq({ id: 1, body: 'from_server' })
139
+ context "with the option 'ignore_parent: true'" do
140
+ before do
141
+ subject.id = 1
142
+ subject.reload
143
+ subject.posts.first.reload(nil, nil, { ignore_parent: true })
144
+ end
145
+
146
+ it "it should fetch server data, with REST url" do
147
+ expect(subject.posts.first.attributes).to eq({ id: 1, body: 'from_resource_url' })
148
+ end
135
149
  end
150
+
136
151
  end
137
152
  end
138
153
 
@@ -1,96 +1,290 @@
1
1
  require "spec_helper"
2
2
 
3
- describe SmoothOperator::RemoteCall do
4
- subject { User }
3
+ describe SmoothOperator::RemoteCall, current: true do
4
+ subject { UserWithAddressAndPosts::Son.new(attributes_for(:user_with_address_and_posts)) }
5
5
 
6
- describe "#success?" do
7
- context "when the server response has a http code in the 200 range" do
8
- it "should return true" do
9
- end
6
+ context "when the server response has a http code in the 200 range" do
7
+ before { subject.save(nil, { status: 200 }) }
8
+
9
+ it "#success? should return true" do
10
+ expect(subject.last_remote_call.success?).to be true
10
11
  end
11
12
 
12
- context "when the server response has a http code NOT in the 200 range" do
13
- it "should return false" do
14
- end
13
+ it "#failure? should return false" do
14
+ expect(subject.last_remote_call.failure?).to be false
15
15
  end
16
16
 
17
- context "when the server response connection fails" do
18
- it "should return false" do
19
- end
17
+ it "#error? should return false" do
18
+ expect(subject.last_remote_call.error?).to be false
19
+ end
20
+
21
+ it "#not_found? should return false" do
22
+ expect(subject.last_remote_call.not_found?).to be false
23
+ end
24
+
25
+ it "#not_processed? should return false" do
26
+ expect(subject.last_remote_call.not_processed?).to be false
27
+ end
28
+
29
+ it "#timeout? should return false" do
30
+ expect(subject.last_remote_call.timeout?).to be false
31
+ end
32
+
33
+ it "#connection_failed? should return false" do
34
+ expect(subject.last_remote_call.connection_failed?).to be false
35
+ end
36
+
37
+ it "#status should return true" do
38
+ expect(subject.last_remote_call.status).to be true
20
39
  end
21
40
  end
22
41
 
23
- describe "#failure?" do
24
- context "when the server response has a http code in the 400 range" do
25
- it "should return true" do
26
- end
42
+ context "when the server response has a http code in the 400 range" do
43
+ before { subject.save(nil, { status: 400 }) }
44
+
45
+ it "#success? should return false" do
46
+ expect(subject.last_remote_call.success?).to be false
27
47
  end
28
48
 
29
- context "when the server response has a http code NOT in the 400 range" do
30
- it "should return false" do
31
- end
49
+ it "#failure? should return true" do
50
+ expect(subject.last_remote_call.failure?).to be true
32
51
  end
33
52
 
34
- context "when the server response connection fails" do
35
- it "should return false" do
36
- end
53
+ it "#error? should return false" do
54
+ expect(subject.last_remote_call.error?).to be false
55
+ end
56
+
57
+ it "#not_found? should return false" do
58
+ expect(subject.last_remote_call.not_found?).to be false
59
+ end
60
+
61
+ it "#not_processed? should return false" do
62
+ expect(subject.last_remote_call.not_processed?).to be false
63
+ end
64
+
65
+ it "#timeout? should return false" do
66
+ expect(subject.last_remote_call.timeout?).to be false
67
+ end
68
+
69
+ it "#connection_failed? should return false" do
70
+ expect(subject.last_remote_call.connection_failed?).to be false
71
+ end
72
+
73
+ it "#status should return nil" do
74
+ expect(subject.last_remote_call.status).to be nil
37
75
  end
38
76
  end
39
77
 
40
- describe "#error?" do
41
- context "when the server response has a http code in the 500 range" do
42
- it "should return true" do
43
- end
78
+ context "when the server response has a http is 404" do
79
+ before { subject.save(nil, { status: 404 }) }
80
+
81
+ it "#success? should return false" do
82
+ expect(subject.last_remote_call.success?).to be false
44
83
  end
45
84
 
46
- context "when the server response has a http code NOT in the 500 range" do
47
- it "should return false" do
48
- end
85
+ it "#failure? should return true" do
86
+ expect(subject.last_remote_call.failure?).to be true
49
87
  end
50
88
 
51
- context "when the server response connection fails" do
52
- it "should return true" do
53
- end
89
+ it "#error? should return false" do
90
+ expect(subject.last_remote_call.error?).to be false
91
+ end
92
+
93
+ it "#not_found? should return true" do
94
+ expect(subject.last_remote_call.not_found?).to be true
95
+ end
96
+
97
+ it "#not_processed? should return false" do
98
+ expect(subject.last_remote_call.not_processed?).to be false
99
+ end
100
+
101
+ it "#timeout? should return false" do
102
+ expect(subject.last_remote_call.timeout?).to be false
103
+ end
104
+
105
+ it "#connection_failed? should return false" do
106
+ expect(subject.last_remote_call.connection_failed?).to be false
107
+ end
108
+
109
+ it "#status should return nil" do
110
+ expect(subject.last_remote_call.status).to be nil
54
111
  end
55
112
  end
56
113
 
57
- describe "#parsed_response" do
58
- context "when the server response's body contains valid json data" do
59
- it "should return the parsed result of that data" do
60
- end
114
+ context "when the server response has a http is 422" do
115
+ before { subject.save(nil, { status: 422 }) }
116
+
117
+ it "#success? should return false" do
118
+ expect(subject.last_remote_call.success?).to be false
61
119
  end
62
120
 
63
- context "when the server response's body does not contains valid json data" do
64
- it "should return nil" do
65
- end
121
+ it "#failure? should return true" do
122
+ expect(subject.last_remote_call.failure?).to be true
123
+ end
124
+
125
+ it "#error? should return false" do
126
+ expect(subject.last_remote_call.error?).to be false
127
+ end
128
+
129
+ it "#not_found? should return false" do
130
+ expect(subject.last_remote_call.not_found?).to be false
131
+ end
132
+
133
+ it "#not_processed? should return true" do
134
+ expect(subject.last_remote_call.not_processed?).to be true
135
+ end
136
+
137
+ it "#timeout? should return false" do
138
+ expect(subject.last_remote_call.timeout?).to be false
139
+ end
140
+
141
+ it "#connection_failed? should return false" do
142
+ expect(subject.last_remote_call.connection_failed?).to be false
143
+ end
144
+
145
+ it "#status should return false" do
146
+ expect(subject.last_remote_call.status).to be false
66
147
  end
67
148
  end
68
149
 
69
- describe "#status" do
70
- context "when the #error? returns true" do
71
- it "should return nil" do
72
- end
150
+ context "when the server response has a http code in the 500 range" do
151
+ before { subject.save(nil, { status: 500 }) }
152
+
153
+ it "#success? should return false" do
154
+ expect(subject.last_remote_call.success?).to be false
73
155
  end
74
156
 
75
- context "when the #error? returns false and #success? returns true" do
76
- it "should return true" do
77
- end
157
+ it "#failure? should return false" do
158
+ expect(subject.last_remote_call.failure?).to be false
159
+ end
160
+
161
+ it "#error? should return true" do
162
+ expect(subject.last_remote_call.error?).to be true
163
+ end
164
+
165
+ it "#not_found? should return false" do
166
+ expect(subject.last_remote_call.not_found?).to be false
167
+ end
168
+
169
+ it "#not_processed? should return false" do
170
+ expect(subject.last_remote_call.not_processed?).to be false
171
+ end
172
+
173
+ it "#timeout? should return false" do
174
+ expect(subject.last_remote_call.timeout?).to be false
175
+ end
176
+
177
+ it "#connection_failed? should return false" do
178
+ expect(subject.last_remote_call.connection_failed?).to be false
179
+ end
180
+
181
+ it "#status should return nil" do
182
+ expect(subject.last_remote_call.status).to be nil
183
+ end
184
+ end
185
+
186
+ context "when the connection is broken" do
187
+ subject { User::BrokenConnection.new }
188
+
189
+ before { subject.save }
190
+
191
+ it "#success? should return false" do
192
+ expect(subject.last_remote_call.success?).to be false
193
+ end
194
+
195
+ it "#failure? should return false" do
196
+ expect(subject.last_remote_call.failure?).to be false
197
+ end
198
+
199
+ it "#error? should return true" do
200
+ expect(subject.last_remote_call.error?).to be true
201
+ end
202
+
203
+ it "#not_found? should return false" do
204
+ expect(subject.last_remote_call.not_found?).to be false
205
+ end
206
+
207
+ it "#not_processed? should return false" do
208
+ expect(subject.last_remote_call.not_processed?).to be false
209
+ end
210
+
211
+ it "#timeout? should return false" do
212
+ expect(subject.last_remote_call.timeout?).to be false
213
+ end
214
+
215
+ it "#connection_failed? should return true" do
216
+ expect(subject.last_remote_call.connection_failed?).to be true
217
+ end
218
+
219
+ it "#status should return nil" do
220
+ expect(subject.last_remote_call.status).to be nil
221
+ end
222
+ end
223
+
224
+ context "when the connection exceeds the timeout" do
225
+ subject { User::TimeoutConnection.new }
226
+
227
+ before { subject.save('timeout') }
228
+
229
+ it "#success? should return false" do
230
+ expect(subject.last_remote_call.success?).to be false
231
+ end
232
+
233
+ it "#failure? should return false" do
234
+ expect(subject.last_remote_call.failure?).to be false
235
+ end
236
+
237
+ it "#error? should return true" do
238
+ expect(subject.last_remote_call.error?).to be true
239
+ end
240
+
241
+ it "#not_found? should return false" do
242
+ expect(subject.last_remote_call.not_found?).to be false
78
243
  end
79
244
 
80
- context "when the #error? returns false and #success? returns false" do
81
- it "should return false" do
245
+ it "#not_processed? should return false" do
246
+ expect(subject.last_remote_call.not_processed?).to be false
247
+ end
248
+
249
+ it "#timeout? should return true" do
250
+ expect(subject.last_remote_call.timeout?).to be true
251
+ end
252
+
253
+ it "#connection_failed? should return false" do
254
+ expect(subject.last_remote_call.connection_failed?).to be false
255
+ end
256
+
257
+ it "#status should return nil" do
258
+ expect(subject.last_remote_call.status).to be nil
259
+ end
260
+ end
261
+
262
+ describe "#parsed_response" do
263
+ context "when the server response's body does not contains valid json data" do
264
+ let(:remote_call) { User::Base.find('bad_json') }
265
+
266
+ it "should return nil" do
267
+ expect(remote_call.data).to be nil
82
268
  end
83
269
  end
84
270
  end
85
271
 
86
272
  describe "#http_status" do
87
273
  context "when a server connection is established" do
88
- it "should return the server's http response code" do
274
+ before { subject.save(nil, { status: 422 }) }
275
+
276
+ it "it should return the server's http response code" do
277
+ expect(subject.last_remote_call.http_status).to be 422
89
278
  end
90
279
  end
91
280
 
92
281
  context "when a server connection fails" do
282
+ subject { User::TimeoutConnection.new }
283
+
284
+ before { subject.save('timeout') }
285
+
93
286
  it "should return 0" do
287
+ expect(subject.last_remote_call.http_status).to be 0
94
288
  end
95
289
  end
96
290
  end
@@ -2,8 +2,6 @@ module User
2
2
 
3
3
  class Base < SmoothOperator::Base
4
4
 
5
- # self.timeout = 2
6
-
7
5
  self.model_name = 'user'
8
6
 
9
7
  self.endpoint_user = 'admin'
@@ -32,4 +30,13 @@ module User
32
30
 
33
31
  end
34
32
 
33
+ class BrokenConnection < SmoothOperator::Base
34
+ self.endpoint = 'http://localhost:1234/'
35
+ end
36
+
37
+ class TimeoutConnection < Base
38
+ self.timeout = 0
39
+ self.model_name = 'user'
40
+ end
41
+
35
42
  end
@@ -8,9 +8,13 @@ class TestServer < Sinatra::Base
8
8
  username == 'admin' and password == 'admin'
9
9
  end
10
10
 
11
-
11
+ get '/posts/:id' do
12
+ post_data = { id: 1, body: 'from_resource_url' }
13
+ json post_data
14
+ end
15
+
12
16
  get '/users/:id/posts/:id' do
13
- post_data = { id: 1, body: 'from_server' }
17
+ post_data = { id: 1, body: 'from_nested_url' }
14
18
  json post_data
15
19
  end
16
20
 
@@ -50,6 +54,10 @@ class TestServer < Sinatra::Base
50
54
  json data
51
55
  end
52
56
 
57
+ get '/users/bad_json' do
58
+ 'ok'
59
+ end
60
+
53
61
  get '/users/:id' do
54
62
  json FactoryGirl.attributes_for(:user_with_address_and_posts)
55
63
  end
@@ -69,6 +77,11 @@ class TestServer < Sinatra::Base
69
77
  post '/users' do
70
78
  common_response
71
79
  end
80
+
81
+ post '/users/timeout' do
82
+ sleep 1
83
+ json 'ok'
84
+ end
72
85
 
73
86
  put '/users/:id' do
74
87
  common_response
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smooth_operator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.9
4
+ version: 1.10.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-08 00:00:00.000000000 Z
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler