class HaveAPI::ClientExample

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.

Attributes

action[R]
action_name[R]
base_url[R]
host[R]
resource[R]
resource_path[R]
version[R]

Public Class Methods

auth(*args) click to toggle source

Shortcut to {ClientExample#auth}

# File lib/haveapi/client_example.rb, line 47
def auth(*args)
  new(*args[0..-3]).auth(*args[-2..])
end
clients() click to toggle source

@return [Array<ClientExample>] sorted array of classes

# File lib/haveapi/client_example.rb, line 57
def clients
  @clients.sort { |a, b| a.order <=> b.order }
end
code(v = nil) click to toggle source

Code name is passed to the syntax highligher.

# File lib/haveapi/client_example.rb, line 25
def code(v = nil)
  @code = v if v
  @code
end
example(*args) click to toggle source

Shortcut to {ClientExample#example}

# File lib/haveapi/client_example.rb, line 52
def example(*args)
  new(*args[0..-2]).example(args.last)
end
init(*) click to toggle source

Shortcut to {ClientExample#init}

# File lib/haveapi/client_example.rb, line 42
def init(*)
  new(*).init
end
label(v = nil) click to toggle source

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

# File lib/haveapi/client_example.rb, line 15
def label(v = nil)
  if v
    @label = v
    HaveAPI::ClientExample.register(self)
  end

  @label
end
new(host, base_url, version, *args) click to toggle source
# File lib/haveapi/client_example.rb, line 64
def initialize(host, base_url, version, *args)
  @host = host
  @base_url = base_url
  @version = version
  @resource_path, @resource, @action_name, @action = args
end
order(v = nil) click to toggle source

A number used for ordering client examples.

# File lib/haveapi/client_example.rb, line 31
def order(v = nil)
  @order = v if v
  @order
end
register(klass) click to toggle source
# File lib/haveapi/client_example.rb, line 36
def register(klass)
  @clients ||= []
  @clients << klass
end

Public Instance Methods

auth(method, desc) click to toggle source
# File lib/haveapi/client_example.rb, line 73
def auth(method, desc); end
init() click to toggle source
# File lib/haveapi/client_example.rb, line 71
def init; end
version_url() click to toggle source
# File lib/haveapi/client_example.rb, line 75
def version_url
  File.join(base_url, "v#{version}", '/')
end

Protected Instance Methods

auth_token_credentials(desc, password: true) click to toggle source

@param password [Boolean] include password parameter @return [Hash<String, String>] parameter => example value

# File lib/haveapi/client_example.rb, line 83
def auth_token_credentials(desc, password: true)
  passwords = %i[password pass passwd]
  params = desc[:resources][:token][:actions]['request'][:input][:parameters].keys - %i[lifetime interval]

  unless password
    params.reject! { |param| passwords.include?(param) }
  end

  params.to_h do |param|
    value =
      if passwords.include?(param)
        'secret'
      else
        param
      end

    [param, value]
  end
end