Class: Veeqo::PathBuilder
- Inherits:
-
Object
- Object
- Veeqo::PathBuilder
- Defined in:
- lib/veeqo/request.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#build(keys = []) ⇒ Object
This takes the @uri and inserts the keys to form a path.
-
#initialize(uri) ⇒ PathBuilder
constructor
A new instance of PathBuilder.
- #to_s ⇒ Object
Constructor Details
#initialize(uri) ⇒ PathBuilder
7 8 9 |
# File 'lib/veeqo/request.rb', line 7 def initialize(uri) @uri = uri end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
5 6 7 |
# File 'lib/veeqo/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/veeqo/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_s ⇒ Object
27 28 29 |
# File 'lib/veeqo/request.rb', line 27 def to_s @uri end |