Class: BackpackTF::Price

Inherits:
Response show all
Defined in:
lib/backpack_tf/price.rb

Overview

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

Constant Summary collapse

INTERFACE =
:IGetPrices

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Response

hash_keys_to_sym, interface, responses, to_sym

Constructor Details

#initialize ⇒ Price

INSTANCE METHODS

Raises:

  • (RuntimeError)


87
88
89
90
91
92
93
# File 'lib/backpack_tf/price.rb', line 87

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

.find_item_by_name(item_name, opt = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/backpack_tf/price.rb', line 41

def self.find_item_by_name item_name, opt = nil
  if @items[item_name].nil?
    raise KeyError, "item with the name #{item_name} was not found"
  else
    if opt.nil?
      @items[item_name]
    elsif @items[item_name].respond_to? opt
      @items[item_name].public_send(opt)
    else
      raise KeyError, "the item, #{item_name} does not have that attribute"
    end
  end
end

.generate_items ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/backpack_tf/price.rb', line 28

def self.generate_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

.generate_price_keys(item_hash) ⇒ Object

Raises:

  • (TypeError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/backpack_tf/price.rb', line 64

def self.generate_price_keys item_hash
  raise TypeError unless item_hash.class == Hash
  prices = item_hash['prices']

  prices.each_pair.inject([]) do |gen_keys, (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

    gen_keys << new_key.join('_')
  end

end

.items ⇒ Object



20
21
22
23
24
25
26
# File 'lib/backpack_tf/price.rb', line 20

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

.random_item(opt = nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/backpack_tf/price.rb', line 55

def self.random_item opt = nil
  case opt
  when :prices, :price
    @items[@items.keys.sample].prices
  else
    @items.keys.sample
  end
end

.response ⇒ Object

CLASS METHODS



16
17
18
# File 'lib/backpack_tf/price.rb', line 16

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