Class: HaveAPI::ClientExample

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/client_example.rb,
lib/haveapi/documentation_schema.rb,
lib/haveapi/documentation_clients.rb,
lib/haveapi/documentation_context.rb

Overview

All client example classes should inherit this class. Depending on the client, the subclass may choose to implement either method example or request and response. example should be implemented if the client example shows only the request or the request and response should be coupled together.

Methods example, request and response take one argument, the example to describe.

Defined Under Namespace

Classes: Context, Profile, Profiles, UnsupportedAuthentication

Constant Summary collapse

SIMPLE_DETAIL_LEVELS =
[:simple].freeze
DEFAULT_DETAIL_LEVELS =
%i[simple thorough].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, base_url, version, *args) ⇒ ClientExample

Returns a new instance of ClientExample.



154
155
156
157
158
159
# File 'lib/haveapi/client_example.rb', line 154

def initialize(host, base_url, version, *args)
  @host = host
  @base_url = base_url
  @version = version
  @resource_path, @resource, @action_name, @action = args
end

Instance Attribute Details

#action ⇒ Object (readonly)

Returns the value of attribute action.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def action
  @action
end

#action_name ⇒ Object (readonly)

Returns the value of attribute action_name.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def action_name
  @action_name
end

#base_url ⇒ Object (readonly)

Returns the value of attribute base_url.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def base_url
  @base_url
end

#context ⇒ Object (readonly)

Returns the value of attribute context.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def context
  @context
end

#host ⇒ Object (readonly)

Returns the value of attribute host.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def host
  @host
end

#resource ⇒ Object (readonly)

Returns the value of attribute resource.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def resource
  @resource
end

#resource_path ⇒ Object (readonly)

Returns the value of attribute resource_path.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def resource_path
  @resource_path
end

#version ⇒ Object (readonly)

Returns the value of attribute version.



152
153
154
# File 'lib/haveapi/client_example.rb', line 152

def version
  @version
end

Class Method Details

.adapter_detail_levels_for(adapter:, kind:, context:, result:, mode:, auth_method: nil, auth_description: nil) ⇒ Object

Older, explicitly pinned generator adapters only provide resolve/render. Their unknown detail semantics retain this wrapper's declaration.



82
83
84
85
86
# File 'lib/haveapi/client_example.rb', line 82

def adapter_detail_levels_for(adapter:, kind:, context:, result:, mode:, auth_method: nil, auth_description: nil)
  return detail_levels unless adapter.respond_to?(:detail_levels_for)

  validated_detail_levels(adapter.detail_levels_for(kind:, context:, result:, mode:, auth_method:, auth_description:))
end

.auth(*args) ⇒ Object

Shortcut to #auth



127
128
129
# File 'lib/haveapi/client_example.rb', line 127

def auth(*args)
  new(*args[0..-3]).auth(*args[-2..])
end

.available?(_context) ⇒ Boolean

Returns:



48
49
50
# File 'lib/haveapi/client_example.rb', line 48

def available?(_context)
  true
end

.clients ⇒ Array<ClientExample>

Returns sorted array of classes.

Returns:



137
138
139
# File 'lib/haveapi/client_example.rb', line 137

def clients
  @clients.sort { |a, b| a.order <=> b.order }
end

.code(v = nil) ⇒ Object

Code name is passed to the syntax highligher.



28
29
30
31
# File 'lib/haveapi/client_example.rb', line 28

def code(v = nil)
  @code = v if v
  @code
end

.detail_levels(*values) ⇒ Object



67
68
69
70
71
72
# File 'lib/haveapi/client_example.rb', line 67

def detail_levels(*values)
  @detail_levels = validated_detail_levels(values) unless values.empty?
  return @detail_levels if instance_variable_defined?(:@detail_levels)

  superclass.respond_to?(:detail_levels) ? superclass.detail_levels : DEFAULT_DETAIL_LEVELS
end

.detail_levels_for(_kind, context:, result:, auth_method: nil, auth_description: nil) ⇒ Object

Custom renderers retain their declaration, including for limitations. Built-ins refine this using the already-rendered Simple result.



76
77
78
# File 'lib/haveapi/client_example.rb', line 76

def detail_levels_for(_kind, context:, result:, auth_method: nil, auth_description: nil)
  detail_levels
end

.example(*args) ⇒ Object

Shortcut to example



132
133
134
# File 'lib/haveapi/client_example.rb', line 132

def example(*args)
  new(*args[0..-2]).example(args.last)
end

.id(value = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/haveapi/client_example.rb', line 39

def id(value = nil)
  @id = value.to_s.freeze unless value.nil?
  return @id if @id

  # Preserve the anchors of the retained built-in formats.
  { 'RubyClient' => '0', 'PhpClient' => '2', 'RubyCli' => '3', 'Curl' => '5', 'Http' => '6' }[name.to_s.split('::').last] ||
    name.to_s.gsub('::', '-').downcase
end

.init ⇒ Object

Shortcut to #init



122
123
124
# File 'lib/haveapi/client_example.rb', line 122

def init(*)
  new(*).init
end

.label(v = nil) ⇒ Object

All subclasses have to call this method to set their label and be registered.



18
19
20
21
22
23
24
25
# File 'lib/haveapi/client_example.rb', line 18

def label(v = nil)
  if v
    @label = v
    HaveAPI::ClientExample.register(self)
  end

  @label
end

.language(value = nil) ⇒ Object



52
53
54
55
# File 'lib/haveapi/client_example.rb', line 52

def language(value = nil)
  @language = value.to_s.freeze unless value.nil?
  @language || id
end

.language_label(value = nil) ⇒ Object



57
58
59
60
# File 'lib/haveapi/client_example.rb', line 57

def language_label(value = nil)
  @language_label = value.to_s.freeze unless value.nil?
  @language_label || label
end

.order(v = nil) ⇒ Object

A number used for ordering client examples.



34
35
36
37
# File 'lib/haveapi/client_example.rb', line 34

def order(v = nil)
  @order = v if v
  @order
end

.register(klass) ⇒ Object



116
117
118
119
# File 'lib/haveapi/client_example.rb', line 116

def register(klass)
  @clients ||= []
  @clients << klass
end

.render(kind, context:, auth_method: nil, auth_description: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/haveapi/client_example.rb', line 88

def render(kind, context:, auth_method: nil, auth_description: nil)
  arguments = [context.host, context.base_url, context.version]
  arguments.push(context.resource_path, context.resource, context.action_name, context.action) if kind == :example
  renderer = new(*arguments)
  renderer.with_context(context)
  result = case kind
           when :init then renderer.init
           when :auth then renderer.auth(auth_method, auth_description)
           when :example
             if renderer.respond_to?(:example)
               renderer.example(context.sample)
             else
               [renderer.request(context.sample), renderer.response(context.sample)].compact.join("\n\n")
             end
           else raise ArgumentError, "Unknown client example kind: #{kind}"
           end
  return if result.nil?
  return { status: :code, code: result.dup.freeze }.freeze if result.is_a?(String)

  if result.is_a?(Hash) && result[:status] == :code && result[:code].is_a?(String)
    { status: :code, code: result[:code].dup.freeze }.freeze
  elsif result.is_a?(Hash) && result[:status] == :limitation && result[:reason].is_a?(String)
    { status: :limitation, reason: result[:reason].dup.freeze }.freeze
  else
    raise ArgumentError, 'Invalid client example result'
  end
end

.variant(value = nil) ⇒ Object



62
63
64
65
# File 'lib/haveapi/client_example.rb', line 62

def variant(value = nil)
  @variant = value.to_s.freeze unless value.nil?
  @variant
end

Instance Method Details

#auth(method, desc) ⇒ Object



163
# File 'lib/haveapi/client_example.rb', line 163

def auth(method, desc); end

#comment_lines(text, prefix) ⇒ Object

Prefix every physical prose line before combining it with executable source.



183
184
185
# File 'lib/haveapi/client_example.rb', line 183

def comment_lines(text, prefix)
  text.gsub(/\r\n?/, "\n").split("\n", -1).map { |line| "#{prefix}#{line}" }.join("\n")
end

#example_comment(key, default:, **values) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/haveapi/client_example.rb', line 174

def example_comment(key, default:, **values)
  if context.respond_to?(:example_comment)
    context.example_comment(key, default:, **values)
  else
    values.empty? ? default : format(default, values)
  end
end

#init ⇒ Object



161
# File 'lib/haveapi/client_example.rb', line 161

def init; end

#thorough? ⇒ Boolean

Returns:



170
171
172
# File 'lib/haveapi/client_example.rb', line 170

def thorough?
  context&.detail == :thorough
end

#version_url ⇒ Object



187
188
189
# File 'lib/haveapi/client_example.rb', line 187

def version_url
  File.join(base_url, "v#{version}", '/')
end

#with_context(value) ⇒ Object



165
166
167
168
# File 'lib/haveapi/client_example.rb', line 165

def with_context(value)
  @context = value
  self
end