Sha256: d5318ca31dff094e391bf2e0d0bc3468dad1c7848170de47e530f251617e422f

Contents?: true

Size: 1.07 KB

Versions: 15

Compression:

Stored size: 1.07 KB

Contents

require 'open-uri'
require 'uri'
require 'cgi'

module HttpUtilities
  module Http
    module Url
      
      def generate_request_url(params = {})
        params.symbolize_keys!
        url = params.delete(:url) { |e| "" }

        sorted_params = params.sort
        query_parts = []

        sorted_params.each do |param_row|
          param = encode_param(param_row.first)
          value = encode_param(param_row.last)
          query_parts << "#{param}=#{value}"
        end

        query = query_parts.join("&")
        request = "#{url}?#{query}"
        return request
      end
      
      def generate_request_params(params)
        sorted_params = params.sort
        query_parts = []

        sorted_params.each do |param_row|
          param = param_row.first
          value = param_row.last
          query_parts << "#{param}=#{value}"
        end

        query = query_parts.join("&")
        
        return query
      end

      def encode_param(param)
        return CGI.escape(param.to_s).to_s.gsub("+", "%20").gsub("%7E", "~") if (param)
      end
      
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
http_utilities-1.2.3 lib/http_utilities/http/url.rb
http_utilities-1.2.2 lib/http_utilities/http/url.rb
http_utilities-1.2.0 lib/http_utilities/http/url.rb
http_utilities-1.1.5 lib/http_utilities/http/url.rb
http_utilities-1.1.2 lib/http_utilities/http/url.rb
http_utilities-1.1.1 lib/http_utilities/http/url.rb
http_utilities-1.1.0 lib/http_utilities/http/url.rb
http_utilities-1.0.9 lib/http_utilities/http/url.rb
http_utilities-1.0.8 lib/http_utilities/http/url.rb
http_utilities-1.0.7.1 lib/http_utilities/http/url.rb
http_utilities-1.0.7 lib/http_utilities/http/url.rb
http_utilities-1.0.6 lib/http_utilities/http/url.rb
http_utilities-1.0.5 lib/http_utilities/http/url.rb
http_utilities-1.0.2 lib/http_utilities/http/url.rb
http_utilities-1.0.1 lib/http_utilities/http/url.rb