lib/stub_requests/uri/builder.rb in stub_requests-0.1.3 vs lib/stub_requests/uri/builder.rb in stub_requests-0.1.4
- old
+ new
@@ -31,16 +31,16 @@
# @raise [UriSegmentMismatch] when there are unused URI segments
# @raise [UriSegmentMismatch] when the template have unplaced URI segments
#
# @param [String] host the URI used to reach the service
# @param [String] template the endpoint template
- # @param [Hash<Symbol>] replacements a list of uri_replacement keys
+ # @param [Hash<Symbol>] route_params a list of uri_replacement keys
#
# @return [String] a validated URI string
#
- def self.build(host, template, replacements = {})
- new(host, template, replacements).build
+ def self.build(host, template, route_params = {})
+ new(host, template, route_params).build
end
#
# @!attribute [r] uri
# @return [String] the request host {Service#service_uri}
@@ -52,16 +52,16 @@
#
# @!attribute [r] path
# @return [String] a valid URI path
attr_reader :path
#
- # @!attribute [r] replacements
+ # @!attribute [r] route_params
# @return [Hash<Symbol] a hash with keys matching the {#template}
- attr_reader :replacements
+ attr_reader :route_params
#
# @!attribute [r] unused
- # @return [Array<String>] a list with unused {#replacements}
+ # @return [Array<String>] a list with unused {#route_params}
attr_reader :unused
#
# @!attribute [r] unreplaced
# @return [Array<String>] a list of uri_segments that should have been replaced
attr_reader :unreplaced
@@ -70,17 +70,17 @@
# Initializes a new Builder
#
#
# @param [String] host the URI used to reach the service
# @param [String] template the endpoint template
- # @param [Hash<Symbol>] replacements a list of uri_replacement keys
+ # @param [Hash<Symbol>] route_params a list of uri_replacement keys
#
- def initialize(host, template, replacements = {})
+ def initialize(host, template, route_params = {})
@host = +host
@template = +template
@path = +@template.dup
- @replacements = replacements
+ @route_params = route_params.to_route_param
end
#
# Builds a URI string
#
@@ -107,50 +107,47 @@
def uri
@uri ||= [host, path].join("/")
end
def run_validations
- validate_replacements_used
- validate_segments_replaced
+ validate_route_params_used
+ validate_route_keys_replaced
validate_uri
end
#
- # Replaces the URI segments with the arguments in replacements
+ # Replaces the URI segments with the arguments in route_params
#
#
- # @return [Array] an list with unused replacements
+ # @return [Array] an list with unused route_params
#
def replace_segments
- @unused = replacements.map do |key, value|
- uri_segment = ":#{key}"
- if path.include?(uri_segment)
- path.gsub!(uri_segment.to_s, value.to_s)
- next
- else
- uri_segment
- end
+ @unused = route_params.map do |key, value|
+ next key unless path.include?(key)
+
+ path.gsub!(key, value.to_s)
+ next
end.compact
end
#
- # Validates that all replacements have been used
+ # Validates that all route_params have been used
#
#
- # @raise [UriSegmentMismatch] when there are unused replacements
+ # @raise [UriSegmentMismatch] when there are unused route_params
#
# @return [void]
#
- def validate_replacements_used
+ def validate_route_params_used
return if replacents_used?
raise UriSegmentMismatch,
"The URI segment(s) [#{unused.join(',')}] are missing in template (#{path})"
end
#
- # Checks that no replacements are left
+ # Checks that no route_params are left
#
#
# @return [true,false]
#
def replacents_used?
@@ -163,29 +160,25 @@
#
# @raise [UriSegmentMismatch] when the path have unplaced URI segments
#
# @return [void]
#
- def validate_segments_replaced
- return if segments_replaced?
+ def validate_route_keys_replaced
+ return if route_keys_replaced?
raise UriSegmentMismatch,
"The URI segment(s) [#{unreplaced.join(',')}]" \
" were not replaced in template (#{path})." \
- " Given replacements=[#{segment_keys.join(',')}]"
+ " Given route_params=[#{route_params.keys.join(',')}]"
end
- def segment_keys
- @segment_keys ||= replacements.keys.map { |segment_key| ":#{segment_key}" }
- end
-
#
- # Checks that all URI segments were replaced
+ # Checks that all URI keys were replaced
#
#
# @return [true,false]
#
- def segments_replaced?
+ def route_keys_replaced?
unreplaced.none?
end
#
# Parses out all unused URI segments