simple_xurrency_buntine 1.1.2
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/.autotest +8 -0
- data/.bundle/config +2 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +45 -0
- data/LICENSE +23 -0
- data/README.rdoc +50 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/autotest/discover.rb +1 -0
- data/lib/core_ext/numeric.rb +3 -0
- data/lib/simple_xurrency.rb +46 -0
- data/lib/simple_xurrency/currency_convertible.rb +128 -0
- data/simple_xurrency_buntine.gemspec +70 -0
- data/spec/simple_xurrency_spec.rb +77 -0
- data/spec/spec_helper.rb +28 -0
- metadata +183 -0
data/.autotest
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Autotest.add_hook(:initialize) {|at|
|
2
|
+
at.add_exception %r{^\.git} # ignore Version Control System
|
3
|
+
at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
|
4
|
+
at.clear_mappings # take out the default (test/test*rb)
|
5
|
+
at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
|
6
|
+
Dir['spec/**/*.rb']
|
7
|
+
}
|
8
|
+
}
|
data/.bundle/config
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use ruby-1.8.7@simple_currency > /dev/null
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (2.3.8)
|
5
|
+
actionpack (= 2.3.8)
|
6
|
+
actionpack (2.3.8)
|
7
|
+
activesupport (= 2.3.8)
|
8
|
+
rack (~> 1.1.0)
|
9
|
+
activerecord (2.3.8)
|
10
|
+
activesupport (= 2.3.8)
|
11
|
+
activeresource (2.3.8)
|
12
|
+
activesupport (= 2.3.8)
|
13
|
+
activesupport (2.3.8)
|
14
|
+
crack (0.1.8)
|
15
|
+
diff-lcs (1.1.2)
|
16
|
+
fakeweb (1.3.0)
|
17
|
+
rack (1.1.0)
|
18
|
+
rails (2.3.8)
|
19
|
+
actionmailer (= 2.3.8)
|
20
|
+
actionpack (= 2.3.8)
|
21
|
+
activerecord (= 2.3.8)
|
22
|
+
activeresource (= 2.3.8)
|
23
|
+
activesupport (= 2.3.8)
|
24
|
+
rake (>= 0.8.3)
|
25
|
+
rake (0.8.7)
|
26
|
+
rspec (2.0.0.beta.22)
|
27
|
+
rspec-core (= 2.0.0.beta.22)
|
28
|
+
rspec-expectations (= 2.0.0.beta.22)
|
29
|
+
rspec-mocks (= 2.0.0.beta.22)
|
30
|
+
rspec-core (2.0.0.beta.22)
|
31
|
+
rspec-expectations (2.0.0.beta.22)
|
32
|
+
diff-lcs (>= 1.1.2)
|
33
|
+
rspec-mocks (2.0.0.beta.22)
|
34
|
+
rspec-core (= 2.0.0.beta.22)
|
35
|
+
rspec-expectations (= 2.0.0.beta.22)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
java
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
crack
|
43
|
+
fakeweb
|
44
|
+
rails
|
45
|
+
rspec (>= 2.0.0.beta.20)
|
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
New BSD Licence
|
2
|
+
|
3
|
+
Copyright (c) 2010, Xurrency All rights reserved.
|
4
|
+
|
5
|
+
Authors: Oriol Gual, Josep M. Bach, Josep Jaume Rey, Alfonso Jimenez
|
6
|
+
|
7
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
|
8
|
+
the following conditions are met:
|
9
|
+
|
10
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
11
|
+
following disclaimer.
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
13
|
+
the following disclaimer in the documentation and/or other materials provided with the distribution.
|
14
|
+
* Neither the name of the Xurrency nor the names of its contributors may be used to endorse or promote
|
15
|
+
products derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY XURRENCY.COM ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
|
19
|
+
NO EVENT SHALL WEBLOGS SL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
21
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
23
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= simple_xurrency
|
2
|
+
|
3
|
+
A really easy interface to the Xurrency API. It's Ruby 1.8, 1.9 and JRuby compatible. This gem is a fork from simple_currency, made by Codegram.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
Just require it and all your numeric stuff gets this fancy DSL for free:
|
8
|
+
|
9
|
+
30.eur.to_usd
|
10
|
+
# => 38.08
|
11
|
+
|
12
|
+
150.eur.to_usd
|
13
|
+
# => 190.4
|
14
|
+
239.usd.to_eur
|
15
|
+
# => 187.98
|
16
|
+
|
17
|
+
# Historical rates:
|
18
|
+
239.usd.to_eur(Date.today - 2)
|
19
|
+
# => 183.98
|
20
|
+
|
21
|
+
1.eur.to_usd_updated_at
|
22
|
+
# => Fri Oct 08 22:54:00 UTC 2010
|
23
|
+
|
24
|
+
If you have a Xurrency API key, you can add it by doing:
|
25
|
+
|
26
|
+
SimpleXurrency.key = 'write_your_api_key'
|
27
|
+
|
28
|
+
If you want to find out how to adquire an API key, please go to http://xurrency.com/license
|
29
|
+
|
30
|
+
== Installation
|
31
|
+
|
32
|
+
=== Rails 3
|
33
|
+
|
34
|
+
In your Gemfile:
|
35
|
+
|
36
|
+
gem "simple_xurrency"
|
37
|
+
|
38
|
+
=== Not using Rails?
|
39
|
+
|
40
|
+
Then you have to manually install the gem:
|
41
|
+
|
42
|
+
gem install simple_xurrency
|
43
|
+
|
44
|
+
And manually require it as well:
|
45
|
+
|
46
|
+
require "simple_xurrency"
|
47
|
+
|
48
|
+
== License
|
49
|
+
|
50
|
+
See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "simple_xurrency_buntine"
|
9
|
+
gem.summary = "A really easy interface to the Xurrency API"
|
10
|
+
gem.description = "A really easy interface to the Xurrency API. It's Ruby 1.8, 1.9 and JRuby compatible and has supoort for historical rates"
|
11
|
+
gem.email = "info@andrewbuntine.com"
|
12
|
+
gem.homepage = "http://github.com/buntine/simple_xurrency"
|
13
|
+
gem.authors = ["Oriol Gual", "Josep M. Bach", "Josep Jaume Rey", "Alfonso Jimenez", "Andrew Buntine"]
|
14
|
+
|
15
|
+
gem.add_dependency 'crack', ">= 0.1.8"
|
16
|
+
|
17
|
+
gem.add_development_dependency "jeweler", '>= 1.4.0'
|
18
|
+
gem.add_development_dependency "rspec", '>= 2.0.0.beta.20'
|
19
|
+
gem.add_development_dependency "fakeweb", '>= 1.3.0'
|
20
|
+
gem.add_development_dependency "bundler", '>= 1.0.0'
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
# Rake RSpec2 task stuff
|
28
|
+
gem 'rspec', '>= 2.0.0.beta.20'
|
29
|
+
gem 'rspec-expectations'
|
30
|
+
|
31
|
+
require 'rspec/core/rake_task'
|
32
|
+
|
33
|
+
desc "Run the specs under spec"
|
34
|
+
RSpec::Core::RakeTask.new do |t|
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.2
|
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'simple_xurrency/currency_convertible'
|
2
|
+
require 'core_ext/numeric'
|
3
|
+
|
4
|
+
class SimpleXurrency
|
5
|
+
class << self
|
6
|
+
attr_accessor :key
|
7
|
+
|
8
|
+
def cache_enabled?
|
9
|
+
if @cache_enabled.nil?
|
10
|
+
@cache_enabled = true
|
11
|
+
end
|
12
|
+
|
13
|
+
@cache_enabled
|
14
|
+
end
|
15
|
+
|
16
|
+
def enable_cache
|
17
|
+
@cache_enabled = true
|
18
|
+
end
|
19
|
+
|
20
|
+
def disable_cache
|
21
|
+
@cache_enabled = false
|
22
|
+
end
|
23
|
+
|
24
|
+
def cache_get(key)
|
25
|
+
ret = nil
|
26
|
+
|
27
|
+
if @cache.is_a?(Hash)
|
28
|
+
ret = @cache[key]
|
29
|
+
end
|
30
|
+
|
31
|
+
ret
|
32
|
+
end
|
33
|
+
|
34
|
+
def cache_add(key, value)
|
35
|
+
if !@cache.is_a?(Hash)
|
36
|
+
@cache = Hash.new
|
37
|
+
end
|
38
|
+
|
39
|
+
@cache[key] = value
|
40
|
+
end
|
41
|
+
|
42
|
+
def cache_flush
|
43
|
+
@cache = Hash.new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'timeout'
|
3
|
+
require 'crack'
|
4
|
+
|
5
|
+
module CurrencyConvertible
|
6
|
+
def method_missing(method, *args, &block)
|
7
|
+
return _from(method.to_s) if method.to_s.length == 3
|
8
|
+
|
9
|
+
if @original && !(method.to_s =~ /^to_(utc|int|str|ary)/) && method.to_s =~/^to_/ && method.to_s.length == 6
|
10
|
+
return _to(*[method.to_s.gsub('to_',''), args].flatten)
|
11
|
+
end
|
12
|
+
|
13
|
+
if @original && !(method.to_s =~ /^(singleton_methods|protected_methods)/) && method.to_s =~/^to_[a-z]{3}_updated_at/ && method.to_s.length == 17
|
14
|
+
return _updated_at(method.to_s.slice(3..5))
|
15
|
+
end
|
16
|
+
|
17
|
+
super(method,*args,&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Called from first currency metamethod to set the original currency.
|
23
|
+
#
|
24
|
+
# 30.eur # => Calls _from and sets @original to 'eur'
|
25
|
+
#
|
26
|
+
def _from(currency)
|
27
|
+
@original = currency
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
# Called from last currency metamethod to set the target currency.
|
32
|
+
#
|
33
|
+
# 30.eur.to_usd
|
34
|
+
# # => Calls _to and returns the final value, say 38.08
|
35
|
+
#
|
36
|
+
def _to(target, date = nil)
|
37
|
+
raise unless @original # Must be called after a _from have set the @original currency
|
38
|
+
|
39
|
+
return 0.0 if self == 0 # Obviously
|
40
|
+
|
41
|
+
original = @original
|
42
|
+
|
43
|
+
amount = self
|
44
|
+
|
45
|
+
result = exchange(original, target, amount.abs, date)
|
46
|
+
|
47
|
+
result
|
48
|
+
end
|
49
|
+
|
50
|
+
# Called from first currency metamethod to set the original currency.
|
51
|
+
#
|
52
|
+
# 30.eur # => Calls _from and sets @original to 'eur'
|
53
|
+
#
|
54
|
+
def _updated_at(target)
|
55
|
+
result = rate(@original, target)
|
56
|
+
|
57
|
+
result[:updated_at]
|
58
|
+
end
|
59
|
+
|
60
|
+
# Main method (called by _to) which calls Xurrency strategies
|
61
|
+
# and returns a nice result.
|
62
|
+
#
|
63
|
+
def exchange(original, target, amount = 1, date = nil)
|
64
|
+
if amount > 999_999_999 # Xurrency API does not support numbers bigger than this
|
65
|
+
amount = 1
|
66
|
+
multiplier = self.abs # Save a multiplier to apply it to the result later
|
67
|
+
end
|
68
|
+
|
69
|
+
negative = (self < 0)
|
70
|
+
|
71
|
+
result = rate(original, target, date)
|
72
|
+
|
73
|
+
result = sprintf("%.4f", result[:rate].to_f*amount*(multiplier || 1)).to_f
|
74
|
+
|
75
|
+
return -(result) if negative
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
# Calls Xurrency API to perform the exchange without a specific date (assuming today)
|
80
|
+
#
|
81
|
+
def rate(original, target, date = nil)
|
82
|
+
cache_name = if date.nil?
|
83
|
+
"#{original}_#{target}"
|
84
|
+
else
|
85
|
+
"#{original}_#{target}_#{date.strftime("%Y%m%d")}"
|
86
|
+
end
|
87
|
+
|
88
|
+
if SimpleXurrency.cache_enabled?
|
89
|
+
cached_result = SimpleXurrency.cache_get(cache_name)
|
90
|
+
|
91
|
+
if !cached_result.nil?
|
92
|
+
return cached_result
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
api_url = "http://xurrency.com/api/#{[original, target].join('/')}/1"
|
97
|
+
api_url << "?key=#{SimpleXurrency.key}" if !SimpleXurrency.key.nil?
|
98
|
+
|
99
|
+
api_url << "#{SimpleXurrency.key.nil? ? "?" : "&"}date=#{date.strftime("%Y%m%d")}" if date
|
100
|
+
|
101
|
+
uri = URI.parse(api_url)
|
102
|
+
|
103
|
+
retries = 10
|
104
|
+
json_response = nil
|
105
|
+
begin
|
106
|
+
Timeout::timeout(1){
|
107
|
+
json_response = uri.open.read || nil # Returns the raw response
|
108
|
+
}
|
109
|
+
rescue Timeout::Error
|
110
|
+
retries -= 1
|
111
|
+
retries > 0 ? sleep(0.42) && retry : raise
|
112
|
+
end
|
113
|
+
|
114
|
+
return nil unless json_response && parsed_response = Crack::JSON.parse(json_response)
|
115
|
+
if parsed_response['status'] == 'fail'
|
116
|
+
raise parsed_response['message']
|
117
|
+
end
|
118
|
+
|
119
|
+
value = Hash.new
|
120
|
+
|
121
|
+
value[:rate] = parsed_response['result']['value'].to_f
|
122
|
+
value[:updated_at] = parsed_response['result']['updated_at'].to_s
|
123
|
+
|
124
|
+
SimpleXurrency.cache_add(cache_name, value) if SimpleXurrency.cache_enabled?
|
125
|
+
|
126
|
+
value
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simple_xurrency_buntine}
|
8
|
+
s.version = "1.1.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Oriol Gual", "Josep M. Bach", "Josep Jaume Rey", "Alfonso Jimenez", "Andrew Buntine"]
|
12
|
+
s.date = %q{2013-08-07}
|
13
|
+
s.description = %q{A really easy interface to the Xurrency API. It's Ruby 1.8, 1.9 and JRuby compatible and has supoort for historical rates}
|
14
|
+
s.email = %q{info@andrewbuntine.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".autotest",
|
21
|
+
".bundle/config",
|
22
|
+
".rspec",
|
23
|
+
".rvmrc",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE",
|
27
|
+
"README.rdoc",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"autotest/discover.rb",
|
31
|
+
"lib/core_ext/numeric.rb",
|
32
|
+
"lib/simple_xurrency.rb",
|
33
|
+
"lib/simple_xurrency/currency_convertible.rb",
|
34
|
+
"simple_xurrency_buntine.gemspec",
|
35
|
+
"spec/simple_xurrency_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/buntine/simple_xurrency}
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.5.3}
|
41
|
+
s.summary = %q{A really easy interface to the Xurrency API}
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_runtime_dependency(%q<crack>, [">= 0"])
|
48
|
+
s.add_runtime_dependency(%q<crack>, [">= 0.1.8"])
|
49
|
+
s.add_development_dependency(%q<jeweler>, [">= 1.4.0"])
|
50
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
51
|
+
s.add_development_dependency(%q<fakeweb>, [">= 1.3.0"])
|
52
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<crack>, [">= 0"])
|
55
|
+
s.add_dependency(%q<crack>, [">= 0.1.8"])
|
56
|
+
s.add_dependency(%q<jeweler>, [">= 1.4.0"])
|
57
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
58
|
+
s.add_dependency(%q<fakeweb>, [">= 1.3.0"])
|
59
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
60
|
+
end
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<crack>, [">= 0"])
|
63
|
+
s.add_dependency(%q<crack>, [">= 0.1.8"])
|
64
|
+
s.add_dependency(%q<jeweler>, [">= 1.4.0"])
|
65
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
66
|
+
s.add_dependency(%q<fakeweb>, [">= 1.3.0"])
|
67
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "SimpleXurrency" do
|
4
|
+
before(:all) do
|
5
|
+
SimpleXurrency.disable_cache
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "in its basic behavior" do
|
9
|
+
it "enhances instances of Numeric with currency methods" do
|
10
|
+
expect { 30.eur && 30.usd && 30.gbp }.to_not raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
it "handles zero values (by returning them straight away)" do
|
14
|
+
0.usd.to_eur.should == 0.0
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
context "using Xurrency API for right-now exchange" do
|
20
|
+
it "returns a converted amount from one currency to another" do
|
21
|
+
amount = 2
|
22
|
+
rate = 1.5422
|
23
|
+
|
24
|
+
mock_xurrency_api('eur', 'usd', amount, rate, Time.now.utc)
|
25
|
+
amount.eur.to_usd.should == amount*rate
|
26
|
+
|
27
|
+
amount = 2
|
28
|
+
rate = 3.4874
|
29
|
+
|
30
|
+
mock_xurrency_api('gbp', 'chf', amount, rate, Time.now.utc)
|
31
|
+
amount.gbp.to_chf.should == amount*rate
|
32
|
+
|
33
|
+
rate = 0.6322
|
34
|
+
|
35
|
+
(0..100).each do |x|
|
36
|
+
mock_xurrency_api('usd', 'gbp', x, rate, Time.now.utc)
|
37
|
+
x.usd.to_gbp.to_s.should == (x*rate).to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns a historical converted amount from one currency to another" do
|
42
|
+
amount = 2
|
43
|
+
rate = 1.4874
|
44
|
+
|
45
|
+
mock_xurrency_api('aud', 'usd', amount, rate, Time.now.utc, {}, Date.today - 1)
|
46
|
+
amount.aud.to_usd(Date.today - 1).should == amount*rate
|
47
|
+
end
|
48
|
+
|
49
|
+
it "raises any error returned by the api call" do
|
50
|
+
mock_xurrency_api('usd', 'xxx', 1, 1.5, Time.now.utc, :fail_with => "Currencies are not valid")
|
51
|
+
mock_xurrency_api('usd', 'eur', 1_000_000_000, 1.5, Time.now.utc)
|
52
|
+
mock_xurrency_api('usd', 'usd', 1, 1.5, Time.now.utc, :fail_with => "Currency codes are the same")
|
53
|
+
|
54
|
+
expect {1.usd.to_xxx}.to raise_error("Currencies are not valid")
|
55
|
+
expect {1_000_000_000.usd.to_eur}.to_not raise_error
|
56
|
+
expect {1.usd.to_usd}.to raise_error("Currency codes are the same")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "handles a negative value returning a negative as well" do
|
60
|
+
mock_xurrency_api('usd', 'eur', 1, 1.5, Time.now.utc)
|
61
|
+
|
62
|
+
-1.usd.to_eur.should == -1.5
|
63
|
+
end
|
64
|
+
|
65
|
+
it "handles big amounts" do
|
66
|
+
mock_xurrency_api('usd', 'eur', 1, 1.5, Time.now.utc)
|
67
|
+
|
68
|
+
1_000_000_000.usd.to_eur.should == 1_500_000_000
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns the updated date" do
|
72
|
+
mock_xurrency_api('usd', 'eur', 1, 1.5, '2010-10-04 00:00:00')
|
73
|
+
|
74
|
+
1.usd.to_eur_updated_at.should == '2010-10-04 00:00:00'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'fakeweb'
|
6
|
+
|
7
|
+
require 'simple_xurrency'
|
8
|
+
require 'rspec'
|
9
|
+
|
10
|
+
module HelperMethods
|
11
|
+
def mock_xurrency_api(from_currency, to_currency, amount, result, updated_at, options = {}, date=nil)
|
12
|
+
args = [from_currency, to_currency, "1"]
|
13
|
+
|
14
|
+
response = "{\"result\":{\"value\":#{result},\"target\":\"#{to_currency}\",\"base\":\"#{from_currency}\",\"updated_at\":\"#{updated_at}\"},\"status\":\"ok\"}"
|
15
|
+
|
16
|
+
response = "{\"message\":\"#{options[:fail_with]}\", \"status\":\"fail\"\}" if options[:fail_with]
|
17
|
+
|
18
|
+
url = "http://xurrency.com/api/#{args.join('/')}"
|
19
|
+
|
20
|
+
if date
|
21
|
+
url += "?date=#{date.strftime("%Y%m%d")}"
|
22
|
+
end
|
23
|
+
|
24
|
+
FakeWeb.register_uri(:get, url, :body => response)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
RSpec.configuration.include(HelperMethods)
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_xurrency_buntine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 1.1.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Oriol Gual
|
14
|
+
- Josep M. Bach
|
15
|
+
- Josep Jaume Rey
|
16
|
+
- Alfonso Jimenez
|
17
|
+
- Andrew Buntine
|
18
|
+
autorequire:
|
19
|
+
bindir: bin
|
20
|
+
cert_chain: []
|
21
|
+
|
22
|
+
date: 2013-08-07 00:00:00 +10:00
|
23
|
+
default_executable:
|
24
|
+
dependencies:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: crack
|
27
|
+
prerelease: false
|
28
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
hash: 3
|
34
|
+
segments:
|
35
|
+
- 0
|
36
|
+
version: "0"
|
37
|
+
type: :runtime
|
38
|
+
version_requirements: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: crack
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 11
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
- 1
|
51
|
+
- 8
|
52
|
+
version: 0.1.8
|
53
|
+
type: :runtime
|
54
|
+
version_requirements: *id002
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: jeweler
|
57
|
+
prerelease: false
|
58
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 7
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 4
|
67
|
+
- 0
|
68
|
+
version: 1.4.0
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id003
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: rspec
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 1596437213
|
80
|
+
segments:
|
81
|
+
- 2
|
82
|
+
- 0
|
83
|
+
- 0
|
84
|
+
- beta
|
85
|
+
- 20
|
86
|
+
version: 2.0.0.beta.20
|
87
|
+
type: :development
|
88
|
+
version_requirements: *id004
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: fakeweb
|
91
|
+
prerelease: false
|
92
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 27
|
98
|
+
segments:
|
99
|
+
- 1
|
100
|
+
- 3
|
101
|
+
- 0
|
102
|
+
version: 1.3.0
|
103
|
+
type: :development
|
104
|
+
version_requirements: *id005
|
105
|
+
- !ruby/object:Gem::Dependency
|
106
|
+
name: bundler
|
107
|
+
prerelease: false
|
108
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
hash: 23
|
114
|
+
segments:
|
115
|
+
- 1
|
116
|
+
- 0
|
117
|
+
- 0
|
118
|
+
version: 1.0.0
|
119
|
+
type: :development
|
120
|
+
version_requirements: *id006
|
121
|
+
description: A really easy interface to the Xurrency API. It's Ruby 1.8, 1.9 and JRuby compatible and has supoort for historical rates
|
122
|
+
email: info@andrewbuntine.com
|
123
|
+
executables: []
|
124
|
+
|
125
|
+
extensions: []
|
126
|
+
|
127
|
+
extra_rdoc_files:
|
128
|
+
- LICENSE
|
129
|
+
- README.rdoc
|
130
|
+
files:
|
131
|
+
- .autotest
|
132
|
+
- .bundle/config
|
133
|
+
- .rspec
|
134
|
+
- .rvmrc
|
135
|
+
- Gemfile
|
136
|
+
- Gemfile.lock
|
137
|
+
- LICENSE
|
138
|
+
- README.rdoc
|
139
|
+
- Rakefile
|
140
|
+
- VERSION
|
141
|
+
- autotest/discover.rb
|
142
|
+
- lib/core_ext/numeric.rb
|
143
|
+
- lib/simple_xurrency.rb
|
144
|
+
- lib/simple_xurrency/currency_convertible.rb
|
145
|
+
- simple_xurrency_buntine.gemspec
|
146
|
+
- spec/simple_xurrency_spec.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
has_rdoc: true
|
149
|
+
homepage: http://github.com/buntine/simple_xurrency
|
150
|
+
licenses: []
|
151
|
+
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
hash: 3
|
163
|
+
segments:
|
164
|
+
- 0
|
165
|
+
version: "0"
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
none: false
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
hash: 3
|
172
|
+
segments:
|
173
|
+
- 0
|
174
|
+
version: "0"
|
175
|
+
requirements: []
|
176
|
+
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 1.5.3
|
179
|
+
signing_key:
|
180
|
+
specification_version: 3
|
181
|
+
summary: A really easy interface to the Xurrency API
|
182
|
+
test_files: []
|
183
|
+
|