Sha256: 4ed1ecf5e4f3130c9df6c7d94235e5e2848ed3e9c9331073e99a2271b482d1cc

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'modern/descriptor/response'
require 'modern/descriptor/content'

require 'docile'

module Modern
  module DSL
    class ResponseBuilder
      attr_reader :value

      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

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

      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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
modern-0.5.0 lib/modern/dsl/response_builder.rb
modern-0.4.6 lib/modern/dsl/response_builder.rb
modern-0.4.5 lib/modern/dsl/response_builder.rb
modern-0.4.4 lib/modern/dsl/response_builder.rb
modern-0.4.3 lib/modern/dsl/response_builder.rb
modern-0.4.2 lib/modern/dsl/response_builder.rb