class Lurker::EndpointPresenter
BasePresenter for an Endpoint
Constants
- ATOMIC_TYPES
Attributes
endpoint[RW]
endpoint_presenter[RW]
service_presenter[RW]
Public Class Methods
new(endpoint, options = {})
click to toggle source
Calls superclass method
Lurker::BasePresenter::new
# 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
Public Instance Methods
base_path()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 121 def base_path zws_ify(@endpoint.service.base_path) end
deprecated?()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 117 def deprecated? @endpoint.deprecated? end
description()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 53 def description description_parts[1..-1].join("\n") end
documentation()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 23 def documentation markup @endpoint.documentation end
example_from_array(array, parent=nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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 #::CodeRay.scan(response.to_json, :jjson).html(wrap: nil, css: :class) # forked compatible version @example_response = ::CodeRay.scan(@example_response, :json).html(wrap: nil, css: :class) @highlighted = true end end @example_response end
failure_response_codes()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 82 def failure_response_codes response_codes.reject(&:successful?) end
form_verb()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 151 def form_verb if endpoint.verb == 'GET' 'GET' else 'POST' end end
named_path()
click to toggle source
for live form WITH :placeholders
# 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()
click to toggle source
for live form WITH values TODO: remove in favor of named_path
# 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()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 43 def post_params example_request.json end
prefix()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 39 def prefix endpoint.prefix || endpoint.path.split("/").first end
relative_path(extension = ".html")
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 57 def root_path URI.parse("file://#{endpoint.endpoint_path}") end
successful_response_codes()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 78 def successful_response_codes response_codes.select(&:successful?) end
title()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 35 def title description_parts.first.to_s end
to_html(options={})
click to toggle source
# 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")
click to toggle source
# 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()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 147 def verb endpoint.verb end
verb_colorname()
click to toggle source
# 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)
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 47 def zws_ify(str) # zero-width-space, makes long lines friendlier for breaking # str.gsub(/\//, '​/') if str str end
Private Instance Methods
description_parts()
click to toggle source
# File lib/lurker/presenters/endpoint_presenter.rb, line 242 def description_parts endpoint.description.to_s.strip.split(/\n+/) end