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
#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.
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
103
104
105
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 103
def base_path
zws_ify(@endpoint.service.base_path)
end
|
#deprecated? ⇒ Boolean
99
100
101
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 99
def deprecated?
@endpoint.deprecated?
end
|
#example_from_array(array, parent = nil) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 204
def example_from_array(array, parent=nil)
if array["items"].kind_of? Array
example = []
array["items"].each do |item|
example << example_from_schema(item, parent)
end
example
elsif (array["items"] || {})["type"].kind_of? 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 177
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
194
195
196
197
198
199
200
201
202
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 194
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 155
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
74
75
76
77
78
79
80
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 74
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 82
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
if defined? ExecJS
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")
elsif defined? CodeRay
@example_response = ::CodeRay.scan(@example_response, :json).html(wrap: nil, css: :class)
end
@example_response
end
|
#failure_response_codes ⇒ Object
70
71
72
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 70
def failure_response_codes
response_codes.reject(&:successful?)
end
|
133
134
135
136
137
138
139
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 133
def form_verb
if endpoint.verb == 'GET'
'GET'
else
'POST'
end
end
|
#named_path ⇒ Object
for live form WITH :placeholders
120
121
122
123
124
125
126
127
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 120
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
109
110
111
112
113
114
115
116
117
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 109
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
35
36
37
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 35
def prefix
endpoint.prefix || endpoint.path.split("/").first
end
|
#relative_path(extension = ".html") ⇒ Object
23
24
25
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 23
def relative_path(extension = ".html")
'%s%s-%s%s' % [ options[:prefix], endpoint.path, endpoint.verb, extension ]
end
|
#request_parameters ⇒ Object
49
50
51
52
53
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 49
def request_parameters
Lurker::SchemaPresenter.new(endpoint.request_parameters,
options.merge(request: true, root_path: root_path)
)
end
|
#response_codes ⇒ Object
60
61
62
63
64
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 60
def response_codes
@response_codes ||= endpoint.response_codes.map do |response_code|
Lurker::ResponseCodePresenter.new(response_code, options)
end
end
|
#response_parameters ⇒ Object
55
56
57
58
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 55
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
45
46
47
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 45
def root_path
URI.parse("file://#{endpoint.endpoint_path}")
end
|
#successful_response_codes ⇒ Object
66
67
68
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 66
def successful_response_codes
response_codes.select(&:successful?)
end
|
#title ⇒ Object
31
32
33
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 31
def title
'%s %s - %s' % [ endpoint.verb, endpoint.path, endpoint.service.name ]
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={})
@service_presenter = service_presenter
@endpoint_presenter = self
@url_params = endpoint.url_params
@post_params = example_request.json
render('show', options)
end
|
#url(extension = ".html") ⇒ Object
27
28
29
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 27
def url(extension = ".html")
Pathname.new(html_directory).join(relative_path(extension)).to_s
end
|
#verb ⇒ Object
129
130
131
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 129
def verb
endpoint.verb
end
|
#verb_colorname ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 141
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
39
40
41
42
43
|
# File 'lib/lurker/presenters/endpoint_presenter.rb', line 39
def zws_ify(str)
str
end
|