Class: BackpackTF::Prices

Inherits:
Response show all
Includes:
Finder
Defined in:
lib/backpack_tf/prices.rb

Overview

ruby representations of a JSON response to IGetPrices['response']

Constant Summary collapse

INTERFACE =
:IGetPrices
@@items =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finder

included

Methods inherited from Response

hash_keys_to_sym, interface, responses, to_sym

Constructor Details

#initialize ⇒ Prices

Returns a new instance of Prices.

Raises:

  • (RuntimeError)


39
40
41
42
43
44
45
# File 'lib/backpack_tf/prices.rb', line 39

def initialize
  msg = "This class is meant to receive the JSON response from the #{self.class.interface} interface."
  msg << "It holds a Hash array of prices of items, but not is meant to be instantiated."
  msg << "See the Item class if you are interested in an item."
  msg << "However, information on items should be stored in the @items property of #{self.class}."
  raise RuntimeError, msg
end

Class Method Details

.gen_items ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/backpack_tf/prices.rb', line 26

def self.gen_items
  @@items = @response[:items].inject({}) do |items, (name)|
    defindex = @response[:items][name]['defindex'][0]

    if defindex.nil? || defindex < 0
      items
    else
      items[name] = Item.new(name, @response[:items][name])
      items
    end
  end
end

.items ⇒ Object



18
19
20
21
22
23
24
# File 'lib/backpack_tf/prices.rb', line 18

def self.items
  if @@items.nil?
    gen_items
  else
    @@items
  end
end

.response ⇒ Object



14
15
16
# File 'lib/backpack_tf/prices.rb', line 14

def self.response
  @response = superclass.responses[to_sym]
end