apige 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0052c0a5cdbc649eef08962d8aa20432f7d67ff7
4
+ data.tar.gz: 3a66942bd9878b0ff1bcf430bc63ce5aaa14c94c
5
+ SHA512:
6
+ metadata.gz: 6a82ba307bceb34e87b8fbe7b073e1c7455e1cb7382d28a82f5e595bab70b1f8280e7ce28b029136ec0bc1f2038f459f220d8a141290955e6095a1a51c6f64ff
7
+ data.tar.gz: f4640aff36e89a5a831bd4207d6d0619e0549f51e2ff0c4fd68600eda5de5824520defd0f92223a6489185df6a10373d8ee49d0ed1bf23718abd238a9c696c3f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apige.gemspec
4
+ gemspec
@@ -0,0 +1,87 @@
1
+ # Apige
2
+
3
+ Just another api doc generator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'apige'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install apige
20
+
21
+ ## Usage
22
+
23
+ Initialize:
24
+
25
+ ```
26
+ # Will create default apige folder with default template and demo files.
27
+ apige init
28
+ # Create with specific folder
29
+ apige init /apidoc
30
+ ```
31
+
32
+ Then
33
+
34
+ ```
35
+ # This will create md files in apige/docs/#{version}(optional)/#{api_filename}.md.
36
+ apige compile
37
+ ```
38
+
39
+ ## FAQ
40
+
41
+ ### How to custom template?
42
+
43
+ Just modify temlate file in apige/templates.
44
+
45
+ Structure.
46
+
47
+ - HEAD
48
+
49
+ - API Content(Render in defined order)
50
+
51
+ ## References
52
+
53
+ ### Configuration
54
+
55
+ - templates: Required, Hash
56
+ - date
57
+ - version
58
+ - author
59
+ - meta
60
+
61
+ ### Scope Method
62
+
63
+ #### group & resources
64
+
65
+ - desc
66
+ - meta
67
+
68
+ #### get/put/post/delete
69
+
70
+ - title
71
+ - desc
72
+ - meta
73
+ - params
74
+ - required
75
+
76
+ ### Template Varable
77
+
78
+ - head: all config value
79
+ - group: level name description parent children type apis
80
+ - resources: Same with group
81
+ - api: level title path method params response_value
82
+
83
+
84
+ ## WIP
85
+
86
+ - Mock server
87
+ - Support Api blueprint format.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apige/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apige"
8
+ spec.version = Apige::VERSION
9
+ spec.authors = ["Jerry Tao"]
10
+ spec.email = ["taojie@yeezon.com"]
11
+
12
+ spec.summary = %q{Just another api doc generator.}
13
+ spec.description = %q{Just another api doc generator.}
14
+ spec.homepage = "https://github.com/jerry-tao/apige"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_dependency "gli", "~> 2.13"
25
+ spec.add_dependency "activesupport", "~> 5.0"
26
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "apige"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,71 @@
1
+ Apige.run do
2
+ post = {:id => "bfed8b96-87a0-429d-ac4c-47f2b281b047",
3
+ :title => "My First Post",
4
+ :image => "http://image.example.com/example.jpg",
5
+ :author => "Jerry Tao",
6
+ :pub_date => "2017-4-1",
7
+ :summary => "This is my first post."}
8
+
9
+ post_simple = {"id": "6d78672f-85a3-44b9-a110-2356754a49c1", "title": "My First Post", "summary": "This is my first post."}
10
+ comment = {content:"Good for you.", date:"2017-4-2"}
11
+
12
+ ## Api scope
13
+ get '/' do
14
+ title 'Service Info'
15
+ desc 'This is service description api.'
16
+ # All return value will generate json default. Custom it in templates.
17
+ {version: '1.0', doc: 'http://doc.example.com', home: 'http://home.example.com', icon: 'http://icon.example.com'}
18
+ end
19
+
20
+ ## Group Scope
21
+ group 'Helper Api' do
22
+ desc 'This is some helper api'
23
+ meta responser: 'Jerry' # Meta is used for custom info, available in api, group and resources scope.
24
+ get '/status' do
25
+ title 'Service Status'
26
+ desc 'This is service status api.'
27
+ {status: 'OK'}
28
+ end
29
+ end
30
+
31
+ ## Resources Scope
32
+ resources 'Post' do
33
+ desc 'Used for resources post'
34
+ get '/posts' do
35
+ desc ''
36
+ params :page
37
+ [post_simple]
38
+ end
39
+
40
+ get '/posts/:id' do
41
+ required :id
42
+ post
43
+ end
44
+
45
+ post '/posts' do
46
+ params :title, :content, :image, :author, :pub_date, :summary # Params could include required or not, doesn't matter.
47
+ required :title, :content
48
+ post
49
+ end
50
+ put '/posts/:id' do
51
+ params :title, :content, :image, :author, :pub_date, :summary
52
+ required :id
53
+ post
54
+ end
55
+
56
+ delete '/posts/:id' do
57
+ required :id
58
+ post
59
+ end
60
+ end
61
+
62
+ ## Put it together
63
+ group 'Post related api' do
64
+ resources 'Comment' do
65
+ get '/posts/:post_id/comments' do
66
+ required :post_id
67
+ [comment]
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,15 @@
1
+ Apige.configure do |config|
2
+ description = <<EOF
3
+ This is my first project.
4
+ EOF
5
+ config.date = Time.now
6
+ config.author = "Jerry Tao"
7
+ config.version = 'beta'
8
+ config.meta = { project_name: 'My first project', description: description }
9
+ config.templates = {
10
+ head: 'templates/head.md.erb',
11
+ group: 'templates/group.md.erb',
12
+ resources: 'templates/resources.md.erb',
13
+ api: 'templates/api.md.erb',
14
+ }
15
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "apige"
5
+ require 'gli'
6
+ require "pry"
7
+
8
+ module GliWrapper
9
+ include GLI::App
10
+ extend self
11
+
12
+ program_desc 'Api generator.'
13
+ version Apige::VERSION
14
+
15
+ subcommand_option_handling :normal
16
+ arguments :strict
17
+
18
+ desc 'Initialize the apige file.'
19
+ arg_name 'folder'
20
+ command [:init] do |c|
21
+ c.action do |global_options, options, args|
22
+ dir = args[0] ==nil ? '.' : args[0]
23
+ dir = File.join(dir, 'apige')
24
+ FileUtils.mkdir_p(dir)
25
+ root = File.expand_path('../../.',__FILE__)
26
+ FileUtils.cp_r(File.join(root,'templates'), dir)
27
+ FileUtils.cp(File.join(root,'examples/config.rb'), dir)
28
+ FileUtils.cp_r(File.join(root,'examples/apis'), dir)
29
+ end
30
+ end
31
+
32
+ desc 'Create the md file.'
33
+ command [:compile] do |c|
34
+ c.action do |global_options, options, args|
35
+ if File.exists?('./apige')
36
+ root_dir = File.join('.', 'apige')
37
+ elsif File.exists?('./apis')
38
+ root_dir = '.'
39
+ else
40
+ raise "Can't find the apige forlder."
41
+ end
42
+ require File.join(root_dir, 'config.rb')
43
+ Apige.compile!(root_dir)
44
+ end
45
+ end
46
+
47
+ pre do |global, command, options, args|
48
+ true
49
+ end
50
+
51
+ on_error do |exception|
52
+ case exception.class
53
+ when Errno::EEXIST
54
+ puts "Folder already exists."
55
+ else
56
+ puts exception
57
+ puts exception.backtrace
58
+ end
59
+ end
60
+
61
+ exit run(ARGV)
62
+ end
@@ -0,0 +1,50 @@
1
+ require "apige/version"
2
+ require "apige/config"
3
+ require "apige/context"
4
+ require "apige/context/api"
5
+ require "apige/context/scope"
6
+ require "apige/context/api_wrapper"
7
+
8
+ module Apige
9
+
10
+ class << self
11
+ def config
12
+ @config ||= Config.new
13
+ end
14
+
15
+ def configure
16
+ yield(config)
17
+ end
18
+
19
+ def run(&block)
20
+ @context = Context.new(block)
21
+ @context.run!
22
+ end
23
+
24
+ def context
25
+ @context
26
+ end
27
+
28
+ def compile!(root_dir)
29
+ config.root_dir = root_dir
30
+ doc_dir = File.join(root_dir, 'docs', Apige.config.version.to_s)
31
+ FileUtils.mkdir_p(doc_dir)
32
+ Dir["#{root_dir}/apis/*.rb"].each { |r|
33
+ api = File.basename r, '.rb'
34
+ require r
35
+ File.open(File.join(doc_dir, api+'.md'), 'w') do |file|
36
+ file.write Apige.context.compile!
37
+ end
38
+ }
39
+ end
40
+
41
+ def templates
42
+ config.templates
43
+ end
44
+
45
+ def root_dir
46
+ config.root_dir
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,7 @@
1
+ require "active_support/all"
2
+ module Apige
3
+ class Config
4
+ include ActiveSupport::Configurable
5
+ config_accessor :date, :meta, :version, :templates, :author, :root_dir
6
+ end
7
+ end
@@ -0,0 +1,98 @@
1
+ module Apige
2
+ class Context
3
+ def initialize(block)
4
+ @block = block
5
+ @scope = Scope.new
6
+ @result = ''
7
+ end
8
+
9
+ def run!
10
+ instance_exec &@block
11
+ end
12
+
13
+ def compile!
14
+ @result << compile_head
15
+ @result << "\n"
16
+ compile(@scope)
17
+ @result
18
+ end
19
+
20
+ def compile_head
21
+ template = File.join(Apige.root_dir, Apige.templates[:head])
22
+ erb = ERB.new(File.read(template))
23
+ erb.filename = template
24
+ erb.def_method(Apige::Config, 'render()')
25
+ Apige.config.render()
26
+ end
27
+
28
+ def compile(scope)
29
+ if !scope.children.empty?
30
+ compile_api(scope)
31
+ scope.children.map { |item| compile(item) }
32
+ else
33
+ template = File.join(Apige.root_dir, Apige.templates[scope.type])
34
+ erb = ERB.new(File.read(template))
35
+ erb.filename= template
36
+ erb.def_method(Scope, 'render()')
37
+ @result << scope.render()
38
+ @result << "\n"
39
+ compile_api(scope)
40
+ end
41
+ end
42
+
43
+ def compile_api(scope)
44
+ scope.apis.map { |wrapper|
45
+ template =File.join(Apige.root_dir, Apige.templates[:api])
46
+ erb = ERB.new(File.read(template))
47
+ erb.filename= template
48
+ erb.def_method(Api, 'render(level)')
49
+ @result << wrapper.api.render(scope.level+1)
50
+ @result << "\n"
51
+ }
52
+ end
53
+
54
+ def default_api(method, path, block)
55
+ @scope.apis << ApiWrapper.new(method, path, block)
56
+ end
57
+
58
+
59
+ def desc(description)
60
+ @scope.description = description
61
+ end
62
+
63
+ def group(name, &block)
64
+ scope = @scope.new_child(name, :group)
65
+ @scope = scope
66
+ instance_exec &block
67
+ @scope = @scope.parent
68
+ end
69
+
70
+ def meta(data)
71
+ @scope.meta = data
72
+ end
73
+
74
+ def resources(name, &block)
75
+ @scope = @scope.new_child(name, :resources)
76
+ instance_exec &block
77
+ @scope = @scope.parent
78
+ end
79
+
80
+ def get(path, &block)
81
+ default_api(:get, path, block)
82
+ end
83
+
84
+ def put(path, &block)
85
+ default_api(:get, path, block)
86
+ end
87
+
88
+ def post(path, &block)
89
+ default_api(:get, path, block)
90
+ end
91
+
92
+ def delete(path, &block)
93
+ default_api(:get, path, block)
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,13 @@
1
+ module Apige
2
+ class Context
3
+ class Api
4
+ attr_accessor :method, :path, :origin_params, :required, :response_value, :title, :desc, :meta
5
+
6
+ def params
7
+ Array((origin_params+required).uniq).map do |item|
8
+ {name: item, required: required.index(item)!=nil}
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,39 @@
1
+ module Apige
2
+ class Context
3
+ class ApiWrapper
4
+ def initialize(method, path, block)
5
+ @api = Api.new
6
+ api.method = method
7
+ api.path = path
8
+ api.origin_params = []
9
+ api.required = []
10
+ @block = block
11
+ api.response_value = instance_exec &block
12
+ end
13
+
14
+ def api
15
+ @api
16
+ end
17
+
18
+ def required(*argv)
19
+ api.required = api.required.concat(argv).uniq
20
+ end
21
+
22
+ def params(*argv)
23
+ api.origin_params = api.origin_params.concat(argv).uniq
24
+ end
25
+
26
+ def meta(data)
27
+ api.meta = data
28
+ end
29
+
30
+ def desc(argv)
31
+ api.desc = argv
32
+ end
33
+
34
+ def title(argv)
35
+ api.title = argv
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ module Apige
2
+ class Context
3
+ class Scope
4
+ def initialize(name='', type='top', parent=nil, level=1)
5
+ @level = level
6
+ @apis = []
7
+ @name = name
8
+ @type = type
9
+ @parent = parent
10
+ @children = []
11
+ @description = ''
12
+ end
13
+
14
+ attr_accessor :meta, :children, :apis, :description, :name,:level, :type, :parent
15
+
16
+ def new_child(name, type)
17
+ scope = Scope.new(name, type, self, @level+1)
18
+ @children << scope
19
+ scope
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Apige
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ <%="#" * level%> <%= @title %> [<%= method.to_s.upcase%> <%= path %>]
2
+
3
+ <%if params.size != 0%>
4
+ Params
5
+
6
+ <% params.each do |item|%>
7
+ <%= item[:name]%>: <%= item[:required]%>
8
+ <% end%>
9
+ <% end%>
10
+ Response
11
+
12
+ ```
13
+ <%=JSON.pretty_generate(response_value)%>
14
+ ```
@@ -0,0 +1,2 @@
1
+ <%="#" * level%> <%= name %>
2
+ <%=description%>
@@ -0,0 +1,7 @@
1
+ Version: <%= version %>
2
+ Author: <%= author %>
3
+ Date: <%= date.strftime '%Y-%m-%d' %>
4
+
5
+ # <%= meta[:project_name]%>
6
+
7
+ <%= meta[:description]%>
@@ -0,0 +1,2 @@
1
+ <%="#" * @level%> <%= name %>
2
+ <%=description%>
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apige
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jerry Tao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gli
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.13'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Just another api doc generator.
84
+ email:
85
+ - taojie@yeezon.com
86
+ executables:
87
+ - apige
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - apige.gemspec
96
+ - bin/console
97
+ - bin/setup
98
+ - examples/apis/post.rb
99
+ - examples/config.rb
100
+ - exe/apige
101
+ - lib/apige.rb
102
+ - lib/apige/config.rb
103
+ - lib/apige/context.rb
104
+ - lib/apige/context/api.rb
105
+ - lib/apige/context/api_wrapper.rb
106
+ - lib/apige/context/scope.rb
107
+ - lib/apige/version.rb
108
+ - templates/api.md.erb
109
+ - templates/group.md.erb
110
+ - templates/head.md.erb
111
+ - templates/resources.md.erb
112
+ homepage: https://github.com/jerry-tao/apige
113
+ licenses: []
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.5.1
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Just another api doc generator.
135
+ test_files: []