atech_foreign_currency 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ module Atech
2
+ module ForeignCurrency
3
+
4
+ class InvalidCurrency < StandardError; end
5
+
6
+ class << self
7
+ attr_accessor :cache_file
8
+
9
+ def currencies
10
+ @currencies || YAML.load(File.read(ForeignCurrency.cache_file))
11
+ end
12
+
13
+ def convert(value, currency)
14
+ raise InvalidCurrency, "'#{currency}' is not configured in '#{cache_file}" unless currencies[currency.to_sym]
15
+ value * currencies[currency.to_sym].to_f
16
+ end
17
+
18
+ def cache_file
19
+ @cache_file ||= File.expand_path('../../../data.yml', __FILE__)
20
+ end
21
+
22
+ def cache
23
+ require 'atech/foreign_currency/cacher'
24
+ Cacher.get
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'yaml'
4
+
5
+ module Atech
6
+ module ForeignCurrency
7
+ module Cacher
8
+ class << self
9
+
10
+ def get
11
+ url = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
12
+ uri = URI.parse(url)
13
+ data = Net::HTTP.start(uri.host, uri.port) {|http| http.get(uri.path) }.body
14
+
15
+ gbp = get_rate_from_string(data, 'GBP')
16
+ eur = 1 / gbp
17
+ usd = get_rate_from_string(data, 'USD')
18
+ usd = usd / gbp
19
+
20
+ cache_file = File.expand_path(ForeignCurrency.cache_file)
21
+ File.open(cache_file, 'w') { |f| f.write({:eur => eur, :usd => usd}.to_yaml)}
22
+ end
23
+
24
+ def get_rate_from_string(string, rate)
25
+ if s = string.match(/\<Cube currency='#{rate}' rate='(.*)'\/>/)
26
+ s[1].to_f
27
+ else
28
+ nil
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ module Atech
2
+ module ForeignCurrency
3
+ module ViewHelpers
4
+
5
+ def number_to_foreign_currency(value, options = {})
6
+ currency = options.delete(:currency)
7
+ options[:unit] ||= case currency
8
+ when :eur then '&euro;'
9
+ when :usd then '&#36;'
10
+ end
11
+ number_to_currency(ForeignCurrency.convert(value, currency), options)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atech_foreign_currency
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Cooke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-03 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email: adam@atechmedia.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/atech/foreign_currency/cacher.rb
21
+ - lib/atech/foreign_currency/view_helpers.rb
22
+ - lib/atech/foreign_currency.rb
23
+ homepage: http://atechmedia.com
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.10
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: A quick and simple currency converter
47
+ test_files: []