moneybags 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.
- data/History.txt +9 -0
- data/Manifest.txt +6 -0
- data/README.txt +46 -0
- data/Rakefile +9 -0
- data/lib/moneybags.rb +53 -0
- data/spec/spec_moneybags.rb +44 -0
- metadata +73 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
= moneybags
|
2
|
+
|
3
|
+
* http://github.com/bianster/moneybags/tree
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
* Currency conversion API based on Yahoo Finance
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Query present conversion rates between 2 currencies
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
Moneybags.lookup('SGD', 'USD')
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
== INSTALL:
|
20
|
+
|
21
|
+
* gem install moneybags
|
22
|
+
|
23
|
+
== LICENSE:
|
24
|
+
|
25
|
+
(The MIT License)
|
26
|
+
|
27
|
+
Copyright (c) 2009
|
28
|
+
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
30
|
+
a copy of this software and associated documentation files (the
|
31
|
+
'Software'), to deal in the Software without restriction, including
|
32
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
33
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
34
|
+
permit persons to whom the Software is furnished to do so, subject to
|
35
|
+
the following conditions:
|
36
|
+
|
37
|
+
The above copyright notice and this permission notice shall be
|
38
|
+
included in all copies or substantial portions of the Software.
|
39
|
+
|
40
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
41
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
42
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
43
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
44
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
45
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
46
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/moneybags.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
class Moneybags
|
5
|
+
VERSION = "0.2.0"
|
6
|
+
|
7
|
+
class ServiceUnavailableError < StandardError ; end
|
8
|
+
class MissingRateError < StandardError ; end
|
9
|
+
|
10
|
+
module RemoteService
|
11
|
+
def initialize
|
12
|
+
make_request(remote_service_url)
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_request(url)
|
16
|
+
Net::HTTP.get(URI.parse(url))
|
17
|
+
rescue Net::HTTPError
|
18
|
+
raise ServiceUnavailableError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class YahooCurrencyService
|
23
|
+
class <<self
|
24
|
+
include RemoteService
|
25
|
+
|
26
|
+
def lookup(from, to)
|
27
|
+
rate = make_request("http://download.finance.yahoo.com/d/quotes.csv?s=#{from}#{to}=X&f=l1&e=.csv").strip.chomp.to_f
|
28
|
+
raise MissingRateError, "response: #{rate}, from: #{from}, to: #{to}" if rate == 0.0
|
29
|
+
|
30
|
+
rate
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class <<self
|
36
|
+
def lookup(from, to)
|
37
|
+
key = build_key(from, to)
|
38
|
+
YahooCurrencyService.lookup(from, to)
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_key(from, to)
|
42
|
+
[sanitize_currency_code!(from),
|
43
|
+
sanitize_currency_code!(to)]
|
44
|
+
end
|
45
|
+
|
46
|
+
def sanitize_currency_code!(code)
|
47
|
+
code.gsub!(/[^A-Z]+/, '') rescue nil
|
48
|
+
code.strip! rescue nil
|
49
|
+
raise ArgumentError if code.nil? || code.empty?
|
50
|
+
code
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'spec'
|
5
|
+
require "moneybags"
|
6
|
+
|
7
|
+
describe Moneybags do
|
8
|
+
it "should raise ArgumentError with empty currency codes" do
|
9
|
+
lambda { Moneybags.lookup(nil, 'USD') }.should raise_error(ArgumentError)
|
10
|
+
lambda { Moneybags.lookup('', 'USD') }.should raise_error(ArgumentError)
|
11
|
+
|
12
|
+
lambda { Moneybags.lookup('USD', nil) }.should raise_error(ArgumentError)
|
13
|
+
lambda { Moneybags.lookup('USD', '') }.should raise_error(ArgumentError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should sanitize currency codes" do
|
17
|
+
Moneybags::YahooCurrencyService.should_receive(:lookup).with('USD', 'SGD').and_return(2)
|
18
|
+
Moneybags.lookup('_USD', 'SGD_')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Moneybags, "looking up a missing rate in Moneybags::YahooCurrencyService" do
|
23
|
+
before(:each) do
|
24
|
+
Moneybags::YahooCurrencyService.stub!(:lookup).and_raise(Moneybags::MissingRateError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should raise Moneybags::MissingRateError" do
|
28
|
+
lambda { Moneybags.lookup('SGD', 'USD') }.should raise_error(Moneybags::MissingRateError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Moneybags::YahooCurrencyService do
|
33
|
+
it "should return the rate for the requested lookup" do
|
34
|
+
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")
|
35
|
+
|
36
|
+
Moneybags::YahooCurrencyService.lookup('FJD', 'USD').should == 0.668
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should raise Moneybags::MissingRateError for a rate of 0.0" do
|
40
|
+
Net::HTTP.stub!(:get).and_return("0.0\r\n")
|
41
|
+
|
42
|
+
lambda { Moneybags::YahooCurrencyService.lookup('XXX', 'USD') }.should raise_error(Moneybags::MissingRateError, "response: 0.0, from: XXX, to: USD")
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moneybags
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Douglas Tan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-30 00:00:00 +08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.12.2
|
24
|
+
version:
|
25
|
+
description: "* Currency conversion API based on Yahoo Finance"
|
26
|
+
email:
|
27
|
+
- douglas@practicalguile.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/moneybags.rb
|
42
|
+
- spec/spec_moneybags.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/bianster/moneybags/tree
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --main
|
50
|
+
- README.txt
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: moneybags
|
68
|
+
rubygems_version: 1.3.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: "* Currency conversion API based on Yahoo Finance"
|
72
|
+
test_files: []
|
73
|
+
|