Class: BackpackTF::Item
- Inherits:
-
Object
- Object
- BackpackTF::Item
- Includes:
- Finder
- Defined in:
- lib/backpack_tf/item.rb
Instance Attribute Summary collapse
-
#defindex ⇒ Fixnum
readonly
The index on which you can link this item to Team Fortress 2's Item Schema.
-
#item_name ⇒ String
readonly
The name of item.
-
#prices ⇒ Hash<Fixnum, ItemPrice>
readonly
A hash object.
Instance Method Summary collapse
- #gen_prices_hash(input_hash) ⇒ Object
-
#initialize(item_name, attr) ⇒ Item
constructor
A new instance of Item.
Methods included from Finder
Constructor Details
#initialize(item_name, attr) ⇒ Item
Returns a new instance of Item.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/backpack_tf/item.rb', line 41 def initialize item_name, attr @item_name = item_name unless attr.class == Hash attr = JSON.parse(attr) end @defindex = attr['defindex'][0] @prices = gen_prices_hash(attr) end |
Instance Attribute Details
#defindex ⇒ Fixnum (readonly)
Returns the index on which you can link this item to Team Fortress 2's Item Schema.
37 38 39 |
# File 'lib/backpack_tf/item.rb', line 37 def defindex @defindex end |
#item_name ⇒ String (readonly)
Returns the name of item.
35 36 37 |
# File 'lib/backpack_tf/item.rb', line 35 def item_name @item_name end |
#prices ⇒ Hash<Fixnum, ItemPrice> (readonly)
Returns a hash object.
39 40 41 |
# File 'lib/backpack_tf/item.rb', line 39 def prices @prices end |
Instance Method Details
#gen_prices_hash(input_hash) ⇒ Object
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/backpack_tf/item.rb', line 52 def gen_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('_') prefix = prices[key][tradability][craftability] if prefix.class == Array item_prices = prefix.first item_price_obj = ItemPrice.new(new_key, item_prices) hash[new_key] = item_price_obj elsif prefix.class == Hash if prefix.keys.length <= 1 item_prices = prefix.values[0] item_price_obj = ItemPrice.new(new_key, item_prices) hash[new_key] = item_price_obj else prefix.keys.each do |prefix_key| temp_key = "#{new_key}_Effect ##{prefix_key.to_i}" item_prices = prefix[prefix_key] item_price_obj = ItemPrice.new(temp_key, item_prices, prefix_key) hash[new_key] = item_price_obj end end end hash end end |