Class: BackpackTF::ItemPrice

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

Constant Summary collapse

KEYNAME_DELIMITER =
'_'
PARTICLE_EFFECTS_KEY =
'attribute_controlled_attached_particles'
PARTICLE_EFFECTS_FILE =
"./lib/backpack_tf/assets/#{PARTICLE_EFFECTS_KEY}.json"
@@qualities =

mapping official API quality integers to quality names wiki.teamfortress.com/wiki/WebAPI/GetSchema#Result_Data

[
  :Normal,
  :Genuine,
  nil,
  :Vintage,
  nil,
  :Unusual,
  :Unique,
  :Community,
  :Valve,
  :"Self-Made",
  nil,
  :Strange,
  nil,
  :Haunted,
  :"Collector's"
]
@@tradabilities =
[:Tradable, :'Non-Tradable']
@@craftabilities =
[:Craftable, :'Non-Craftable']
@@required_keys =
['currency', 'value', 'last_update', 'difference']
@@particle_effects =
self.hash_particle_effects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, attr, priceindex = nil) ⇒ ItemPrice

Returns a new instance of ItemPrice.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/backpack_tf/item_price.rb', line 107

def initialize key, attr, priceindex = nil
  attr = JSON.parse(attr) unless attr.class == Hash

  key = key.split(KEYNAME_DELIMITER)
  key = process_key(key)

  validate_attributes(attr)

  @quality        = key[0]
  @tradability    = key[1]
  @craftability   = key[2]
  @currency       = attr['currency'].to_sym
  @value          = attr['value']
  @value_high     = attr['value_high']
  @value_raw      = attr['value_raw']
  @value_high_raw = attr['value_high_raw']
  @last_update    = attr['last_update']
  @difference     = attr['difference']
  @priceindex     = priceindex
  @effect         = self.class.particle_effects[priceindex.to_i]
end

Instance Attribute Details

#craftabilitySymbol (readonly)

Returns either :Craftable or :‘Non-Craftable’.

Returns:

  • (Symbol)

    either :Craftable or :‘Non-Craftable’



87
88
89
# File 'lib/backpack_tf/item_price.rb', line 87

def craftability
  @craftability
end

#currencySymbol (readonly)

Returns The currency that the item’s price is based on.

Returns:

  • (Symbol)

    The currency that the item’s price is based on



91
92
93
# File 'lib/backpack_tf/item_price.rb', line 91

def currency
  @currency
end

#differenceFixnum (readonly)

Returns A relative difference between the former price and the current price. If 0, assume new price.

Returns:

  • (Fixnum)

    A relative difference between the former price and the current price. If 0, assume new price.



103
104
105
# File 'lib/backpack_tf/item_price.rb', line 103

def difference
  @difference
end

#effectString (readonly)

Returns Result of @@particle_effects.

Returns:



105
106
107
# File 'lib/backpack_tf/item_price.rb', line 105

def effect
  @effect
end

#last_updateFixnum (readonly)

Returns A timestamp of when the price was last updated.

Returns:

  • (Fixnum)

    A timestamp of when the price was last updated



101
102
103
# File 'lib/backpack_tf/item_price.rb', line 101

def last_update
  @last_update
end

#priceindexNilClass or Fixnum (readonly)

Returns Primarily used to signify crate series or unusual effect. Otherwise, this is 0.

Returns:

  • (NilClass or Fixnum)

    Primarily used to signify crate series or unusual effect. Otherwise, this is 0



89
90
91
# File 'lib/backpack_tf/item_price.rb', line 89

def priceindex
  @priceindex
end

#qualityString (readonly)

Returns the quality of the item being priced, converted to String.

Returns:

  • (String)

    the quality of the item being priced, converted to String



83
84
85
# File 'lib/backpack_tf/item_price.rb', line 83

def quality
  @quality
end

#tradabilitySymbol (readonly)

Returns either :Tradable or :‘Non-Tradable’.

Returns:

  • (Symbol)

    either :Tradable or :‘Non-Tradable’



85
86
87
# File 'lib/backpack_tf/item_price.rb', line 85

def tradability
  @tradability
end

#valueFloat (readonly)

Returns The value of the item in said currency.

Returns:

  • (Float)

    The value of the item in said currency



93
94
95
# File 'lib/backpack_tf/item_price.rb', line 93

def value
  @value
end

#value_highFloat (readonly)

Returns The item’s upper value measured in said currency. only set if the item has a price range.

Returns:

  • (Float)

    The item’s upper value measured in said currency. only set if the item has a price range



95
96
97
# File 'lib/backpack_tf/item_price.rb', line 95

def value_high
  @value_high
end

#value_high_rawFloat (readonly)

Returns The item’s value in the lowest currency without rounding. Reques raw to be enabled and set to 2.

Returns:

  • (Float)

    The item’s value in the lowest currency without rounding. Reques raw to be enabled and set to 2



99
100
101
# File 'lib/backpack_tf/item_price.rb', line 99

def value_high_raw
  @value_high_raw
end

#value_rawFloat (readonly)

Returns The item’s value in the lowest currency without rounding. If raw is set to 2, this is the lower value if a high value exists. Otherwise, this is the average between the high and low value. Requires raw to be enabled.

Returns:

  • (Float)

    The item’s value in the lowest currency without rounding. If raw is set to 2, this is the lower value if a high value exists. Otherwise, this is the average between the high and low value. Requires raw to be enabled.



97
98
99
# File 'lib/backpack_tf/item_price.rb', line 97

def value_raw
  @value_raw
end

Class Method Details

.hash_particle_effectsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/backpack_tf/item_price.rb', line 44

def self.hash_particle_effects

  file = File.open(PARTICLE_EFFECTS_FILE).read
  effects_arr = JSON.parse(file)[PARTICLE_EFFECTS_KEY]

  effects_arr.inject({}) do |hash, pe|
    id = pe['id']
    name = pe['name']
    hash[id] = name
    hash
  end

end

.ins_sp(str) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/backpack_tf/item_price.rb', line 61

def self.ins_sp str
  str = str.split('')
  new_str = ''
  str.each_with_index do |c, i|
    if i == 0
      new_str += c
    else
      unless c == c.upcase && str[i-1] == str[i-1].upcase
        new_str += c
      else
        new_str += (' ' + c)
      end
    end
  end
  new_str
end

.particle_effectsObject



59
# File 'lib/backpack_tf/item_price.rb', line 59

def self.particle_effects; @@particle_effects; end

.qualitiesObject



37
# File 'lib/backpack_tf/item_price.rb', line 37

def self.qualities; @@qualities; end

.quality_name_to_index(q) ⇒ Object



40
41
42
# File 'lib/backpack_tf/item_price.rb', line 40

def self.quality_name_to_index q
  @@qualities.index(q.to_sym) unless q.nil?
end

.required_keysObject



38
# File 'lib/backpack_tf/item_price.rb', line 38

def self.required_keys; @@required_keys; end