Class: BackpackTF::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/backpack_tf/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_name, attr) ⇒ Item

Returns a new instance of Item.



15
16
17
18
19
20
21
22
23
24
# File 'lib/backpack_tf/item.rb', line 15

def initialize item_name, attr
  @item_name  = item_name

  unless attr.class == Hash
    attr = JSON.parse(attr) 
  end

  @defindex   = process_defindex(attr['defindex'])
  @prices     = generate_prices_hash(attr)
end

Instance Attribute Details

#defindexFixnum (readonly)

Returns the index to which you can link this item to Team Fortress 2’s Item Schema.

Returns:

  • (Fixnum)

    the index to which you can link this item to Team Fortress 2’s Item Schema



11
12
13
# File 'lib/backpack_tf/item.rb', line 11

def defindex
  @defindex
end

#item_nameString (readonly)

Returns the name of item.

Returns:

  • (String)

    the name of item



9
10
11
# File 'lib/backpack_tf/item.rb', line 9

def item_name
  @item_name
end

#pricesHash<Fixnum, ItemPrice> (readonly)

Returns a hash object.

Returns:

  • (Hash<Fixnum, ItemPrice>)

    a hash object



13
14
15
# File 'lib/backpack_tf/item.rb', line 13

def prices
  @prices
end

Instance Method Details

#generate_prices_hash(input_hash) ⇒ Object

Raises:

  • (TypeError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/backpack_tf/item.rb', line 32

def generate_prices_hash input_hash

  raise TypeError, 'expecting a Hash object' unless input_hash.class == Hash
  unless input_hash.has_key? 'prices'
    msg = "input_hash must be at the one level above the point where 'prices' is a key in the JSON hash"
    raise KeyError, msg
  end

  prices = input_hash['prices']

  prices.inject({}) do |hash, (key, val)|

    quality = BackpackTF::ItemPrice.qualities[key.to_i]
    new_key = [quality.to_s]

    tradability = val.keys.first
    new_key << tradability

    craftability = prices[key][tradability].keys.first
    new_key << craftability

    new_key = new_key.join(ItemPrice::KEYNAME_DELIMITER)

    prefix = prices[key][tradability][craftability]

    if (prefix.length <= 1)
      item_prices = prefix[0]
      # patch for oddly-structured items, ie: Aqua Summer 2013 Cooler
      item_prices = prefix.values.first if item_prices.nil?

      item_price_obj = ItemPrice.new(new_key, item_prices)
      hash[new_key] = item_price_obj
    elsif key == '5' # item with 'Unusual' quality
      prefix.keys.each do |prefix_key|
        item_prices = prefix[prefix_key]
        item_price_obj = ItemPrice.new(new_key, item_prices, prefix_key)
        hash[item_price_obj.effect] = item_price_obj
      end
    else # a Crate
      prefix.keys.each do |prefix_key|
        temp_key = "#{new_key}_##{prefix_key.to_i}"
        item_prices = prefix[prefix_key]
        item_price_obj = ItemPrice.new(temp_key, item_prices, prefix_key)
        hash[temp_key] = item_price_obj
      end
    end
    hash
  end

end

#process_defindex(arr) ⇒ Object



26
27
28
29
30
# File 'lib/backpack_tf/item.rb', line 26

def process_defindex arr
  return nil if arr.length == 0
  return arr[0] if arr.length == 1
  arr
end