Class: ApidocoDsl::ApiDoc
- Inherits:
-
Object
- Object
- ApidocoDsl::ApiDoc
- 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
-
#doc_description ⇒ Object
Returns the value of attribute doc_description.
-
#doc_endpoint ⇒ Object
Returns the value of attribute doc_endpoint.
-
#doc_examples ⇒ Object
Returns the value of attribute doc_examples.
-
#doc_header ⇒ Object
Returns the value of attribute doc_header.
-
#doc_http_method ⇒ Object
Returns the value of attribute doc_http_method.
-
#doc_markdown ⇒ Object
Returns the value of attribute doc_markdown.
-
#doc_name ⇒ Object
Returns the value of attribute doc_name.
-
#doc_namespace ⇒ Object
Returns the value of attribute doc_namespace.
-
#doc_published ⇒ Object
Returns the value of attribute doc_published.
-
#doc_request_example ⇒ Object
Returns the value of attribute doc_request_example.
-
#doc_request_params ⇒ Object
Returns the value of attribute doc_request_params.
-
#doc_resource ⇒ Object
Returns the value of attribute doc_resource.
-
#doc_response_example ⇒ Object
Returns the value of attribute doc_response_example.
-
#doc_response_params ⇒ Object
Returns the value of attribute doc_response_params.
-
#doc_return_code ⇒ Object
Returns the value of attribute doc_return_code.
-
#doc_sort_order ⇒ Object
Returns the value of attribute doc_sort_order.
Attributes included from Documentable
Instance Method Summary collapse
- #description(txt = nil, path: nil) ⇒ Object
- #doc_file ⇒ Object
- #doc_folder ⇒ Object
- #example_request(path: nil) ⇒ Object
- #example_response(path: nil) ⇒ Object
-
#initialize(api) ⇒ ApiDoc
constructor
A new instance of ApiDoc.
- #method_missing(name, *args) ⇒ Object
- #param_key ⇒ Object
- #returns(code: 200, &block) ⇒ Object
- #set_attribute(name, value) ⇒ Object
- #to_json ⇒ Object
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_description ⇒ Object
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_endpoint ⇒ Object
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_examples ⇒ Object
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_header ⇒ Object
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_method ⇒ Object
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_markdown ⇒ Object
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_name ⇒ Object
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_namespace ⇒ Object
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_published ⇒ Object
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_example ⇒ Object
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_params ⇒ Object
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_resource ⇒ Object
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_example ⇒ Object
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_params ⇒ Object
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_code ⇒ Object
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_order ⇒ Object
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_file ⇒ Object
75 76 77 |
# File 'lib/apidoco_dsl/api_doc.rb', line 75 def doc_file doc_name.gsub(/\s/, '').underscore end |
#doc_folder ⇒ Object
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_key ⇒ Object
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_json ⇒ Object
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 |