Module: Io::Flow::V0::HttpClient::Preconditions

Defined in:
lib/flow_commerce/flow_api_v0_client.rb

Class Method Summary collapse

Class Method Details

.assert_boolean(field_name, value) ⇒ Object



73388
73389
73390
73391
73392
73393
73394
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73388

def Preconditions.assert_boolean(field_name, value)
  Preconditions.check_not_nil('field_name', field_name)
  Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name)
  Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass),
                            "Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]")
  value
end

.assert_boolean_or_nil(field_name, value) ⇒ Object



73396
73397
73398
73399
73400
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73396

def Preconditions.assert_boolean_or_nil(field_name, value)
  if !value.nil?
    Preconditions.assert_boolean(field_name, value)
  end
end

.assert_class(field_name, value, klass) ⇒ Object

Asserts that value is not nill and is_?(klass). Returns value. Common use is

amount = Preconditions.assert_class(‘amount’, amount, BigDecimal)



73373
73374
73375
73376
73377
73378
73379
73380
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73373

def Preconditions.assert_class(field_name, value, klass)
  Preconditions.check_not_nil('field_name', field_name)
  Preconditions.check_not_nil('klass', klass)
  Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name])
  Preconditions.check_state(value.is_a?(klass),
                            "Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]")
  value
end

.assert_class_or_nil(field_name, value, klass) ⇒ Object



73382
73383
73384
73385
73386
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73382

def Preconditions.assert_class_or_nil(field_name, value, klass)
  if !value.nil?
    Preconditions.assert_class(field_name, value, klass)
  end
end

.assert_collection_of_class(field_name, values, klass) ⇒ Object



73402
73403
73404
73405
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73402

def Preconditions.assert_collection_of_class(field_name, values, klass)
  Preconditions.assert_class(field_name, values, Array)
  values.each { |v| Preconditions.assert_class(field_name, v, klass) }
end

.assert_empty_opts(opts) ⇒ Object

Throws an error if opts is not empty. Useful when parsing arguments to a function



73353
73354
73355
73356
73357
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73353

def Preconditions.assert_empty_opts(opts)
  if !opts.empty?
    raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}")
  end
end

.assert_hash_of_class(field_name, hash, klass) ⇒ Object



73407
73408
73409
73410
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73407

def Preconditions.assert_hash_of_class(field_name, hash, klass)
  Preconditions.assert_class(field_name, hash, Hash)
  values.each { |k, v| Preconditions.assert_class(field_name, v, klass) }
end

.check_argument(expression, error_message = nil) ⇒ Object



73323
73324
73325
73326
73327
73328
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73323

def Preconditions.check_argument(expression, error_message=nil)
  if !expression
    raise PreconditionException.new(error_message || "check_argument failed")
  end
  nil
end

.check_not_blank(field_name, reference, error_message = nil) ⇒ Object



73344
73345
73346
73347
73348
73349
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73344

def Preconditions.check_not_blank(field_name, reference, error_message=nil)
  if reference.to_s.strip == ""
    raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name)
  end
  reference
end

.check_not_nil(field_name, reference, error_message = nil) ⇒ Object



73337
73338
73339
73340
73341
73342
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73337

def Preconditions.check_not_nil(field_name, reference, error_message=nil)
  if reference.nil?
    raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name)
  end
  reference
end

.check_state(expression, error_message = nil) ⇒ Object



73330
73331
73332
73333
73334
73335
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73330

def Preconditions.check_state(expression, error_message=nil)
  if !expression
    raise PreconditionException.new(error_message || "check_state failed")
  end
  nil
end

.require_keys(hash, fields, error_prefix = nil) ⇒ Object

Requires that the provided hash has the specified keys.

Parameters:

  • fields

    A list of symbols



73361
73362
73363
73364
73365
73366
73367
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 73361

def Preconditions.require_keys(hash, fields, error_prefix=nil)
  missing = fields.select { |f| !hash.has_key?(f) }
  if !missing.empty?
    msg = "Missing required fields: " + missing.join(", ")
    raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}")
  end
end