Class: Lurker::EndpointPresenter
Overview
BasePresenter for an Endpoint
Constant Summary
collapse
- ATOMIC_TYPES =
%w(string integer number boolean null)
Instance Attribute Summary collapse
#options
Instance Method Summary
collapse
#css_path, #get_binding, #html_directory, #index_path, #render, #render_erb, #tag_with_anchor, #url_base_path
Constructor Details
#initialize(endpoint, options = {}) ⇒ EndpointPresenter
Returns a new instance of EndpointPresenter.
5
6
7
8
9
10
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 5
def initialize(endpoint, options = {})
super(options)
@endpoint = endpoint
@endpoint_presenter = self
@service_presenter = Lurker::ServicePresenter.new(endpoint.service)
end
|
Instance Attribute Details
#endpoint ⇒ Object
Returns the value of attribute endpoint.
3
4
5
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 3
def endpoint
@endpoint
end
|
#endpoint_presenter ⇒ Object
Returns the value of attribute endpoint_presenter.
3
4
5
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 3
def endpoint_presenter
@endpoint_presenter
end
|
#service_presenter ⇒ Object
Returns the value of attribute service_presenter.
3
4
5
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 3
def service_presenter
@service_presenter
end
|
Instance Method Details
#base_path ⇒ Object
111
112
113
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 111
def base_path
zws_ify(@endpoint.service.base_path)
end
|
#deprecated? ⇒ Boolean
107
108
109
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 107
def deprecated?
@endpoint.deprecated?
end
|
#description ⇒ Object
43
44
45
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 43
def description
description_parts[1..-1].join("\n")
end
|
#example_from_array(array, parent = nil) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 212
def example_from_array(array, parent=nil)
if array["items"].is_a? Array
example = []
array["items"].each do |item|
example << example_from_schema(item, parent)
end
example
elsif (array["items"] || {})["type"].is_a? Array
example = []
array["items"]["type"].each do |item|
example << example_from_schema(item, parent)
end
example
else
[example_from_schema(array["items"], parent)]
end
end
|
#example_from_atom(schema, parent = nil) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 185
def example_from_atom(schema, parent=nil)
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, parent = nil) ⇒ Object
202
203
204
205
206
207
208
209
210
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 202
def example_from_object(object, parent=nil)
example = {}
if object["properties"]
object["properties"].each do |key, value|
example[key] = example_from_schema(value, parent)
end
end
example
end
|
#example_from_schema(schema, parent = nil) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 163
def example_from_schema(schema, parent=nil)
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, parent)
elsif type.include?("object") || schema["properties"]
example_from_object(schema, parent)
elsif type.include?("array") || schema["items"]
example_from_array(schema, parent)
elsif (ref_path = schema['$ref'])
root_path = parent.respond_to?(:abs_path) ? parent.abs_path : send(:root_path)
ref_schema = Lurker::RefObject.new(ref_path, root_path)
example_from_object(ref_schema.schema, ref_schema)
else
{}
end
end
|
#example_request ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 76
def example_request
return if endpoint.request_parameters.empty?
Lurker::JsonPresenter.new(
example_from_schema(endpoint.request_parameters, endpoint.schema)
.reject { |k, _| endpoint.url_params.keys.include? k }
)
end
|
#example_response ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 84
def example_response
return @example_response if @example_response
return if endpoint.response_parameters.empty?
response = example_from_schema(endpoint.response_parameters, endpoint.schema)
@example_response = response.to_json
@highlighted = false
Lurker.safe_require("execjs", "to get samples highlighted") do
jsfile = File.expand_path('javascripts/highlight.pack.js', Lurker::Cli.source_root)
source = open(jsfile).read
context = ExecJS.compile(source)
@example_response = context.exec("return hljs.highlightAuto(JSON.stringify(#{@example_response}, null, 2)).value")
@highlighted = true
end
unless @highlighted
Lurker.safe_require("coderay", "to get samples highlighted") do
@example_response = ::CodeRay.scan(@example_response, :json).html(wrap: nil, css: :class)
@highlighted = true
end
end
@example_response
end
|
#failure_response_codes ⇒ Object
72
73
74
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 72
def failure_response_codes
response_codes.reject(&:successful?)
end
|
141
142
143
144
145
146
147
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 141
def form_verb
if endpoint.verb == 'GET'
'GET'
else
'POST'
end
end
|
#named_path ⇒ Object
for live form WITH :placeholders
128
129
130
131
132
133
134
135
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 128
def named_path
return @named_path if @named_path
@named_path = base_path.sub(/\/?$/, '/') + endpoint.path.gsub(/__/, ':')
if (suffix = endpoint.schema['extensions'].try(:[], 'suffix')).present?
@named_path = @named_path.gsub(/-#{suffix}/, '')
end
@named_path
end
|
#path ⇒ Object
for live form WITH values TODO: remove in favor of named_path
117
118
119
120
121
122
123
124
125
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 117
def path
return @path if @path
unless (@path = @endpoint.schema['extensions'].try(:[], 'path_info')).present?
@path = @endpoint.path.gsub(/__/, ':')
@path = @path.gsub(/-#{@end}/) if @endpoint.schema.extensions.try(:[], 'suffix').present?
end
@path = '/' + @path.split('/').select(&:present?).join('/')
@path
end
|
#prefix ⇒ Object
33
34
35
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 33
def prefix
endpoint.prefix || endpoint.path.split("/").first
end
|
#relative_path(extension = ".html") ⇒ Object
21
22
23
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 21
def relative_path(extension = ".html")
'%s%s-%s%s' % [options[:prefix], endpoint.path, endpoint.verb, extension]
end
|
#request_parameters ⇒ Object
51
52
53
54
55
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 51
def request_parameters
Lurker::SchemaPresenter.new(endpoint.request_parameters,
options.merge(request: true, root_path: root_path)
)
end
|
#response_codes ⇒ Object
62
63
64
65
66
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 62
def response_codes
@response_codes ||= endpoint.response_codes.map do |response_code|
Lurker::ResponseCodePresenter.new(response_code, options)
end
end
|
#response_parameters ⇒ Object
57
58
59
60
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 57
def response_parameters
return if endpoint.response_parameters.empty?
Lurker::SchemaPresenter.new(endpoint.response_parameters, options.merge(root_path: root_path))
end
|
#root_path ⇒ Object
47
48
49
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 47
def root_path
URI.parse("file://#{endpoint.endpoint_path}")
end
|
#successful_response_codes ⇒ Object
68
69
70
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 68
def successful_response_codes
response_codes.select(&:successful?)
end
|
#title ⇒ Object
29
30
31
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 29
def title
description_parts.first.to_s
end
|
#to_html(options = {}) ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 12
def to_html(options={})
@service_presenter = service_presenter
@endpoint_presenter = self
@url_params = endpoint.url_params
@post_params = example_request.json
@title = "#{service_presenter.title} | #{title}"
render('show', options)
end
|
#url(extension = ".html") ⇒ Object
25
26
27
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 25
def url(extension = ".html")
Pathname.new(html_directory).join(relative_path(extension)).to_s
end
|
#verb ⇒ Object
137
138
139
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 137
def verb
endpoint.verb
end
|
#verb_colorname ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 149
def verb_colorname
case endpoint.verb
when 'GET' then 'success'
when 'POST' then 'primary'
when 'PUT' then 'warning'
when 'PATCH' then 'warning'
when 'DELETE' then 'danger'
else
'default'
end
end
|
#zws_ify(str) ⇒ Object
37
38
39
40
41
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 37
def zws_ify(str)
str
end
|