Class: Wrappi::PathGen

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappi/path_gen.rb

Defined Under Namespace

Classes: MissingParamError

Constant Summary collapse

PATTERN =
/:\w+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path, input_params) ⇒ PathGen

Returns a new instance of PathGen.



6
7
8
9
10
# 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

Instance Attribute Details

#input_paramsObject (readonly)

Returns the value of attribute input_params.



5
6
7
# File 'lib/wrappi/path_gen.rb', line 5

def input_params
  @input_params
end

#input_pathObject (readonly)

Returns the value of attribute input_path.



5
6
7
# File 'lib/wrappi/path_gen.rb', line 5

def input_path
  @input_path
end

Instance Method Details

#compiled_pathObject Also known as: path



12
13
14
15
# File 'lib/wrappi/path_gen.rb', line 12

def compiled_path
  return input_path unless interpolable?
  @compiled_path ||= URI.escape(new_sections.join('/'))
end

#for_uriObject

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"


35
36
37
38
# 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

#processed_paramsObject Also known as: params



40
41
42
43
# 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