Sha256: 744d195833d4ef40be707d3954c49a0a28d234bcec7bc7a4545ea482613da25c

Contents?: true

Size: 1.45 KB

Versions: 12

Compression:

Stored size: 1.45 KB

Contents

module Yawast
  module Shared
    class Http
      def self.setup(proxy, cookie)
        if proxy != nil && proxy.include?(':')
          @proxy_host, @proxy_port = proxy.split(':')
          @proxy = true

          puts "Using Proxy: #{proxy}"
        else
          @proxy = false
        end

        @cookie = cookie
        puts "Using Cookie: #{@cookie}" if @cookie != nil
      end

      def self.head(uri)
        req = get_http(uri)
        req.use_ssl = uri.scheme == 'https'
        req.head(uri.path, get_headers)
      end

      def self.get(uri)
        body = ''

        begin
          req = get_http(uri)
          req.use_ssl = uri.scheme == 'https'
          res = req.request_get(uri.path, get_headers)
          body = res.read_body
        rescue
          #do nothing for now
        end

        body
      end

      def self.get_status_code(uri)
        req = get_http(uri)
        req.use_ssl = uri.scheme == 'https'
        res = req.head(uri.path, get_headers)
        res.code
      end

      def self.get_http(uri)
        if @proxy
          req = Net::HTTP.new(uri.host, uri.port, @proxy_host, @proxy_port)
        else
          req = Net::HTTP.new(uri.host, uri.port)
        end

        req
      end

      def self.get_headers
        if @cookie == nil
          headers = { 'User-Agent' => HTTP_UA }
        else
          headers = { 'User-Agent' => HTTP_UA, 'Cookie' => @cookie }
        end

        headers
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
yawast-0.4.0.beta4 lib/shared/http.rb
yawast-0.4.0.beta3 lib/shared/http.rb
yawast-0.4.0.beta2 lib/shared/http.rb
yawast-0.4.0.beta1 lib/shared/http.rb
yawast-0.3.0 lib/shared/http.rb
yawast-0.3.0.beta2 lib/shared/http.rb
yawast-0.3.0.beta1 lib/shared/http.rb
yawast-0.2.2 lib/shared/http.rb
yawast-0.2.1 lib/shared/http.rb
yawast-0.2.0.beta3 lib/shared/http.rb
yawast-0.2.0.beta2 lib/shared/http.rb
yawast-0.2.0.beta1 lib/shared/http.rb