Class: ApidocoDsl::ApiDoc

Inherits:
Object
  • Object
show all
Includes:
Documentable
Defined in:
lib/apidoco_dsl/api_doc.rb

Constant Summary collapse

SETTABLE =

attr_reader :api

['published', 'name', 'endpoint',
'http_method', 'header', 'markdown', 'sort_order']

Instance Attribute Summary collapse

Attributes included from Documentable

#api

Instance Method Summary collapse

Methods included from Documentable

#param, #param_group, #property

Constructor Details

#initialize(api) ⇒ ApiDoc

Returns a new instance of ApiDoc.



16
17
18
19
20
21
22
23
24
# File 'lib/apidoco_dsl/api_doc.rb', line 16

def initialize(api)
  @api                 = api
  @doc_request_params  = []
  @doc_response_params = []
  @doc_examples        = []
  @doc_namespace       = @api.namespace
  @doc_resource        = @api.resource
  @param_destination   = 'request'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



37
38
39
40
# File 'lib/apidoco_dsl/api_doc.rb', line 37

def method_missing(name, *args)
  return set_attribute(name, *args) if SETTABLE.include?(name.to_s)
  super
end

Instance Attribute Details

#doc_descriptionObject

Returns the value of attribute doc_description.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_description
  @doc_description
end

#doc_endpointObject

Returns the value of attribute doc_endpoint.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_endpoint
  @doc_endpoint
end

#doc_examplesObject

Returns the value of attribute doc_examples.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_examples
  @doc_examples
end

#doc_headerObject

Returns the value of attribute doc_header.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_header
  @doc_header
end

#doc_http_methodObject

Returns the value of attribute doc_http_method.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_http_method
  @doc_http_method
end

#doc_markdownObject

Returns the value of attribute doc_markdown.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_markdown
  @doc_markdown
end

#doc_nameObject

Returns the value of attribute doc_name.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_name
  @doc_name
end

#doc_namespaceObject

Returns the value of attribute doc_namespace.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_namespace
  @doc_namespace
end

#doc_publishedObject

Returns the value of attribute doc_published.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_published
  @doc_published
end

#doc_request_exampleObject

Returns the value of attribute doc_request_example.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_request_example
  @doc_request_example
end

#doc_request_paramsObject

Returns the value of attribute doc_request_params.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_request_params
  @doc_request_params
end

#doc_resourceObject

Returns the value of attribute doc_resource.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_resource
  @doc_resource
end

#doc_response_exampleObject

Returns the value of attribute doc_response_example.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_response_example
  @doc_response_example
end

#doc_response_paramsObject

Returns the value of attribute doc_response_params.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_response_params
  @doc_response_params
end

#doc_return_codeObject

Returns the value of attribute doc_return_code.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_return_code
  @doc_return_code
end

#doc_sort_orderObject

Returns the value of attribute doc_sort_order.



6
7
8
# File 'lib/apidoco_dsl/api_doc.rb', line 6

def doc_sort_order
  @doc_sort_order
end

Instance Method Details

#description(txt = nil, path: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/apidoco_dsl/api_doc.rb', line 60

def description(txt = nil, path: nil)
  if path
    txt  = File.read(path)
    erb  = ERB.new(txt).result
    html = Kramdown::Document.new(erb).to_html
    @doc_description = ERB.new(html).result
  else
    @doc_description = txt
  end
end

#doc_fileObject



75
76
77
# File 'lib/apidoco_dsl/api_doc.rb', line 75

def doc_file
  doc_name.gsub(/\s/, '').underscore
end

#doc_folderObject



71
72
73
# File 'lib/apidoco_dsl/api_doc.rb', line 71

def doc_folder
  doc_namespace.split('::').map(&:underscore).map(&:downcase).join('/') + "/#{doc_resource.to_s.underscore}/"
end

#example_request(path: nil) ⇒ Object



46
47
48
49
50
51
# File 'lib/apidoco_dsl/api_doc.rb', line 46

def example_request(path: nil)
  ex = {}
  ex['request'] = yield if block_given?
  ex['request'] = JSON.parse(File.read(path)) if path
  @doc_examples << ex
end

#example_response(path: nil) ⇒ Object



53
54
55
56
57
58
# File 'lib/apidoco_dsl/api_doc.rb', line 53

def example_response(path: nil)
  ex = {}
  ex['response'] = yield if block_given?
  ex['response'] = JSON.parse(File.read(path)) if path
  @doc_examples << ex
end

#param_keyObject



26
27
28
# File 'lib/apidoco_dsl/api_doc.rb', line 26

def param_key
  "Document"
end

#returns(code: 200, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/apidoco_dsl/api_doc.rb', line 30

def returns(code: 200, &block)
  @doc_return_code = translate_return_code(code)
  @param_destination = 'response'
  self.instance_exec(&block) if block_given?
  @param_destination = 'request'
end

#set_attribute(name, value) ⇒ Object



42
43
44
# File 'lib/apidoco_dsl/api_doc.rb', line 42

def set_attribute(name, value)
  self.send(:"doc_#{name}=", value)
end

#to_jsonObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/apidoco_dsl/api_doc.rb', line 79

def to_json
  doc = {}
  doc['published']       = doc_published unless doc_published.nil?
  doc['name']            = doc_name unless doc_name.nil?
  doc['end_point']       = doc_endpoint unless doc_endpoint.nil?
  doc['http_method']     = doc_http_method unless doc_http_method.nil?
  doc['params']          = unroll_parameters(doc_request_params, [])  unless doc_request_params.empty?
  doc['response_params'] = unroll_parameters(doc_response_params, []) unless doc_response_params.empty?
  doc['header']          = doc_header unless doc_header.nil?
  doc['description']     = doc_description unless doc_description.nil?
  doc['examples']        = doc_examples unless doc_examples.empty?
  doc['sort_order']      = doc_sort_order unless doc_sort_order.nil?
  doc['return_code']     = doc_return_code
  doc['markdown']        = doc_markdown

  return JSON.pretty_generate(doc)
end