Sha256: a567e21ff3aa39a8cb617382cc629cf7d11c0f00c76ca897e4c65933e030dbd8

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module GrapeApiary
  class Blueprint
    attr_reader :api_class, :blueprint_template, :properties_template

    delegate(*GrapeApiary::Config::SETTINGS, to: 'GrapeApiary::Config')

    def initialize(api_class)
      @api_class           = api_class
      @blueprint_template  = template_for(:blueprint)
      @properties_template = template_for(:properties)
    end

    def generate
      ERB.new(blueprint_template, nil, '-').result(binding)
    end

    def write
      raise 'Not yet supported'
    end

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

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

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

    def properties_table(resource)
      ERB.new(properties_template, nil, '-').result(resource.resource_binding)
    end

    def formatted_request_headers
      formatted_headers(GrapeApiary::Config.request_headers)
    end

    def formatted_response_headers
      formatted_headers(GrapeApiary::Config.response_headers)
    end

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

    private

    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

2 entries across 2 versions & 1 rubygems

Version Path
grape-apiary-1.0.0 lib/grape-apiary/blueprint.rb
grape-apiary-0.2.0 lib/grape-apiary/blueprint.rb