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.
10 11 12 13 14 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 10 def initialize @source_type = nil @source_uri = nil @products_list = nil end |
Instance Attribute Details
#products_list ⇒ Object (readonly)
Returns the value of attribute products_list.
9 10 11 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 9 def products_list @products_list end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type.
9 10 11 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 9 def source_type @source_type end |
#source_uri ⇒ Object (readonly)
Returns the value of attribute source_uri.
9 10 11 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 9 def source_uri @source_uri end |
Instance Method Details
#handle_query(query) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 16 def handle_query(query) remaining_props_seen = {} remaining_props = [] @products_list.each do |product| next if product['product_type'] != query.product_type search_start = query..length option_types = product['options'].keys (search_start..option_types.length - 1).each do |i| option_type = option_types[i] option_value = product['options'][option_type] if remaining_props_seen[option_type].nil? remaining_props_seen[option_type] = {} remaining_props_seen[option_type][option_value] = true remaining_props.push("#{option_type.capitalize}: #{option_value}") elsif !remaining_props_seen[option_type][option_value] remaining_props_seen[option_type][option_value] = true remaining_props[i - search_start] += ", #{option_value}" end end end query.performed_at = DateTime.now query.results = remaining_props query end |
#load_products_list_from_default ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 62 def load_products_list_from_default load_from_file_url(@@DEFAULT_PRODUCTS_LIST_URL) unless @products_list.nil? @source_type = 'DEFAULT' @source_uri = @@DEFAULT_PRODUCTS_LIST_URL end @products_list end |
#load_products_list_from_source(source_type, source_uri) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/coding_challenge/commands/util/Inventory.rb', line 47 def load_products_list_from_source(source_type, source_uri) if source_type == 'FILE PATH' load_from_file_path(source_uri) elsif source_type == 'URL' load_from_file_url(source_uri) end unless @products_list.nil? @source_type = source_type @source_uri = source_uri end @products_list end |