Class: Wowget::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/wowget/item.rb

Constant Summary collapse

QUALITIES =
['Poor', 'Common', 'Uncommon', 'Rare', 'Epic', 'Legendary', 'Artifact', 'Heirloom']
CATEGORIES =
{
  0 => {:name => 'Consumables', :slug => 'consumables'},
  1 => {:name => 'Containers', :slug => 'containers'},
  2 => {:name => 'Weapons', :slug => 'weapons'},
  3 => {:name => 'Gems', :slug => 'gems'},
  4 => {:name => 'Armor', :slug => 'armor'},
  7 => {:name => 'Trade Goods', :slug => 'tradegoods'},
  9 => {:name => 'Recipes', :slug => 'recipes'},
  10 => {:name => 'Currency', :slug => 'currency'},
  12 => {:name => 'Quest', :slug => 'quest'},
  13 => {:name => 'Keys', :slug => 'keys'},
  15 => {:name => 'Miscellaneous', :slug => 'miscellaneous'},
  16 => {:name => 'Glyphs', :slug => 'glyphs'}
}
SUBCATEGORIES =
{
  'Consumables' => {
    0 => {:name => 'Consumables', :slug => 'consumables'},
    1 => {:name => 'Potions', :slug => 'potions'},
    2 => {:name => 'Elixirs', :slug => 'elixirs'},
    3 => {:name => 'Flasks', :slug => 'flasks'},
    4 => {:name => 'Scrolls', :slug => 'scrolls'},
    5 => {:name => 'Food & Drinks', :slug => 'food_and_drinks'},
    6 => {:name => 'Item Enhancements (Permanent)', :slug => 'item_enhancements_permanent'},
    -3 => {:name => 'Item Enhancements (Temporary)', :slug => 'item_enhancements_temporary'},
    7 => {:name => 'Bandages', :slug => 'bandages'},
    8 => {:name => 'Other', :slug => 'other'}
  },
  'Containers' => {
    0 => {:name => 'Bags', :slug => 'bags'},
    2 => {:name => 'Herb Bags', :slug => 'herb'},
    3 => {:name => 'Enchanting Bags', :slug => 'enchanting'},
    4 => {:name => 'Engineering Bags', :slug => 'engineering'},
    5 => {:name => 'Gem Bags', :slug => 'gem'},
    6 => {:name => 'Mining Bags', :slug => 'mining'},
    7 => {:name => 'Leatherworking Bags', :slug => 'leatherworking'},
    8 => {:name => 'Inscription Bags', :slug => 'inscription'},
    9 => {:name => 'Tackle Boxes', :slug => 'tackleboxes'}
  },
  'Weapons' => {
    1 => {:name => 'Two-Handed Axes', :slug => 'two_handed_axes'},
    2 => {:name => 'Bows', :slug => 'bows'},
    3 => {:name => 'Guns', :slug => 'guns'},
    4 => {:name => 'One-Handed Maces', :slug => 'one_handed_maces'},
    5 => {:name => 'Two-Handed Maces', :slug => 'two_handed_maces'},
    6 => {:name => 'Polearms', :slug => 'polearms'},
    7 => {:name => 'One-Handed Swords', :slug => 'one_handed_swords'},
    8 => {:name => 'Two-Handed Swords', :slug => 'two_handed_swords'},
    10 => {:name => 'Staves', :slug => 'staves'},
    13 => {:name => 'Fist Weapons', :slug => 'fist_weapons'},
    14 => {:name => 'Miscellaneous', :slug => 'miscellaneous'},
    15 => {:name => 'Daggers', :slug => 'daggers'},
    16 => {:name => 'Thrown', :slug => 'thrown'},
    18 => {:name => 'Crossbows', :slug => 'crossbows'},
    19 => {:name => 'Wands', :slug => 'wands'},
    20 => {:name => 'Fishing Poles', :slug => 'fishing_poles'}
  },
  'Gems' => {
    1 => {:name => 'Blue', :slug => 'blue'},
    2 => {:name => 'Yellow', :slug => 'yellow'},
    3 => {:name => 'Purple', :slug => 'purple'},
    4 => {:name => 'Green', :slug => 'green'},
    5 => {:name => 'Orange', :slug => 'orange'},
    6 => {:name => 'Meta', :slug => 'meta'},
    7 => {:name => 'Simple', :slug => 'simple'},
    8 => {:name => 'Prismatic', :slug => 'prismatic'},
    9 => {:name => 'Hydraulic', :slug => 'hydraulic'},
    10 => {:name => 'Cogwheel', :slug => 'cogwheel'}
  },
  'Armor' => {
    -8 => {:name => 'Shirts', :slug => 'shirts'},
    -7 => {:name => 'Tabards', :slug => 'tabards'},
    -6 => {:name => 'Cloaks', :slug => 'cloaks'},
    -5 => {:name => 'Off-hand Frills', :slug => 'off_hand_frills'},
    -4 => {:name => 'Trinkets', :slug => 'trinkets'},
    -3 => {:name => 'Amulets', :slug => 'amulets'},
    -2 => {:name => 'Rings', :slug => 'rings'},
    0 => {:name => 'Miscellaneous', :slug => 'miscellaneous'},
    1 => {:name => 'Cloth', :slug => 'cloth', :inventoryslots => true},
    2 => {:name => 'Leather', :slug => 'leather', :inventoryslots => true},
    3 => {:name => 'Mail', :slug => 'mail', :inventoryslots => true},
    4 => {:name => 'Plate', :slug => 'plate', :inventoryslots => true},
    6 => {:name => 'Shields', :slug => 'shields'},
    11 => {:name => 'Relics', :slug => 'relics'}
  },
  'Trade Goods' => {
    1 => {:name => 'Parts', :slug => 'parts'},
    2 => {:name => 'Explosives', :slug => 'explosives'},
    3 => {:name => 'Devices', :slug => 'devices'},
    4 => {:name => 'Jewelcrafting', :slug => 'jewelcrafting'},
    5 => {:name => 'Cloth', :slug => 'cloth'},
    6 => {:name => 'Leather', :slug => 'leather'},
    7 => {:name => 'Metal & Stone', :slug => 'metal_and_stone'},
    8 => {:name => 'Meat', :slug => 'meat'},
    9 => {:name => 'Herbs', :slug => 'herbs'},
    10 => {:name => 'Elemental', :slug => 'elemental'},
    11 => {:name => 'Other', :slug => 'other'},
    12 => {:name => 'Enchanting', :slug => 'enchanting'},
    13 => {:name => 'Materials', :slug => 'materials'},
    14 => {:name => 'Armor Enchantments', :slug => 'armor_enchantments'},
    15 => {:name => 'Weapon Enchantments', :slug => 'weapon_enchantments'}
  },
  'Recipes' => {
    0 => {:name => 'Books ', :slug => 'books'},
    1 => {:name => 'Leatherworking Patterns', :slug => 'leatherworking'},
    2 => {:name => 'Tailoring Patterns', :slug => 'tailoring'},
    3 => {:name => 'Engineering Schematics', :slug => 'engineering'},
    4 => {:name => 'Blacksmithing Plans', :slug => 'blacksmithing'},
    5 => {:name => 'Cooking Recipes', :slug => 'cooking'},
    6 => {:name => 'Alchemy Recipes', :slug => 'alchemy'},
    7 => {:name => 'First Aid Manuals', :slug => 'first_aid'},
    8 => {:name => 'Enchanting Formulae', :slug => 'enchanting'},
    9 => {:name => 'Fishing Books', :slug => 'fishing'},
    10 => {:name => 'Jewelcrafting Designs', :slug => 'jewelcrafting'},
    11 => {:name => 'Inscription Techniques', :slug => 'inscription'}
  },
  'Miscellaneous' => {
    -2 => {:name => 'Armor Tokens', :slug => 'armor_tokens'},
    0 => {:name => 'Junk', :slug => 'junk'},
    1 => {:name => 'Reagents', :slug => 'reagents'},
    2 => {:name => 'Companions', :slug => 'companions'},
    3 => {:name => 'Holiday', :slug => 'holiday'},
    4 => {:name => 'Other', :slug => 'other'},
    5 => {:name => 'Mounts', :slug => 'mounts'}
  },
  'Glyphs' => {
    1 => {:name => 'Warrior', :slug => 'warrior'},
    2 => {:name => 'Paladin', :slug => 'paladin'},
    3 => {:name => 'Hunter', :slug => 'hunter'},
    4 => {:name => 'Rogue', :slug => 'rogue'},
    5 => {:name => 'Priest', :slug => 'priest'},
    6 => {:name => 'Death Knight', :slug => 'death_knight'},
    7 => {:name => 'Shaman', :slug => 'shaman'},
    8 => {:name => 'Mage', :slug => 'mage'},
    9 => {:name => 'Warlock', :slug => 'warlock'},
    11 => {:name => 'Druid', :slug => 'druid'}
  }
}
INVENTORY_SLOTS =
{
  1 => {:name => 'Head', :slug => 'head', :armor => true},
  2 => {:name => 'Neck', :slug => 'neck'},
  3 => {:name => 'Shoulder', :slug => 'shoulder', :armor => true},
  4 => {:name => 'Shirt', :slug => 'shirt'},
  5 => {:name => 'Chest', :slug => 'chest', :armor => true},
  6 => {:name => 'Waist', :slug => 'waist', :armor => true},
  7 => {:name => 'Legs', :slug => 'legs', :armor => true},
  8 => {:name => 'Feet', :slug => 'feet', :armor => true},
  9 => {:name => 'Wrist', :slug => 'wrist', :armor => true},
  10 => {:name => 'Hands', :slug => 'hands', :armor => true},
  11 => {:name => 'Finger', :slug => 'finger'},
  12 => {:name => 'Trinket', :slug => 'trinket'},
  13 => {:name => 'One-Hand', :slug => 'one_hand'},
  14 => {:name => 'Shield', :slug => 'shield'},
  15 => {:name => 'Ranged', :slug => 'ranged'},
  16 => {:name => 'Back', :slug => 'back'},
  17 => {:name => 'Two-Hand', :slug => 'two_hand'},
  18 => {:name => 'Bag', :slug => 'bag'},
  19 => {:name => 'Tabard', :slug => 'tabard'},
  21 => {:name => 'Main Hand', :slug => 'main_hand'},
  22 => {:name => 'Off Hand', :slug => 'off_hand'},
  23 => {:name => 'Held In Off-hand', :slug => 'held_in_off_hand'},
  24 => {:name => 'Projectile', :slug => 'projectile'},
  25 => {:name => 'Thrown', :slug => 'thrown'},
  28 => {:name => 'Relic', :slug => 'relic'}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_id) ⇒ Item

Returns a new instance of Item.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/wowget/item.rb', line 248

def initialize(item_id)
  item_xml = Nokogiri::XML(open("http://www.wowhead.com/item=#{item_id}&xml"))
  if item_id.nil?
    self.error = {:error => "no item ID supplied"}
  elsif item_xml.css('wowhead error').length == 1
    self.error = {:error => "not found"}
  else
    item_json              = JSON "{#{item_xml.css('wowhead item json').inner_text.strip.to_s}}"
    item_equip_json        = JSON "{#{item_xml.css('wowhead item jsonEquip').inner_text.strip.to_s}}"
    self.id                = item_id.to_i
    self.name              = item_xml.css('wowhead item name').inner_text.strip.to_s
    self.level             = item_xml.css('wowhead item level').inner_text.strip.to_i
    self.quality_id        = item_xml.css('wowhead item quality').attribute('id').content.to_i
    self.category_id       = item_xml.css('wowhead item class').attribute('id').content.to_i
    self.subcategory_id    = item_xml.css('wowhead item subclass').attribute('id').content.to_i
    self.icon_id           = item_xml.css('wowhead item icon').attribute('displayId').content.to_i
    self.icon_name         = item_xml.css('wowhead item icon').inner_text.strip.to_s
    self.required_level    = item_json['reqlevel']
    self.inventory_slot_id = item_xml.css('wowhead item inventorySlot').attribute('id').content.to_i
    self.buy_price         = item_equip_json['buyprice'].to_i
    self.sell_price        = item_equip_json['sellprice'].to_i
    self.soulbound         = item_xml.css('wowhead item htmlTooltip').inner_text.match('Binds when picked up') ? true : false

    if item_xml.css('wowhead item createdBy').length == 1
      self.recipe_id = item_xml.css('wowhead item createdBy spell').attribute('id').content.to_i
    end
  end
end

Instance Attribute Details

#buy_priceObject

Returns the value of attribute buy_price.



19
20
21
# File 'lib/wowget/item.rb', line 19

def buy_price
  @buy_price
end

#category_idObject

Returns the value of attribute category_id.



13
14
15
# File 'lib/wowget/item.rb', line 13

def category_id
  @category_id
end

#errorObject

Returns the value of attribute error.



23
24
25
# File 'lib/wowget/item.rb', line 23

def error
  @error
end

#icon_idObject

Returns the value of attribute icon_id.



15
16
17
# File 'lib/wowget/item.rb', line 15

def icon_id
  @icon_id
end

#icon_nameObject

Returns the value of attribute icon_name.



16
17
18
# File 'lib/wowget/item.rb', line 16

def icon_name
  @icon_name
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/wowget/item.rb', line 9

def id
  @id
end

#inventory_slot_idObject

Returns the value of attribute inventory_slot_id.



18
19
20
# File 'lib/wowget/item.rb', line 18

def inventory_slot_id
  @inventory_slot_id
end

#levelObject

Returns the value of attribute level.



11
12
13
# File 'lib/wowget/item.rb', line 11

def level
  @level
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/wowget/item.rb', line 10

def name
  @name
end

#quality_idObject

Returns the value of attribute quality_id.



12
13
14
# File 'lib/wowget/item.rb', line 12

def quality_id
  @quality_id
end

#recipe_idObject

Returns the value of attribute recipe_id.



21
22
23
# File 'lib/wowget/item.rb', line 21

def recipe_id
  @recipe_id
end

#required_levelObject

Returns the value of attribute required_level.



17
18
19
# File 'lib/wowget/item.rb', line 17

def required_level
  @required_level
end

#sell_priceObject

Returns the value of attribute sell_price.



20
21
22
# File 'lib/wowget/item.rb', line 20

def sell_price
  @sell_price
end

#soulboundObject

Returns the value of attribute soulbound.



22
23
24
# File 'lib/wowget/item.rb', line 22

def soulbound
  @soulbound
end

#subcategory_idObject

Returns the value of attribute subcategory_id.



14
15
16
# File 'lib/wowget/item.rb', line 14

def subcategory_id
  @subcategory_id
end

Class Method Details

.find(query) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/wowget/item.rb', line 191

def self.find(query)
  item_ids = []
  items    = []
  
  if query.class == Fixnum
    # easy — e.g. 12345
    item_ids << query
  elsif query.class == String
    # try parsing a number, e.g. "12345"
    item_id = id_from_string(query)
    
    unless item_id.nil?
      item_ids << item_id
    else
      # try searching for this item by name
      item_redirect = Net::HTTP.get_response(URI.parse("http://www.wowhead.com/search?q=#{uri_escape(query)}"))["Location"]
      if item_redirect
        item_ids << item_redirect.match(/^\/item=([1-9][0-9]*)$/)[1].to_i
      else
        # try retrieving a list of items that match the supplied query
        begin
          # horrendously messy. fuck you very much, wowhead.
          item_json = JSON Nokogiri::XML(open("http://www.wowhead.com/search?q=#{uri_escape(query)}")).inner_text.match(/new Listview\(\{template: 'item'.*, data: (\[.*\])\}\);$/)[1]
        rescue
          no_results = true
        end
        
        if no_results || item_json.length == 0
          self.error = {:error => "no items found"}
        else
          item_json.each do |item|
            item_ids << item["id"].to_i
          end
        end
      end
    end
  elsif query.class == NilClass
    item_ids << nil
  end
  
  if item_ids.length > 0
    item_ids.each do |item_id|
      items << Wowget::Item.new(item_id)
    end
  end
  
  items.length == 1 ? items[0] : items
end

.inventory_slot_from_slug(slug) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/wowget/item.rb', line 240

def self.inventory_slot_from_slug(slug)
  found = nil
  INVENTORY_SLOTS.each_pair do |id, slot|
    found = slot if slot[:slug] == slug
  end
  found
end

Instance Method Details

#categoryObject



281
282
283
# File 'lib/wowget/item.rb', line 281

def category
  CATEGORIES[self.category_id]
end

#category_nameObject



285
286
287
# File 'lib/wowget/item.rb', line 285

def category_name
  CATEGORIES[self.category_id][:name]
end

#category_slugObject



289
290
291
# File 'lib/wowget/item.rb', line 289

def category_slug
  CATEGORIES[self.category_id][:slug]
end

#inventory_slotObject



305
306
307
# File 'lib/wowget/item.rb', line 305

def inventory_slot
  INVENTORY_SLOTS[self.inventory_slot_id]
end

#inventory_slot_nameObject



309
310
311
# File 'lib/wowget/item.rb', line 309

def inventory_slot_name
  self.inventory_slot_id == 0 ? nil : INVENTORY_SLOTS[self.inventory_slot_id][:name]
end

#inventory_slot_slugObject



313
314
315
# File 'lib/wowget/item.rb', line 313

def inventory_slot_slug
  self.inventory_slot_id == 0 ? nil : INVENTORY_SLOTS[self.inventory_slot_id][:slug]      
end

#qualityObject



277
278
279
# File 'lib/wowget/item.rb', line 277

def quality
  QUALITIES[self.quality_id]
end

#subcategoryObject



293
294
295
# File 'lib/wowget/item.rb', line 293

def subcategory
  SUBCATEGORIES[self.category[:name]][self.subcategory_id]
end

#subcategory_nameObject



297
298
299
# File 'lib/wowget/item.rb', line 297

def subcategory_name
  SUBCATEGORIES[self.category[:name]][self.subcategory_id][:name]
end

#subcategory_slugObject



301
302
303
# File 'lib/wowget/item.rb', line 301

def subcategory_slug
  SUBCATEGORIES[self.category[:name]][self.subcategory_id][:slug]
end


317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/wowget/item.rb', line 317

def to_link
  color = case self.quality.downcase.to_sym
  when :poor then 'white'
  when :common then 'white'
  when :uncommon then 'green'
  when :rare then 'blue'
  when :epic then 'magenta'
  when :legendary then 'red'
  when :artifact then 'yellow'
  when :heirloom then 'yellow'
  end
  Colored.colorize "[#{self.name}]", :foreground => color
end