Class: Modern::DSL::RouteBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/modern/dsl/route_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, http_method, path, settings) ⇒ RouteBuilder

Returns a new instance of RouteBuilder.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/modern/dsl/route_builder.rb', line 14

def initialize(id, http_method, path, settings)
  new_path_segments = path&.split("/") || []
  @value = Modern::Descriptor::Route.new(
    id: id.to_s,
    path: "/" + ([settings.path_segments] + new_path_segments).flatten.compact.join("/"),
    http_method: http_method.upcase,
    summary: nil,
    description: nil,
    deprecated: settings.deprecated,
    tags: settings.tags,
    parameters: settings.parameters,
    request_body: nil,
    responses: [settings.default_response],
    input_converters: settings.input_converters,
    output_converters: settings.output_converters,
    security: settings.security,
    helpers: settings.helpers,
    action: proc {}
  )
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'lib/modern/dsl/route_builder.rb', line 12

def value
  @value
end

Class Method Details

.evaluate(id, http_method, path, settings, &block) ⇒ Object



122
123
124
125
126
127
# File 'lib/modern/dsl/route_builder.rb', line 122

def self.evaluate(id, http_method, path, settings, &block)
  builder = RouteBuilder.new(id, http_method, path, settings)
  builder.instance_exec(&block)

  builder.value
end

Instance Method Details

#action(&block) ⇒ Object



118
119
120
# File 'lib/modern/dsl/route_builder.rb', line 118

def action(&block)
  @value = @value.copy(action: block)
end

#clear_input_converters!Object



84
85
86
# File 'lib/modern/dsl/route_builder.rb', line 84

def clear_input_converters!
  @value = @value.copy(input_converters: [])
end

#clear_output_converters!Object



102
103
104
# File 'lib/modern/dsl/route_builder.rb', line 102

def clear_output_converters!
  @value = @value.copy(output_converters: [])
end

#clear_security!Object



106
107
108
# File 'lib/modern/dsl/route_builder.rb', line 106

def clear_security!
  @value = @value.copy(security: [])
end

#deprecate!Object



43
44
45
# File 'lib/modern/dsl/route_builder.rb', line 43

def deprecate!
  @value = @value.copy(deprecated: true)
end

#description(s) ⇒ Object



39
40
41
# File 'lib/modern/dsl/route_builder.rb', line 39

def description(s)
  @value = @value.copy(description: s)
end

#helper(h) ⇒ Object



114
115
116
# File 'lib/modern/dsl/route_builder.rb', line 114

def helper(h)
  @value = @value.copy(helpers: @value.helpers + [h])
end

#input_converter(media_type_or_converter, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/modern/dsl/route_builder.rb', line 70

def input_converter(media_type_or_converter, &block)
  if media_type_or_converter.is_a?(Modern::Descriptor::Converters::Input::Base)
    @value = @value.copy(input_converters: @value.input_converters + [media_type_or_converter])
  elsif media_type_or_converter.is_a?(String) && !block.nil?
    input_converter(
      Modern::Descriptor::Converters::Input::Base.new(
        media_type: media_type_or_converter, converter: block
      )
    )
  else
    raise "must pass a String and block or a Modern::Descriptor::Converters::Input::Base."
  end
end

#output_converter(media_type_or_converter, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/modern/dsl/route_builder.rb', line 88

def output_converter(media_type_or_converter, &block)
  if media_type_or_converter.is_a?(Modern::Descriptor::Converters::Output::Base)
    @value = @value.copy(output_converters: @value.output_converters + [media_type_or_converter])
  elsif media_type_or_converter.is_a?(String) && !block.nil?
    output_converter(
      Modern::Descriptor::Converters::Output::Base.new(
        media_type: media_type_or_converter, converter: block
      )
    )
  else
    raise "must pass a String and block or a Modern::Descriptor::Converters::Output::Base."
  end
end

#parameter(name, parameter_type, opts) ⇒ Object



51
52
53
54
55
56
# File 'lib/modern/dsl/route_builder.rb', line 51

def parameter(name, parameter_type, opts)
  param = Modern::Descriptor::Parameters.from_inputs(name, parameter_type, opts)
  raise "Duplicate parameter '#{name}'.'" if @value.parameters.any? { |p| p.name.casecmp(param.name).zero? }

  @value = @value.copy(parameters: @value.parameters + [param])
end

#request_body(type, opts = { required: true }) ⇒ Object



58
59
60
61
# File 'lib/modern/dsl/route_builder.rb', line 58

def request_body(type, opts = { required: true })
  opts = { required: true }.merge(opts).merge(type: type)
  @value = @value.copy(request_body: Modern::Descriptor::RequestBody.new(opts))
end

#response(http_code, &block) ⇒ Object



63
64
65
66
67
68
# File 'lib/modern/dsl/route_builder.rb', line 63

def response(http_code, &block)
  existing_response = @value.responses.find { |r| r.http_code == http_code }

  resp = ResponseBuilder.evaluate(existing_response || http_code, &block)
  @value = @value.copy(responses: @value.responses + [resp])
end

#security(sec) ⇒ Object



110
111
112
# File 'lib/modern/dsl/route_builder.rb', line 110

def security(sec)
  @value = @value.copy(security: @value.security + [sec])
end

#summary(s) ⇒ Object



35
36
37
# File 'lib/modern/dsl/route_builder.rb', line 35

def summary(s)
  @value = @value.copy(summary: s)
end

#tag(s) ⇒ Object



47
48
49
# File 'lib/modern/dsl/route_builder.rb', line 47

def tag(s)
  @value = @value.copy(tags: @value.tags + [s])
end