wowget 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/wowget/item.rb +56 -21
  2. data/spec/item_spec.rb +21 -3
  3. metadata +6 -4
data/lib/wowget/item.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'net/http'
1
2
  require 'open-uri'
2
3
  require 'nokogiri'
3
4
  require 'json'
@@ -185,30 +186,54 @@ module Wowget
185
186
  28 => 'Relic'
186
187
  }
187
188
 
188
- def initialize(item_id)
189
- item_xml = Nokogiri::XML(open("http://www.wowhead.com/item=#{item_id}&xml"))
190
- if item_xml.css('wowhead error').length == 1
191
- self.error = {:error => "not found"}
192
- else
193
- item_json = JSON "{#{item_xml.css('wowhead item json').inner_text.strip.to_s}}"
194
- item_equip_json = JSON "{#{item_xml.css('wowhead item jsonEquip').inner_text.strip.to_s}}"
195
- self.id = item_id.to_i
196
- self.name = item_xml.css('wowhead item name').inner_text.strip.to_s
197
- self.level = item_xml.css('wowhead item level').inner_text.strip.to_i
198
- self.quality_id = item_xml.css('wowhead item quality').attribute('id').content.to_i
199
- self.item_class_id = item_xml.css('wowhead item class').attribute('id').content.to_i
200
- self.item_subclass_id = item_xml.css('wowhead item subclass').attribute('id').content.to_i
201
- self.icon_id = item_xml.css('wowhead item icon').attribute('displayId').content.to_i
202
- self.icon_name = item_xml.css('wowhead item icon').inner_text.strip.to_s
203
- self.required_level = item_json['reqlevel']
204
- self.inventory_slot_id = item_xml.css('wowhead item inventorySlot').attribute('id').content.to_i
205
- self.buy_price = item_equip_json['buyprice']
206
- self.sell_price = item_equip_json['sellprice']
189
+ def initialize(query)
190
+
191
+ if query.class == Fixnum
192
+ # easy e.g. 12345
193
+ item_id = query
194
+ elsif query.class == String
195
+ # try parsing a number, e.g. "12345"
196
+ item_id = id_from_string(query)
207
197
 
208
- if item_xml.css('wowhead item createdBy').length == 1
209
- self.recipe_id = item_xml.css('wowhead item createdBy spell').attribute('id').content.to_i
198
+ # try searching for this item by name
199
+ unless item_id
200
+ item_redirect = Net::HTTP.get_response(URI.parse("http://www.wowhead.com/search?q=#{uri_escape(query)}"))["Location"]
201
+ if item_redirect
202
+ item_id = item_redirect.match(/^\/item=([1-9][0-9]*)$/)[1].to_i
203
+ end
210
204
  end
211
205
  end
206
+
207
+
208
+ if item_id
209
+ item_xml = Nokogiri::XML(open("http://www.wowhead.com/item=#{item_id}&xml"))
210
+ if item_xml.css('wowhead error').length == 1
211
+ self.error = {:error => "not found"}
212
+ else
213
+ item_json = JSON "{#{item_xml.css('wowhead item json').inner_text.strip.to_s}}"
214
+ item_equip_json = JSON "{#{item_xml.css('wowhead item jsonEquip').inner_text.strip.to_s}}"
215
+ self.id = item_id.to_i
216
+ self.name = item_xml.css('wowhead item name').inner_text.strip.to_s
217
+ self.level = item_xml.css('wowhead item level').inner_text.strip.to_i
218
+ self.quality_id = item_xml.css('wowhead item quality').attribute('id').content.to_i
219
+ self.item_class_id = item_xml.css('wowhead item class').attribute('id').content.to_i
220
+ self.item_subclass_id = item_xml.css('wowhead item subclass').attribute('id').content.to_i
221
+ self.icon_id = item_xml.css('wowhead item icon').attribute('displayId').content.to_i
222
+ self.icon_name = item_xml.css('wowhead item icon').inner_text.strip.to_s
223
+ self.required_level = item_json['reqlevel']
224
+ self.inventory_slot_id = item_xml.css('wowhead item inventorySlot').attribute('id').content.to_i
225
+ self.buy_price = item_equip_json['buyprice']
226
+ self.sell_price = item_equip_json['sellprice']
227
+
228
+ if item_xml.css('wowhead item createdBy').length == 1
229
+ self.recipe_id = item_xml.css('wowhead item createdBy spell').attribute('id').content.to_i
230
+ end
231
+ end
232
+ elsif query.class == NilClass
233
+ self.error = {:error => "no item ID supplied"}
234
+ else
235
+ self.error = {:error => "unsupported initialiser"}
236
+ end
212
237
  end
213
238
 
214
239
  def quality
@@ -227,5 +252,15 @@ module Wowget
227
252
  INVENTORY_SLOTS[self.inventory_slot_id]
228
253
  end
229
254
 
255
+ private
256
+
257
+ def id_from_string(string)
258
+ string.to_i if string.match /^[1-9][0-9]*$/
259
+ end
260
+
261
+ def uri_escape(string)
262
+ URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
263
+ end
264
+
230
265
  end
231
266
  end
data/spec/item_spec.rb CHANGED
@@ -67,6 +67,13 @@ describe Wowget::Item do
67
67
 
68
68
  end
69
69
 
70
+ describe "With an ID passed as a string" do
71
+ item = Wowget::Item.new("4817")
72
+ it "should have an item name" do
73
+ item.name.should == "Blessed Claymore"
74
+ end
75
+ end
76
+
70
77
  describe "With a recipe" do
71
78
  item = Wowget::Item.new(45559)
72
79
 
@@ -76,14 +83,25 @@ describe Wowget::Item do
76
83
  end
77
84
 
78
85
  describe "With an invalid ID" do
79
- item = Wowget::Item.new(nil)
86
+ item = Wowget::Item.new(-1000)
80
87
  it "should return an error" do
81
88
  item.error.should == {:error => "not found"}
82
- end
89
+ end
83
90
  end
84
91
 
85
- describe "With an item name" do
92
+ describe "With no item ID supplied" do
93
+ item = Wowget::Item.new(nil)
94
+ it "should return an error" do
95
+ item.error.should == {:error => "no item ID supplied"}
96
+ end
97
+ end
98
+
99
+ describe "When the name of an item is supplied" do
100
+ item = Wowget::Item.new("Blessed Claymore")
86
101
 
102
+ it "should find the appropriate item" do
103
+ item.id.should == 4817
104
+ end
87
105
  end
88
106
 
89
107
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wowget
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Darlow
@@ -75,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
75
  requirements:
76
76
  - - ">="
77
77
  - !ruby/object:Gem::Version
78
- version: "0"
78
+ version: 1.9.2
79
79
  required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  none: false
81
81
  requirements:
@@ -89,5 +89,7 @@ rubygems_version: 1.6.2
89
89
  signing_key:
90
90
  specification_version: 3
91
91
  summary: wowget
92
- test_files: []
93
-
92
+ test_files:
93
+ - spec/item_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/spell_spec.rb