logical_model 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,12 @@ LogicalModel allows to use a restfull resources as models.
4
4
 
5
5
  It was written following this tutorial: http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3
6
6
 
7
+ == Usage
8
+
9
+ In your Gemfile:
10
+
11
+ gem "logical_model"
12
+
7
13
  == Contributing to logical_model
8
14
 
9
15
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -13,6 +19,7 @@ It was written following this tutorial: http://www.slideshare.net/ihower/service
13
19
  * Commit and push until you are happy with your contribution
14
20
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
15
21
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
22
+ * PivotalTracker: https://www.pivotaltracker.com/projects/339595
16
23
 
17
24
  == Copyright
18
25
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.1.9
@@ -1,3 +1,4 @@
1
+ require 'timeout'
1
2
  require 'active_model'
2
3
  require 'typhoeus'
3
4
  require 'active_support' # todo migrate to yajl
@@ -56,13 +57,22 @@ class LogicalModel
56
57
  @attribute_keys
57
58
  end
58
59
 
60
+ DEFAULT_TIMEOUT = 10000
61
+
59
62
  class << self
60
- attr_accessor :host, :hydra, :resource_path, :use_ssl, :use_api_key, :api_key, :api_key_name, :log_path
63
+ attr_accessor :host, :hydra, :resource_path, :api_key, :api_key_name, :timeout, :use_ssl, :log_path, :use_api_key
64
+
65
+ def timeout; @timeout ||= DEFAULT_TIMEOUT; end
66
+ def use_ssl; @use_ssl ||= false; end
67
+ def log_path; @log_path ||= "log/logical_model.log"; end
68
+ def use_api_key; @use_api_key ||= false; end
61
69
 
62
70
  # host eg: "127.0.0.1:3010"
63
71
  # resource_path eg: "/api/v1/people"
64
72
  end
65
73
 
74
+
75
+
66
76
  def self.resource_uri(id=nil)
67
77
  prefix = (use_ssl)? "https://" : "http://"
68
78
  sufix = (id.nil?)? "" : "/#{id}"
@@ -105,8 +115,11 @@ class LogicalModel
105
115
  end
106
116
 
107
117
  def self.log_failed(response)
108
- # error_message = ActiveSupport::JSON.decode(response.body)["message"]
109
- error_message = "error"
118
+ begin
119
+ error_message = ActiveSupport::JSON.decode(response.body)["message"]
120
+ rescue => e
121
+ error_message = "error"
122
+ end
110
123
  msg = "LogicalModel Log: #{response.code} #{response.request.url} in #{response.time}s FAILED: #{error_message}"
111
124
  self.logger.warn(msg)
112
125
  self.logger.debug("LogicalModel Log RESPONSE: #{response.body}")
@@ -120,8 +133,7 @@ class LogicalModel
120
133
  if defined?(Rails)
121
134
  Rails.logger
122
135
  else
123
- path = self.log_path.nil?? "log.log" : self.log_path
124
- Logger.new(path)
136
+ Logger.new(self.log_path)
125
137
  end
126
138
  end
127
139
 
@@ -184,8 +196,13 @@ class LogicalModel
184
196
  def self.paginate(options={})
185
197
  result = nil
186
198
  async_paginate(options){|i| result = i}
187
- self.hydra.run
199
+ Timeout::timeout(self.timeout/1000) do
200
+ self.hydra.run
201
+ end
188
202
  result
203
+ rescue Timeout::Error
204
+ self.logger.warn("timeout")
205
+ return nil
189
206
  end
190
207
 
191
208
  # Asynchronic Find
@@ -216,8 +233,13 @@ class LogicalModel
216
233
  def self.find(id)
217
234
  result = nil
218
235
  async_find(id){|i| result = i}
219
- self.hydra.run
236
+ Timeout::timeout(self.timeout/1000) do
237
+ self.hydra.run
238
+ end
220
239
  result
240
+ rescue Timeout::Error
241
+ self.logger.warn("timeout")
242
+ return nil
221
243
  end
222
244
 
223
245
  #
@@ -235,9 +257,12 @@ class LogicalModel
235
257
  return false unless valid?
236
258
 
237
259
  params = self.attributes
238
- params = self.merge_key(params)
260
+ params = self.class.merge_key(params)
239
261
 
240
- response = Typhoeus::Request.post( self.resource_uri, :params => params )
262
+ response = nil
263
+ Timeout::timeout(self.class.timeout/1000) do
264
+ response = Typhoeus::Request.post( self.class.resource_uri, :params => params, :timeout => self.class.timeout )
265
+ end
241
266
  if response.code == 201
242
267
  log_ok(response)
243
268
  self.id = ActiveSupport::JSON.decode(response.body)["id"]
@@ -245,9 +270,13 @@ class LogicalModel
245
270
  log_failed(response)
246
271
  return nil
247
272
  end
273
+ rescue Timeout::Error
274
+ self.class.logger.warn "timeout"
275
+ return nil
248
276
  end
249
277
 
250
- # Updates Objects attributes.
278
+ # Updates Objects attributes, this will only send attributes passed as arguments
279
+ #
251
280
  #
252
281
  # Returns false if Object#valid? is false.
253
282
  # Returns updated object if successfull.
@@ -256,14 +285,67 @@ class LogicalModel
256
285
  # Usage:
257
286
  # @person.update(params[:person])
258
287
  def update(attributes)
288
+ self.attributes = attributes
289
+
290
+ return false unless valid?
291
+
292
+ sending_params = attributes
293
+ sending_params.delete(:id)
294
+
295
+ params = { self.class.to_s.underscore => sending_params }
296
+ params = self.class.merge_key(params)
297
+
298
+
299
+ e = Typhoeus::Easy.new
300
+ e.url = self.class.resource_uri(id)
301
+ e.method = :put
302
+ e.params = params
303
+
304
+ response = nil
305
+ Timeout::timeout(self.class.timeout/1000) do
306
+ # using Typhoeus::Easy avoids PUT hang issue: https://github.com/dbalatero/typhoeus/issues/69
307
+ e.perform
308
+ end
259
309
 
310
+ if e.response_code == 200
311
+ self.class.logger.info("LogicalModel Log: #{e.response_code} #{e.url} in #{e.total_time_taken}s")
312
+ self.class.logger.debug("LogicalModel Log RESPONSE: #{e.response_body}")
313
+ return self
314
+ else
315
+ msg = "LogicalModel Log: #{e.response_code} #{e.url} in #{e.total_time_taken}s FAILED"
316
+ self.logger.warn(msg)
317
+ self.logger.debug("LogicalModel Log RESPONSE: #{e.response_body}")
318
+ return nil
319
+ end
320
+
321
+ rescue Timeout::Error
322
+ self.class.logger.warn("request timed out")
323
+ return nil
324
+ end
325
+
326
+ # Saves Objects attributes
327
+ #
328
+ #
329
+ # Returns false if Object#valid? is false.
330
+ # Returns updated object if successfull.
331
+ # Returns nil if update failed
332
+ #
333
+ # Usage:
334
+ # @person.save
335
+ def save
260
336
  self.attributes = attributes
261
337
 
262
338
  return false unless valid?
263
339
 
264
- params = { self.class.underscore => self.attributes }
265
- params = self.merge_key(params)
266
- response = Typhoeus::Request.put( self.resource_uri(id), :params => params )
340
+ sending_params = self.attributes
341
+ sending_params.delete(:id)
342
+
343
+ params = { self.class.to_s.underscore => sending_params }
344
+ params = self.class.merge_key(params)
345
+ response = nil
346
+ Timeout::timeout(self.class.timeout/1000) do
347
+ response = Typhoeus::Request.put( self.class.resource_uri(id), :params => params, :timeout => self.class.timeout )
348
+ end
267
349
  if response.code == 200
268
350
  log_ok(response)
269
351
  return self
@@ -271,6 +353,9 @@ class LogicalModel
271
353
  log_failed(response)
272
354
  return nil
273
355
  end
356
+ rescue Timeout::Error
357
+ self.class.logger.warn "timeout"
358
+ return nil
274
359
  end
275
360
 
276
361
  # Deletes Object#id
@@ -283,7 +368,13 @@ class LogicalModel
283
368
 
284
369
  params = self.merge_key
285
370
 
286
- response = Typhoeus::Request.delete( self.resource_uri(id), :params => params )
371
+ response = nil
372
+ Timeout::timeout(self.timeout/1000) do
373
+ response = Typhoeus::Request.delete( self.resource_uri(id),
374
+ :params => params,
375
+ :timeout => self.class.timeout
376
+ )
377
+ end
287
378
  if response == 200
288
379
  log_ok(response)
289
380
  return self
@@ -291,6 +382,9 @@ class LogicalModel
291
382
  log_failed(response)
292
383
  return nil
293
384
  end
385
+ rescue Timeout::Error
386
+ self.logger.warn "timeout"
387
+ return nil
294
388
  end
295
389
 
296
390
  # Destroy object
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{logical_model}
8
- s.version = "0.1.7"
8
+ s.version = "0.1.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Dwayne Macgowan}]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logical_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-07-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &71817330 !ruby/object:Gem::Requirement
16
+ requirement: &85591940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *71817330
24
+ version_requirements: *85591940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &71816850 !ruby/object:Gem::Requirement
27
+ requirement: &85591550 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *71816850
35
+ version_requirements: *85591550
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: typhoeus
38
- requirement: &71816230 !ruby/object:Gem::Requirement
38
+ requirement: &85591170 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.2.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *71816230
46
+ version_requirements: *85591170
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: will_paginate
49
- requirement: &71815650 !ruby/object:Gem::Requirement
49
+ requirement: &85590790 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 3.0.pre2
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *71815650
57
+ version_requirements: *85590790
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: shoulda
60
- requirement: &71815190 !ruby/object:Gem::Requirement
60
+ requirement: &85590460 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *71815190
68
+ version_requirements: *85590460
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
- requirement: &71814930 !ruby/object:Gem::Requirement
71
+ requirement: &85590100 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.0.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *71814930
79
+ version_requirements: *85590100
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jeweler
82
- requirement: &71814010 !ruby/object:Gem::Requirement
82
+ requirement: &85589200 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.6.4
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *71814010
90
+ version_requirements: *85589200
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rcov
93
- requirement: &71813650 !ruby/object:Gem::Requirement
93
+ requirement: &85588130 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *71813650
101
+ version_requirements: *85588130
102
102
  description: LogicalModel allows to use a resource as a model. It is based on web
103
103
  presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3
104
104
  email: dwaynemac@gmail.com
@@ -134,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  segments:
136
136
  - 0
137
- hash: 482849903
137
+ hash: -590611747
138
138
  required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements: