calamum 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile.lock +44 -0
  4. data/LICENSE.txt +7 -0
  5. data/README.md +35 -0
  6. data/Rakefile +0 -0
  7. data/bin/calamum +10 -0
  8. data/calamum.gemspec +25 -0
  9. data/lib/calamum/calamum_helper.rb +10 -0
  10. data/lib/calamum/config.rb +13 -0
  11. data/lib/calamum/definition_parser.rb +71 -0
  12. data/lib/calamum/doc_generator.rb +61 -0
  13. data/lib/calamum/request.rb +40 -0
  14. data/lib/calamum/runner.rb +84 -0
  15. data/lib/calamum/templates/bootstrap/assets/img/glyphicons-halflings-white.png +0 -0
  16. data/lib/calamum/templates/bootstrap/assets/img/glyphicons-halflings.png +0 -0
  17. data/lib/calamum/templates/bootstrap/assets/javascripts/bootstrap.js +2276 -0
  18. data/lib/calamum/templates/bootstrap/assets/javascripts/jquery-1.7.2.min.js +4 -0
  19. data/lib/calamum/templates/bootstrap/assets/stylesheets/bootstrap-responsive.css +1109 -0
  20. data/lib/calamum/templates/bootstrap/assets/stylesheets/bootstrap.css +6158 -0
  21. data/lib/calamum/templates/bootstrap/assets/stylesheets/docs.css +1067 -0
  22. data/lib/calamum/templates/bootstrap/assets/stylesheets/javascripts/bootstrap.js +2276 -0
  23. data/lib/calamum/templates/bootstrap/assets/stylesheets/javascripts/jquery-1.4.2.js +6240 -0
  24. data/lib/calamum/templates/bootstrap/assets/stylesheets/javascripts/jquery-1.7.2.min.js +4 -0
  25. data/lib/calamum/templates/bootstrap/assets/stylesheets/main.css +20 -0
  26. data/lib/calamum/templates/bootstrap/index.html.erb +120 -0
  27. data/lib/calamum/version.rb +3 -0
  28. data/lib/calamum.rb +9 -0
  29. data/sample/sample.yml +82 -0
  30. data/spec/data/my_definition.yml +41 -0
  31. data/spec/definition_parser_spec.rb +17 -0
  32. data/spec/request_spec.rb +13 -0
  33. data/spec/spec_helper.rb +3 -0
  34. metadata +165 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ tmp
18
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in turntable.gemspec
4
+ gemspec
5
+
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ calamum (1.0.0)
5
+ activemodel
6
+ mixlib-cli
7
+ mixlib-config
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (3.0.17)
13
+ activesupport (= 3.0.17)
14
+ builder (~> 2.1.2)
15
+ i18n (~> 0.5.0)
16
+ activesupport (3.0.17)
17
+ builder (2.1.2)
18
+ coderay (1.0.8)
19
+ diff-lcs (1.1.3)
20
+ i18n (0.5.0)
21
+ method_source (0.8.1)
22
+ mixlib-cli (1.3.0)
23
+ mixlib-config (1.1.2)
24
+ pry (0.9.12)
25
+ coderay (~> 1.0.5)
26
+ method_source (~> 0.8)
27
+ slop (~> 3.4)
28
+ rspec (2.12.0)
29
+ rspec-core (~> 2.12.0)
30
+ rspec-expectations (~> 2.12.0)
31
+ rspec-mocks (~> 2.12.0)
32
+ rspec-core (2.12.2)
33
+ rspec-expectations (2.12.1)
34
+ diff-lcs (~> 1.1.3)
35
+ rspec-mocks (2.12.1)
36
+ slop (3.4.3)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ calamum!
43
+ pry
44
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (C) <year> <copyright holders>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ Calamum
2
+ =============
3
+
4
+ Calamum is a simple ruby build program to generate a REST API documentation from a YAML file definition.
5
+
6
+ ## Installation
7
+
8
+
9
+ ### Gem Installation
10
+
11
+ Download and install rake with the following.
12
+
13
+ $gem install calamum
14
+
15
+ ## Usage
16
+
17
+ calamum -h
18
+ Usage: /bin/calamum (options)
19
+ -d, --debug Show actions to do (default)
20
+ -f, --file DEFINITION Definition YAML file (required)
21
+ -p, --path PATH The distination path for the generated doc directory
22
+ -t, --template TEMPLATE Documentation HTML template
23
+ -v, --version Show calamum version
24
+ -h, --help Show this message
25
+ ### Simple Example
26
+ First, you must write a YAML file which contains the definition of your REST API.
27
+ A simple example file 'sample.yml' in sample directory.
28
+
29
+ Next just run the calamum command as following:
30
+
31
+ $calamum -f my_api_definition.yml
32
+
33
+ A "doc" directory is generated (by default in your home path, to change it specify the option '-p/--path') with a index file that contain your API Documentation.
34
+
35
+ Open the index.html file in your browser to show your api doc.
data/Rakefile ADDED
File without changes
data/bin/calamum ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Author:: Mehrez Alachheb (<lachheb.mehrez@gmail.com>)
4
+
5
+ $: << File.join(File.dirname(__FILE__), "..", "lib")
6
+
7
+ require 'rubygems'
8
+ require 'calamum/runner'
9
+
10
+ Calamum::Runner.new.run
data/calamum.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'calamum/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "calamum"
7
+ gem.version = Calamum::VERSION
8
+ gem.authors = ["Mehrez Alachheb"]
9
+ gem.email = ["lachheb.mehrez@gmail.com"]
10
+ gem.description = %q{Calamum is a simple ruby build program to generate a REST API documentation from a YAML file definition}
11
+ gem.summary = %q{Calamum is a simple ruby build program to generate a REST API documentation from a YAML file definition}
12
+ gem.homepage = "https://github.com/malachheb/liblvm"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ gem.bindir = "bin"
19
+
20
+ gem.add_dependency 'activemodel'
21
+ gem.add_dependency 'mixlib-cli'
22
+ gem.add_dependency 'mixlib-config'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'pry'
25
+ end
@@ -0,0 +1,10 @@
1
+
2
+
3
+ def puts_info(msg)
4
+ $stdout.puts "\e[32m[INFO] #{msg}\e[0m" if Calamum::Config[:debug]
5
+ end
6
+
7
+ def puts_error(msg)
8
+ $stderr.puts "\e[31m[ERROR] #{msg}\e[0m" if Calamum::Config[:debug]
9
+ exit(1)
10
+ end
@@ -0,0 +1,13 @@
1
+ require 'calamum'
2
+ require 'mixlib/config'
3
+
4
+ module Calamum
5
+ class Config
6
+ extend Mixlib::Config
7
+
8
+ def self.inspect
9
+ configuration.inspect
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,71 @@
1
+
2
+ class Calamum::DefinitionParser
3
+ attr_accessor :definition, :resources
4
+
5
+ def initialize(definition_yaml)
6
+ @definition = definition_yaml
7
+ @resources = Hash.new
8
+ end
9
+
10
+ def get_resources
11
+ @definition['resources']
12
+ end
13
+
14
+ def get_name
15
+ @definition['name']
16
+ end
17
+
18
+ def get_url
19
+ @definition['url']
20
+ end
21
+
22
+ def get_resources_names
23
+ @definition['resources'].inject([]){|resources, (key, content)| resources << key}
24
+ end
25
+
26
+ def load_requests
27
+ self.get_resources.each do |resource, requests|
28
+ @resources[resource] = []
29
+ index = 0
30
+ requests.each do |request|
31
+ req = initialize_request(request)
32
+ unless req.nil?
33
+ @resources[resource][index]= req
34
+ index += 1
35
+ end
36
+ end
37
+ end
38
+ @resources
39
+ end
40
+
41
+ def initialize_request(request)
42
+ req = Calamum::Request.new({
43
+ :uri => request['uri'],
44
+ :action => request['action'],
45
+ :params => request['params'],
46
+ :description => request['description'],
47
+ :headers => request['headers'],
48
+ :response => request['response'],
49
+ })
50
+ if req.errors.empty?
51
+ puts_info "Request #{request['action']}: #{request['uri']} loading success"
52
+ req
53
+ else
54
+ puts_error "Request #{request['action']}: #{request['uri']} not valid: #{req.errors}"
55
+ nil
56
+ end
57
+ end
58
+
59
+ def get_resource_uris(name)
60
+ @resources[name]
61
+ end
62
+
63
+ def find_resource_uri_by_label(resource, label)
64
+ @resources.select{|r| r['label']==label}.first['uri']
65
+ end
66
+
67
+ def find_resource_action_by_label(resource, label)
68
+ @resources.select{|r| r['label']==label}.first['action']
69
+ end
70
+
71
+ end
@@ -0,0 +1,61 @@
1
+
2
+ class Calamum::DocGenerator
3
+ include ERB::Util
4
+ attr_accessor :resources, :templaete, :name, :url
5
+
6
+ def initialize(template, resources, name, url)
7
+ @template = template
8
+ @resources = resources
9
+ @name = name
10
+ @url = url
11
+ end
12
+
13
+ def render()
14
+ ERB.new(@template).result(binding)
15
+ end
16
+
17
+ def self.load_template
18
+ File.read(File.join(File.dirname(__FILE__), "templates", "bootstrap", "index.html.erb"))
19
+ end
20
+
21
+ def save_result(file)
22
+ initialize_doc_dir
23
+ File.open(file, "w+") do |f|
24
+ f.write(render)
25
+ end
26
+ end
27
+
28
+ def initialize_doc_dir
29
+ create_doc_dir
30
+ Dir.mkdir File.join(Calamum::Config[:path], 'doc', 'assets')
31
+ copy_assets
32
+ end
33
+
34
+ def copy_assets
35
+ src = File.join(File.dirname(__FILE__), "templates", Calamum::Config[:template], "assets")
36
+ dst = File.join(Calamum::Config[:path], 'doc', 'assets')
37
+ begin
38
+ FileUtils.copy_entry(src, dst)
39
+ rescue => e
40
+ puts_error e.message
41
+ end
42
+ end
43
+
44
+ def create_doc_dir
45
+ doc_dir = File.join(Calamum::Config[:path], 'doc')
46
+ if File.exist?(doc_dir)
47
+ while true
48
+ print "The filename directoy #{doc_dir} already exists. Do you want to overwrite it? [Y/N]: "
49
+ case $stdin.gets.chomp!.downcase
50
+ when 'y'
51
+ FileUtils.rm_r(doc_dir, :force => true)
52
+ break
53
+ when 'n'
54
+ exit(1)
55
+ end
56
+ end
57
+ end
58
+ Dir.mkdir doc_dir
59
+ end
60
+
61
+ end
@@ -0,0 +1,40 @@
1
+ class Calamum::Request
2
+ include ActiveModel::Validations
3
+
4
+ attr_reader :errors
5
+ attr_accessor :uri, :action, :params, :description, :headers, :response
6
+ ACTIONS= %{GET POST PUT DELETE}
7
+
8
+ validates :uri, :action, :presence => true
9
+ # validates :uri, :uniqueness => true
10
+ validates :action, :inclusion => { :in => ACTIONS, :message => "is not included in [#{ACTIONS}]"}
11
+
12
+ def initialize(attrs)
13
+ @uri= attrs[:uri]
14
+ @action= attrs[:action].upcase
15
+ @params= attrs[:params] || {}
16
+ @headers = attrs[:headers] || {}
17
+ @response = attrs[:response] || ''
18
+ @description = attrs[:description]
19
+ @errors = ActiveModel::Errors.new(self)
20
+ self.valid?
21
+ end
22
+
23
+ def action_label
24
+ case @action
25
+ when 'GET'
26
+ 'label-info'
27
+ when 'POST'
28
+ 'label-success'
29
+ when 'PUT'
30
+ 'label-warning'
31
+ when 'DELETE'
32
+ 'label-important'
33
+ end
34
+ end
35
+
36
+ def to_s
37
+ "#{action}: #{uri}"
38
+ end
39
+
40
+ end
@@ -0,0 +1,84 @@
1
+ require 'calamum'
2
+ require 'calamum/config'
3
+ require 'calamum/doc_generator'
4
+ require 'mixlib/cli'
5
+
6
+ class Calamum::Runner
7
+ include Mixlib::CLI
8
+ CMD_NAME = "calamum"
9
+
10
+ def initialize
11
+ super
12
+ end
13
+
14
+ option :help,
15
+ :short => "-h",
16
+ :long => "--help",
17
+ :description => "Show this message",
18
+ :on => :tail,
19
+ :boolean => true,
20
+ :show_options => true,
21
+ :exit => 0
22
+
23
+ option :version,
24
+ :short => "-v",
25
+ :long => "--version",
26
+ :description => "Show #{CMD_NAME} version",
27
+ :boolean => true,
28
+ :proc => lambda {|v| puts "Calamum: #{Calamum::VERSION}"},
29
+ :exit => 0
30
+
31
+ option :debug,
32
+ :short => "-d",
33
+ :long => "--debug",
34
+ :description => "Show actions to do (default)",
35
+ :boolean => true,
36
+ :default => true,
37
+ :proc => lambda { |p| true }
38
+
39
+ option :definition,
40
+ :short => "-f DEFINITION",
41
+ :long => "--file DEFINITION",
42
+ :description => "Definition YAML file",
43
+ :required => true
44
+
45
+ option :template,
46
+ :short => "-t TEMPLATE",
47
+ :long => "--template TEMPLATE",
48
+ :description => "Documentation HTML template",
49
+ :default => "bootstrap"
50
+
51
+ option :path,
52
+ :short => "-p PATH",
53
+ :long => "--path PATH",
54
+ :description => "The distination path for the generated doc directory",
55
+ :default => ENV['HOME']
56
+
57
+ def run
58
+ load_options
59
+ Calamum::Config.merge!(config)
60
+ api_definition = load_definition_file
61
+ @definition = Calamum::DefinitionParser.new(api_definition)
62
+ @definition.load_requests
63
+ template = Calamum::DocGenerator.load_template
64
+ html_output = Calamum::DocGenerator.new(template, @definition.resources, @definition.get_name, @definition.get_url)
65
+ html_output.save_result(File.join(Calamum::Config[:path], 'doc', 'index.html'))
66
+ end
67
+
68
+ def load_definition_file
69
+ begin
70
+ YAML.load(File.open(config[:definition]))
71
+ rescue => e
72
+ puts_error e.message
73
+ end
74
+ end
75
+
76
+ def load_options
77
+ begin
78
+ parse_options
79
+ rescue => e
80
+ puts_error e.message
81
+ end
82
+ end
83
+
84
+ end