lib/stub_requests/uri/builder.rb in stub_requests-0.1.10 vs lib/stub_requests/uri/builder.rb in stub_requests-0.1.11
- old
+ new
@@ -19,67 +19,57 @@
# @author Mikael Henriksson <mikael@zoolutions.se>
#
class Builder
#
# @return [Regexp] A pattern for matching url segment keys
- URL_SEGMENT_REGEX = /(:\w+)/.freeze
+ URI_KEY = /(:[a-zA-Z_]+)/.freeze
#
# Convenience method to avoid .new.build
#
#
# @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 [String] the URI to the service endpoint
# @param [Hash<Symbol>] route_params a list of uri_replacement keys
#
# @return [String] a validated URI string
#
- def self.build(host, template, route_params = {})
- new(host, template, route_params).build
+ def self.build(uri, route_params = {})
+ new(uri, route_params).build
end
#
# @!attribute [r] uri
- # @return [String] the request host {Service#service_uri}
- attr_reader :host
+ # @return [String] the URI to the service endpoint
+ attr_reader :uri
#
- # @!attribute [r] template
- # @return [String] a string template for the endpoint
- attr_reader :template
- #
- # @!attribute [r] path
- # @return [String] a valid URI path
- attr_reader :path
- #
# @!attribute [r] route_params
# @return [Hash<Symbol] a hash with keys matching the {#template}
attr_reader :route_params
#
- # @!attribute [r] unused
- # @return [Array<String>] a list with unused {#route_params}
- attr_reader :unused
+ # @!attribute [r] received_keys
+ # @return [Array<String>] a list with actual {#route_params} keys
+ attr_reader :received_keys
#
- # @!attribute [r] unreplaced
- # @return [Array<String>] a list of uri_segments that should have been replaced
- attr_reader :unreplaced
+ # @!attribute [r] expected_keys
+ # @return [Array<String>] a list of expected route keys
+ attr_reader :expected_keys
#
# Initializes a new Builder
#
#
- # @param [String] host the URI used to reach the service
- # @param [String] template the endpoint template
+ # @param [String] uri the URI used to reach the service
# @param [Hash<Symbol>] route_params a list of uri_replacement keys
#
- def initialize(host, template, route_params = {})
- @host = +host
- @template = +template
- @path = +@template.dup
- @route_params = route_params.to_route_param
+ def initialize(uri, route_params = {})
+ @uri = +uri
+ @route_params = route_params.to_route_param
+ @received_keys = @route_params.keys
+ @expected_keys = uri.scan(URI_KEY).flatten.uniq
end
#
# Builds a URI string
#
@@ -88,107 +78,36 @@
# @raise [UriSegmentMismatch] when the template have unplaced URI segments
#
# @return [String] a validated URI string
#
def build
+ validate_uri_has_route_params!
build_uri
- run_validations
+ validate_uri
uri
end
private
- def build_uri
- replace_segments
- parse_unreplaced_segments
- end
+ def validate_uri_has_route_params!
+ return if validate_uri_has_route_params
- def uri
- @uri ||= [host, path].join("/")
+ raise UriSegmentMismatch, uri: uri, expected_keys: expected_keys, received_keys: received_keys
end
- def run_validations
- validate_route_params_used
- validate_route_keys_replaced
- validate_uri
+ def validate_uri_has_route_params
+ expected_keys.sort == received_keys.sort
end
- #
- # Replaces the URI segments with the arguments in route_params
- #
- #
- # @return [Array] an list with unused route_params
- #
- def replace_segments
- @unused = route_params.map do |key, value|
- next key unless path.include?(key)
-
- path.gsub!(key, value.to_s)
- next
- end.compact
+ def build_uri
+ route_params.each do |key, value|
+ replace_key(key, value)
+ end
end
- #
- # Validates that all route_params have been used
- #
- #
- # @raise [UriSegmentMismatch] when there are unused route_params
- #
- # @return [void]
- #
- 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 route_params are left
- #
- #
- # @return [true,false]
- #
- def replacents_used?
- unused.none?
- end
-
- #
- # Validates that all URI segments have been replaced in {#path}
- #
- #
- # @raise [UriSegmentMismatch] when the path have unplaced URI segments
- #
- # @return [void]
- #
- 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 route_params=[#{route_params.keys.join(',')}]"
- end
-
- #
- # Checks that all URI keys were replaced
- #
- #
- # @return [true,false]
- #
- def route_keys_replaced?
- unreplaced.none?
- end
-
- #
- # Parses out all unused URI segments
- #
- #
- # @return [Array<String>] a list of not replaced uri_segments
- #
- def parse_unreplaced_segments
- @unreplaced = URL_SEGMENT_REGEX.match(path).to_a.uniq
+ def replace_key(key, value)
+ uri.gsub!(key, value.to_s)
end
#
# Validates {#uri} is valid
#