Class: Jdoc::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/jdoc/link.rb

Defined Under Namespace

Classes: ExampleNotFound, RequestGenerator, ResponseGenerator

Instance Method Summary collapse

Constructor Details

#initialize(link) ⇒ Link

Returns a new instance of Link.

Parameters:

  • link (JsonSchema::Schema::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

Returns:

  • (Fixnum)


17
18
19
# File 'lib/jdoc/link.rb', line 17

def <=>(schema)
  sort_key <=> schema.sort_key
end

#anchorString

Returns Href anchor for putting link in ToC.

Examples:

link.anchor #=> "#get-apps"

Returns:

  • (String)

    Href anchor for putting link in ToC



37
38
39
# File 'lib/jdoc/link.rb', line 37

def anchor
  "#" + endpoint.gsub(" ", "-").gsub(/[:\/]/, "").downcase
end

#descriptionString

Returns Description for this endpoint, defined in description property.

Examples:

link.description #=> "List existing apps."

Returns:

  • (String)

    Description for this endpoint, defined in description property



30
31
32
# File 'lib/jdoc/link.rb', line 30

def description
  @raw_link.description
end

#endpointString

Returns method + path.

Examples:

link.endpoint #=> "GET /apps"

Returns:

  • (String)

    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.

Returns:

  • (true, false)

    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

Returns Markdown styled link text for endpoint.

Examples:

link.hyperlink #=> "[GET /apps](#get-apps)"

Returns:

  • (String)

    Markdown styled link text for endpoint



44
45
46
# File 'lib/jdoc/link.rb', line 44

def hyperlink
  "[#{endpoint}](#{anchor})"
end

#methodString

Returns Upper cased HTTP request method name.

Examples:

link.method #=> "GET"

Returns:

  • (String)

    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

#pathString

Note:

URI Template is replaced with placeholder

Returns Request path name, defined at href property.

Examples:

link.path #=> "GET /apps/:id"

Returns:

  • (String)

    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

#request_bodyString?

Returns Example request body in JSON format.

Returns:

  • (String, nil)

    Example request body in JSON format



66
67
68
# File 'lib/jdoc/link.rb', line 66

def request_body
  JSON.pretty_generate(RequestGenerator.call(request_schema.properties)) + "\n"
end

#request_schemaJsonSchema::Schema

Returns Request schema for this link.

Returns:

  • (JsonSchema::Schema)

    Request schema for this link



92
93
94
# File 'lib/jdoc/link.rb', line 92

def request_schema
  @raw_link.schema || @raw_link.parent
end

#resourceJson::Link::Resource

Note:

Resource means each property of top-level properties in this context

Returns:

  • (Json::Link::Resource)


98
99
100
# File 'lib/jdoc/link.rb', line 98

def resource
  @resource ||= Resource.new(response_schema)
end

#response_bodyString

Returns JSON response body generated from example properties.

Returns:

  • (String)

    JSON response body generated from example properties



76
77
78
79
# File 'lib/jdoc/link.rb', line 76

def response_body
  object = has_list_data? ? [response_hash] : response_hash
  JSON.pretty_generate(object)
end

#response_schemaJsonSchema::Schema

Returns Response schema for this link.

Returns:

  • (JsonSchema::Schema)

    Response schema for this link



87
88
89
# File 'lib/jdoc/link.rb', line 87

def response_schema
  @raw_link.target_schema || @raw_link.parent
end

#response_statusFixnum

Returns Preferred respone status code for this endpoint.

Returns:

  • (Fixnum)

    Preferred respone status code for this endpoint



82
83
84
# File 'lib/jdoc/link.rb', line 82

def response_status
  method == "POST" ? 201 : 200
end

#sort_keyString

For #<=> method

Returns:

  • (String)


23
24
25
# File 'lib/jdoc/link.rb', line 23

def sort_key
  "#{path} #{method_order_score}"
end