Class: Inventory
- Inherits:
-
Object
- Object
- Inventory
- Defined in:
- lib/coding_challenge/commands/util/Inventory.rb
Constant Summary collapse
- @@DEFAULT_PRODUCTS_LIST_URL =
'https://gist.githubusercontent.com/michaelporter/b2743e0cdad0664fa9517c0a6b82cdda/raw/67e4606007391f678c9330ee3a77a9024fce4e64/products.json'
Instance Attribute Summary collapse
-
#products_list ⇒ Object
readonly
Returns the value of attribute products_list.
-
#source_type ⇒ Object
readonly
Returns the value of attribute source_type.
-
#source_uri ⇒ Object
readonly
Returns the value of attribute source_uri.
Instance Method Summary collapse
- #handle_query(query) ⇒ Object
-
#initialize ⇒ Inventory
constructor
A new instance of Inventory.
- #load_products_list_from_default ⇒ Object
- #load_products_list_from_source(source_type, source_uri) ⇒ Object
Constructor Details
#initialize ⇒ Inventory
Returns a new instance of Inventory.
14 15 16 17 18 19 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 14 def initialize @source_type = nil @source_uri = nil @products_list = nil @products_schema = nil end |
Instance Attribute Details
#products_list ⇒ Object (readonly)
Returns the value of attribute products_list.
13 14 15 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 13 def products_list @products_list end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type.
13 14 15 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 13 def source_type @source_type end |
#source_uri ⇒ Object (readonly)
Returns the value of attribute source_uri.
13 14 15 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 13 def source_uri @source_uri end |
Instance Method Details
#handle_query(query) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 21 def handle_query(query) = @products_schema[query.product_type.downcase] is_invalid_product_type = .nil? raise InvalidProductTypeError, query.product_type if is_invalid_product_type results = [] .each_with_index do |(option_type, option_values_map), arg_position| option_argument = query.[arg_position] is_argument_provided = !option_argument.nil? if is_argument_provided is_invalid_argument = !option_values_map.key?(option_argument) raise InvalidOptionError.new(query.product_type, option_type, option_argument) if is_invalid_argument else possible_option_values = option_values_map.keys results << "#{option_type.capitalize}: #{possible_option_values.join(', ')}" end end query.results = results query end |
#load_products_list_from_default ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 55 def load_products_list_from_default begin products_list = load_from_file_url(@@DEFAULT_PRODUCTS_LIST_URL) raise StandardError if products_list.nil? rescue StandardError raise FileReadError, source_uri end set_as_data_source('DEFAULT', @@DEFAULT_PRODUCTS_LIST_URL, products_list) end |
#load_products_list_from_source(source_type, source_uri) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 44 def load_products_list_from_source(source_type, source_uri) begin products_list = load_from_file_path(source_uri) if source_type == 'FILE PATH' products_list = load_from_file_url(source_uri) if source_type == 'URL' raise StandardError if products_list.nil? rescue StandardError raise FileReadError, source_uri end set_as_data_source(source_type, source_uri, products_list) end |