Module: Io::Flow::V0::HttpClient::Preconditions
- Defined in:
- lib/flow_commerce/flow_api_v0_client.rb
Class Method Summary collapse
- .assert_boolean(field_name, value) ⇒ Object
- .assert_boolean_or_nil(field_name, value) ⇒ Object
-
.assert_class(field_name, value, klass) ⇒ Object
Asserts that value is not nill and is_?(klass).
- .assert_class_or_nil(field_name, value, klass) ⇒ Object
- .assert_collection_of_class(field_name, values, klass) ⇒ Object
-
.assert_empty_opts(opts) ⇒ Object
Throws an error if opts is not empty.
- .assert_hash_of_class(field_name, hash, klass) ⇒ Object
- .check_argument(expression, error_message = nil) ⇒ Object
- .check_not_blank(field_name, reference, error_message = nil) ⇒ Object
- .check_not_nil(field_name, reference, error_message = nil) ⇒ Object
- .check_state(expression, error_message = nil) ⇒ Object
-
.require_keys(hash, fields, error_prefix = nil) ⇒ Object
Requires that the provided hash has the specified keys.
Class Method Details
.assert_boolean(field_name, value) ⇒ Object
68124 68125 68126 68127 68128 68129 68130 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68124 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
68132 68133 68134 68135 68136 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68132 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)
68109 68110 68111 68112 68113 68114 68115 68116 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68109 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
68118 68119 68120 68121 68122 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68118 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
68138 68139 68140 68141 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68138 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
68089 68090 68091 68092 68093 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68089 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
68143 68144 68145 68146 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68143 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
68059 68060 68061 68062 68063 68064 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68059 def Preconditions.check_argument(expression, =nil) if !expression raise PreconditionException.new( || "check_argument failed") end nil end |
.check_not_blank(field_name, reference, error_message = nil) ⇒ Object
68080 68081 68082 68083 68084 68085 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68080 def Preconditions.check_not_blank(field_name, reference, =nil) if reference.to_s.strip == "" raise PreconditionException.new( || "argument for %s cannot be blank" % field_name) end reference end |
.check_not_nil(field_name, reference, error_message = nil) ⇒ Object
68073 68074 68075 68076 68077 68078 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68073 def Preconditions.check_not_nil(field_name, reference, =nil) if reference.nil? raise PreconditionException.new( || "argument for %s cannot be nil" % field_name) end reference end |
.check_state(expression, error_message = nil) ⇒ Object
68066 68067 68068 68069 68070 68071 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68066 def Preconditions.check_state(expression, =nil) if !expression raise PreconditionException.new( || "check_state failed") end nil end |
.require_keys(hash, fields, error_prefix = nil) ⇒ Object
Requires that the provided hash has the specified keys.
68097 68098 68099 68100 68101 68102 68103 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 68097 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 |