Sha256: 01d55b3dfbff8cc68c952d8a7f112f335b50ade9549da167d07f466528034ee3

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

require 'cgi'
require 'addressable/uri'

module Github
  module Utils
    module Url
      extend self

      DEFAULT_QUERY_SEP = /[&;] */n

      KEY_VALUE_SEP = '='.freeze

      def escape_uri(s) Addressable::URI.escape(s.to_s) end

      def escape(s) CGI.escape(s.to_s) end

      def unescape(s) CGI.unescape(s.to_s) end

      def normalize(s) Addressable::URI.parse(s.to_s).normalize.path end

      def build_query(params)
        params.map { |k, v|
          if v.class == Array
            build_query(v.map { |x| [k, x] })
          else
            v.nil? ? escape(k) : "#{escape(k)}=#{escape(v)}"
          end
        }.join("&")
      end

      def parse_query(query_string)
        return '' if query_string.nil? || query_string.empty?
        params = {}

        query_string.split(DEFAULT_QUERY_SEP).each do |part|
          k, v = part.split(KEY_VALUE_SEP, 2).map { |el| unescape(el) }

          if cur = params[k]
            if cur.class == Array
              params[k] << v
            else
              params[k] = [cur, v]
            end
          else
            params[k] = v
          end
        end
        params
      end

      def parse_query_for_param(query_string, name)
        parse_query(query_string).each do |key, val|
          return val.first if (name == key) && val.is_a?(Array)
          return val if (name == key)
        end
        return nil
      end

    end # Url
  end # Utils
end # Github

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
github_api-0.17.0 lib/github_api/utils/url.rb
github_api-0.16.0 lib/github_api/utils/url.rb
github_api-0.15.0 lib/github_api/utils/url.rb
github_api-0.14.5 lib/github_api/utils/url.rb
github_api-0.14.4 lib/github_api/utils/url.rb
github_api-0.14.3 lib/github_api/utils/url.rb
github_api-0.14.2 lib/github_api/utils/url.rb
github_api-0.14.1 lib/github_api/utils/url.rb
github_api-0.14.0 lib/github_api/utils/url.rb
github_api-0.13.1 lib/github_api/utils/url.rb
github_api-0.13.0 lib/github_api/utils/url.rb
github_api-0.12.4 lib/github_api/utils/url.rb