Class: Bigcommerce::PathBuilder
- Inherits:
-
Object
- Object
- Bigcommerce::PathBuilder
- Defined in:
- lib/bigcommerce/path_builder.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#build(keys = []) ⇒ String
This takes the @uri and inserts the keys to form a path.
-
#initialize(uri) ⇒ PathBuilder
constructor
A new instance of PathBuilder.
- #to_s ⇒ String
Constructor Details
#initialize(uri) ⇒ PathBuilder
Returns a new instance of PathBuilder.
12 13 14 |
# File 'lib/bigcommerce/path_builder.rb', line 12 def initialize(uri) @uri = uri end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/bigcommerce/path_builder.rb', line 7 def uri @uri end |
Instance Method Details
#build(keys = []) ⇒ String
This takes the @uri and inserts the keys to form a path. To start we make sure that for nil/numeric values, we wrap those into an array. We then scan the string for %d and %s to find the number of times we possibly need to insert keys into the URI. Next, we check the size of the keys array, if the keys size is less than the number of possible keys in the URI, we will remove the trailing %d or %s, then remove the trailing /. We then pass the keys into the uri to form the path. ex. foo/%d/bar/%d => foo/1/bar/2
29 30 31 32 33 34 35 |
# File 'lib/bigcommerce/path_builder.rb', line 29 def build(keys = []) keys = [] if keys.nil? keys = [keys] if keys.is_a? Numeric ids = uri.scan('%d').count + uri.scan('%s').count str = ids > keys.size ? uri.chomp('%d').chomp('%s').chomp('/') : uri (str % keys).chomp('/') end |
#to_s ⇒ String
40 41 42 |
# File 'lib/bigcommerce/path_builder.rb', line 40 def to_s @uri end |