rails_currency 1.1 → 1.2
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.
- data/Rakefile +1 -1
- data/lib/rails_currency/convertor.rb +8 -8
- metadata +1 -1
data/Rakefile
CHANGED
@@ -12,29 +12,29 @@ end
|
|
12
12
|
module RailsCurrency
|
13
13
|
class RailsCurrencyError < StandardError
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
class InvalidAmountError < RailsCurrencyError
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
class UnknownCurrencyError < RailsCurrencyError
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
class ServerTimeoutError < RailsCurrencyError
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
class Convertor
|
26
26
|
class << self
|
27
27
|
def convert(amount, from, to)
|
28
28
|
raise InvalidAmountError, "Only numeric value allowed" unless amount.is_a?(Numeric)
|
29
29
|
raise UnknownCurrencyError, "Unknown Currency" unless CurrencyList.key?(from.to_sym) && CurrencyList.key?(to.to_sym)
|
30
|
-
|
30
|
+
|
31
31
|
begin
|
32
32
|
url = "#{CurrencyUrl}?Amount=#{amount}&From=#{from}&To=#{to}"
|
33
|
-
|
34
|
-
timeout(30) do
|
33
|
+
|
34
|
+
timeout(30) do
|
35
35
|
doc = open(url, Params) { |f| Hpricot(f) }
|
36
36
|
result = doc.search("//table[@class='XEtbl_sub']/tr/td[@id='XEenlarge']/h2[@class='XE']")[2].inner_text
|
37
|
-
result.scan(/\d+\.?\d+/).first.to_f
|
37
|
+
result.gsub(",", "").scan(/\d+\.?\d+/).first.to_f
|
38
38
|
end
|
39
39
|
rescue TimeoutError
|
40
40
|
raise ServerTimeoutError, "Can't connect to server #{url}"
|