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.
-
#content_type ⇒ String
Request content type.
-
#content_type_json? ⇒ true, false
True if encType of request is multipart/form-data.
-
#content_type_multipart? ⇒ true, false
True if encType of request is multipart/form-data.
-
#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) ⇒ Link
constructor
A new instance of Link.
-
#method ⇒ String
Upper cased HTTP request method name.
-
#path ⇒ String
Request path name, defined at href property.
-
#query_string ⇒ String?
Adds query string if a link has a schema property and method is GET.
-
#request_body ⇒ String?
Example request body in JSON format.
-
#request_parameters ⇒ Hash
Example request parameters for this endpoint.
-
#request_schema ⇒ JsonSchema::Schema
Request schema for this link.
- #resource ⇒ Json::Link::Resource
-
#response_body ⇒ String
JSON response body generated from example properties.
-
#response_schema ⇒ JsonSchema::Schema
Response schema for this link.
-
#response_status ⇒ Fixnum
Preferred respone status code for this endpoint.
-
#sort_key ⇒ String
For #<=> method.
Constructor Details
#initialize(link) ⇒ Link
Returns a new instance of Link.
4 5 6 |
# File 'lib/jdoc/link.rb', line 4 def initialize(link) @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 |
#content_type ⇒ String
default value is “application/json”
Returns request content type.
67 68 69 70 71 |
# File 'lib/jdoc/link.rb', line 67 def content_type type = @raw_link.enc_type type += "; #{Request::Multipart.boundary}" if content_type_multipart? type end |
#content_type_json? ⇒ true, false
Returns True if encType of request is multipart/form-data.
102 103 104 |
# File 'lib/jdoc/link.rb', line 102 def content_type_json? Rack::Mime.match?(@raw_link.enc_type, "application/json") end |
#content_type_multipart? ⇒ true, false
Returns True if encType of request is multipart/form-data.
97 98 99 |
# File 'lib/jdoc/link.rb', line 97 def content_type_multipart? Rack::Mime.match?(@raw_link.enc_type, "multipart/form-data") 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.
118 119 120 |
# File 'lib/jdoc/link.rb', line 118 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(/[()\s]/, "").split("/").last end end |
#query_string ⇒ String?
Adds query string if a link has a schema property and method is GET
77 78 79 80 81 |
# File 'lib/jdoc/link.rb', line 77 def query_string if method == "GET" && !request_parameters.empty? "?#{request_parameters.to_query}" end end |
#request_body ⇒ String?
Returns Example request body in JSON format.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jdoc/link.rb', line 84 def request_body body = case when content_type_multipart? request_body_in_multipart when content_type_json? request_body_in_json else "" end body + "\n" end |
#request_parameters ⇒ Hash
Returns Example request parameters for this endpoint.
107 108 109 110 111 112 113 114 115 |
# File 'lib/jdoc/link.rb', line 107 def request_parameters @request_parameters ||= begin if has_schema_in_link? RequestGenerator.call(request_schema.properties) else {} end end end |
#request_schema ⇒ JsonSchema::Schema
Returns Request schema for this link.
139 140 141 |
# File 'lib/jdoc/link.rb', line 139 def request_schema @raw_link.schema || @raw_link.parent end |
#resource ⇒ Json::Link::Resource
Resource means each property of top-level properties in this context
145 146 147 |
# File 'lib/jdoc/link.rb', line 145 def resource @resource ||= Resource.new(response_schema) end |
#response_body ⇒ String
Returns JSON response body generated from example properties.
123 124 125 126 |
# File 'lib/jdoc/link.rb', line 123 def response_body object = has_list_data? ? [response_hash] : response_hash JSON.pretty_generate(object) end |
#response_schema ⇒ JsonSchema::Schema
Returns Response schema for this link.
134 135 136 |
# File 'lib/jdoc/link.rb', line 134 def response_schema @raw_link.target_schema || @raw_link.parent end |
#response_status ⇒ Fixnum
Returns Preferred respone status code for this endpoint.
129 130 131 |
# File 'lib/jdoc/link.rb', line 129 def response_status method == "POST" ? 201 : 200 end |
#sort_key ⇒ String
For #<=> method
23 24 25 |
# File 'lib/jdoc/link.rb', line 23 def sort_key "#{path} #{method_order_score}" end |