Sha256: f351833f7006e1eb46ddd26ff0c3fea8c08b0f547154c6a45f0f49c36dfffbe7

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

#
# Abstraction over WebMock to reduce duplication
#
# @author Mikael Henriksson <mikael@zoolutions.se>
# @since 0.1.0
#
module StubRequests
  #
  # Module URI organizes all gem logic regarding URI
  #
  # @author Mikael Henriksson <mikael@zoolutions.se>
  # @since 0.1.2
  #
  module URI
    #
    # @return [Regexp] A pattern for matching route parameters
    ROUTE_PARAM = %r{/:(\w+)/?}.freeze

    #
    # Extracts route parameters from a string
    #
    # @param [String] string a regular string to scan for route parameters
    #
    # @return [Array<Symbol>] an array with all route parameter keys
    #
    def self.route_params(string)
      string.scan(ROUTE_PARAM).flatten.map(&:to_sym)
    end

    #
    # Safely joins two string without any extra ///
    #
    # @param [String] host the host of the URI
    # @param [String] path the path of the URI
    #
    # @return [String] the full URI
    #
    def self.safe_join(host, path)
      [host.chomp("/"), path.sub(%r{\A/}, "")].join("/")
    end

    #
    # UtilityFunction to construct the full URI for a service endpoint
    #
    # @param [Symbol] service_id the id of a service
    # @param [Symbol] endpoint_id the id of an endpoint
    # @param [Hash<Symbol>] route_params hash with route_params
    #
    # @return [Array<Service, Endpoint, String] the service, endpoint and full URI
    #
    def self.for_service_endpoint(service_id, endpoint_id, route_params)
      service  = ServiceRegistry.instance.find!(service_id)
      endpoint = service.endpoints.find!(endpoint_id)
      uri      = URI::Builder.build(service.uri, endpoint.path, route_params)

      [service, endpoint, uri]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
stub_requests-0.1.9 lib/stub_requests/uri.rb
stub_requests-0.1.8 lib/stub_requests/uri.rb
stub_requests-0.1.7 lib/stub_requests/uri.rb
stub_requests-0.1.6 lib/stub_requests/uri.rb
stub_requests-0.1.5 lib/stub_requests/uri.rb
stub_requests-0.1.4 lib/stub_requests/uri.rb