Class: Modern::DSL::ResponseBuilder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_code_or_response) ⇒ ResponseBuilder

Returns a new instance of ResponseBuilder.



13
14
15
16
17
18
19
20
# File 'lib/modern/dsl/response_builder.rb', line 13

def initialize(http_code_or_response)
  @value =
    if http_code_or_response.is_a?(Modern::Descriptor::Response)
      http_code_or_response
    else
      Modern::Descriptor::Response.new(http_code: http_code_or_response)
    end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/modern/dsl/response_builder.rb', line 11

def value
  @value
end

Class Method Details

.evaluate(http_code_or_response, &block) ⇒ Object



34
35
36
37
38
# File 'lib/modern/dsl/response_builder.rb', line 34

def self.evaluate(http_code_or_response, &block)
  builder = ResponseBuilder.new(http_code_or_response)
  builder.instance_exec(&block)
  builder.value
end

Instance Method Details

#content(media_type, type = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/modern/dsl/response_builder.rb', line 26

def content(media_type, type = nil)
  raise "Duplicate content type: #{media_type}" \
    if @value.content.any? { |c| c.media_type.casecmp(media_type).zero? }

  new_content = Modern::Descriptor::Content.new(media_type: media_type, type: type)
  @value = @value.copy(content: @value.content + [new_content])
end

#description(s) ⇒ Object



22
23
24
# File 'lib/modern/dsl/response_builder.rb', line 22

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