class ApiSketch::DSL

Constants

COMPLEX_ATTRIBUTE_NAMES

Attributes

definitions_dir[R]

Public Class Methods

new(definitions_dir=ApiSketch::Config[:definitions_dir]) click to toggle source
# File lib/api_sketch/dsl.rb, line 7
def initialize(definitions_dir=ApiSketch::Config[:definitions_dir])
  @definitions_dir = definitions_dir
end

Public Instance Methods

init!() click to toggle source
# File lib/api_sketch/dsl.rb, line 11
def init!
  if File.directory?(config_dir)
    puts_info("Load configuration")
    load_dir_files(config_dir)
  end

  if File.directory?(resources_dir)
    puts_info("Load resources")
    load_dir_files(resources_dir)
  end
end
resource(name, &block) click to toggle source
# File lib/api_sketch/dsl.rb, line 27
def resource(name, &block)
  attributes = get_attrs(name, &block)

  COMPLEX_ATTRIBUTE_NAMES.each do |attribute_name|
    block_value = attributes[attribute_name]
    attributes[attribute_name] = get_complex_attribute(attribute_name, &block_value) if block_value
  end

  # Assign resource namespace
  attributes[:namespace] ||= block.source_location[0].gsub(resources_dir, "").gsub(".rb", "").split("/").reject { |ns| ns.nil? || ns == "" }.join("/")

  ::ApiSketch::Model::Resource.create(attributes)
end
shared_block(name, &block) click to toggle source
# File lib/api_sketch/dsl.rb, line 23
def shared_block(name, &block)
  ::ApiSketch::Model::SharedBlock.add(name, block)
end

Private Instance Methods

config_dir() click to toggle source

Definitions loading

# File lib/api_sketch/dsl.rb, line 60
def config_dir
  "#{definitions_dir}/config"
end
get_attrs(name, &block) click to toggle source
# File lib/api_sketch/dsl.rb, line 43
def get_attrs(name, &block)
  ::ApiSketch::DSL::AttributeParser.new(:root, &block).to_h.merge(name: name)
end
get_complex_attribute(attribute_name, &block) click to toggle source
# File lib/api_sketch/dsl.rb, line 47
def get_complex_attribute(attribute_name, &block)
  case attribute_name
  when :headers
    ::ApiSketch::DSL::Headers.new(&block).to_a
  when :parameters
    params = ::ApiSketch::DSL::Parameters.new(&block).to_h
    ::ApiSketch::Model::Parameters.new(params)
  when :responses
    ::ApiSketch::DSL::Responses.new(&block).to_a
  end
end
load_dir_files(dir) click to toggle source
# File lib/api_sketch/dsl.rb, line 68
def load_dir_files(dir)
  Dir.glob("#{dir}/**/*.rb").each do |file_path|
    puts_info("\t read: #{file_path}")
    binding.eval(File.open(File.expand_path(file_path)).read, file_path)
  end
end
resources_dir() click to toggle source
# File lib/api_sketch/dsl.rb, line 64
def resources_dir
  "#{definitions_dir}/resources"
end