right_support 1.0.4 → 1.0.5

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.
@@ -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 and other useful features while preserving
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 supports a subset of the module methods provided by RestClient and is interface-compatible
28
- # with those methods it replaces; the only difference is that the HTTPClient version of each method accepts an
29
- # additional, optional parameter which is a request timeout in seconds. The RestClient gem does not allow
30
- # timeouts without instantiating a "heavyweight" HTTPClient object.
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(options = {})
67
- [:get, :post, :put, :delete].each do |method|
68
- define_instance_method(method) {|*args| query(method, *args)}
69
- end
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
- protected
73
-
74
- # Helps to add default methods to class
75
- def define_instance_method(method, &block)
76
- (class << self; self; end).module_eval do
77
- define_method(method, &block)
78
- end
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
- def query(type, url, options={}, &block)
82
- options[:timeout] ||= DEFAULT_TIMEOUT
83
- options[:open_timeout] ||= DEFAULT_OPEN_TIMEOUT
84
- options[:headers] ||= {}
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
- request(options, &block)
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 request(options, &block)
119
+ def request_internal(options, &block)
91
120
  if HAS_REST_CLIENT
92
121
  RestClient::Request.execute(options, &block)
93
122
  else
@@ -7,7 +7,7 @@ spec = Gem::Specification.new do |s|
7
7
  s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
8
8
 
9
9
  s.name = 'right_support'
10
- s.version = '1.0.4'
10
+ s.version = '1.0.5'
11
11
  s.date = '2011-10-06'
12
12
 
13
13
  s.authors = ['Tony Spataro']
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: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 4
10
- version: 1.0.4
9
+ - 5
10
+ version: 1.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Spataro