Sha256: 2e14e877b9c8a881a690e7f54ae25c4c2fb00b36c8c5e17056703ed88314b581

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module WebClient
  class Connection

    include Net

    def initialize(*args)
      if args.first.is_a? Hash
        options = args[0].inject({}) { |h, i| h[i[0].to_sym] = i[1]; h }
        host = options[:host]
        port = options[:port]
      else
        host = args[0]
        port = args[1]
      end
      @http = HTTP.new(host, port)
    end

    def host
      @http.address
    end

    def port
      @http.port
    end

    def request!(request)
      begin
        WebClient.logger.debug "[WebClient] #{request.type.to_s.upcase} Url: http://#{host}#{(port != 80) ? ":#{port}" : ''}#{request.url} | Body: #{request.body} | Headers: #{request.headers}"
        response = Response.new @http.request(request.to_http)
        WebClient.logger.debug "[WebClient] RESPONSE Status: #{response.code} | Content: #{response.body}"
        if block_given?
          if response.success?
            yield(response)
          else
            WebClient.logger.error "[WebClient] #{response.code} - Unexpected error\n#{response.body}"
            nil
          end
        else
          response
        end
      rescue Timeout::Error,
          Errno::EINVAL,
          Errno::ECONNRESET,
          EOFError,
          HTTPBadResponse,
          HTTPHeaderSyntaxError,
          ProtocolError,
          SocketError,
          Errno::ECONNREFUSED => e
        WebClient.logger.error "[WebClient] #{e.class}: #{e.message}"
        raise Error, e
      end
    end

    def request(request, &block)
      begin
        request!(request, &block)
      rescue WebClient::Error => e
        nil
      end
    end

    Request::TYPES.each do |request_type|
      define_method "#{request_type}!" do |url, options={}|
        request! Request.new(options.merge(type: request_type, url: url))
      end

      define_method request_type do |url, options={}, &block|
        request Request.new(options.merge(type: request_type, url: url)), &block
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
web_client-0.0.4 lib/web_client/connection.rb