class Acfs::Location

@api private

Describes a URL with placeholders.

Constants

REGEXP

Attributes

arguments[R]
raw[R]
struct[R]
vars[R]

Public Class Methods

new(uri, vars = {}) click to toggle source
# File lib/acfs/location.rb, line 13
def initialize(uri, vars = {})
  @raw       = URI.parse uri
  @vars      = vars
  @struct    = raw.path.split('/').reject(&:empty?).map {|s| s =~ REGEXP ? Regexp.last_match[1].to_sym : s }
  @arguments = struct.select {|s| s.is_a?(Symbol) }
end

Public Instance Methods

build(vars) click to toggle source
# File lib/acfs/location.rb, line 20
def build(vars)
  self.class.new raw.to_s, vars.stringify_keys.merge(self.vars)
end
extract_from(*args) click to toggle source
# File lib/acfs/location.rb, line 24
def extract_from(*args)
  vars = {}
  arguments.each {|key| vars[key.to_s] = extract_arg(key, args) }

  build(vars)
end
raw_uri() click to toggle source
# File lib/acfs/location.rb, line 37
def raw_uri
  raw.to_s
end
Also aliased as: to_s
str() click to toggle source
# File lib/acfs/location.rb, line 31
def str
  uri = raw.dup
  uri.path = "/#{struct.map(&method(:lookup_variable)).join('/')}"
  uri.to_s
end
to_s()
Alias for: raw_uri

Private Instance Methods

extract_arg(key, hashes) click to toggle source
# File lib/acfs/location.rb, line 44
def extract_arg(key, hashes)
  hashes.each_with_index do |hash, index|
    if hash.key?(key)
      return (index.zero? ? hash.delete(key) : hash.fetch(key))
    end
  end

  nil
end
lookup_variable(name) click to toggle source
# File lib/acfs/location.rb, line 54
    def lookup_variable(name)
      return name unless name.is_a?(Symbol)

      value = vars.fetch(name.to_s) do
        if @raise.nil? || @raise
          raise ArgumentError.new <<~ERROR.strip
            URI path argument `#{name}' missing on `#{self}'. Given: `#{vars}.inspect'
          ERROR
        end

        ":#{name}"
      end

      value = value.to_s.strip

      if value.empty?
        raise ArgumentError.new "Cannot replace path argument `#{name}' with empty string."
      end

      ::URI.encode_www_form_component(value)
    end