class Wrappi::PathGen

Constants

PATTERN

Attributes

input_params[R]
input_path[R]

Public Class Methods

new(input_path, input_params) click to toggle source
# File lib/wrappi/path_gen.rb, line 6
def initialize(input_path, input_params)
  @input_path = input_path
  @input_params = Fusu::HashWithIndifferentAccess.new(input_params)
  @interpolable = input_path =~ PATTERN
end

Public Instance Methods

compiled_path() click to toggle source
# File lib/wrappi/path_gen.rb, line 12
def compiled_path
  return input_path unless interpolable?
  @compiled_path ||= URI.escape(new_sections.join('/'))
end
Also aliased as: path
for_uri() click to toggle source

removes first character if path starts with `/` this is because URI will remove all the paths in between example:

URI.join('https://hello.com/foo/bar', '/bin').to_s
=> "https://hello.com/bin"

URI.join('https://hello.com/foo/bar', 'bin').to_s
=> "https://hello.com/foo/bin"

URI.join('https://hello.com/foo/bar/', '/bin').to_s
=> "https://hello.com/bin"

We want this behaviour:
URI.join('https://hello.com/foo/bar/', 'bin').to_s
=> "https://hello.com/foo/bar/bin"
# File lib/wrappi/path_gen.rb, line 35
def for_uri
  return compiled_path unless compiled_path =~ /^\//
  compiled_path.dup.tap { |s| s[0] = '' }
end
params()
Alias for: processed_params
path()
Alias for: compiled_path
processed_params() click to toggle source
# File lib/wrappi/path_gen.rb, line 40
def processed_params
  return input_params unless interpolable?
  @processed_params ||= input_params.reject{ |k, v| keys_in_params.include?(k.to_sym) }
end
Also aliased as: params

Private Instance Methods

interpolable?() click to toggle source
# File lib/wrappi/path_gen.rb, line 48
def interpolable?
  @interpolable
end
interpolations() click to toggle source
# File lib/wrappi/path_gen.rb, line 74
def interpolations
  input_path.scan(PATTERN)
end
keys_in_params() click to toggle source
# File lib/wrappi/path_gen.rb, line 68
def keys_in_params
  interpolations.map do |k|
    k.delete(':').to_sym
  end
end
new_sections() click to toggle source
# File lib/wrappi/path_gen.rb, line 52
def new_sections
  sections.map do |section|
    if section =~ PATTERN
      key = section.delete(':')
      raise MissingParamError.new("path: #{input_path} requires param ':#{key}'") unless input_params.key?(key)
      input_params[key]
    else
      section
    end
  end
end
sections() click to toggle source
# File lib/wrappi/path_gen.rb, line 64
def sections
  input_path.split("/")
end