rest 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 0
4
- :patch: 1
4
+ :patch: 2
5
5
  :build:
@@ -5,6 +5,20 @@ require 'logger'
5
5
  # The purpose of this is so that users who can't install binaries easily (like windoze users) can have fallbacks that work
6
6
 
7
7
  module Rest
8
+
9
+ class ClientError < StandardError
10
+
11
+ end
12
+
13
+
14
+ class TimeoutError < ClientError
15
+ def initialize(msg=nil)
16
+ msg ||= "HTTP Request Timed out."
17
+ super(msg)
18
+ end
19
+ end
20
+
21
+
8
22
  def self.gem=(g)
9
23
  @gem = g
10
24
  end
@@ -16,115 +30,48 @@ module Rest
16
30
  begin
17
31
  require 'typhoeus'
18
32
  Rest.gem = :typhoeus
33
+ require_relative 'wrappers/typhoeus_wrapper'
19
34
  rescue LoadError => ex
20
35
  puts "Could not load typhoeus. #{ex.class.name}: #{ex.message}. Falling back to rest-client. Please install 'typhoeus' gem for best performance."
21
36
  require 'rest_client'
22
37
  Rest.gem = :rest_client
38
+ require_relative 'wrappers/typhoeus_wrapper'
23
39
  end
24
40
 
25
- class RestClientResponseWrapper
26
- def initialize(response)
27
- @response = response
28
- end
29
-
30
- def code
31
- @response.code
32
- end
33
-
34
- def body
35
- @response.body
36
- end
37
-
38
- end
39
-
40
- class ClientError < StandardError
41
-
42
- end
43
-
44
- class RestClientExceptionWrapper < ClientError
45
- def initialize(ex)
46
- super(ex.message)
47
- @ex = ex
48
- end
49
- end
50
-
51
- class TimeoutError < ClientError
52
- def initialize(msg=nil)
53
- msg ||= "HTTP Request Timed out."
54
- super(msg)
55
- end
56
- end
57
-
58
- class TyphoeusTimeoutError < TimeoutError
59
- def initialize(response)
60
- msg ||= "HTTP Request Timed out. Curl code: #{response.curl_return_code}. Curl error msg: #{response.curl_error_message}."
61
- super(msg)
62
- end
63
- end
64
41
 
65
42
  class Client
66
43
 
44
+ # options:
45
+ # - :gem => specify gem explicitly
46
+ #
67
47
  def initialize(options={})
68
48
  @logger = Logger.new(STDOUT)
69
49
  @logger.level=Logger::INFO
70
50
 
71
- end
72
-
73
- def default_typhoeus_options
74
- req_hash = {}
75
- req_hash[:connect_timeout] = 5000
76
- req_hash[:timeout] ||= 10000
77
- req_hash[:follow_location] = true
78
- req_hash[:max_redirects] = 2
79
- req_hash
80
- end
51
+ Rest.gem = options[:gem] if options[:gem]
81
52
 
82
- def get(url, req_hash={})
83
53
  if Rest.gem == :typhoeus
84
- req_hash = default_typhoeus_options.merge(req_hash)
85
-
86
- # puts "REQ_HASH=" + req_hash.inspect
87
- response = Typhoeus::Request.get(url, req_hash)
88
- #p response
89
- if response.timed_out?
90
- raise TyphoeusTimeoutError.new(response)
91
- end
54
+ @wrapper = Rest::Wrappers::TyphoeusWrapper.new
92
55
  else
93
- begin
94
- headers = req_hash[:headers] || {}
95
- r2 = RestClient.get(url, req_hash.merge(headers))
96
- response = RestClientResponseWrapper.new(r2)
97
- # todo: make generic exception
98
- rescue RestClient::Exception => ex
99
- raise RestClientExceptionWrapper.new(ex)
100
- end
56
+ @wrapper = Rest::Wrappers::RestClientWrapper.new
101
57
  end
102
- response
58
+
103
59
  end
104
60
 
61
+
62
+ def get(url, req_hash={})
63
+ @wrapper.get(url, req_hash)
64
+ end
65
+
66
+ # req_hash options:
67
+ # - :body => post body
68
+ #
105
69
  def post(url, req_hash={})
106
- if Rest.gem == :typhoeus
107
- # todo: should change this timeout to longer if it's for posting file
108
- req_hash[:connect_timeout] = 5000
109
- req_hash[:timeout] ||= 10000
110
- # puts "REQ_HASH=" + req_hash.inspect
111
- response = Typhoeus::Request.post(url, req_hash)
112
- #p response
113
- if response.timed_out?
114
- raise TyphoeusTimeoutError.new(response)
115
- end
116
- else
117
- begin
118
- headers = req_hash[:headers] || {}
119
- r2 = RestClient.post(url, req_hash[:body], headers)
120
- response = RestClientResponseWrapper.new(r2)
121
- # todo: make generic exception
122
- rescue RestClient::Exception => ex
123
- raise RestClientExceptionWrapper.new(ex)
124
- end
125
- end
126
- response
70
+ @wrapper.post(url, req_hash)
127
71
  end
128
72
 
73
+ def delete(url, req_hash={})
74
+ @wrapper.delete(url, req_hash)
75
+ end
129
76
  end
130
77
  end
@@ -0,0 +1,75 @@
1
+ require 'rest_client'
2
+
3
+ module Rest
4
+
5
+ module Wrappers
6
+ class RestClientExceptionWrapper < ClientError
7
+ def initialize(ex)
8
+ super(ex.message)
9
+ @ex = ex
10
+ end
11
+ end
12
+
13
+ class RestClientResponseWrapper
14
+ def initialize(response)
15
+ @response = response
16
+ end
17
+
18
+ def code
19
+ @response.code
20
+ end
21
+
22
+ def body
23
+ @response.body
24
+ end
25
+
26
+ end
27
+
28
+ class RestClientWrapper
29
+
30
+ def get(url, req_hash={})
31
+ response = nil
32
+ begin
33
+ req_hash[:method] = :get
34
+ req_hash[:url] = url
35
+ r2 = RestClient::Request.execute(req_hash)
36
+ response = RestClientResponseWrapper.new(r2)
37
+ rescue RestClient::Exception => ex
38
+ raise RestClientExceptionWrapper.new(ex)
39
+ end
40
+ response
41
+ end
42
+
43
+ def post(url, req_hash={})
44
+ response = nil
45
+ begin
46
+ req_hash[:method] = :post
47
+ req_hash[:url] = url
48
+ req_hash[:payload] = req_hash[:body] if req_hash[:body]
49
+ r2 = RestClient::Request.execute(req_hash)
50
+ response = RestClientResponseWrapper.new(r2)
51
+ rescue RestClient::Exception => ex
52
+ raise RestClientExceptionWrapper.new(ex)
53
+ end
54
+ response
55
+ end
56
+
57
+ def delete(url, req_hash={})
58
+ response = nil
59
+ begin
60
+ req_hash[:method] = :delete
61
+ req_hash[:url] = url
62
+ req_hash[:payload] = req_hash[:body] if req_hash[:body]
63
+ r2 = RestClient::Request.execute(req_hash)
64
+ response = RestClientResponseWrapper.new(r2)
65
+ # todo: make generic exception
66
+ rescue RestClient::Exception => ex
67
+ raise RestClientExceptionWrapper.new(ex)
68
+ end
69
+ response
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,62 @@
1
+ require 'typhoeus'
2
+
3
+ module Rest
4
+
5
+ module Wrappers
6
+
7
+ class TyphoeusTimeoutError < Rest::TimeoutError
8
+ def initialize(response)
9
+ msg ||= "HTTP Request Timed out. Curl code: #{response.curl_return_code}. Curl error msg: #{response.curl_error_message}."
10
+ super(msg)
11
+ end
12
+ end
13
+
14
+ class TyphoeusWrapper
15
+
16
+ def default_typhoeus_options
17
+ req_hash = {}
18
+ # todo: should change this timeout to longer if it's for posting file
19
+ req_hash[:connect_timeout] = 5000
20
+ req_hash[:timeout] = 10000
21
+ req_hash[:follow_location] = true
22
+ req_hash[:max_redirects] = 2
23
+ req_hash
24
+ end
25
+
26
+ def get(url, req_hash={})
27
+ req_hash = default_typhoeus_options.merge(req_hash)
28
+ # puts "REQ_HASH=" + req_hash.inspect
29
+ response = Typhoeus::Request.get(url, req_hash)
30
+ #p response
31
+ if response.timed_out?
32
+ raise TyphoeusTimeoutError.new(response)
33
+ end
34
+
35
+ response
36
+ end
37
+
38
+ def post(url, req_hash={})
39
+ req_hash = default_typhoeus_options.merge(req_hash)
40
+ # puts "REQ_HASH=" + req_hash.inspect
41
+ response = Typhoeus::Request.post(url, req_hash)
42
+ #p response
43
+ if response.timed_out?
44
+ raise TyphoeusTimeoutError.new(response)
45
+ end
46
+ response
47
+ end
48
+
49
+ def delete(url, req_hash={})
50
+ req_hash = default_typhoeus_options.merge(req_hash)
51
+ response = Typhoeus::Request.delete(url, req_hash)
52
+ if response.timed_out?
53
+ raise TyphoeusTimeoutError.new(response)
54
+ end
55
+ response
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rest"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
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 = "2011-12-16"
12
+ s.date = "2011-12-18"
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 = [
@@ -21,6 +21,8 @@ Gem::Specification.new do |s|
21
21
  "VERSION.yml",
22
22
  "lib/rest.rb",
23
23
  "lib/rest/client.rb",
24
+ "lib/rest/wrappers/rest_client_wrapper.rb",
25
+ "lib/rest/wrappers/typhoeus_wrapper.rb",
24
26
  "rest.gemspec",
25
27
  "test/test_base.rb",
26
28
  "test/test_rest.rb"
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.0.1
4
+ version: 0.0.2
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: 2011-12-16 00:00:00.000000000 Z
12
+ date: 2011-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &19547060 !ruby/object:Gem::Requirement
16
+ requirement: &17129060 !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: *19547060
24
+ version_requirements: *17129060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rest-client
27
- requirement: &19546560 !ruby/object:Gem::Requirement
27
+ requirement: &17128580 !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: *19546560
35
+ version_requirements: *17128580
36
36
  description: Rest client wrapper that chooses best installed client.
37
37
  email: travis@iron.io
38
38
  executables: []
@@ -45,6 +45,8 @@ files:
45
45
  - VERSION.yml
46
46
  - lib/rest.rb
47
47
  - lib/rest/client.rb
48
+ - lib/rest/wrappers/rest_client_wrapper.rb
49
+ - lib/rest/wrappers/typhoeus_wrapper.rb
48
50
  - rest.gemspec
49
51
  - test/test_base.rb
50
52
  - test/test_rest.rb