rest 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 1
4
- :patch: 3
3
+ :minor: 2
4
+ :patch: 0
5
5
  :build: !!null
@@ -41,12 +41,14 @@ module Rest
41
41
 
42
42
  class Client
43
43
 
44
+ attr_accessor :options
44
45
  # options:
45
46
  # - :gem => specify gem explicitly
46
47
  #
47
48
  def initialize(options={})
48
49
  @logger = Logger.new(STDOUT)
49
50
  @logger.level=Logger::INFO
51
+ @options = options
50
52
 
51
53
  Rest.gem = options[:gem] if options[:gem]
52
54
 
@@ -58,24 +60,65 @@ module Rest
58
60
 
59
61
  end
60
62
 
61
-
62
63
  def get(url, req_hash={})
63
- @wrapper.get(url, req_hash)
64
+ res = nil
65
+ perform_op do
66
+ res = @wrapper.get(url, req_hash)
67
+ end
68
+ return res
69
+ end
70
+
71
+ def perform_op(&blk)
72
+ max_retries = @options[:max_retries] || 5
73
+ current_retry = 0
74
+ success = false
75
+ res = nil
76
+ while current_retry < max_retries do
77
+ res = yield blk
78
+ #p res
79
+ #p res.code
80
+ if res.code == 503
81
+ pow = (4 ** (current_retry)) * 100 # milliseconds
82
+ #puts 'pow=' + pow.to_s
83
+ s = Random.rand * pow
84
+ #puts 's=' + s.to_s
85
+ sleep_secs = 1.0 * s / 1000.0
86
+ #puts 'sleep for ' + sleep_secs.to_s
87
+ current_retry += 1
88
+ @logger.debug "503 Error. Retrying #{current_retry} out of #{max_retries} max."
89
+ sleep sleep_secs
90
+ else
91
+ break
92
+ end
93
+ end
94
+ res
64
95
  end
65
96
 
66
97
  # req_hash options:
67
98
  # - :body => post body
68
99
  #
69
100
  def post(url, req_hash={})
70
- @wrapper.post(url, req_hash)
101
+ res = nil
102
+ perform_op do
103
+ res = @wrapper.post(url, req_hash)
104
+ end
105
+ return res
71
106
  end
72
107
 
73
108
  def put(url, req_hash={})
74
- @wrapper.put(url, req_hash)
109
+ res = nil
110
+ perform_op do
111
+ res = @wrapper.put(url, req_hash)
112
+ end
113
+ return res
75
114
  end
76
115
 
77
116
  def delete(url, req_hash={})
78
- @wrapper.delete(url, req_hash)
117
+ res = nil
118
+ perform_op do
119
+ res = @wrapper.delete(url, req_hash)
120
+ end
121
+ return res
79
122
  end
80
123
  end
81
- end
124
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rest"
8
- s.version = "0.1.3"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Travis Reeder"]
12
- s.date = "2012-02-29"
12
+ s.date = "2012-03-05"
13
13
  s.description = "Rest client wrapper that chooses best installed client."
14
14
  s.email = "travis@iron.io"
15
15
  s.extra_rdoc_files = [
@@ -15,8 +15,8 @@ class TestTests < TestBase
15
15
  def test_basics
16
16
  response = @rest.get("http://www.github.com")
17
17
  assert response.code == 200
18
- assert response.body
19
18
  #p response.body
19
+ assert response.body.include?("Social Coding")
20
20
 
21
21
  end
22
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-29 00:00:00.000000000Z
12
+ date: 2012-03-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &11020600 !ruby/object:Gem::Requirement
16
+ requirement: &23203180 !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: *11020600
24
+ version_requirements: *23203180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rest-client
27
- requirement: &11019860 !ruby/object:Gem::Requirement
27
+ requirement: &23202640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *11019860
35
+ version_requirements: *23202640
36
36
  description: Rest client wrapper that chooses best installed client.
37
37
  email: travis@iron.io
38
38
  executables: []