Class: CoreLibrary::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/types/parameter.rb

Overview

This data class represents the parameter to be sent in the request.

Instance Method Summary collapse

Constructor Details

#initializeParameter

Initializes a new instance of Parameter.



5
6
7
8
9
10
11
12
13
# File 'lib/apimatic-core/types/parameter.rb', line 5

def initialize
  @key = nil
  @value = nil
  @is_required = false
  @should_encode = false
  @default_content_type = nil
  @value_convertor = nil
  @validator = nil
end

Instance Method Details

#default_content_type(default_content_type) ⇒ Parameter

The setter for the default content type of the multipart request.

Parameters:

  • default_content_type (String)

    The content type to be used, applicable for multipart request parameters.

Returns:

  • (Parameter)

    An updated instance of Parameter.



80
81
82
83
# File 'lib/apimatic-core/types/parameter.rb', line 80

def default_content_type(default_content_type)
  @default_content_type = default_content_type
  self
end

#get_default_content_typeString

The getter for the default content type of the multipart request.

Returns:

  • (String)

    The default content type to be used applicable for multipart request parameters.



87
88
89
# File 'lib/apimatic-core/types/parameter.rb', line 87

def get_default_content_type
  @default_content_type
end

#get_keyString

The getter for the parameter key.

Returns:

  • (String)

    The parameter key to send.



25
26
27
# File 'lib/apimatic-core/types/parameter.rb', line 25

def get_key
  @key
end

#get_valueObject

The getter for the parameter’s actual/converted value where applicable.

Returns:

  • (Object)

    The parameter value to send.



39
40
41
42
43
# File 'lib/apimatic-core/types/parameter.rb', line 39

def get_value
  return @value_convertor.call(@value) unless @value_convertor.nil?

  @value
end

#is_required(is_required) ⇒ Parameter

The setter for the flag if the parameter is required. rubocop:disable Naming/PredicateName

Parameters:

  • is_required (Boolean)

    true if the parameter is required otherwise false, by default the value is false.

Returns:

  • (Parameter)

    An updated instance of Parameter.



49
50
51
52
# File 'lib/apimatic-core/types/parameter.rb', line 49

def is_required(is_required)
  @is_required = is_required
  self
end

#key(key) ⇒ Parameter

The setter for the parameter key.

Parameters:

  • key (String)

    The parameter key to send.

Returns:

  • (Parameter)

    An updated instance of Parameter.



18
19
20
21
# File 'lib/apimatic-core/types/parameter.rb', line 18

def key(key)
  @key = key
  self
end

#need_to_encodeBoolean

The getter for the flag if the parameter value is to be encoded.

Returns:

  • (Boolean)

    true if the parameter value is to be encoded otherwise false, by default the value is false.



73
74
75
# File 'lib/apimatic-core/types/parameter.rb', line 73

def need_to_encode
  @should_encode
end

#should_encode(should_encode) ⇒ Parameter

The setter for the flag if the parameter value is to be encoded.

Parameters:

  • should_encode (Boolean)

    true if the parameter value is to be encoded otherwise false, default is false.

Returns:

  • (Parameter)

    An updated instance of Parameter.



58
59
60
61
# File 'lib/apimatic-core/types/parameter.rb', line 58

def should_encode(should_encode)
  @should_encode = should_encode
  self
end

#validateObject

Validates the parameter value to be sent in the request.

Raises:

  • (ArgumentError)

    If the parameter is required but the value is nil.



101
102
103
104
105
106
107
108
# File 'lib/apimatic-core/types/parameter.rb', line 101

def validate
  raise ArgumentError, "Required parameter #{@key} cannot be nil." if @is_required && @value.nil?

  return if @validator.nil?

  validated_type = @validator.call(@value)
  @value_convertor = proc { |value| validated_type.serialize(value) } if validated_type.is_valid
end

#validator(validator) ⇒ Parameter

Setter for the validator.

Parameters:

  • validator (callable)

    The validator function to be set.

Returns:

  • (Parameter)

    An updated instance of the Parameter class.



94
95
96
97
# File 'lib/apimatic-core/types/parameter.rb', line 94

def validator(validator)
  @validator = validator
  self
end

#value(value) ⇒ Parameter

The setter for the parameter value.

Parameters:

  • value (Object)

    The parameter value to send.

Returns:

  • (Parameter)

    An updated instance of Parameter.



32
33
34
35
# File 'lib/apimatic-core/types/parameter.rb', line 32

def value(value)
  @value = value
  self
end

#value_convertor(value_convertor) ⇒ Parameter

The setter for the function of converting value for form params.

Parameters:

  • value_convertor (Callable)

    The function to execute for conversion.

Returns:

  • (Parameter)

    An updated instance of Parameter.



66
67
68
69
# File 'lib/apimatic-core/types/parameter.rb', line 66

def value_convertor(value_convertor)
  @value_convertor = value_convertor
  self
end