Class: Gdoc::Generate

Inherits:
Object
  • Object
show all
Defined in:
lib/gdoc/generate.rb

Instance Method Summary collapse

Constructor Details

#initialize(out_dir) ⇒ Generate

Returns a new instance of Generate.



8
9
10
11
12
13
# File 'lib/gdoc/generate.rb', line 8

def initialize(out_dir)
  @out_dir = out_dir
  @doc_md = []
  @api_list = []
  @docs = ''
end

Instance Method Details

#auth(ison = false) ⇒ Object



70
71
72
# File 'lib/gdoc/generate.rb', line 70

def auth ison=false
  @ison = ison
end

#create_fileObject



128
129
130
131
132
133
134
# File 'lib/gdoc/generate.rb', line 128

def create_file
  file = File.open "#{@out_dir}/#{@model}.md", 'w'
  file.puts @doc_md.join("\n* * *\n\n") 
  file.close
  @doc_md = []
  @api_list << "[#{@model}](./wiki/#{@model})"
end

#del(url, &blk) ⇒ Object



40
41
42
# File 'lib/gdoc/generate.rb', line 40

def del url, &blk
  gen_api_md 'delete', url, &blk
end

#desc(value) ⇒ Object



53
54
55
# File 'lib/gdoc/generate.rb', line 53

def desc value
  @desc = value
end

#gen_api_list_mdObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gdoc/generate.rb', line 84

def gen_api_list_md
  file = File.open "#{@out_dir}/Home.md", 'w'
  file.puts <<-md
# index
> api 列表

api 地址: #{@root}

#{@api_list.join "\n"}
  md
  file.close
end

#gen_api_md(method, url, &blk) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gdoc/generate.rb', line 97

def gen_api_md method, url, &blk
  blk.call
  @doc_md << <<-md
### #{@root}#{@url}#{url}
> #{@desc}

auth: #{@ison}

headers:
```javascript
#{JSON.pretty_generate (@header || {})}
```

content-type: #{@typ}

method: #{method}

params:

```javascript
#{JSON.pretty_generate @params}
```

return:

```
#{JSON.pretty_generate @ok}
```
  md
end

#get(url, &blk) ⇒ Object



28
29
30
# File 'lib/gdoc/generate.rb', line 28

def get url, &blk
  gen_api_md 'get', url, &blk
end

#headers(header_ = {}) ⇒ Object



66
67
68
# File 'lib/gdoc/generate.rb', line 66

def headers(header_={})
  @header = header_
end

#import(source) ⇒ Object



48
49
50
51
# File 'lib/gdoc/generate.rb', line 48

def import source
  eval(File.read "./#{source}.gdoc")
  create_file
end

#jsonObject



74
75
76
# File 'lib/gdoc/generate.rb', line 74

def json
  @typ = 'application/json'
end

#model(value) ⇒ Object



20
21
22
# File 'lib/gdoc/generate.rb', line 20

def model value
  @model = value
end

#ok(code, json_) ⇒ Object



62
63
64
# File 'lib/gdoc/generate.rb', line 62

def ok code, json_
  @ok = json_
end

#params(typ, json_) ⇒ Object



57
58
59
60
# File 'lib/gdoc/generate.rb', line 57

def params typ, json_
  typ
  @params = json_
end

#patch(url, &blk) ⇒ Object



44
45
46
# File 'lib/gdoc/generate.rb', line 44

def patch url, &blk
  gen_api_md 'patch', url, &blk
end

#post(url, &blk) ⇒ Object



32
33
34
# File 'lib/gdoc/generate.rb', line 32

def post url, &blk
  gen_api_md 'post', url, &blk
end

#put(url, &blk) ⇒ Object



36
37
38
# File 'lib/gdoc/generate.rb', line 36

def put url, &blk
  gen_api_md 'put', url, &blk
end

#root(value) ⇒ Object

dsl api



16
17
18
# File 'lib/gdoc/generate.rb', line 16

def root value
  @root = value
end

#to_md(source) ⇒ Object

output



79
80
81
82
# File 'lib/gdoc/generate.rb', line 79

def to_md source
  eval(File.read "./#{source}.gdoc")
  gen_api_list_md
end

#url(value) ⇒ Object



24
25
26
# File 'lib/gdoc/generate.rb', line 24

def url value
  @url = value
end