Class: Apidiesel::Dsl::ExpectationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/apidiesel/dsl.rb

Overview

ExpectationBuilder defines the methods available within an expects block when defining an API action.

Instance Method Summary collapse

Instance Method Details

#boolean(param_name, **args) ⇒ nil

Defines a boolean parameter.

FIXME: sensible duck typing check

Examples:

expects do
  boolean :per_page, :optional => true
end

Parameters:

  • param_name (Symbol)

    name of the parameter

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :optional (Boolean) — default: false

    defines whether this parameter may be omitted

  • :optional_if_present (Symbol)

    param_name is optional, if the parameter given here is present instead

  • :required_if_present (Symbol)

    param_name is required if param_name is also present

  • :submitted_as (Symbol)

    submit param_name to the API under the name given here

  • :default (Object)

    a default parameter to be set when no value is specified

  • :submit (true, false) — default: true

    set to false for arguments that should not be submitted as API parameters

  • :allowed_values (Enumerable)

    only accept the values in this Enumerable. If Enumerable is a Hash, use the hash values to define what is actually sent to the server. Example: :allowed_values => {:foo => "f"} allows the value ':foo', but sends it as 'f'

Returns:

  • (nil)


120
121
122
123
# File 'lib/apidiesel/dsl.rb', line 120

def boolean(param_name, **args)
  validation_builder(:to_s, param_name, **args)
  parameters_to_filter << param_name if args[:submit] == false
end

#datetime(param_name, **args) ⇒ nil Also known as: time, date

Defines a date, time or datetime parameter.

Examples:

expects do
  datetime :starts_at, format: '%d-%m-%Y'
end

Parameters:

  • param_name (Symbol)

    name of the parameter

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :optional (Boolean) — default: false

    defines whether this parameter may be omitted

  • :optional_if_present (Symbol)

    param_name is optional, if the parameter given here is present instead

  • :required_if_present (Symbol)

    param_name is required if param_name is also present

  • :submitted_as (Symbol)

    submit param_name to the API under the name given here

  • :default (Object)

    a default parameter to be set when no value is specified

  • :submit (true, false) — default: true

    set to false for arguments that should not be submitted as API parameters

  • :allowed_values (Enumerable)

    only accept the values in this Enumerable. If Enumerable is a Hash, use the hash values to define what is actually sent to the server. Example: :allowed_values => {:foo => "f"} allows the value ':foo', but sends it as 'f'

  • :format (String)

    a format string as supported by Rubys #strftime

Returns:

  • (nil)


135
136
137
138
139
140
141
142
# File 'lib/apidiesel/dsl.rb', line 135

def datetime(param_name, **args)
  if args[:format]
    args[:processor] = ->(value) { value.try(:strftime, args[:format]) }
  end

  validation_builder(:strftime, param_name, **args)
  parameters_to_filter << param_name if args[:submit] == false
end

#integer(param_name, **args) ⇒ nil

Defines an integer parameter.

Examples:

expects do
  integer :per_page, :optional => true
end

Parameters:

  • param_name (Symbol)

    name of the parameter

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :optional (Boolean) — default: false

    defines whether this parameter may be omitted

  • :optional_if_present (Symbol)

    param_name is optional, if the parameter given here is present instead

  • :required_if_present (Symbol)

    param_name is required if param_name is also present

  • :submitted_as (Symbol)

    submit param_name to the API under the name given here

  • :default (Object)

    a default parameter to be set when no value is specified

  • :submit (true, false) — default: true

    set to false for arguments that should not be submitted as API parameters

  • :allowed_values (Enumerable)

    only accept the values in this Enumerable. If Enumerable is a Hash, use the hash values to define what is actually sent to the server. Example: :allowed_values => {:foo => "f"} allows the value ':foo', but sends it as 'f'

Returns:

  • (nil)


105
106
107
108
# File 'lib/apidiesel/dsl.rb', line 105

def integer(param_name, **args)
  validation_builder(:to_i, param_name, **args)
  parameters_to_filter << param_name if args[:submit] == false
end

#object(param_name, **args) ⇒ nil

Defines an object parameter

Examples:

expects do
  object :contract, klass: Contract
end

Parameters:

  • param_name (Symbol)

    name of the parameter

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :optional (Boolean) — default: false

    defines whether this parameter may be omitted

  • :optional_if_present (Symbol)

    param_name is optional, if the parameter given here is present instead

  • :required_if_present (Symbol)

    param_name is required if param_name is also present

  • :submitted_as (Symbol)

    submit param_name to the API under the name given here

  • :default (Object)

    a default parameter to be set when no value is specified

  • :submit (true, false) — default: true

    set to false for arguments that should not be submitted as API parameters

  • :allowed_values (Enumerable)

    only accept the values in this Enumerable. If Enumerable is a Hash, use the hash values to define what is actually sent to the server. Example: :allowed_values => {:foo => "f"} allows the value ':foo', but sends it as 'f'

  • :klass (Class)

    the expected class of the value

Returns:

  • (nil)


157
158
159
160
161
162
163
164
165
166
# File 'lib/apidiesel/dsl.rb', line 157

def object(param_name, **args)
  type_check = ->(value, param_name) {
    unless value.is_a?(args[:klass])
      raise Apidiesel::InputError, "arg #{param_name} must be a #{args[:klass].name}"
    end
  }

  validation_builder(type_check, param_name, **args)
  parameters_to_filter << param_name if args[:submit] == false
end

#string(param_name, **args) ⇒ nil

Defines a string parameter.

Examples:

expects do
  string :email, :submitted_as => :username
  string :value1, :optional_if_present => :value2
  string :value2, :optional_if_present => :value1
end

# This action expects to be given an 'email', which is sent to the API as 'username',
# and requires either a 'value1', a 'value2' or both to be present.

Parameters:

  • param_name (Symbol)

    name of the parameter

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :optional (Boolean) — default: false

    defines whether this parameter may be omitted

  • :optional_if_present (Symbol)

    param_name is optional, if the parameter given here is present instead

  • :required_if_present (Symbol)

    param_name is required if param_name is also present

  • :submitted_as (Symbol)

    submit param_name to the API under the name given here

  • :default (Object)

    a default parameter to be set when no value is specified

  • :submit (true, false) — default: true

    set to false for arguments that should not be submitted as API parameters

  • :allowed_values (Enumerable)

    only accept the values in this Enumerable. If Enumerable is a Hash, use the hash values to define what is actually sent to the server. Example: :allowed_values => {:foo => "f"} allows the value ':foo', but sends it as 'f'

Returns:

  • (nil)


92
93
94
95
# File 'lib/apidiesel/dsl.rb', line 92

def string(param_name, **args)
  validation_builder(:to_s, param_name, **args)
  parameters_to_filter << param_name if args[:submit] == false
end