Sha256: d76db25f0b08b8ff0e93c99223d8189950cb11e89ca245062c194f90b849de02

Contents?: true

Size: 1.46 KB

Versions: 239

Compression:

Stored size: 1.46 KB

Contents

module Avo
  module Services
    class URIService
      class << self
        def parse(path)
          new path
        end
      end

      attr_reader :uri

      def initialize(path = "")
        @uri = Addressable::URI.parse(path)
      end

      def call
        to_s
      end

      def append_paths(*paths)
        paths = Array.wrap(paths).flatten

        return self if paths.blank?

        # Add the intermediary forward slash
        @uri.path = @uri.path.concat("/") unless @uri.path.ends_with? "/"

        # Add the paths to the URI
        @uri.merge!(path: @uri.path.concat(join_paths(paths)))

        self
      end
      alias_method :append_path, :append_paths

      def append_query(params)
        params = if params.is_a? Hash
          params.map do |key, value|
            "#{key}=#{value}"
          end
        else
          {}
        end

        return self if params.blank?

        # Add the query params to the URI
        @uri.merge!(query: [@uri.query, *params].compact.join("&"))

        self
      end

      def to_s
        @uri.to_s
      end

      private

      def join_paths(paths)
        paths
          .map do |path|
            sanitize_path path
          end
          .join("/")
      end

      # Removes the forward slash if it's present at the start of the path
      def sanitize_path(path)
        if path.to_s.starts_with? "/"
          path = path[1..]
        end

        ERB::Util.url_encode path
      end
    end
  end
end

Version data entries

239 entries across 239 versions & 1 rubygems

Version Path
avo-3.18.1.tw4 lib/avo/services/uri_service.rb
avo-3.18.1 lib/avo/services/uri_service.rb
avo-3.18.0.tw4 lib/avo/services/uri_service.rb
avo-3.18.0 lib/avo/services/uri_service.rb
avo-3.17.9.beta2 lib/avo/services/uri_service.rb
avo-3.17.9.beta1 lib/avo/services/uri_service.rb
avo-3.17.9.tw4 lib/avo/services/uri_service.rb
avo-3.17.9 lib/avo/services/uri_service.rb
avo-3.17.8.tw4 lib/avo/services/uri_service.rb
avo-3.17.8 lib/avo/services/uri_service.rb
avo-3.17.7 lib/avo/services/uri_service.rb
avo-3.17.6.tw4 lib/avo/services/uri_service.rb
avo-3.17.6 lib/avo/services/uri_service.rb
avo-3.17.5 lib/avo/services/uri_service.rb
avo-3.17.4 lib/avo/services/uri_service.rb
avo-3.17.3 lib/avo/services/uri_service.rb
avo-3.17.5.tw4 lib/avo/services/uri_service.rb
avo-3.17.4.tw4 lib/avo/services/uri_service.rb
avo-3.17.3.tw4 lib/avo/services/uri_service.rb
avo-3.17.2.tw4 lib/avo/services/uri_service.rb