Sha256: 73c07d889984af036a5e763e4a8c451defbc9c749219d324ef95f59e9d52b1b3

Contents?: true

Size: 1.43 KB

Versions: 75

Compression:

Stored size: 1.43 KB

Contents

module Avo
  module Services
    class URIService
      class << self
        def parse(path)
          self.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..-1]
        end

        path
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 1 rubygems

Version Path
avo-2.26.1.pr1584.pre.1 lib/avo/services/uri_service.rb
avo-2.26.0 lib/avo/services/uri_service.rb
avo-2.25.0 lib/avo/services/uri_service.rb
avo-2.24.1 lib/avo/services/uri_service.rb
avo-2.24.0 lib/avo/services/uri_service.rb
avo-2.23.3.pre.1.pr1529 lib/avo/services/uri_service.rb
avo-2.23.2 lib/avo/services/uri_service.rb
avo-2.23.1 lib/avo/services/uri_service.rb
avo-2.23.0 lib/avo/services/uri_service.rb
avo-2.22.0 lib/avo/services/uri_service.rb
avo-2.21.3.pre.pr1489 lib/avo/services/uri_service.rb
avo-2.21.2.pre.pr1486 lib/avo/services/uri_service.rb
avo-2.21.1.pre.pr1484 lib/avo/services/uri_service.rb
avo-2.21.0 lib/avo/services/uri_service.rb
avo-2.21.1.pre.pr1476 lib/avo/services/uri_service.rb
avo-2.21.1.pre.issue1450 lib/avo/services/uri_service.rb
avo-2.21.1.pre.issue1444 lib/avo/services/uri_service.rb
avo-2.20.0 lib/avo/services/uri_service.rb
avo-2.19.0 lib/avo/services/uri_service.rb
avo-2.18.1 lib/avo/services/uri_service.rb