bianster-currency-convertor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/currency-convertor.gemspec +18 -0
- data/currency_convertor.rb +4 -0
- data/lib/currency_convertor.rb +48 -0
- data/spec/currency_convertor_spec.rb +43 -0
- metadata +57 -0
data/README
ADDED
File without changes
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "currency-convertor"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.date = "2008-08-15"
|
5
|
+
s.summary = "Lookup exchange rates between currencies"
|
6
|
+
s.email = "douglas@practicalguile.com"
|
7
|
+
s.homepage = "http://github.com/bianster/currency-convertor"
|
8
|
+
s.description = ""
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["Douglas Tan"]
|
11
|
+
s.files = [
|
12
|
+
'currency-convertor.gemspec',
|
13
|
+
'README',
|
14
|
+
'currency_convertor.rb',
|
15
|
+
'lib/currency_convertor.rb',
|
16
|
+
'spec/currency_convertor_spec.rb'
|
17
|
+
]
|
18
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class CurrencyConvertor
|
2
|
+
class ServiceUnavailableError < StandardError ; end
|
3
|
+
class MissingRateError < StandardError ; end
|
4
|
+
|
5
|
+
module RemoteService
|
6
|
+
def initialize
|
7
|
+
make_request(remote_service_url)
|
8
|
+
end
|
9
|
+
|
10
|
+
def make_request(url)
|
11
|
+
Net::HTTP.get(URI.parse(url))
|
12
|
+
rescue Net::HTTPError
|
13
|
+
raise ServiceUnavailableError
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class YahooCurrencyService
|
18
|
+
class <<self
|
19
|
+
include RemoteService
|
20
|
+
|
21
|
+
def lookup(from, to)
|
22
|
+
rate = make_request("http://download.finance.yahoo.com/d/quotes.csv?s=#{from}#{to}=X&f=l1&e=.csv").strip.chomp.to_f
|
23
|
+
raise MissingRateError, "response: #{rate}, from: #{from}, to: #{to}" if rate == 0.0
|
24
|
+
|
25
|
+
rate
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class <<self
|
31
|
+
def lookup(from, to)
|
32
|
+
key = build_key(from, to)
|
33
|
+
YahooCurrencyService.lookup(from, to)
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_key(from, to)
|
37
|
+
[sanitize_currency_code!(from),
|
38
|
+
sanitize_currency_code!(to)]
|
39
|
+
end
|
40
|
+
|
41
|
+
def sanitize_currency_code!(code)
|
42
|
+
code.gsub!(/[^A-Z]+/, '') rescue nil
|
43
|
+
code.strip! rescue nil
|
44
|
+
raise ArgumentError if code.nil? || code.empty?
|
45
|
+
code
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../currency_convertor")
|
5
|
+
|
6
|
+
describe CurrencyConvertor do
|
7
|
+
it "should raise ArgumentError with empty currency codes" do
|
8
|
+
lambda { CurrencyConvertor.lookup(nil, 'USD') }.should raise_error(ArgumentError)
|
9
|
+
lambda { CurrencyConvertor.lookup('', 'USD') }.should raise_error(ArgumentError)
|
10
|
+
|
11
|
+
lambda { CurrencyConvertor.lookup('USD', nil) }.should raise_error(ArgumentError)
|
12
|
+
lambda { CurrencyConvertor.lookup('USD', '') }.should raise_error(ArgumentError)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should sanitize currency codes" do
|
16
|
+
CurrencyConvertor::YahooCurrencyService.should_receive(:lookup).with('USD', 'SGD').and_return(2)
|
17
|
+
CurrencyConvertor.lookup('_USD', 'SGD_')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe CurrencyConvertor, "looking up a missing rate in CurrencyConvertor::YahooCurrencyService" do
|
22
|
+
before(:each) do
|
23
|
+
CurrencyConvertor::YahooCurrencyService.stub!(:lookup).and_raise(CurrencyConvertor::MissingRateError)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should raise CurrencyConvertor::MissingRateError" do
|
27
|
+
lambda { CurrencyConvertor.lookup('SGD', 'USD') }.should raise_error(CurrencyConvertor::MissingRateError)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe CurrencyConvertor::YahooCurrencyService do
|
32
|
+
it "should return the rate for the requested lookup" do
|
33
|
+
Net::HTTP.should_receive(:get).with(URI.parse('http://download.finance.yahoo.com/d/quotes.csv?s=FJDUSD=X&f=l1&e=.csv')).and_return("0.668\r\n")
|
34
|
+
|
35
|
+
CurrencyConvertor::YahooCurrencyService.lookup('FJD', 'USD').should == 0.668
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should raise CurrencyConvertor::MissingRateError for a rate of 0.0" do
|
39
|
+
Net::HTTP.stub!(:get).and_return("0.0\r\n")
|
40
|
+
|
41
|
+
lambda { CurrencyConvertor::YahooCurrencyService.lookup('XXX', 'USD') }.should raise_error(CurrencyConvertor::MissingRateError, "response: 0.0, from: XXX, to: USD")
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bianster-currency-convertor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Douglas Tan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: douglas@practicalguile.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- currency-convertor.gemspec
|
26
|
+
- README
|
27
|
+
- currency_convertor.rb
|
28
|
+
- lib/currency_convertor.rb
|
29
|
+
- spec/currency_convertor_spec.rb
|
30
|
+
has_rdoc: false
|
31
|
+
homepage: http://github.com/bianster/currency-convertor
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: Lookup exchange rates between currencies
|
56
|
+
test_files: []
|
57
|
+
|