Sha256: b0d82a0a980634d05ba54cfaf3a0539c59f8b26e6d33dfd049c09184f3116a24

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

module URI
  class HTTP

    # Query parameters
    attr_reader :query_params

    #
    # Creates a new URI::HTTP object and initializes query_params as a
    # new Hash.
    #
    def initialize(*args)
      super(*args)

      @query_params = {}
      parse_query_params
    end

    #
    # Sets the query data and updates query_params.
    #
    def query=(query_str)
      new_query = super(query_str)
      parse_query_params
      return new_query
    end

    protected

    #
    # Parses the query parameters from the query data, populating
    # query_params with the parsed parameters.
    #
    def parse_query_params
      @query_params.clear

      if @query
        @query.split('&').each do |param|
          name, value = param.split('=')

          if value
            @query_params[name] = URI.decode(value)
          else
            @query_params[name] = nil
          end
        end
      end
    end

    private

    # :nodoc
    def path_query
      str = @path

      unless @query_params.empty?
        str += '?' + @query_params.to_a.map { |name,value|
          if value==true
            "#{name}=active"
          elsif value
            "#{name}=#{URI.encode(value.to_s)}"
          else
            "#{name}="
          end
        }.join('&')
      end

      return str
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gscraper-0.1.0 lib/gscraper/extensions/uri/http.rb
gscraper-0.1.1 lib/gscraper/extensions/uri/http.rb
gscraper-0.1.2 lib/gscraper/extensions/uri/http.rb
gscraper-0.1.3 lib/gscraper/extensions/uri/http.rb