Class: Bigcommerce::PathBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/bigcommerce/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ PathBuilder

Returns a new instance of PathBuilder.


7
8
9
# File 'lib/bigcommerce/request.rb', line 7

def initialize(uri)
  @uri = uri
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri


5
6
7
# File 'lib/bigcommerce/request.rb', line 5

def uri
  @uri
end

Instance Method Details

#build(keys = []) ⇒ Object

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


19
20
21
22
23
24
25
# File 'lib/bigcommerce/request.rb', line 19

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_sObject


27
28
29
# File 'lib/bigcommerce/request.rb', line 27

def to_s
  @uri
end