wowecon 0.1.3 → 0.2.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.
@@ -27,8 +27,8 @@ module Wowecon
27
27
  currency[currency_type] = value[0].to_i
28
28
  end
29
29
  end
30
-
31
- currency.merge({:float => (currency[:gold] + (currency[:silver].to_f / 100) + (currency[:copper].to_f / 10000)).to_f})
30
+
31
+ {:value => "#{currency[:gold]}#{sprintf("%02d", currency[:silver])}#{sprintf("%02d", currency[:copper])}".to_i}
32
32
  else
33
33
  {:error => "price not found"}
34
34
  end
@@ -4,23 +4,16 @@ module Wowecon
4
4
  class Currency
5
5
  include Wowecon::CurrencyHelpers
6
6
 
7
- attr_accessor :gold, :silver, :copper
8
-
9
7
  def initialize(value)
10
- if value.class == Float
11
- f = sprintf("%.4f", value).split(".")
12
- self.gold = f[0].to_i
13
- self.silver = f[1][0,2].to_i
14
- self.copper = f[1][2,2].to_i
8
+ if value.class == Fixnum
9
+ @value = value
10
+ elsif value.class == Float
11
+ @value = sprintf("%.4f", value).split(".").join("").to_i
15
12
  elsif value.class == Hash
16
13
  if value.key?(:gold) and value.key?(:silver) and value.key?(:copper)
17
- self.gold = value[:gold]
18
- self.silver = value[:silver]
19
- self.copper = value[:copper]
14
+ @value = "#{value[:gold]}#{sprintf("%02d", value[:silver])}#{sprintf("%02d", value[:copper])}".to_i
20
15
  else
21
- self.gold = 0
22
- self.silver = 0
23
- self.copper = 0
16
+ @value = 0
24
17
  end
25
18
  end
26
19
  end
@@ -1,12 +1,21 @@
1
1
  module Wowecon
2
2
  module CurrencyHelpers
3
3
 
4
+ @value = 0
5
+
6
+ def to_i
7
+ @value
8
+ end
9
+
4
10
  def to_hash
5
- {:gold => @gold, :silver => @silver, :copper => @copper}
11
+ copper = @value.to_s[-2..-1].to_i
12
+ silver = @value.to_s[-4..-3].to_i
13
+ gold = @value.to_s[0..-5].to_i
14
+ {:gold => gold, :silver => silver, :copper => copper}
6
15
  end
7
16
 
8
17
  def to_f
9
- (@gold + (@silver.to_f / 100) + (@copper.to_f / 10000)).to_f
18
+ @value.to_f / 10000
10
19
  end
11
20
 
12
21
  def to_s
@@ -18,7 +27,7 @@ module Wowecon
18
27
  end
19
28
 
20
29
  if output == ""
21
- "no price"
30
+ "0"
22
31
  else
23
32
  output.strip
24
33
  end
@@ -34,5 +43,9 @@ module Wowecon
34
43
  tags
35
44
  end
36
45
 
46
+ def ==(other)
47
+ @value == other.to_i
48
+ end
49
+
37
50
  end
38
51
  end
@@ -16,6 +16,15 @@ describe Wowecon::Currency do
16
16
  currency.class.should == Wowecon::Currency
17
17
  end
18
18
 
19
+ it "can be initialised using an integer" do
20
+ currency = Wowecon::Currency.new(1420091)
21
+ currency.class.should == Wowecon::Currency
22
+ end
23
+
24
+ it "responds to integer" do
25
+ (@currency.should respond_to :to_i) && @currency.to_i.should == 14200910
26
+ end
27
+
19
28
  it "responds to hash" do
20
29
  (@currency.should respond_to :to_hash) && @currency.to_hash.should == {:gold => 1420, :silver => 9, :copper => 10}
21
30
  end
@@ -24,9 +33,9 @@ describe Wowecon::Currency do
24
33
  (@currency.should respond_to :to_f) && @currency.to_f.should == 1420.091
25
34
  end
26
35
 
27
- it "returns a currency value of zero if initialised with an invalid hash" do
36
+ it "returns zero if initialised with an invalid hash" do
28
37
  zero = Wowecon::Currency.new({:error => "price not found"})
29
- zero.to_s.should == "no price" && zero.to_f.should == 0 && zero.to_hash.should == {:gold => 0, :silver => 0, :copper => 0}
38
+ zero.should == 0
30
39
  end
31
40
 
32
41
  it "can create a string representation of the currency value" do
@@ -8,20 +8,8 @@ describe Wowecon do
8
8
 
9
9
  unless price.has_key? :error
10
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
11
+ it "should have an integer value" do
12
+ (price.should have_key :value) && price[:value].class.should == Fixnum
25
13
  end
26
14
  end
27
15
  else
@@ -36,9 +24,9 @@ describe Wowecon do
36
24
 
37
25
  # use http://data.wowecon.com/?type=price&item_name=Rock%20Furrow%20Boots to confirm
38
26
  describe "With known market data" do
39
- it "should return a value of 7401.0 for this item" do
27
+ it "should return a value of 62000100 for this item" do
40
28
  price = Wowecon.price("Rock Furrow Boots")
41
- price.should == {:gold=>7401, :silver=>0, :copper=>0, :float=>7401.0}
29
+ price.should == {:value => 62000100}
42
30
  end
43
31
  end
44
32
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wowecon
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Darlow
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-04 00:00:00 +01:00
13
+ date: 2011-07-05 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency