Sha256: 5db83f1acea497fe229f9bf229d79113d718df510561da2517d4c58e84ac7bc5

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 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)
        begin
          req = get_http(uri)
          req.use_ssl = uri.scheme == 'https'
          req.head(uri.path, get_headers)
        rescue
          #if we get here, the HEAD failed - but GET may work
          #so we silently fail back to using GET instead
          req = get_http(uri)
          req.use_ssl = uri.scheme == 'https'
          res = req.request_get(uri.path, get_headers)
          res
        end
      end

      def self.get(uri, headers = nil)
        body = ''

        begin
          req = get_http(uri)
          req.use_ssl = uri.scheme == 'https'
          res = req.request_get(uri.path, get_headers(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

      # noinspection RubyStringKeysInHashInspection
      def self.get_headers(extra_headers = nil)
        if @cookie == nil
          headers = { 'User-Agent' => HTTP_UA }
        else
          headers = { 'User-Agent' => HTTP_UA, 'Cookie' => @cookie }
        end

        if extra_headers != nil
          headers.merge! extra_headers
        end

        headers
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
yawast-0.5.2 lib/shared/http.rb
yawast-0.5.1 lib/shared/http.rb
yawast-0.5.1.beta1 lib/shared/http.rb
yawast-0.5.0 lib/shared/http.rb
yawast-0.5.0.beta8 lib/shared/http.rb
yawast-0.5.0.beta7 lib/shared/http.rb