Class: Fdoc::EndpointPresenter

Inherits:
BasePresenter show all
Defined in:
lib/fdoc/presenters/endpoint_presenter.rb

Overview

BasePresenter for an Endpoint

Constant Summary collapse

ATOMIC_TYPES =
%w(string integer number boolean null)

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#options

Instance Method Summary collapse

Methods inherited from BasePresenter

#css_path, #get_binding, #html_directory, #index_path, #render_erb, #render_markdown, #tag_with_anchor

Constructor Details

#initialize(endpoint, options = {}) ⇒ EndpointPresenter

Returns a new instance of EndpointPresenter.



5
6
7
8
9
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 5

def initialize(endpoint, options = {})
  super(options)
  @endpoint = endpoint
  @endpoint_presenter = self
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



3
4
5
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 3

def endpoint
  @endpoint
end

#endpoint_presenterObject

Returns the value of attribute endpoint_presenter.



3
4
5
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 3

def endpoint_presenter
  @endpoint_presenter
end

#service_presenterObject

Returns the value of attribute service_presenter.



3
4
5
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 3

def service_presenter
  @service_presenter
end

Instance Method Details

#base_pathObject



84
85
86
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 84

def base_path
  zws_ify(@endpoint.service.base_path)
end

#deprecated?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 80

def deprecated?
  @endpoint.deprecated?
end

#descriptionObject



36
37
38
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 36

def description
  render_markdown(endpoint.description)
end

#example_from_array(array) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object



129
130
131
132
133
134
135
136
137
# 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) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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_requestObject



72
73
74
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 72

def example_request
  Fdoc::JsonPresenter.new(example_from_schema(endpoint.request_parameters))
end

#example_responseObject



76
77
78
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 76

def example_response
  Fdoc::JsonPresenter.new(example_from_schema(endpoint.response_parameters))
end

#failure_response_codesObject



68
69
70
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 68

def failure_response_codes
  response_codes.select { |response_code| !response_code.successful? }
end

#pathObject



88
89
90
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 88

def path
  zws_ify(@endpoint.path)
end

#prefixObject



27
28
29
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 27

def prefix
  endpoint.path.split("/").first
end

#request_parametersObject



48
49
50
51
52
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 48

def request_parameters
  Fdoc::SchemaPresenter.new(endpoint.request_parameters,
    options.merge(:request => true)
  )
end

#response_codesObject



58
59
60
61
62
# 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_parametersObject



54
55
56
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 54

def response_parameters
  Fdoc::SchemaPresenter.new(endpoint.response_parameters, options)
end

#show_request?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 40

def show_request?
  !endpoint.request_parameters.empty?
end

#show_response?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 44

def show_response?
  !endpoint.response_parameters.empty?
end

#successful_response_codesObject



64
65
66
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 64

def successful_response_codes
  response_codes.select { |response_code| response_code.successful? }
end

#titleObject



23
24
25
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 23

def title
  '%s %s - %s' % [ endpoint.verb, endpoint.path, endpoint.service.name ]
end

#to_htmlObject



11
12
13
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 11

def to_html
  render_erb('endpoint.html.erb')
end

#to_markdownObject



15
16
17
# File 'lib/fdoc/presenters/endpoint_presenter.rb', line 15

def to_markdown
  render_erb('endpoint.md.erb')
end

#url(extension = ".html") ⇒ Object



19
20
21
# 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) ⇒ Object



31
32
33
34
# 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