Sha256: 14691a59394edc4cac4ca5f11392a87158a87c328174ac9863558f8ef11d778a

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module GrapeMarkdown
  class Document
    attr_reader :api_class, :document_template, :properties_template

    delegate(
      *GrapeMarkdown::Configuration::SETTINGS,
      to: 'GrapeMarkdown::Configuration'
    )

    def initialize(api_class)
      @api_class           = api_class
      @document_template   = template_for(:document)
      @properties_template = template_for(:properties)
    end

    def generate
      render(document_template, binding)
    end

    def write
      raise 'Not yet supported'
    end

    def routes
      @routes ||= api_class.routes.map do |route|
        GrapeMarkdown::Route.new(route)
      end
    end

    def resources
      @resources ||= begin
        grouped_routes = routes.group_by(&:route_name).reject do |name, _routes|
          resource_exclusion.include?(name.parameterize.underscore.to_sym)
        end

        grouped_routes.map { |name, routes| Resource.new(name, routes) }
      end
    end

    def properties_table(resource)
      render(properties_template, resource.route_binding)
    end

    def formatted_request_headers
      formatted_headers(GrapeMarkdown::Configuration.request_headers)
    end

    def formatted_response_headers
      formatted_headers(GrapeMarkdown::Configuration.response_headers)
    end

    def show_request_sample?(route)
      %w(PUT POST).include?(route.request_method)
    end

    private

    def render(template, binding = nil)
      ERB.new(template, nil, '-').result(binding)
    end

    def template_for(name)
      directory = File.dirname(File.expand_path(__FILE__))
      path = File.join(directory, "./templates/#{name}.md.erb")

      File.read(path)
    end

    def formatted_headers(headers)
      return '' unless headers.present?

      spacer  = "\n" + (' ' * 12)

      strings = headers.map do |header|
        key, value = *header.first

        "#{key}: #{value}"
      end

      "    + Headers\n" + spacer + strings.join(spacer)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-markdown-0.0.7 lib/grape-markdown/document.rb