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

Defined in:
lib/flow_reference_v0_client.rb

Class Method Summary collapse

Class Method Details

.assert_boolean(field_name, value) ⇒ Object



1242
1243
1244
1245
1246
1247
1248
# File 'lib/flow_reference_v0_client.rb', line 1242

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



1250
1251
1252
1253
1254
# File 'lib/flow_reference_v0_client.rb', line 1250

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)



1227
1228
1229
1230
1231
1232
1233
1234
# File 'lib/flow_reference_v0_client.rb', line 1227

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



1236
1237
1238
1239
1240
# File 'lib/flow_reference_v0_client.rb', line 1236

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



1256
1257
1258
1259
# File 'lib/flow_reference_v0_client.rb', line 1256

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



1207
1208
1209
1210
1211
# File 'lib/flow_reference_v0_client.rb', line 1207

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



1261
1262
1263
1264
# File 'lib/flow_reference_v0_client.rb', line 1261

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



1177
1178
1179
1180
1181
1182
# File 'lib/flow_reference_v0_client.rb', line 1177

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



1198
1199
1200
1201
1202
1203
# File 'lib/flow_reference_v0_client.rb', line 1198

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



1191
1192
1193
1194
1195
1196
# File 'lib/flow_reference_v0_client.rb', line 1191

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



1184
1185
1186
1187
1188
1189
# File 'lib/flow_reference_v0_client.rb', line 1184

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



1215
1216
1217
1218
1219
1220
1221
# File 'lib/flow_reference_v0_client.rb', line 1215

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