right_support 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/right_support/net/http_client.rb +54 -25
- data/right_support.gemspec +1 -1
- metadata +3 -3
@@ -16,20 +16,19 @@ module RightSupport::Net
|
|
16
16
|
class NoProvider < Exception; end
|
17
17
|
|
18
18
|
#
|
19
|
-
# A wrapper for the rest-client gem that provides timeouts
|
20
|
-
# the simplicity and ease of use of RestClient's simple, static (class-level) interface.
|
19
|
+
# A wrapper for the rest-client gem that provides timeouts that make it harder to misuse RestClient.
|
21
20
|
#
|
22
21
|
# Even though this code relies on RestClient, the right_support gem does not depend on the rest-client
|
23
22
|
# gem because not all users of right_support will want to make use of this interface. If one of HTTPClient
|
24
23
|
# instance's method is called and RestClient is not available, an exception will be raised.
|
25
24
|
#
|
26
25
|
#
|
27
|
-
# HTTPClient
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
26
|
+
# HTTPClient is a thin wrapper around the RestClient::Request class, with a few minor changes to its
|
27
|
+
# interface:
|
28
|
+
# * initializer accepts some default request options that can be overridden per-request
|
29
|
+
# * it has discrete methods for get/put/post/delete, instead of a single "request" method
|
31
30
|
#
|
32
|
-
# # create an instance ot HTTPClient
|
31
|
+
# # create an instance ot HTTPClient with some default request options
|
33
32
|
# @client = HTTPClient.new()
|
34
33
|
#
|
35
34
|
# # GET
|
@@ -63,31 +62,61 @@ module RightSupport::Net
|
|
63
62
|
DEFAULT_TIMEOUT = 5
|
64
63
|
DEFAULT_OPEN_TIMEOUT = 2
|
65
64
|
|
66
|
-
def initialize(
|
67
|
-
|
68
|
-
|
69
|
-
|
65
|
+
def initialize(defaults = {})
|
66
|
+
@defaults = defaults.clone
|
67
|
+
@defaults[:timeout] ||= DEFAULT_TIMEOUT
|
68
|
+
@defaults[:open_timeout] ||= DEFAULT_OPEN_TIMEOUT
|
69
|
+
@defaults[:headers] ||= {}
|
70
70
|
end
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
72
|
+
def get(*args)
|
73
|
+
request(:get, *args)
|
74
|
+
end
|
75
|
+
|
76
|
+
def post(*args)
|
77
|
+
request(:post, *args)
|
78
|
+
end
|
79
|
+
|
80
|
+
def put(*args)
|
81
|
+
request(:put, *args)
|
82
|
+
end
|
83
|
+
|
84
|
+
def delete(*args)
|
85
|
+
request(:delete, *args)
|
79
86
|
end
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
87
|
+
|
88
|
+
# A very thin wrapper around RestClient::Request.execute.
|
89
|
+
#
|
90
|
+
# === Parameters
|
91
|
+
# type(Symbol):: an HTTP verb, e.g. :get, :post, :put or :delete
|
92
|
+
# url(String):: the URL to request, including any query-string parameters
|
93
|
+
#
|
94
|
+
# === Options
|
95
|
+
# This method can accept any of the options that RestClient::Request can accept, since
|
96
|
+
# all options are proxied through after merging in defaults, etc. Interesting options:
|
97
|
+
# * :payload - hash containing the request body (e.g. POST or PUT parameters)
|
98
|
+
# * :headers - hash containing additional HTTP request headers
|
99
|
+
# * :cookies - will replace possible cookies in the :headers
|
100
|
+
# * :user and :password - for basic auth, will be replaced by a user/password available in the url
|
101
|
+
# * :raw_response - return a low-level RawResponse instead of a Response
|
102
|
+
# * :verify_ssl - enable ssl verification, possible values are constants from OpenSSL::SSL
|
103
|
+
# * :timeout and :open_timeout - specify overall request timeout + socket connect timeout
|
104
|
+
# * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
|
105
|
+
#
|
106
|
+
# === Block
|
107
|
+
# If the request succeeds, this method will yield the response body to its block.
|
108
|
+
#
|
109
|
+
def request(type, url, options={}, &block)
|
110
|
+
options = @defaults.merge(options)
|
85
111
|
options.merge!(:method => type, :url => url)
|
86
|
-
|
112
|
+
|
113
|
+
request_internal(options, &block)
|
87
114
|
end
|
88
115
|
|
116
|
+
protected
|
117
|
+
|
89
118
|
# Wrapper around RestClient::Request.execute -- see class documentation for details.
|
90
|
-
def
|
119
|
+
def request_internal(options, &block)
|
91
120
|
if HAS_REST_CLIENT
|
92
121
|
RestClient::Request.execute(options, &block)
|
93
122
|
else
|
data/right_support.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: right_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 5
|
10
|
+
version: 1.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tony Spataro
|