class Send_request::Connection_Adapter

Public Class Methods

connect(http_method, url, options, limit = 10) click to toggle source

attr_accessor :http_method, :options, :last_response, :redirect, :last_uri, :url

# File lib/easy_http/request/connection_adapter.rb, line 14
def self.connect http_method, url, options, limit = 10
    raise ArgumentError, 'too many HTTP redirects' if limit == 0
    @url =  self.normalize_url url
    uri = URI.parse @url
    if uri.hostname.nil?
       raise URI::InvalidURIError.new("bad URI(no host provided): #{url}")
    end

    request = http_method.new uri
    response = Net::HTTP.start(uri.hostname, uri.port) do |http|
      http.request(request)
    end
    puts response['Content-Type']
    case response
    when Net::HTTPSuccess then self.response_parser response.body
    when Net::HTTPRedirection then self.connect http_method,response['location'],nil, limit - 1
    when Net::HTTPServerException then   response = {'status': '405', 'message': 'Method Not Allowed'}
    else
      response.error!
    end
end
normalize_url(url) click to toggle source
# File lib/easy_http/request/connection_adapter.rb, line 36
def self.normalize_url url
  url = 'http://' + url unless url.match(%r{\A[a-z][a-z0-9+.-]*://}i)
  url
end
response_parser(response) click to toggle source
# File lib/easy_http/request/connection_adapter.rb, line 41
def self.response_parser response
  p_response =  Easy_http_Response::Http_response.new(response).parse_body
end