kieranj-currency_source 0.0.1

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/Readme.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = Read Me
2
+
3
+ CurrencySource - Fetches exchange rates from currencysource.com
4
+
5
+ by Kieran Johnson
6
+ http://github.com/kieranj/currency_source
7
+
8
+ == Installation:
9
+
10
+ $ gem sources -a http://gems.github.com/ (you only need to do this once)
11
+ $ gem install kieranj-currency_source
12
+
13
+ == Usage:
14
+
15
+ require 'rubygems'
16
+ require 'kieranj-currency_source'
17
+
18
+ c = CurrenySource::Currency.new("GBP")
19
+ c.exchange_rate("USD") # => "1.45"
20
+
data/history.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 0.1.0 2008-10-28
2
+
3
+ * Initial release.
@@ -0,0 +1,46 @@
1
+ module CurrencySource
2
+
3
+ Version = "0.0.1"
4
+
5
+ class InvalidCurrency < StandardError; end
6
+ class MissingExchangeRate < StandardError; end
7
+
8
+ class Currency
9
+
10
+ BaseURL = "http://www.currencysource.com/rss/"
11
+
12
+ attr_reader :currency
13
+
14
+ def initialize(currency)
15
+ raise InvalidCurrency unless Currencies::ValidCurrencies.include?(currency)
16
+ @currency = currency
17
+ end
18
+
19
+ def exchange_rate(other_currency)
20
+ raise InvalidCurrency unless Currencies::ValidCurrencies.include?(currency)
21
+ raise MissingExchangeRate unless exchange_rates.keys.include?(other_currency)
22
+ exchange_rates[other_currency]
23
+ end
24
+
25
+ def exchange_rates
26
+ @exchange_rates ||= parse_exchange_rates
27
+ end
28
+
29
+ private
30
+
31
+ def doc
32
+ @doc ||= Hpricot(open(BaseURL + @currency + ".xml"))
33
+ end
34
+
35
+ def parse_exchange_rates
36
+ doc.search("//item/title").inject({}) do |result, item|
37
+ i = item.inner_html
38
+ input, output, v1, v2 = i.match(/\d\s(\S{3}) = (\S{3}) \((\d+,\d+|\d+)(\.\d{2})/).captures
39
+ result[output] = v1 + v2
40
+ result
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,11 @@
1
+ module CurrencySource
2
+
3
+ require "rubygems"
4
+ require "hpricot"
5
+ require "open-uri"
6
+
7
+ %w(currency currencies).each do |lib|
8
+ require File.join(File.dirname(__FILE__), 'currency_source', lib)
9
+ end
10
+
11
+ end
data/license.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Kieran Johnson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ module CurrencySource
4
+
5
+ describe Currency do
6
+
7
+ before do
8
+ @currency = Currency.new("GBP")
9
+ @currency.stub!(:open).and_return(open(File.dirname(__FILE__) + "/fixtures/GBP.xml"))
10
+ end
11
+
12
+ it "should only except listed currencies" do
13
+ Currencies::ValidCurrencies.should include("GBP")
14
+ end
15
+
16
+ it "should not except unlisted currencies" do
17
+ lambda { Currency.new("ABC") }.should raise_error(InvalidCurrency)
18
+ end
19
+
20
+ it "should fetch the latest exchange rates for assigned currency" do
21
+ @currency.should_receive(:open).with("http://www.currencysource.com/rss/GBP.xml")
22
+ @currency.exchange_rate("USD")
23
+ end
24
+
25
+ it "should have a hash of exchange rates" do
26
+ @currency.exchange_rates.should be_instance_of(Hash)
27
+ end
28
+
29
+ it "should return the exchange rate" do
30
+ @currency.exchange_rate("USD").should match(/\d+\.\d+/)
31
+ end
32
+
33
+ it "should raise MissingExchangeRate for missing key" do
34
+ lambda { @currency.exchange_rate("ZED") }.should raise_error(MissingExchangeRate)
35
+ end
36
+
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kieranj-currency_source
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kieran Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-22 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Ruby library for fetching exchange rates from currency source
25
+ email: kieran[AT]invisiblelines.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - Readme.rdoc
32
+ files:
33
+ - history.txt
34
+ - license.txt
35
+ - lib/currency_source.rb
36
+ - lib/currency_source/currency.rb
37
+ - README.rdoc
38
+ - Readme.rdoc
39
+ has_rdoc: true
40
+ homepage: http://github.com/kieranj/currency_source
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --main
44
+ - Readme.rdoc
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: currency_source
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: Fetches exchange rates from Currency Source
66
+ test_files:
67
+ - spec/currency_spec.rb