currency_rates 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Alastair Brunton
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.
data/README ADDED
@@ -0,0 +1,56 @@
1
+ CurrencyRates
2
+ =============
3
+
4
+ Very simple class for getting up to date currency rates in an array. You pass a base currency which the rates will be converted into. Connects to the european bank
5
+ to get a daily breakdown of the rates.
6
+
7
+ This is deliberated lightweight to allow you to build on top of this.
8
+
9
+ Example
10
+ =======
11
+
12
+ <pre>
13
+ parser = CurrencyRates::Parser.new('GBP')
14
+ puts parser.engage.inspect
15
+ </pre>
16
+
17
+ Valid values for the initialize call are:
18
+
19
+ <pre>
20
+ "USD"
21
+ "JPY"
22
+ "BGN"
23
+ "CZK"
24
+ "DKK"
25
+ "EEK"
26
+ "GBP"
27
+ "HUF"
28
+ "LTL"
29
+ "LVL"
30
+ "PLN"
31
+ "RON"
32
+ "SEK"
33
+ "CHF"
34
+ "NOK"
35
+ "HRK"
36
+ "RUB"
37
+ "TRY"
38
+ "AUD"
39
+ "BRL"
40
+ "CAD"
41
+ "CNY"
42
+ "HKD"
43
+ "IDR"
44
+ "INR"
45
+ "KRW"
46
+ "MXN"
47
+ "MYR"
48
+ "NZD"
49
+ "PHP"
50
+ "SGD"
51
+ "THB"
52
+ "ZAR"
53
+ "EUR"
54
+ </pre>
55
+
56
+ Copyright (c) 2011 Alastair Brunton, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the currency_rates plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the currency_rates plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'CurrencyRates'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'currency_rates'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,66 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ # This is a little currency class that grabs rates from the european bank
5
+ # and converts them into the base currency of your choice.
6
+ module CurrencyRates
7
+
8
+ class Parser
9
+
10
+ XML_URL = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
11
+
12
+ def initialize(base_currency)
13
+ @base_currency = base_currency
14
+ end
15
+
16
+ def engage
17
+ doc = Nokogiri::XML(open(XML_URL))
18
+ currency_collection = doc.css("Cube > Cube > Cube")
19
+
20
+ # Just return if the base currency is EUR as that is
21
+ # the format of the xml in the firstplace.
22
+ return to_hash(currency_collection) if @base_currency == 'EUR'
23
+
24
+ rate = get_rate(currency_collection, @base_currency)
25
+ converted_rates = convert_rates(currency_collection, rate)
26
+
27
+ return to_hash(converted_rates) << euro_rate(rate)
28
+ end
29
+
30
+
31
+ private
32
+
33
+ def get_rate(currency_collection, currency_code = 'GBP')
34
+ currency_collection.each do |currency|
35
+ if currency['currency'].to_s == currency_code
36
+ return currency['rate']
37
+ end
38
+ end
39
+ raise "Unable to find the currency specified."
40
+ end
41
+
42
+ def convert_rates(currency_collection, rate)
43
+ currency_collection.each do |currency|
44
+ new_rate = currency['rate'].to_f / rate.to_f
45
+ currency['rate'] = format("%.5f", new_rate)
46
+ end
47
+ currency_collection
48
+ end
49
+
50
+ def euro_rate(rate)
51
+ new_rate = format("%.5f", 1 / rate.to_f)
52
+ {'currency' => 'EUR', 'rate' => new_rate}
53
+ end
54
+
55
+ def to_hash(converted_rates)
56
+ converted_rates.map do |rate|
57
+ {'currency' => rate['currency'], 'rate' => rate['rate']}
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+
64
+ end
65
+
66
+
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'shoulda'
5
+ require 'currency_rates'
6
+
7
+
8
+
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class TestCurrencyRates < ActiveSupport::TestCase
4
+
5
+ ['USD', 'GBP', 'EUR'].each do |currency|
6
+ should "get the rates for #{currency}" do
7
+ parser = CurrencyRates::Parser.new(currency)
8
+ rates = parser.engage
9
+ puts rates.inspect
10
+ assert(rates.size > 0, "Empty rates array or not an array")
11
+ end
12
+ end
13
+
14
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: currency_rates
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Alastair Brunton
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-26 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: A little library for grabbing currency rates and providing conversion between currencies.
34
+ email:
35
+ - info@simplyexcited.co.uk
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - MIT-LICENSE
44
+ - README
45
+ - Rakefile
46
+ - init.rb
47
+ - install.rb
48
+ - lib/currency_rates.rb
49
+ - test/test_helper.rb
50
+ - test/unit/test_currency_rates.rb
51
+ - uninstall.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/pyrat/currency_rates
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 1
76
+ - 3
77
+ - 1
78
+ version: 1.3.1
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Simple currency rates and conversion.
86
+ test_files: []
87
+