Class: Kumonos::Routes::Route

Inherits:
Struct
  • Object
show all
Defined in:
lib/kumonos/routes.rb

Constant Summary collapse

ALLOWED_METHODS =
%w[GET HEAD POST PUT DELETE].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clusterObject

Returns the value of attribute cluster

Returns:

  • (Object)

    the current value of cluster



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def cluster
  @cluster
end

#host_headerObject

Returns the value of attribute host_header

Returns:

  • (Object)

    the current value of host_header



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def host_header
  @host_header
end

#methodObject

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def method
  @method
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def path
  @path
end

#prefixObject

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def prefix
  @prefix
end

#retry_policyObject

Returns the value of attribute retry_policy

Returns:

  • (Object)

    the current value of retry_policy



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def retry_policy
  @retry_policy
end

#timeout_msObject

Returns the value of attribute timeout_ms

Returns:

  • (Object)

    the current value of timeout_ms



42
43
44
# File 'lib/kumonos/routes.rb', line 42

def timeout_ms
  @timeout_ms
end

Class Method Details

.build(h, cluster, host_header) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kumonos/routes.rb', line 44

def build(h, cluster, host_header)
  retry_policy =
    if h.key?('retry_policy')
      RetryPolicy.build(h.fetch('retry_policy'))
    else
      nil
    end

  new(h['prefix'], h['path'], h['method'], cluster, h.fetch('timeout_ms'),
      retry_policy, host_header)
end

Instance Method Details

#to_hObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kumonos/routes.rb', line 57

def to_h
  h = super
  h.delete(:retry_policy)
  h.delete(:host_header)
  h.delete(:method)

  if prefix
    h.delete(:path)
    h[:prefix] = prefix
  elsif path
    h.delete(:prefix)
    h[:path] = path
  else
    raise '`path` or `prefix` is required'
  end

  if host_header
    h[:host_rewrite] = host_header
  else
    h[:auto_host_rewrite] = true
  end
  h
end

#to_h_with_retryObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kumonos/routes.rb', line 82

def to_h_with_retry
  h = to_h
  h[:retry_policy] = retry_policy.to_h if retry_policy

  if method
    m = method.upcase
    raise "method must be one of #{ALLOWED_METHODS.join(',')}: given `#{m}`" unless ALLOWED_METHODS.include?(m)

    h[:headers] = [{ name: ':method', value: m, regex: false }]
  else
    h[:headers] = [{ name: ':method', value: '(GET|HEAD)', regex: true }]
  end

  h
end