Class: Lurker::EndpointPresenter
- Inherits:
-
BasePresenter
show all
- Extended by:
- Forwardable
- Defined in:
- lib/lurker/presenters/endpoint_presenter.rb
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
#asset_path, #assets, #html_directory, #index_path, #markup, #tag_with_anchor, #url_base_path
Constructor Details
#initialize(endpoint, options = {}) ⇒ EndpointPresenter
Returns a new instance of EndpointPresenter.
8
9
10
11
12
13
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 8
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
121
122
123
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 121
def base_path
zws_ify(@endpoint.service.base_path)
end
|
#deprecated? ⇒ Boolean
117
118
119
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 117
def deprecated?
@endpoint.deprecated?
end
|
#description ⇒ Object
53
54
55
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 53
def description
description_parts[1..-1].join("\n")
end
|
#documentation ⇒ Object
23
24
25
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 23
def documentation
markup @endpoint.documentation
end
|
#example_from_array(array, parent = nil) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 222
def example_from_array(array, parent=nil)
if array["items"].respond_to?(:each) && !array["items"].respond_to?(:each_pair)
example = []
array["items"].each do |item|
example << example_from_schema(item, parent)
end
example
elsif (types = (array["items"] || {})["type"]).respond_to?(:each)
example = []
types.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 195
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
212
213
214
215
216
217
218
219
220
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 212
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 173
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
86
87
88
89
90
91
92
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 86
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 94
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
82
83
84
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 82
def failure_response_codes
response_codes.reject(&:successful?)
end
|
151
152
153
154
155
156
157
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 151
def form_verb
if endpoint.verb == 'GET'
'GET'
else
'POST'
end
end
|
#named_path ⇒ Object
for live form WITH :placeholders
138
139
140
141
142
143
144
145
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 138
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
127
128
129
130
131
132
133
134
135
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 127
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
|
#post_params ⇒ Object
43
44
45
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 43
def post_params
example_request.json
end
|
#prefix ⇒ Object
39
40
41
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 39
def prefix
endpoint.prefix || endpoint.path.split("/").first
end
|
#relative_path(extension = ".html") ⇒ Object
27
28
29
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 27
def relative_path(extension = ".html")
'%s%s-%s%s' % [options[:prefix], endpoint.path, endpoint.verb, extension]
end
|
#request_parameters ⇒ Object
61
62
63
64
65
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 61
def request_parameters
Lurker::SchemaPresenter.new(endpoint.request_parameters,
options.merge(request: true, root_path: root_path)
)
end
|
#response_codes ⇒ Object
72
73
74
75
76
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 72
def response_codes
@response_codes ||= endpoint.response_codes.map do |response_code|
Lurker::ResponseCodePresenter.new(response_code, options)
end
end
|
#response_parameters ⇒ Object
67
68
69
70
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 67
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
57
58
59
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 57
def root_path
URI.parse("file://#{endpoint.endpoint_path}")
end
|
#successful_response_codes ⇒ Object
78
79
80
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 78
def successful_response_codes
response_codes.select(&:successful?)
end
|
#title ⇒ Object
35
36
37
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 35
def title
description_parts.first.to_s
end
|
#to_html(options = {}) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 15
def to_html(options={})
controller = Lurker::RenderingController.new
controller.service_presenter = service_presenter
controller.endpoint_presenter = self
controller.instance_variable_set :@title, "#{service_presenter.title} | #{title}"
controller.render_to_string 'show', options
end
|
#url(extension = ".html") ⇒ Object
31
32
33
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 31
def url(extension = ".html")
Pathname.new(html_directory).join(relative_path(extension)).to_s
end
|
#verb ⇒ Object
147
148
149
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 147
def verb
endpoint.verb
end
|
#verb_colorname ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 159
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
47
48
49
50
51
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 47
def zws_ify(str)
str
end
|