class Fdoc::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 Fdoc::BasePresenter::new
# File lib/fdoc/presenters/endpoint_presenter.rb, line 5
def initialize(endpoint, options = {})
  super(options)
  @endpoint = endpoint
  @endpoint_presenter = self
end

Public Instance Methods

base_path() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 84
def base_path
  zws_ify(@endpoint.service.base_path)
end
deprecated?() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 80
def deprecated?
  @endpoint.deprecated?
end
description() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 36
def description
  render_markdown(endpoint.description)
end
example_from_array(array) click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 139
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) click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 112
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) click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 129
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) click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 94
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_request() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 72
def example_request
  Fdoc::JsonPresenter.new(example_from_schema(endpoint.request_parameters))
end
example_response() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 76
def example_response
  Fdoc::JsonPresenter.new(example_from_schema(endpoint.response_parameters))
end
failure_response_codes() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 68
def failure_response_codes
  response_codes.select { |response_code| !response_code.successful? }
end
path() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 88
def path
  zws_ify(@endpoint.path)
end
prefix() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 27
def prefix
  endpoint.path.split("/").first
end
request_parameters() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 48
def request_parameters
  Fdoc::SchemaPresenter.new(endpoint.request_parameters,
    options.merge(:request => true)
  )
end
response_codes() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 58
def response_codes
  @response_codes ||= endpoint.response_codes.map do |response_code|
    Fdoc::ResponseCodePresenter.new(response_code, options)
  end
end
response_parameters() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 54
def response_parameters
  Fdoc::SchemaPresenter.new(endpoint.response_parameters, options)
end
show_request?() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 40
def show_request?
  !endpoint.request_parameters.empty?
end
show_response?() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 44
def show_response?
  !endpoint.response_parameters.empty?
end
successful_response_codes() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 64
def successful_response_codes
  response_codes.select { |response_code| response_code.successful? }
end
title() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 23
def title
  '%s %s - %s' % [ endpoint.verb, endpoint.path, endpoint.service.name ]
end
to_html() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 11
def to_html
  render_erb('endpoint.html.erb')
end
to_markdown() click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 15
def to_markdown
  render_erb('endpoint.md.erb')
end
url(extension = ".html") click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 19
def url(extension = ".html")
  '%s%s-%s%s' % [ options[:prefix], endpoint.path, endpoint.verb, extension ]
end
zws_ify(str) click to toggle source
# File lib/fdoc/presenters/endpoint_presenter.rb, line 31
def zws_ify(str)
  # zero-width-space, makes long lines friendlier for breaking
  str.gsub(/\//, '&#8203;/') if str
end