Sha256: 4e4343f34e9aaad6ba2e4b0c72710a9560809020bb33ce5494f2271288aaa5df

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 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, 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

5 entries across 5 versions & 1 rubygems

Version Path
yawast-0.5.0.beta6 lib/shared/http.rb
yawast-0.5.0.beta5 lib/shared/http.rb
yawast-0.5.0.beta4 lib/shared/http.rb
yawast-0.5.0.beta3 lib/shared/http.rb
yawast-0.5.0.beta2 lib/shared/http.rb