Module: ErrorChecking

Defined in:
lib/error_checking.rb

Overview

Check for valid data, provide helpful errors if not

Class Method Summary collapse

Class Method Details

.product_array_valid?(product_array) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (StandardError)


7
8
9
10
11
12
13
14
# File 'lib/error_checking.rb', line 7

def product_array_valid?(product_array)
  errors = []
  errors.push("AmazonBundler expects an Array of Products") unless product_array.is_a?(Array) || !product_array.empty?
  product_array.each do |product|
    product_valid?(product, errors)
  end
  raise StandardError.new(errors.join("\n")) unless errors.empty?
end

.product_valid?(product, errors) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
# File 'lib/error_checking.rb', line 16

def product_valid?(product, errors)
  valid_hash = product.is_a?(Hash)
  errors.push("AmazonBundler expects Products to be a Hash") unless valid_hash
  return unless valid_hash

  errors.push("AmazonBundler expects Products to have an ASIN") unless product.key?(:asin) || !product[:asin].to_s.empty?
  errors.push("AmazonBundler expects Products to have a Quantity") unless product.key?(:quantity) || !product[:quantity].to_s.empty?
end