Class: PuppetX::Patching::HTTPHelper
- Inherits:
-
Object
- Object
- PuppetX::Patching::HTTPHelper
- Defined in:
- lib/puppet_x/encore/patching/http_helper.rb
Overview
Helper class for HTTP calls
Direct Known Subclasses
Instance Method Summary collapse
- #delete(url, body: nil, headers: @headers) ⇒ Object
- #execute(method, url, body: nil, headers: {}, redirect_limit: @redirect_limit) ⇒ Object
- #get(url, body: nil, headers: @headers) ⇒ Object
-
#initialize(username: nil, password: nil, ssl: true, ssl_verify: OpenSSL::SSL::VERIFY_NONE, redirect_limit: 10, headers: {}) ⇒ HTTPHelper
constructor
A new instance of HTTPHelper.
- #ip?(str) ⇒ Boolean
- #net_http_request_class(method) ⇒ Object
- #post(url, body: nil, headers: @headers) ⇒ Object
Constructor Details
#initialize(username: nil, password: nil, ssl: true, ssl_verify: OpenSSL::SSL::VERIFY_NONE, redirect_limit: 10, headers: {}) ⇒ HTTPHelper
Returns a new instance of HTTPHelper.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 8 def initialize(username: nil, password: nil, ssl: true, ssl_verify: OpenSSL::SSL::VERIFY_NONE, redirect_limit: 10, headers: {}) @username = username @password = password @ssl = ssl @ssl_verify = ssl_verify @redirect_limit = redirect_limit @headers = headers end |
Instance Method Details
#delete(url, body: nil, headers: @headers) ⇒ Object
78 79 80 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 78 def delete(url, body: nil, headers: @headers) execute('delete', url, body: body, headers: headers, redirect_limit: @redirect_limit) end |
#execute(method, url, body: nil, headers: {}, redirect_limit: @redirect_limit) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 22 def execute(method, url, body: nil, headers: {}, redirect_limit: @redirect_limit) raise ArgumentError, 'HTTP redirect too deep' if redirect_limit.zero? # setup our HTTP class uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = @ssl http.verify_mode = @ssl_verify # create our request req = net_http_request_class(method).new(uri) req.basic_auth(@username, @password) if @username && @password # copy headers into the request headers.each { |k, v| req[k] = v } # set the body in the request req.body = body if body # execute resp = http.request(req) # check response for success, redirect or error case resp when Net::HTTPSuccess then resp when Net::HTTPRedirection then execute(method, resp['location'], body: body, headers: headers, redirect_limit: redirect_limit - 1) else = 'code=' + resp.code += ' message=' + resp. += ' body=' + resp.body raise resp.error_type.new(, resp) end end |
#get(url, body: nil, headers: @headers) ⇒ Object
70 71 72 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 70 def get(url, body: nil, headers: @headers) execute('get', url, body: body, headers: headers, redirect_limit: @redirect_limit) end |
#ip?(str) ⇒ Boolean
63 64 65 66 67 68 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 63 def ip?(str) IPAddr.new(str) true rescue false end |
#net_http_request_class(method) ⇒ Object
59 60 61 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 59 def net_http_request_class(method) Net::HTTP.const_get(method.capitalize, false) end |
#post(url, body: nil, headers: @headers) ⇒ Object
74 75 76 |
# File 'lib/puppet_x/encore/patching/http_helper.rb', line 74 def post(url, body: nil, headers: @headers) execute('post', url, body: body, headers: headers, redirect_limit: @redirect_limit) end |