Class: StubRequests::URI::Builder

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

Overview

Builder constructs and validates URIs

:reek:TooManyInstanceVariables { max_instance_variables: 6 }

Since:

  • 0.1.0

Constant Summary collapse

URL_SEGMENT_REGEX =

Returns A pattern for matching url segment keys

Returns:

  • (Regexp)

    A pattern for matching url segment keys

Since:

  • 0.1.0

/(:\w+)/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, template, replacements = {}) ⇒ Builder

Initializes a new Builder

Parameters:

  • host (String)

    the URI used to reach the service

  • template (String)

    the endpoint template

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

    a list of uri_replacement keys

Since:

  • 0.1.0



73
74
75
76
77
78
# File 'lib/stub_requests/uri/builder.rb', line 73

def initialize(host, template, replacements = {})
  @host         = +host
  @template     = +template
  @path         = +@template.dup
  @replacements = replacements
end

Instance Attribute Details

#hostObject (readonly)

Since:

  • 0.1.0



43
44
45
# File 'lib/stub_requests/uri/builder.rb', line 43

def host
  @host
end

#pathObject (readonly)

Since:

  • 0.1.0



51
52
53
# File 'lib/stub_requests/uri/builder.rb', line 51

def path
  @path
end

#replacementsObject (readonly)

Since:

  • 0.1.0



55
56
57
# File 'lib/stub_requests/uri/builder.rb', line 55

def replacements
  @replacements
end

#templateObject (readonly)

Since:

  • 0.1.0



47
48
49
# File 'lib/stub_requests/uri/builder.rb', line 47

def template
  @template
end

#unreplacedObject (readonly)

Since:

  • 0.1.0



63
64
65
# File 'lib/stub_requests/uri/builder.rb', line 63

def unreplaced
  @unreplaced
end

#unusedObject (readonly)

Since:

  • 0.1.0



59
60
61
# File 'lib/stub_requests/uri/builder.rb', line 59

def unused
  @unused
end

Class Method Details

.build(host, template, replacements = {}) ⇒ String

Convenience method to avoid .new.build

Parameters:

  • host (String)

    the URI used to reach the service

  • template (String)

    the endpoint template

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

    a list of uri_replacement keys

Returns:

  • (String)

    a validated URI string

Raises:

Since:

  • 0.1.0



36
37
38
# File 'lib/stub_requests/uri/builder.rb', line 36

def self.build(host, template, replacements = {})
  new(host, template, replacements).build
end

Instance Method Details

#buildString

Builds a URI string

Returns:

  • (String)

    a validated URI string

Raises:

Since:

  • 0.1.0



89
90
91
92
93
94
# File 'lib/stub_requests/uri/builder.rb', line 89

def build
  build_uri
  run_validations

  uri
end