Class: Excon::Connection
- Inherits:
-
Object
- Object
- Excon::Connection
- Defined in:
- lib/excon/connection.rb
Instance Method Summary collapse
-
#initialize(url, params = {}) ⇒ Connection
constructor
A new instance of Connection.
- #request(params, &block) ⇒ Object
Constructor Details
#initialize(url, params = {}) ⇒ Connection
Returns a new instance of Connection.
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/excon/connection.rb', line 4 def initialize(url, params = {}) uri = URI.parse(url) @connection = { :headers => {}, :host => uri.host, :path => uri.path, :port => uri.port, :query => uri.query, :scheme => uri.scheme }.merge!(params) reset_socket end |
Instance Method Details
#request(params, &block) ⇒ Object
17 18 19 20 21 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/excon/connection.rb', line 17 def request(params, &block) begin params[:path] ||= @connection[:path] unless params[:path][0..0] == '/' params[:path] = "/#{params[:path]}" end request = "#{params[:method]} #{params[:path]}" if (params[:query] && !params[:query].empty?) || @connection[:query] request << "?#{params[:query]}" end request << " HTTP/1.1\r\n" params[:headers] ||= @connection[:headers] params[:headers]['Host'] ||= params[:host] || @connection[:host] unless params[:headers]['Content-Length'] params[:headers]['Content-Length'] = (params[:body] && params[:body].length) || 0 end for key, value in params[:headers] request << "#{key}: #{value}\r\n" end request << "\r\n" socket.write(request) if params[:body] ||= @connection[:body] if params[:body].is_a?(String) socket.write(params[:body]) else while chunk = params[:body].read(CHUNK_SIZE) socket.write(chunk) end end end response = Excon::Response.parse(socket, params, &block) if response.headers['Connection'] == 'close' reset_socket end response rescue => socket_error reset_socket raise(socket_error) end if params[:expects] && ![*params[:expects]].include?(response.status) reset_socket raise(Excon::Errors.status_error(params, response)) else response end rescue => request_error if params[:idempotent] && (!request_error.is_a?(Excon::Errors::Error) || response.status != 404) retries_remaining ||= 4 retries_remaining -= 1 if retries_remaining > 0 retry else raise(request_error) end else raise(request_error) end end |