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

#initializePrice

INSTANCE METHODS

Raises:

  • (RuntimeError)


74
75
76
77
78
79
80
# File 'lib/backpack_tf/price.rb', line 74

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
54
55
56
# File 'lib/backpack_tf/price.rb', line 41

def self.find_item_by_name item_name, opt = nil

  items if @items.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_itemsObject



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

def self.generate_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(opt = nil) ⇒ Object



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

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

.random_item(opt = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/backpack_tf/price.rb', line 58

def self.random_item opt = nil

  items if @items.nil?

  case opt
  when :prices, :price
    @items[@items.keys.sample].prices
  else
    @items.keys.sample
  end
end

.responseObject

CLASS METHODS



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

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