Class: WebFunction::Argument

Inherits:
Object
  • Object
show all
Includes:
Flaggable
Defined in:
lib/web_function/argument.rb

Overview

Arguments are used to define Web Function Endpoint request parameters.

See the arguments section on the Web Function website for more details.

Instance Attribute Summary collapse

Attributes included from Flaggable

#flags

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Flaggable

#flag?

Constructor Details

#initialize(name:, type:, group: nil, choices: [], flags: [], docs: nil) ⇒ Argument

Returns a new instance of Argument.



13
14
15
16
17
18
19
20
# File 'lib/web_function/argument.rb', line 13

def initialize(name:, type:, group: nil, choices: [], flags: [], docs: nil)
  @name = name
  @type = Type.parse(type)
  @group = group
  @choices = choices
  @flags = flags
  @docs = docs.to_s
end

Instance Attribute Details

#choicesArray (readonly)

An array specifying the exact, case-sensitive values that are permitted for this argument. Each value in the choices array must conform to the data type specified in the argument's type key.

Note that if the argument type is array, choices may contain strings or numbers representing the allowed values that can be included in the array.

Returns:

  • (Array)


99
100
101
# File 'lib/web_function/argument.rb', line 99

def choices
  @choices
end

#docsString (readonly)

Description of the argument. It must be formatted as markdown.

Returns:

  • (String)


105
106
107
# File 'lib/web_function/argument.rb', line 105

def docs
  @docs
end

#groupString (readonly)

A name used to categorize or group similar arguments together. This should be used by documentation tools to organize related arguments.

Returns:

  • (String)


89
90
91
# File 'lib/web_function/argument.rb', line 89

def group
  @group
end

#nameString (readonly)

The name of the argument.

Returns:

  • (String)


70
71
72
# File 'lib/web_function/argument.rb', line 70

def name
  @name
end

#typeString (readonly)

The type of the argument. It must be one of:

  • object
  • array
  • string
  • number
  • boolean

Returns:

  • (String)


82
83
84
# File 'lib/web_function/argument.rb', line 82

def type
  @type
end

Class Method Details

.from_array(arguments) ⇒ Array<Argument>

Instantiate a collection of Argument from an array of hash, typically coming from a Package. Uses from_hash under the hood.

Parameters:

  • arguments (Array<Hash>)

Returns:



59
60
61
62
63
# File 'lib/web_function/argument.rb', line 59

def from_array(arguments)
  Utils.normalize_array arguments do |argument|
    from_hash(argument)
  end
end

.from_hash(argument) ⇒ Argument?

Instantiate a new Argument from a hash, typically coming from a Package.

Parameters:

  • argument (Hash)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/web_function/argument.rb', line 29

def from_hash(argument)
  unless argument.is_a?(Hash)
    return
  end

  unless argument["name"]
    return
  end

  unless argument["type"]
    return
  end

  new(
    name: argument["name"],
    type: argument["type"],
    group: argument["group"],
    choices: [*argument["choices"]],
    flags: Utils.normalize_array_of_strings(argument["flags"]),
    docs: argument["docs"],
  )
end

Instance Method Details

#optional?Boolean

Whether the argument is optional.

Returns:

  • (Boolean)


119
120
121
# File 'lib/web_function/argument.rb', line 119

def optional?
  !required?
end

#required?Boolean

Whether the argument is required.

Returns:

  • (Boolean)


111
112
113
# File 'lib/web_function/argument.rb', line 111

def required?
  flag?("required")
end