wowecon 0.1.0

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.
@@ -0,0 +1,41 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require 'wowecon/currency.rb'
4
+
5
+ module Wowecon
6
+ def self.price(item_name, opts={})
7
+ request_url = "http://data.wowecon.com/?type=price&item_name=#{uri_escape(item_name)}"
8
+ opts.each_pair {|name, value| request_url += "&#{name.to_s}=#{uri_escape(value)}" }
9
+ response = Nokogiri::HTML(open(request_url))
10
+
11
+ if response.css('table.wowecon_wowcurrency tr td').length > 0
12
+ currency_types = {:g => :gold, :s => :silver, :c => :copper}
13
+ currency_type = nil
14
+ currency = {:gold => 0, :silver => 0, :copper => 0}
15
+
16
+ cells = response.css('table.wowecon_wowcurrency tr td')
17
+ cells.reverse_each do |cell|
18
+ if img = cell.at_css('img')
19
+ coin = img.attribute('alt').content.match(/([gsc])/)
20
+ currency_type = currency_types[coin[0].to_sym]
21
+ next
22
+ end
23
+
24
+ value = cell.inner_text.strip.match(/([0-9]+)/)
25
+ if value
26
+ currency[currency_type] = value[0].to_i
27
+ end
28
+ end
29
+
30
+ currency.merge({:float => (currency[:gold] + (currency[:silver].to_f / 100) + (currency[:copper].to_f / 10000)).to_f})
31
+ else
32
+ {:error => "price not found"}
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def self.uri_escape(string)
39
+ URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
40
+ end
41
+ end
@@ -0,0 +1,52 @@
1
+ module Wowecon
2
+ class Currency
3
+
4
+ attr_accessor :gold, :silver, :copper
5
+
6
+ def initialize(value)
7
+ if value.class == Float
8
+ f = sprintf("%.4f", value).split(".")
9
+ self.gold = f[0].to_i
10
+ self.silver = f[1][0,2].to_i
11
+ self.copper = f[1][2,2].to_i
12
+ elsif value.class == Hash
13
+ self.gold = value[:gold]
14
+ self.silver = value[:silver]
15
+ self.copper = value[:copper]
16
+ end
17
+ end
18
+
19
+ def self.from_hash(denoms)
20
+ (denoms[:gold] + (denoms[:silver].to_f / 100) + (denoms[:copper].to_f / 10000)).to_f
21
+ end
22
+
23
+ def to_hash
24
+ {:gold => @gold, :silver => @silver, :copper => @copper}
25
+ end
26
+
27
+ def to_f
28
+ (@gold + (@silver.to_f / 100) + (@copper.to_f / 10000)).to_f
29
+ end
30
+
31
+ def to_s
32
+ output = ""
33
+ self.to_hash.each do |denom, value|
34
+ if value > 0 || output.length > 0
35
+ output += "#{value.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")}#{denom.to_s[0,1]} "
36
+ end
37
+ end
38
+ output.strip
39
+ end
40
+
41
+ def to_html
42
+ tags = ""
43
+ self.to_hash.each do |denom, value|
44
+ if value > 0 || tags.length > 0
45
+ tags += "<var class=\"#{denom.to_s}\">#{value.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")}</var>"
46
+ end
47
+ end
48
+ tags
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "../lib/wowecon/currency.rb"
2
+
3
+ describe Wowecon::Currency do
4
+
5
+ before :each do
6
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
7
+ end
8
+
9
+ it "can be initialised using a floating point value" do
10
+ currency = Wowecon::Currency.new(1420.091)
11
+ currency.class.should == Wowecon::Currency
12
+ end
13
+
14
+ it "can be initialised using a hash" do
15
+ currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
16
+ currency.class.should == Wowecon::Currency
17
+ end
18
+
19
+ it "responds to hash" do
20
+ (@currency.should respond_to :to_hash) && @currency.to_hash.should == {:gold => 1420, :silver => 9, :copper => 10}
21
+ end
22
+
23
+ it "responds to float" do
24
+ (@currency.should respond_to :to_f) && @currency.to_f.should == 1420.091
25
+ end
26
+
27
+ it "can create a string representation of the currency value" do
28
+ @currency.to_s.should == "1,420g 9s 10c"
29
+ end
30
+
31
+ it "can create a HTML tag representation of the currency value" do
32
+ @currency.to_html.should == '<var class="gold">1,420</var><var class="silver">9</var><var class="copper">10</var>'
33
+ end
34
+
35
+ end
@@ -0,0 +1,45 @@
1
+ require_relative "../lib/wowecon.rb"
2
+
3
+ describe Wowecon do
4
+ realm = { :name => "Alonsus", :region => "EU", :faction => "alliance" }
5
+
6
+ describe "With a valid item" do
7
+ price = Wowecon.price("Rock Furrow Boots")
8
+
9
+ unless price.has_key? :error
10
+ describe "if price data was found" do
11
+ it "should have an integer gold value" do
12
+ (price.should have_key :gold) && price[:gold].class.should == Fixnum
13
+ end
14
+
15
+ it "should have an integer silver value" do
16
+ (price.should have_key :silver) && price[:silver].class.should == Fixnum
17
+ end
18
+
19
+ it "should have an integer copper value" do
20
+ (price.should have_key :copper) && price[:copper].class.should == Fixnum
21
+ end
22
+
23
+ it "should have float value representing the complete price" do
24
+ (price.should have_key :float) && price[:float].class.should == Float
25
+ end
26
+ end
27
+ else
28
+ describe "if no price data was found" do
29
+ it "should return an error" do
30
+ price.should == {:error => "price not found"}
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ # use http://data.wowecon.com/?type=price&item_name=Rock%20Furrow%20Boots to confirm
38
+ describe "With known market data" do
39
+ it "should return a value of 7401.0 for this item" do
40
+ price = Wowecon.price("Rock Furrow Boots")
41
+ price.should == {:gold=>7401, :silver=>0, :copper=>0, :float=>7401.0}
42
+ end
43
+ end
44
+
45
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wowecon
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Ben Darlow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-30 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: nokogiri
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "1.4"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "2.6"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Ruby API to wowecon.com market data.
39
+ email: ben@kapowaz.net
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - lib/wowecon/currency.rb
48
+ - lib/wowecon.rb
49
+ - spec/currency_spec.rb
50
+ - spec/wowecon_spec.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/kapowaz/wowecon
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.9.2
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.6.2
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: wowecon
79
+ test_files:
80
+ - spec/currency_spec.rb
81
+ - spec/wowecon_spec.rb