Class: Jdoc::Link
- Inherits:
-
Object
- Object
- Jdoc::Link
- Defined in:
- lib/jdoc/link.rb
Defined Under Namespace
Classes: ExampleNotFound, RequestGenerator, ResponseGenerator
Instance Method Summary collapse
-
#<=>(schema) ⇒ Fixnum
Responds to .sort method.
-
#anchor ⇒ String
Href anchor for putting link in ToC.
-
#description ⇒ String
Description for this endpoint, defined in description property.
-
#endpoint ⇒ String
Method + path.
-
#has_request_body? ⇒ true, false
True if this endpoint must have request body.
-
#hyperlink ⇒ String
Markdown styled link text for endpoint.
-
#initialize(link: nil) ⇒ Link
constructor
A new instance of Link.
-
#method ⇒ String
Upper cased HTTP request method name.
-
#path ⇒ String
Request path name, defined at href property.
-
#request_body ⇒ String?
Example request body in JSON format.
- #resource ⇒ Json::Link::Resource
-
#response_body ⇒ String
JSON response body generated from example properties.
-
#response_status ⇒ Fixnum
Preferred respone status code for this endpoint.
-
#schema ⇒ JsonSchema::Schema
Schema for this link, specified by targetSchema or parent schema.
-
#sort_key ⇒ String
For #<=> method.
Constructor Details
#initialize(link: nil) ⇒ Link
Returns a new instance of Link.
4 5 6 |
# File 'lib/jdoc/link.rb', line 4 def initialize(link: nil) @raw_link = link end |
Instance Method Details
#<=>(schema) ⇒ Fixnum
Responds to .sort method
17 18 19 |
# File 'lib/jdoc/link.rb', line 17 def <=>(schema) sort_key <=> schema.sort_key end |
#anchor ⇒ String
Returns Href anchor for putting link in ToC.
37 38 39 |
# File 'lib/jdoc/link.rb', line 37 def anchor "#" + endpoint.gsub(" ", "-").gsub(/[:\/]/, "").downcase end |
#description ⇒ String
Returns Description for this endpoint, defined in description property.
30 31 32 |
# File 'lib/jdoc/link.rb', line 30 def description @raw_link.description end |
#endpoint ⇒ String
Returns method + path.
11 12 13 |
# File 'lib/jdoc/link.rb', line 11 def endpoint "#{method} #{path}" end |
#has_request_body? ⇒ true, false
Returns True if this endpoint must have request body.
71 72 73 |
# File 'lib/jdoc/link.rb', line 71 def has_request_body? ["PATCH", "POST", "PUT"].include?(method) end |
#hyperlink ⇒ String
Returns Markdown styled link text for endpoint.
44 45 46 |
# File 'lib/jdoc/link.rb', line 44 def hyperlink "[#{endpoint}](#{anchor})" end |
#method ⇒ String
Returns Upper cased HTTP request method name.
51 52 53 |
# File 'lib/jdoc/link.rb', line 51 def method @method ||= @raw_link.method.to_s.upcase end |
#path ⇒ String
URI Template is replaced with placeholder
Returns Request path name, defined at href property.
59 60 61 62 63 |
# File 'lib/jdoc/link.rb', line 59 def path @path ||= @raw_link.href.gsub(/{(.+)}/) do |matched| ":" + CGI.unescape($1).gsub(/[()]/, "").split("/").last end end |
#request_body ⇒ String?
Returns Example request body in JSON format.
66 67 68 |
# File 'lib/jdoc/link.rb', line 66 def request_body MultiJson.encode(RequestGenerator.call(schema), pretty: true) + "\n" end |
#resource ⇒ Json::Link::Resource
Resource means each property of top-level properties in this context
92 93 94 |
# File 'lib/jdoc/link.rb', line 92 def resource @resource ||= Resource.new(schema) end |
#response_body ⇒ String
Returns JSON response body generated from example properties.
76 77 78 |
# File 'lib/jdoc/link.rb', line 76 def response_body MultiJson.encode(response_hash, pretty: true) end |
#response_status ⇒ Fixnum
Returns Preferred respone status code for this endpoint.
81 82 83 |
# File 'lib/jdoc/link.rb', line 81 def response_status method == "POST" ? 201 : 200 end |
#schema ⇒ JsonSchema::Schema
Returns Schema for this link, specified by targetSchema or parent schema.
86 87 88 |
# File 'lib/jdoc/link.rb', line 86 def schema @raw_link.target_schema || @raw_link.parent end |
#sort_key ⇒ String
For #<=> method
23 24 25 |
# File 'lib/jdoc/link.rb', line 23 def sort_key "#{path} #{method_order_score}" end |