active_exchange 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +36 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/active_exchange.gemspec +55 -0
- data/app/models/exchange_rate.rb +4 -0
- data/db/migrate/20091021192708_create_exchange_rates.rb +14 -0
- data/lib/active_exchange.rb +45 -0
- data/tasks/daily_exchange_rates.rake +12 -0
- data/tasks/data_migration_exchange_rates.rake +6 -0
- data/test/helper.rb +9 -0
- data/test/test_active_exchange.rb +13 -0
- metadata +70 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 nerbie69
|
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.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= active_exchange
|
2
|
+
|
3
|
+
Active Exchange is a Rails Engine based Gem that is using the XavierMedia API. You can now get daily Exchange rates that are then uploaded into your own ExchangeRate table in the Database for use inside your Rails app.
|
4
|
+
|
5
|
+
Using a Rake task, that is ran once a day, you collect all the exchange rate information.
|
6
|
+
|
7
|
+
Now inside your rails app, you can convert one currency into another, or save everything into one currency on the fly.
|
8
|
+
|
9
|
+
By going to XavierMedia once a day, you get stable rates to use throughout the day, which should be sufficient enough for your online store.
|
10
|
+
|
11
|
+
I personally wouldn't use this if my model was, say an F/X trader on a big time exchange where second by second data is needed, however.
|
12
|
+
|
13
|
+
== Useage
|
14
|
+
|
15
|
+
* add config.gem 'active_exchange' to your environment.rb file
|
16
|
+
* Run the rake task : "sudo rake active_exchange:seed" (you may not need sudo... your call)
|
17
|
+
then you will have your migrations in place.
|
18
|
+
* Run "rake db:migrate" to migrate the migration files.
|
19
|
+
* Run the rake task : "sudo rake active_exchange:daily_seed" to get the latest Exchange Rate data.
|
20
|
+
|
21
|
+
You can now integrate this into your app as you see fit.
|
22
|
+
|
23
|
+
== Note on Patches/Pull Requests
|
24
|
+
|
25
|
+
* Fork the project.
|
26
|
+
* Make your feature addition or bug fix.
|
27
|
+
* Add tests for it. This is important so I don't break it in a
|
28
|
+
future version unintentionally.
|
29
|
+
* Commit, do not mess with rakefile, version, or history.
|
30
|
+
(if you want to have your own version, that is fine but
|
31
|
+
bump version in a commit by itself I can ignore when I pull)
|
32
|
+
* Send me a pull request. Bonus points for topic branches.
|
33
|
+
|
34
|
+
== Copyright
|
35
|
+
|
36
|
+
Copyright (c) 2009 nerbie69. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "active_exchange"
|
8
|
+
gem.summary = %Q{Foreign Exchange for your rails app}
|
9
|
+
gem.description = %Q{Foreign Exchange for your Rails app.}
|
10
|
+
gem.email = "philipjingram@yahoo.com"
|
11
|
+
gem.homepage = "http://github.com/nerbie69/active_exchange"
|
12
|
+
gem.authors = ["nerbie69"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "active_exchange #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{active_exchange}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["nerbie69"]
|
12
|
+
s.date = %q{2009-10-26}
|
13
|
+
s.description = %q{Foreign Exchange for your Rails app.}
|
14
|
+
s.email = %q{philipjingram@yahoo.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"active_exchange.gemspec",
|
27
|
+
"app/models/exchange_rate.rb",
|
28
|
+
"db/migrate/20091021192708_create_exchange_rates.rb",
|
29
|
+
"lib/active_exchange.rb",
|
30
|
+
"tasks/daily_exchange_rates.rake",
|
31
|
+
"tasks/data_migration_exchange_rates.rake",
|
32
|
+
"test/helper.rb",
|
33
|
+
"test/test_active_exchange.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/nerbie69/active_exchange}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{Foreign Exchange for your rails app}
|
40
|
+
s.test_files = [
|
41
|
+
"test/helper.rb",
|
42
|
+
"test/test_active_exchange.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
else
|
51
|
+
end
|
52
|
+
else
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "cgi"
|
2
|
+
require "uri"
|
3
|
+
require "net/https"
|
4
|
+
require "rexml/document"
|
5
|
+
require "date"
|
6
|
+
|
7
|
+
module ActiveExchange
|
8
|
+
# Originially massaged from the web post of http://geoff.evason.name/2008/10/27/simple-currency-conversion-rate-api-consumption-for-ruby-rails/
|
9
|
+
# The active_exchange gem is used to go out to XavierMedia and pull quotes for the currencies they offer.
|
10
|
+
|
11
|
+
# We'll need to have a task to look for new data and parse it into DB, to cut down calls.
|
12
|
+
def self.retrieve_data(date = Date.today)
|
13
|
+
url = URI.parse("http://api.finance.xaviermedia.com/api/#{date.year}/#{date.strftime("%m")}/#{date.strftime("%d")}.xml")
|
14
|
+
resp = Net::HTTP.get(url)
|
15
|
+
xml = REXML::Document.new(resp)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.get_daily_data
|
19
|
+
#Get data
|
20
|
+
daily_rates = ActiveExchange.retrieve_data
|
21
|
+
#put into database. Run the migrations, obviously, before you do this.
|
22
|
+
if daily_rates.root.attributes["responsecode"] == "200"
|
23
|
+
daily_rates.elements.each("//exchange_rates/fx") { |element|
|
24
|
+
ExchangeRate.create(:currency => element.elements[1].text, :rate => element.elements[2].text)
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
#used to find an exchange rate on a certain day. this will make a call to the api.
|
30
|
+
def self.exchange_rate_on(date, currency_from = "USD", currency_to = "AUD")
|
31
|
+
xml = retrieve_data(date)
|
32
|
+
us_to_eur = 1.0
|
33
|
+
au_to_eur = 1.0
|
34
|
+
xml.elements.each("//exchange_rates/fx") { |el|
|
35
|
+
if el.elements[1].text == currency_from
|
36
|
+
us_to_eur = el.elements[2].text.to_f rescue 1.0
|
37
|
+
end
|
38
|
+
if el.elements[1].text == currency_to
|
39
|
+
au_to_eur = el.elements[2].text.to_f rescue 1.0
|
40
|
+
end
|
41
|
+
}
|
42
|
+
|
43
|
+
return us_to_eur/au_to_eur
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "active_exchange"
|
2
|
+
namespace :active_exchange do
|
3
|
+
desc "Go out to Xavier and get the Exchange Data for Today and put it into the ExchangeRate db"
|
4
|
+
task :daily_seed => :environment do
|
5
|
+
daily_rates = ActiveExchange.retrieve_data
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Sync migration files into existing RAILS app - to be done inside rails apps."
|
9
|
+
task :sync do
|
10
|
+
system "rsync -ruv #{File.expand_path(File.dirname(__FILE__) + "/db/migrate")} #{RAILS_ROOT}/db"
|
11
|
+
end
|
12
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestActiveExchange < Test::Unit::TestCase
|
4
|
+
def test_it_works
|
5
|
+
assert_nothing_thrown do
|
6
|
+
actual = ActiveExchange.exchange_rate_on(Date.today, currency_from = "CAD", currency_to = "AUD")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
def test_for_amount_on_oct_seventh
|
10
|
+
actual = ActiveExchange.exchange_rate_on(Date.parse("10/07/2009"))
|
11
|
+
assert_equal("0.889898255813954", actual.to_s)
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_exchange
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nerbie69
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-26 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Foreign Exchange for your Rails app.
|
17
|
+
email: philipjingram@yahoo.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- active_exchange.gemspec
|
33
|
+
- app/models/exchange_rate.rb
|
34
|
+
- db/migrate/20091021192708_create_exchange_rates.rb
|
35
|
+
- lib/active_exchange.rb
|
36
|
+
- tasks/daily_exchange_rates.rake
|
37
|
+
- tasks/data_migration_exchange_rates.rake
|
38
|
+
- test/helper.rb
|
39
|
+
- test/test_active_exchange.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/nerbie69/active_exchange
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Foreign Exchange for your rails app
|
68
|
+
test_files:
|
69
|
+
- test/helper.rb
|
70
|
+
- test/test_active_exchange.rb
|