Class: LighterpackParser::ItemParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lighterpack_parser/item_parser.rb

Overview

Parser for extracting item data from Lighterpack HTML elements.

Instance Method Summary collapse

Instance Method Details

#parse(element) ⇒ Item?

Parse a single item element and return an Item object.

Parameters:

  • element (Nokogiri::XML::Element)

    The item HTML element

Returns:

  • (Item, nil)

    The parsed item, or nil if name is missing



10
11
12
13
14
15
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
46
47
48
49
# File 'lib/lighterpack_parser/item_parser.rb', line 10

def parse(element)
  name = extract_name(element)
  return nil unless name

  weight_data = extract_weight(element)
  quantity = extract_quantity(element)
  description = extract_description(element)
  image_url = extract_image_url(element)
  consumable = extract_consumable_flag(element)
  worn = extract_worn_flag(element)

  # Calculate per-item weight
  weight_per_item = weight_data[:weight_grams]

  # Calculate total weights
  total_weight = weight_per_item * quantity

  # In Lighterpack, if an item is consumable, the consumable_weight is always the full weight
  # Calculate total consumable weight (per item * quantity)
  total_consumable_weight = consumable ? weight_per_item * quantity : nil

  # In Lighterpack, if an item is worn, only the first item is worn (worn_quantity = 1)
  # regardless of total quantity
  worn_quantity = worn ? 1 : nil
  total_worn_weight = worn ? weight_per_item * 1 : nil

  Item.new(
    name: name,
    description: description,
    weight: weight_per_item,
    total_weight: total_weight,
    quantity: quantity,
    image_url: image_url,
    consumable: consumable,
    total_consumable_weight: total_consumable_weight,
    worn: worn,
    worn_quantity: worn_quantity,
    total_worn_weight: total_worn_weight
  )
end