Class: Calamum::Runner

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/calamum/runner.rb

Overview

Provides a class-based command line opts. See github.com/opscode/mixlib-cli

Instance Method Summary collapse

Instance Method Details

#load_sourceObject

Open and load the source file of api definition



81
82
83
84
85
86
87
88
89
90
# File 'lib/calamum/runner.rb', line 81

def load_source
  case File.extname(config[:source])
  when '.json'
    Yajl.load(File.open(config[:source]))
  when '.yml'
    YAML.load(File.open(config[:source]))
  else
    raise 'unknown source file extension'
  end
end

#process_errorsObject



118
119
120
121
122
123
124
125
126
# File 'lib/calamum/runner.rb', line 118

def process_errors
  bindings = {
    :name => @definition.get_name,
    :version => @definition.get_version,
    :errors => @definition.get_errors,
  }
  page = Calamum::DocGenerator.new(:errors)
  page.save_template("errors.html", bindings)
end

#process_indexObject

Bind values to index page and save it.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/calamum/runner.rb', line 93

def process_index
  bindings = {
    :url => @definition.get_url,
    :name => @definition.get_name,
    :resources => @definition.resources,
    :description => @definition.get_description,
    :version => @definition.get_version
  }

  page = Calamum::DocGenerator.new(:index)
  page.save_template('index.html', bindings)
end

#process_pagesObject

Bind values to view pages and save them.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/calamum/runner.rb', line 129

def process_pages
  bindings = {
    :name => @definition.get_name,
    :version => @definition.get_version
  }

  page = Calamum::DocGenerator.new(:view)
  @definition.resources.each do |methods|
    methods[1].each do |resource|
      bindings.merge!(:resource => resource)
      filename = "#{resource.slug}.html"
      page.save_template(filename, bindings)
    end
  end
end

#process_section(section, content) ⇒ Object

Bind values to overview and authentication pages and save them.



107
108
109
110
111
112
113
114
115
116
# File 'lib/calamum/runner.rb', line 107

def process_section(section, content)
  bindings = {
    :section => section,
    :name => @definition.get_name,
    :version => @definition.get_version,
    :description => content,
  }
  page = Calamum::DocGenerator.new(:section)
  page.save_template("#{section}.html", bindings)
end

#runObject

Parses command line options and generates API documentation. See samples for details how to define meta-data for your API.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/calamum/runner.rb', line 62

def run
  parse_options
  Calamum::Config.apply(config)
  @definition = Calamum::DocParser.new(load_source)
  @definition.load_resources
  Calamum::DocGenerator.init_base_dir
  process_index

  if config[:template] == 'twitter'
    process_pages
    process_section("overview",  @definition.get_description)
    process_section("authentication", @definition.get_authentication)
    process_errors
  end
rescue => ex
  puts_error ex.message
end