Class: CDNGet::HttpConnection
- Inherits:
-
Object
- Object
- CDNGet::HttpConnection
- Defined in:
- lib/cdnget.rb
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #get(uri) ⇒ Object
-
#initialize(uri, headers = nil) ⇒ HttpConnection
constructor
A new instance of HttpConnection.
- #post(uri, payload) ⇒ Object
Constructor Details
#initialize(uri, headers = nil) ⇒ HttpConnection
Returns a new instance of HttpConnection.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cdnget.rb', line 38 def initialize(uri, headers=nil) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end http.start() @http = http @headers = headers end |
Class Method Details
.open(uri, headers = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/cdnget.rb', line 49 def self.open(uri, headers=nil) http = self.new(uri, headers) return http unless block_given?() begin return yield http ensure http.close() end end |
Instance Method Details
#close ⇒ Object
80 81 82 |
# File 'lib/cdnget.rb', line 80 def close() @http.finish() end |
#get(uri) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cdnget.rb', line 59 def get(uri) resp = @http.send_request('GET', uri.path, nil, @headers) case resp when Net::HTTPSuccess return resp.body #when HTTPInformation, Net::HTTPRedirection, HTTPClientError, HTTPServerError else raise HttpError.new(resp.code.to_i, resp.) end end |
#post(uri, payload) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/cdnget.rb', line 70 def post(uri, payload) path = uri.path path += "?"+uri.query if uri.query && !uri.query.empty? resp = @http.send_request('POST', path, payload, @headers) case resp when Net::HTTPSuccess ; return resp.body else ; raise HttpError.new(resp.code.to_i, resp.) end end |