Sha256: 46331c0b51492724320c7f316a6ab6229e4ab51d68a534e2deb4f627d379c2da

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

module Springboard
  class Client
    ##
    # Extensions to the Addressable::URI class.
    module URIExt
      ##
      # Returns a new URI with the given subpath appended to it. Ensures a single
      # forward slash between the URI's path and the given subpath.
      def subpath(subpath)
        uri = dup
        uri.path = "#{path}/" unless path.end_with?('/')
        uri.join subpath.to_s.gsub(/^\//, '')
      end

      ##
      # Merges the given hash of query string parameters and values with the URI's
      # existing query string parameters (if any).
      def merge_query_values!(values)
        self.springboard_query_values = (self.query_values || {}).merge(normalize_query_hash(values))
      end

      private

      def springboard_query_values=(values)
        retval = self.query_values = normalize_query_hash(values)
        # Hack to strip digits from Addressable::URI's subscript notation
        self.query = self.query.gsub(/\[\d+\]=/, '[]=')
        retval
      end

      def normalize_query_hash(hash)
        hash.inject({}) do |copy, (k, v)|
          copy[k.to_s] = case v
            when Hash then normalize_query_hash(v)
            when true, false then v.to_s
            else v end
          copy
        end
      end
    end
  end
end

##
# We include Springboard::Client::URIExt into Addressable::URI because its design
# doesn't support subclassing.
#
# @see http://addressable.rubyforge.org/api/Addressable/URI.html Addressable::URI docs
class Addressable::URI
  include Springboard::Client::URIExt
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
springboard-retail-4.1.1 lib/springboard/client/uri_ext.rb
springboard-retail-4.1.0 lib/springboard/client/uri_ext.rb
springboard-retail-4.0.1 lib/springboard/client/uri_ext.rb
springboard-retail-4.0.0 lib/springboard/client/uri_ext.rb