killbill-currency-plugin 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +25 -0
- data/Gemfile +5 -0
- data/Jarfile +6 -0
- data/README.md +4 -0
- data/Rakefile +17 -0
- data/VERSION +1 -0
- data/db/ddl.sql +20 -0
- data/db/schema.rb +25 -0
- data/killbill.properties +3 -0
- data/lib/currency_plugin.rb +5 -0
- data/lib/currency_plugin/api.rb +79 -0
- data/lib/currency_plugin/models/currency_rate.rb +15 -0
- data/lib/currency_plugin/models/currency_update.rb +17 -0
- data/pom.xml +36 -0
- data/release.sh +28 -0
- data/spec/currency_plugin/api_spec.rb +116 -0
- data/spec/currency_plugin/model_spec.rb +115 -0
- data/spec/spec_helper.rb +28 -0
- metadata +185 -0
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
coverage
|
6
|
+
InstalledFiles
|
7
|
+
lib/bundler/man
|
8
|
+
pkg
|
9
|
+
rdoc
|
10
|
+
spec/reports
|
11
|
+
test/tmp
|
12
|
+
test/version_tmp
|
13
|
+
tmp
|
14
|
+
build
|
15
|
+
test.db
|
16
|
+
killbill-currency-plugin*
|
17
|
+
# YARD artifacts
|
18
|
+
.yardoc
|
19
|
+
_yardoc
|
20
|
+
doc/
|
21
|
+
.jbundler
|
22
|
+
Jarfile.lock
|
23
|
+
Gemfile.lock
|
24
|
+
*.swp
|
25
|
+
.idea
|
data/Gemfile
ADDED
data/Jarfile
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
jar 'com.ning.billing:killbill-api', '0.7.2'
|
2
|
+
jar 'com.ning.billing.plugin:killbill-plugin-api-notification', '0.6.0'
|
3
|
+
jar 'com.ning.billing.plugin:killbill-plugin-api-payment', '0.6.0'
|
4
|
+
jar 'com.ning.billing.plugin:killbill-plugin-api-currency', '0.6.0'
|
5
|
+
jar 'com.ning.billing:killbill-util:tests', '0.7.0'
|
6
|
+
jar 'javax.servlet:javax.servlet-api', '3.0.1'
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
# Install tasks to build and release the plugin
|
4
|
+
require 'bundler/setup'
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
# Install test tasks
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
desc "Run RSpec"
|
10
|
+
RSpec::Core::RakeTask.new
|
11
|
+
|
12
|
+
# Install tasks to package the plugin for Killbill
|
13
|
+
require 'killbill/rake_task'
|
14
|
+
Killbill::PluginHelper.install_tasks
|
15
|
+
|
16
|
+
# Run tests by default
|
17
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/db/ddl.sql
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
CREATE TABLE `currency_updates` (
|
2
|
+
`id` int(11) NOT NULL AUTO_INCREMENT,
|
3
|
+
`base_currency` varchar(3) NOT NULL,
|
4
|
+
`conversion_date` datetime NOT NULL,
|
5
|
+
`created_at` datetime NOT NULL,
|
6
|
+
`updated_at` datetime NOT NULL,
|
7
|
+
PRIMARY KEY (`id`),
|
8
|
+
KEY `index_base_currency` (`base_currency`)
|
9
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin;;
|
10
|
+
|
11
|
+
CREATE TABLE `currency_rates` (
|
12
|
+
`id` int(11) NOT NULL AUTO_INCREMENT,
|
13
|
+
`target_currency` varchar(3) NOT NULL,
|
14
|
+
`rate` decimal(10,4) NOT NULL,
|
15
|
+
`currency_update_id` int(11) NOT NULL,
|
16
|
+
`created_at` datetime NOT NULL,
|
17
|
+
`updated_at` datetime NOT NULL,
|
18
|
+
PRIMARY KEY (`id`),
|
19
|
+
KEY `index_currency_update_id` (`currency_update_id`)
|
20
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin;;
|
data/db/schema.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define(:version => 20130911853636) do
|
4
|
+
create_table "currency_updates", :force => true do |t|
|
5
|
+
t.string "base_currency", :null => false
|
6
|
+
t.datetime "conversion_date", :null => false
|
7
|
+
t.datetime "created_at", :null => false
|
8
|
+
t.datetime "updated_at", :null => false
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index(:currency_updates, :base_currency)
|
12
|
+
|
13
|
+
create_table "currency_rates", :force => true do |t|
|
14
|
+
t.string "target_currency", :null => false
|
15
|
+
t.float "rate", :null => false
|
16
|
+
t.integer "currency_update_id", :null => false
|
17
|
+
t.datetime "created_at", :null => false
|
18
|
+
t.datetime "updated_at", :null => false
|
19
|
+
end
|
20
|
+
|
21
|
+
add_index(:currency_rates, :currency_update_id)
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
data/killbill.properties
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
require 'killbill'
|
4
|
+
|
5
|
+
require 'currency_plugin/models/currency_rate'
|
6
|
+
require 'currency_plugin/models/currency_update'
|
7
|
+
|
8
|
+
module Killbill
|
9
|
+
module CurrencyPlugin
|
10
|
+
class DefaultPlugin < Killbill::Plugin::Currency
|
11
|
+
|
12
|
+
def initialize()
|
13
|
+
@raise_exception = false
|
14
|
+
super()
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def start_plugin
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
# return DB connections to the Pool if required
|
23
|
+
def after_request
|
24
|
+
ActiveRecord::Base.connection.close
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_base_currencies(options = {})
|
28
|
+
(Killbill::CurrencyPlugin::CurrencyUpdate.distinct_base_currencies || []).map do |c|
|
29
|
+
c.base_currency
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_latest_conversion_date(base_currency, options = {})
|
34
|
+
res = Killbill::CurrencyPlugin::CurrencyUpdate.latest_base_currency(base_currency)
|
35
|
+
res[0].conversion_date.utc unless res.size == 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_conversion_dates(base_currency, options = {})
|
39
|
+
(Killbill::CurrencyPlugin::CurrencyUpdate.historical_base_currencies(base_currency) || []).map do |r|
|
40
|
+
r.conversion_date.utc
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_current_rates(base_currency, options = {})
|
45
|
+
|
46
|
+
base_latest = Killbill::CurrencyPlugin::CurrencyUpdate.latest_base_currency(base_currency)
|
47
|
+
if base_latest.nil? || base_latest.size == 0
|
48
|
+
return []
|
49
|
+
end
|
50
|
+
|
51
|
+
get_rates_for_currency_update(base_latest[0].id, base_currency, base_latest[0].conversion_date)
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_rates(base_currency, conversion_date, options = {})
|
55
|
+
|
56
|
+
(Killbill::CurrencyPlugin::CurrencyUpdate.historical_base_currencies(base_currency) || []).each do |e|
|
57
|
+
if Time.at(e.conversion_date) <= Time.at(conversion_date)
|
58
|
+
return get_rates_for_currency_update(e.id, base_currency, e.conversion_date)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
[]
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def get_rates_for_currency_update(currency_update_id, base_currency, conversion_date)
|
67
|
+
(Killbill::CurrencyPlugin::CurrencyRate.latest_rates_for_base_currency(currency_update_id) || []).map do |r|
|
68
|
+
rate = Killbill::Plugin::Model::Rate.new
|
69
|
+
rate.base_currency = base_currency
|
70
|
+
rate.currency = r.target_currency
|
71
|
+
rate.value = r.rate
|
72
|
+
rate.conversion_date = conversion_date
|
73
|
+
rate
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Killbill
|
2
|
+
module CurrencyPlugin
|
3
|
+
|
4
|
+
class CurrencyRate < ActiveRecord::Base
|
5
|
+
|
6
|
+
belongs_to :currency_update
|
7
|
+
|
8
|
+
attr_accessible :target_currency,
|
9
|
+
:rate,
|
10
|
+
:currency_update_id
|
11
|
+
|
12
|
+
scope :latest_rates_for_base_currency, ->(currency_update_id) { where("currency_update_id = ?", currency_update_id).order("target_currency ASC") }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Killbill
|
2
|
+
module CurrencyPlugin
|
3
|
+
class CurrencyUpdate < ActiveRecord::Base
|
4
|
+
|
5
|
+
has_many :currency_rates
|
6
|
+
|
7
|
+
attr_accessible :base_currency,
|
8
|
+
:conversion_date
|
9
|
+
|
10
|
+
scope :historical_base_currencies, ->(base_currency_arg) { where("base_currency = ?", base_currency_arg).order("conversion_date DESC") }
|
11
|
+
scope :latest_base_currency, ->(base_currency_arg) { historical_base_currencies(base_currency_arg).limit(1) }
|
12
|
+
scope :distinct_base_currencies, -> { select("DISTINCT base_currency").order("base_currency ASC") }
|
13
|
+
|
14
|
+
#scope :latest_rates_for_currency, ->(base_currency_arg) { latest_base_currency(base_currency_arg).joins(:currency_rates).order("target_currency ASC") }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/pom.xml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!--
|
3
|
+
~ Copyright 2010-2013 Ning, Inc.
|
4
|
+
~
|
5
|
+
~ Ning licenses this file to you under the Apache License, version 2.0
|
6
|
+
~ (the "License"); you may not use this file except in compliance with the
|
7
|
+
~ License. You may obtain a copy of the License at:
|
8
|
+
~
|
9
|
+
~ http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
~
|
11
|
+
~ Unless required by applicable law or agreed to in writing, software
|
12
|
+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
~ License for the specific language governing permissions and limitations
|
15
|
+
~ under the License.
|
16
|
+
-->
|
17
|
+
|
18
|
+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
19
|
+
<parent>
|
20
|
+
<groupId>org.sonatype.oss</groupId>
|
21
|
+
<artifactId>oss-parent</artifactId>
|
22
|
+
<version>5</version>
|
23
|
+
</parent>
|
24
|
+
<modelVersion>4.0.0</modelVersion>
|
25
|
+
<groupId>com.ning.killbill.ruby</groupId>
|
26
|
+
<artifactId>killbill-currency-plugin</artifactId>
|
27
|
+
<packaging>pom</packaging>
|
28
|
+
<version>1.0.0</version>
|
29
|
+
<name>killbill-currency-plugin</name>
|
30
|
+
<description></description>
|
31
|
+
<scm>
|
32
|
+
<connection>scm:git:git://github.com/killbill/killbill-killbill-currency-plugin.git</connection>
|
33
|
+
<url>https://github.com/killbill/killbill-killbill-currency-plugin/</url>
|
34
|
+
<developerConnection>scm:git:git@github.com:killbill/killbill-killbill-currency-plugin.git</developerConnection>
|
35
|
+
</scm>
|
36
|
+
</project>
|
data/release.sh
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
set -e
|
2
|
+
|
3
|
+
VERSION=`grep -E '<version>([0-9]+\.[0-9]+\.[0-9]+)</version>' pom.xml | sed 's/[\t \n]*<version>\(.*\)<\/version>[\t \n]*/\1/'`
|
4
|
+
if [ "$VERSION" != "$(cat $PWD/VERSION)" ]; then
|
5
|
+
echo "Unable to release: make sure the versions in pom.xml and VERSION match"
|
6
|
+
exit 1
|
7
|
+
fi
|
8
|
+
|
9
|
+
echo "Cleaning up"
|
10
|
+
rake killbill:clean ; rake build
|
11
|
+
|
12
|
+
echo "Pushing the gem to Rubygems"
|
13
|
+
rake release
|
14
|
+
|
15
|
+
echo "Building artifact"
|
16
|
+
rake killbill:package
|
17
|
+
|
18
|
+
ARTIFACT="$PWD/pkg/killbill-payment-test-$VERSION.tar.gz"
|
19
|
+
echo "Pushing $ARTIFACT to Maven Central"
|
20
|
+
mvn gpg:sign-and-deploy-file \
|
21
|
+
-DgroupId=com.ning.killbill.ruby \
|
22
|
+
-DartifactId=payment-test-plugin \
|
23
|
+
-Dversion=$VERSION \
|
24
|
+
-Dpackaging=tar.gz \
|
25
|
+
-DrepositoryId=ossrh-releases \
|
26
|
+
-Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ \
|
27
|
+
-Dfile=$ARTIFACT \
|
28
|
+
-DpomFile=pom.xml
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
describe Killbill::CurrencyPlugin::DefaultPlugin do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.delete_all
|
9
|
+
Killbill::CurrencyPlugin::CurrencyRate.delete_all
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
it 'should test plugin apis with no data' do
|
14
|
+
|
15
|
+
api = Killbill::CurrencyPlugin::DefaultPlugin.new
|
16
|
+
|
17
|
+
base_currencies = api.get_base_currencies
|
18
|
+
base_currencies.size.should == 0
|
19
|
+
|
20
|
+
usd_latest_conversion_date = api.get_latest_conversion_date('USD')
|
21
|
+
usd_latest_conversion_date.should be_nil
|
22
|
+
|
23
|
+
conversion_dates = api.get_conversion_dates('EUR')
|
24
|
+
conversion_dates.size.should == 0
|
25
|
+
|
26
|
+
rates = api.get_current_rates('USD')
|
27
|
+
rates.size.should == 0
|
28
|
+
|
29
|
+
rates = api.get_rates('USD', Time.now)
|
30
|
+
rates.size.should == 0
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should test plugin apis' do
|
34
|
+
|
35
|
+
api = Killbill::CurrencyPlugin::DefaultPlugin.new
|
36
|
+
|
37
|
+
d1 = Time.parse('2013-10-1T20:41:09Z')
|
38
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
39
|
+
:conversion_date => d1
|
40
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'EUR',
|
41
|
+
:conversion_date => d1
|
42
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'BRL',
|
43
|
+
:conversion_date => d1
|
44
|
+
|
45
|
+
d2 = Time.parse('2013-10-2T21:32:07Z')
|
46
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
47
|
+
:conversion_date => d2
|
48
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'EUR',
|
49
|
+
:conversion_date => d2
|
50
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'BRL',
|
51
|
+
:conversion_date => d2
|
52
|
+
|
53
|
+
d3 = Time.parse('2013-10-3T21:42:07Z')
|
54
|
+
usd_d3 = Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
55
|
+
:conversion_date => d3
|
56
|
+
eur_d3 = Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'EUR',
|
57
|
+
:conversion_date => d3
|
58
|
+
brl_d3 = Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'BRL',
|
59
|
+
:conversion_date => d3
|
60
|
+
|
61
|
+
|
62
|
+
base_currencies = api.get_base_currencies
|
63
|
+
base_currencies.size.should == 3
|
64
|
+
base_currencies[0].should == 'BRL'
|
65
|
+
base_currencies[1].should == 'EUR'
|
66
|
+
base_currencies[2].should == 'USD'
|
67
|
+
|
68
|
+
usd_latest_conversion_date = api.get_latest_conversion_date('USD')
|
69
|
+
Time.at(usd_latest_conversion_date).should == Time.at(d3)
|
70
|
+
|
71
|
+
conversion_dates = api.get_conversion_dates('EUR')
|
72
|
+
conversion_dates.size.should == 3
|
73
|
+
Time.at(conversion_dates[0]).should == Time.at(d3)
|
74
|
+
Time.at(conversion_dates[1]).should == Time.at(d2)
|
75
|
+
Time.at(conversion_dates[2]).should == Time.at(d1)
|
76
|
+
|
77
|
+
|
78
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'BRL',
|
79
|
+
:rate => 0.45731,
|
80
|
+
:currency_update_id => usd_d3.id
|
81
|
+
|
82
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'EUR',
|
83
|
+
:rate => 1.38055,
|
84
|
+
:currency_update_id => usd_d3.id
|
85
|
+
|
86
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'GBP',
|
87
|
+
:rate => 1.61660,
|
88
|
+
:currency_update_id => usd_d3.id
|
89
|
+
|
90
|
+
rates = api.get_current_rates('USD')
|
91
|
+
rates.size.should == 3
|
92
|
+
|
93
|
+
rates[0].base_currency.should == 'USD'
|
94
|
+
rates[0].currency.should == 'BRL'
|
95
|
+
rates[0].value.should == 0.45731
|
96
|
+
Time.at(rates[0].conversion_date).should == Time.at(d3)
|
97
|
+
|
98
|
+
rates[1].base_currency.should == 'USD'
|
99
|
+
rates[1].currency.should == 'EUR'
|
100
|
+
rates[1].value.should == 1.38055
|
101
|
+
Time.at(rates[1].conversion_date).should == Time.at(d3)
|
102
|
+
|
103
|
+
rates[2].base_currency.should == 'USD'
|
104
|
+
rates[2].currency.should == 'GBP'
|
105
|
+
rates[2].value.should == 1.61660
|
106
|
+
Time.at(rates[2].conversion_date).should == Time.at(d3)
|
107
|
+
|
108
|
+
|
109
|
+
rates = api.get_rates('USD', d2)
|
110
|
+
rates.size.should == 0
|
111
|
+
|
112
|
+
rates = api.get_rates('USD', d3)
|
113
|
+
rates.size.should == 3
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
describe Killbill::CurrencyPlugin::CurrencyUpdate do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.delete_all
|
9
|
+
Killbill::CurrencyPlugin::CurrencyRate.delete_all
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should test currency update scope queries' do
|
13
|
+
|
14
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.historical_base_currencies('EUR').size.should == 0
|
15
|
+
|
16
|
+
d1 = Time.parse('2013-10-25T20:41:09Z')
|
17
|
+
|
18
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
19
|
+
:conversion_date => d1
|
20
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'EUR',
|
21
|
+
:conversion_date => d1
|
22
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'BRL',
|
23
|
+
:conversion_date => d1
|
24
|
+
|
25
|
+
d2 = Time.parse('2013-10-27T21:32:07Z')
|
26
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
27
|
+
:conversion_date => d2
|
28
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'EUR',
|
29
|
+
:conversion_date => d2
|
30
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'BRL',
|
31
|
+
:conversion_date => d2
|
32
|
+
|
33
|
+
d3 = Time.parse('2013-10-26T21:42:07Z')
|
34
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
35
|
+
:conversion_date => d3
|
36
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'EUR',
|
37
|
+
:conversion_date => d3
|
38
|
+
Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'BRL',
|
39
|
+
:conversion_date => d3
|
40
|
+
|
41
|
+
|
42
|
+
eur_historical = Killbill::CurrencyPlugin::CurrencyUpdate.historical_base_currencies('EUR')
|
43
|
+
eur_historical.size.should == 3
|
44
|
+
Time.at(eur_historical[0].conversion_date).should == Time.at(d2)
|
45
|
+
Time.at(eur_historical[1].conversion_date).should == Time.at(d3)
|
46
|
+
Time.at(eur_historical[2].conversion_date).should == Time.at(d1)
|
47
|
+
|
48
|
+
eur_latest = Killbill::CurrencyPlugin::CurrencyUpdate.latest_base_currency('EUR')
|
49
|
+
eur_latest.size.should == 1
|
50
|
+
eur_latest[0].base_currency.should == 'EUR'
|
51
|
+
Time.at(eur_latest[0].conversion_date).should == Time.at(d2)
|
52
|
+
|
53
|
+
distinct_base_currencies = Killbill::CurrencyPlugin::CurrencyUpdate.distinct_base_currencies
|
54
|
+
distinct_base_currencies.size.should == 3
|
55
|
+
distinct_base_currencies.each_with_index do |c, i|
|
56
|
+
c.base_currency.should == 'BRL' if i == 0
|
57
|
+
c.base_currency.should == 'EUR' if i == 1
|
58
|
+
c.base_currency.should == 'USD' if i == 2
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should test currency rate scope queries' do
|
63
|
+
|
64
|
+
d1 = Time.parse('2013-10-10T20:41:09Z')
|
65
|
+
|
66
|
+
res1 = Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
67
|
+
:conversion_date => d1
|
68
|
+
|
69
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'BRL',
|
70
|
+
:rate => 0.45721,
|
71
|
+
:currency_update_id => res1.id
|
72
|
+
|
73
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'EUR',
|
74
|
+
:rate => 1.38045,
|
75
|
+
:currency_update_id => res1.id
|
76
|
+
|
77
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'GBP',
|
78
|
+
:rate => 1.61650,
|
79
|
+
:currency_update_id => res1.id
|
80
|
+
|
81
|
+
|
82
|
+
d2 = Time.parse('2013-10-11T20:41:09Z')
|
83
|
+
res2 = Killbill::CurrencyPlugin::CurrencyUpdate.create :base_currency => 'USD',
|
84
|
+
:conversion_date => d2
|
85
|
+
|
86
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'BRL',
|
87
|
+
:rate => 0.45731,
|
88
|
+
:currency_update_id => res2.id
|
89
|
+
|
90
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'EUR',
|
91
|
+
:rate => 1.38055,
|
92
|
+
:currency_update_id => res2.id
|
93
|
+
|
94
|
+
Killbill::CurrencyPlugin::CurrencyRate.create :target_currency => 'GBP',
|
95
|
+
:rate => 1.61660,
|
96
|
+
:currency_update_id => res2.id
|
97
|
+
|
98
|
+
|
99
|
+
#
|
100
|
+
# Can't gte it to work in one join query using scope-- SQL generated is weird:
|
101
|
+
# (SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "currency_updates" INNER JOIN "currency_rates" ON "currency_rates"."currency_update_id" = "currency_updates"."id" WHERE (base_currency = 'USD') LIMIT 1) subquery_for_count)
|
102
|
+
#
|
103
|
+
#latest_rates_for_usd = Killbill::CurrencyPlugin::CurrencyUpdate.latest_rates_for_currency('USD')
|
104
|
+
|
105
|
+
usd_latest = Killbill::CurrencyPlugin::CurrencyUpdate.latest_base_currency('USD')
|
106
|
+
latest_rates_for_usd = Killbill::CurrencyPlugin::CurrencyRate.latest_rates_for_base_currency(usd_latest[0].id)
|
107
|
+
latest_rates_for_usd.size.should == 3
|
108
|
+
latest_rates_for_usd[0].target_currency.should == 'BRL'
|
109
|
+
latest_rates_for_usd[0].rate.should == 0.45731
|
110
|
+
latest_rates_for_usd[1].target_currency.should == 'EUR'
|
111
|
+
latest_rates_for_usd[1].rate.should == 1.38055
|
112
|
+
latest_rates_for_usd[2].target_currency.should == 'GBP'
|
113
|
+
latest_rates_for_usd[2].rate.should == 1.61660
|
114
|
+
end
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'logger'
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
require 'currency_plugin'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.color_enabled = true
|
9
|
+
config.tty = true
|
10
|
+
config.formatter = 'documentation'
|
11
|
+
end
|
12
|
+
|
13
|
+
class Object
|
14
|
+
def blank?
|
15
|
+
respond_to?(:empty?) ? empty? : !self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
require 'active_record'
|
21
|
+
|
22
|
+
#ActiveRecord::Base.logger = Logger.new(STDOUT)
|
23
|
+
ActiveRecord::Base.establish_connection(
|
24
|
+
:adapter => 'sqlite3',
|
25
|
+
:database => 'test.db'
|
26
|
+
)
|
27
|
+
# Create the schema
|
28
|
+
require File.expand_path(File.dirname(__FILE__) + '../../db/schema.rb')
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: killbill-currency-plugin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kill Bill core team
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: killbill
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 1.8.0
|
21
|
+
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.8.0
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :runtime
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - "~>"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 3.2.1
|
37
|
+
none: false
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 3.2.1
|
43
|
+
none: false
|
44
|
+
prerelease: false
|
45
|
+
type: :runtime
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activerecord-jdbcmysql-adapter
|
48
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 1.2.9
|
53
|
+
none: false
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - "~>"
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.2.9
|
59
|
+
none: false
|
60
|
+
prerelease: false
|
61
|
+
type: :runtime
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: jbundler
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.4.1
|
69
|
+
none: false
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.4.1
|
75
|
+
none: false
|
76
|
+
prerelease: false
|
77
|
+
type: :development
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 10.0.0
|
85
|
+
none: false
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 10.0.0
|
91
|
+
none: false
|
92
|
+
prerelease: false
|
93
|
+
type: :development
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - "~>"
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.12.0
|
101
|
+
none: false
|
102
|
+
requirement: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - "~>"
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 2.12.0
|
107
|
+
none: false
|
108
|
+
prerelease: false
|
109
|
+
type: :development
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: activerecord-jdbcsqlite3-adapter
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 1.2.6
|
117
|
+
none: false
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.2.6
|
123
|
+
none: false
|
124
|
+
prerelease: false
|
125
|
+
type: :development
|
126
|
+
description: Currency Plugin based on a static currency table.
|
127
|
+
email: killbilling-users@googlegroups.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- Gemfile
|
134
|
+
- Jarfile
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- VERSION
|
138
|
+
- db/ddl.sql
|
139
|
+
- db/schema.rb
|
140
|
+
- killbill-currency-plugin.gemspec
|
141
|
+
- killbill.properties
|
142
|
+
- lib/currency_plugin.rb
|
143
|
+
- lib/currency_plugin/api.rb
|
144
|
+
- lib/currency_plugin/models/currency_rate.rb
|
145
|
+
- lib/currency_plugin/models/currency_update.rb
|
146
|
+
- pom.xml
|
147
|
+
- release.sh
|
148
|
+
- spec/currency_plugin/api_spec.rb
|
149
|
+
- spec/currency_plugin/model_spec.rb
|
150
|
+
- spec/spec_helper.rb
|
151
|
+
homepage: http://kill-bill.org
|
152
|
+
licenses:
|
153
|
+
- Apache License (2.0)
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options:
|
156
|
+
- "--exclude"
|
157
|
+
- "."
|
158
|
+
require_paths:
|
159
|
+
- lib
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: 1.9.3
|
165
|
+
none: false
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
segments:
|
171
|
+
- 0
|
172
|
+
version: !binary |-
|
173
|
+
MA==
|
174
|
+
hash: 2
|
175
|
+
none: false
|
176
|
+
requirements: []
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 1.8.24
|
179
|
+
signing_key:
|
180
|
+
specification_version: 3
|
181
|
+
summary: Default Currency Plugin.
|
182
|
+
test_files:
|
183
|
+
- spec/currency_plugin/api_spec.rb
|
184
|
+
- spec/currency_plugin/model_spec.rb
|
185
|
+
- spec/spec_helper.rb
|