Class: Fdoc::EndpointPresenter

Inherits:
HtmlPresenter show all
Defined in:
lib/fdoc/presenters/endpoint_presenter.rb

Overview

HtmlPresenter for an Endpoint

Constant Summary collapse

ATOMIC_TYPES =
%w(string integer number boolean null)

Instance Attribute Summary collapse

Attributes inherited from HtmlPresenter

#options

Instance Method Summary collapse

Methods inherited from HtmlPresenter

#css_path, #html_directory, #index_path, #render_erb, #render_json, #render_markdown, #tag_with_anchor

Constructor Details

#initialize(endpoint, options = {}) ⇒ EndpointPresenter



5
6
7
8
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 5

def initialize(endpoint, options = {})
  super(options)
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



3
4
5
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 3

def endpoint
  @endpoint
end

#service_presenterObject

Returns the value of attribute service_presenter.



3
4
5
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 3

def service_presenter
  @service_presenter
end

Instance Method Details

#descriptionObject



50
51
52
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 50

def description
  render_markdown(endpoint.description)
end

#example_from_array(array) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 141

def example_from_array(array)
  if array["items"].kind_of? Array
    example = []
    array["items"].each do |item|
      example << example_from_schema(item)
    end
    example
  elsif (array["items"] || {})["type"].kind_of? Array
    example = []
    array["items"]["type"].each do |item|
      example << example_from_schema(item)
    end
    example
  else
    [ example_from_schema(array["items"]) ]
  end
end

#example_from_atom(schema) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 114

def example_from_atom(schema)
  type = Array(schema["type"])
  hash = schema.hash

  if type.include?("boolean")
    [true, false][hash % 2]
  elsif type.include?("integer")
    hash % 1000
  elsif type.include?("number")
    Math.sqrt(hash % 1000).round 2
  elsif type.include?("string")
    ""
  else
    nil
  end
end

#example_from_object(object) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 131

def example_from_object(object)
  example = {}
  if object["properties"]
    object["properties"].each do |key, value|
      example[key] = example_from_schema(value)
    end
  end
  example
end

#example_from_schema(schema) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 96

def example_from_schema(schema)
  if schema.nil?
    return nil
  end

  type = Array(schema["type"])

  if type.any? { |t| ATOMIC_TYPES.include?(t) }
    schema["example"] || schema["default"] || example_from_atom(schema)
  elsif type.include?("object") || schema["properties"]
    example_from_object(schema)
  elsif type.include?("array") || schema["items"]
    example_from_array(schema)
  else
    {}
  end
end

#example_requestObject



86
87
88
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 86

def example_request
  render_json(example_from_schema(endpoint.request_parameters))
end

#example_responseObject



90
91
92
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 90

def example_response
  render_json(example_from_schema(endpoint.response_parameters))
end

#failure_response_codesObject



82
83
84
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 82

def failure_response_codes
  response_codes.select { |response_code| !response_code.successful? }
end

#nameObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 14

def name
  "  <span class=\"endpoint-name \#{@endpoint.deprecated? ? 'deprecated' : nil}\">\n    <span class=\"verb\">\#{@endpoint.verb}</span>\n    <span class=\"root\">\#{zws_ify(@endpoint.service.base_path)}</span><span\n     class=\"path\">\#{zws_ify(@endpoint.path)}</span>\n    \#{@endpoint.deprecated? ? '(deprecated)' : nil}\n  </span>\n  EOS\nend\n"


25
26
27
28
29
30
31
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 25

def name_as_link
  "  <a href=\"\#{url}\">\n    \#{name}\n  </a>\n  EOS\nend\n"

#prefixObject



41
42
43
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 41

def prefix
  endpoint.path.split("/").first
end

#request_parametersObject



62
63
64
65
66
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 62

def request_parameters
  Fdoc::SchemaPresenter.new(endpoint.request_parameters,
    options.merge(:request => true)
  ).to_html
end

#response_codesObject



72
73
74
75
76
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 72

def response_codes
  @response_codes ||= endpoint.response_codes.map do |response_code|
    Fdoc::ResponseCodePresenter.new(response_code, options)
  end
end

#response_parametersObject



68
69
70
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 68

def response_parameters
  Fdoc::SchemaPresenter.new(endpoint.response_parameters, options).to_html
end

#show_request?Boolean



54
55
56
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 54

def show_request?
  !endpoint.request_parameters.empty?
end

#show_response?Boolean



58
59
60
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 58

def show_response?
  !endpoint.response_parameters.empty?
end

#successful_response_codesObject



78
79
80
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 78

def successful_response_codes
  response_codes.select { |response_code| response_code.successful? }
end

#titleObject



37
38
39
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 37

def title
  '%s %s - %s' % [ endpoint.verb, endpoint.path, endpoint.service.name ]
end

#to_htmlObject



10
11
12
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 10

def to_html
  render_erb('endpoint.html.erb')
end

#url(extension = ".html") ⇒ Object



33
34
35
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 33

def url(extension = ".html")
  '%s%s-%s%s' % [ options[:prefix], endpoint.path, endpoint.verb, extension ]
end

#zws_ify(str) ⇒ Object



45
46
47
48
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 45

def zws_ify(str)
  # zero-width-space, makes long lines friendlier for breaking
  str.gsub(/\//, '&#8203;/') if str
end