Class: StubRequests::URI::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/stub_requests/uri/builder.rb

Overview

Builder constructs and validates URIs

Author:

Since:

  • 0.1.0

Constant Summary collapse

URI_KEY =

Returns A pattern for matching url segment keys

Returns:

  • (Regexp)

    A pattern for matching url segment keys

Since:

  • 0.1.0

/(:[a-zA-Z_]+)/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, route_params = {}) ⇒ Builder

Initializes a new Builder

Parameters:

  • uri (String)

    the URI used to reach the service

  • route_params (Hash<Symbol>) (defaults to: {})

    a list of uri_replacement keys

Since:

  • 0.1.0



66
67
68
69
70
71
# File 'lib/stub_requests/uri/builder.rb', line 66

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

Instance Attribute Details

#expected_keysObject (readonly)

Since:

  • 0.1.0



57
58
59
# File 'lib/stub_requests/uri/builder.rb', line 57

def expected_keys
  @expected_keys
end

#received_keysObject (readonly)

Since:

  • 0.1.0



53
54
55
# File 'lib/stub_requests/uri/builder.rb', line 53

def received_keys
  @received_keys
end

#route_paramsObject (readonly)

Since:

  • 0.1.0



49
50
51
# File 'lib/stub_requests/uri/builder.rb', line 49

def route_params
  @route_params
end

#uriObject (readonly)

Since:

  • 0.1.0



45
46
47
# File 'lib/stub_requests/uri/builder.rb', line 45

def uri
  @uri
end

Class Method Details

.build(uri, route_params = {}) ⇒ String

Convenience method to avoid .new.build

Parameters:

  • the (String)

    URI to the service endpoint

  • route_params (Hash<Symbol>) (defaults to: {})

    a list of uri_replacement keys

Returns:

  • (String)

    a validated URI string

Raises:

Since:

  • 0.1.0



38
39
40
# File 'lib/stub_requests/uri/builder.rb', line 38

def self.build(uri, route_params = {})
  new(uri, route_params).build
end

Instance Method Details

#buildString

Builds a URI string

Returns:

  • (String)

    a validated URI string

Raises:

Since:

  • 0.1.0



82
83
84
85
86
87
88
# File 'lib/stub_requests/uri/builder.rb', line 82

def build
  validate_uri_has_route_params!
  build_uri
  validate_uri

  uri
end