xurrency 001
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemified +11 -0
- data/History.txt +4 -0
- data/License.txt +56 -0
- data/README.txt +21 -0
- data/Rakefile +7 -0
- data/bin/xurrency +34 -0
- data/lib/xurrency.rb +116 -0
- data/spec/request_spec.rb +40 -0
- data/spec/xurrency_spec.rb +21 -0
- metadata +69 -0
data/.gemified
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
---
|
2
|
+
:summary: ruby-xurrency is a wrapper library for API xurrency(http://xurrency.com/api).
|
3
|
+
:email: keita.yamaguchi@gmail.com
|
4
|
+
:has_rdoc: true
|
5
|
+
:name: xurrency
|
6
|
+
:homepage: http://rubyforge.org/projects/xurrency/
|
7
|
+
:version: "001"
|
8
|
+
:dependencies:
|
9
|
+
- soap4r
|
10
|
+
:rubyforge_project: xurrency
|
11
|
+
:author: Keita Yamaguchi
|
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
ruby-xurrency is copyrighted free software by Keita Yamaguchi <keita.yamaguchi@gmail.com>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the GPL
|
3
|
+
(see the file GPL), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a) place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b) use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c) give non-standard binaries non-standard names, with
|
21
|
+
instructions on where to get the original software distribution.
|
22
|
+
|
23
|
+
d) make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or binary form,
|
26
|
+
provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a) distribute the binaries and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b) accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c) give non-standard binaries non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d) make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under these terms.
|
43
|
+
|
44
|
+
For the list of those files and their copying conditions, see the
|
45
|
+
file LEGAL.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
data/README.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= ruby-xurrency
|
2
|
+
|
3
|
+
Author:: Keita Yamaguchi(山口 慶太) <keita.yamaguchi@gmail.com>
|
4
|
+
Copyright:: © Keita Yamaguchi, 2008. All rights reserved.
|
5
|
+
License:: Ruby License
|
6
|
+
|
7
|
+
ruby-xurrency is a wrapper library for {API Xurrency}[http://xurrency.com/api].
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
See {the document}[http://github.com/keita/ruby-xurrency/wikis].
|
12
|
+
|
13
|
+
== Links
|
14
|
+
|
15
|
+
* {Xurrency}[http://xurrency.com/]
|
16
|
+
* {API Xurrency}[http://xurrency.com/api]
|
17
|
+
* ruby-xurrency
|
18
|
+
* {RubyForge}[http://rubyforge.org/projects/xurrency/]
|
19
|
+
* {GitHub}[http://github.com/keita/ruby-xurrency/tree/master]
|
20
|
+
* author's blog(written in Japanese)
|
21
|
+
* {¬¬日常日記}[http://d.hatena.ne.jp/keita_yamaguchi/]
|
data/Rakefile
ADDED
data/bin/xurrency
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "xurrency"
|
5
|
+
require "optparse"
|
6
|
+
|
7
|
+
Option = OptionParser.new do |opt|
|
8
|
+
opt.banner = "Usage: xurrency [options] amount base target"
|
9
|
+
|
10
|
+
opt.on("-l", "--list", "Show currencies supported") do
|
11
|
+
puts Xurrency.new.currencies.sort.join(", ")
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
|
15
|
+
opt.on("-h", "--help", "Show this message") do
|
16
|
+
puts opt.help
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
|
20
|
+
opt.on("-v", "--version", "Show the version of ruby-xurrency") do
|
21
|
+
puts "ruby-xurrency " + Xurrency::VERSION
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
|
25
|
+
opt.parse!(ARGV)
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
xu = Xurrency.new
|
30
|
+
puts xu.value(ARGV[0].to_i, ARGV[1], ARGV[2])
|
31
|
+
puts "(1 #{ARGV[1]} = #{xu.values(ARGV[1])[ARGV[2]]} #{ARGV[2]})"
|
32
|
+
rescue ArgumentError
|
33
|
+
puts Option.help
|
34
|
+
end
|
data/lib/xurrency.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "soap/soap"
|
3
|
+
require "soap/wsdlDriver"
|
4
|
+
require "singleton"
|
5
|
+
require "delegate"
|
6
|
+
|
7
|
+
class Xurrency
|
8
|
+
VERSION = "001"
|
9
|
+
|
10
|
+
# Soap client.
|
11
|
+
class Client < Delegator #:nodoc:
|
12
|
+
include Singleton
|
13
|
+
|
14
|
+
WSDL = "http://xurrency.com/api.wsdl"
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@driver = SOAP::WSDLDriverFactory.new(WSDL).create_rpc_driver
|
18
|
+
@driver.endpoint_url = "http://www.xurrency.com/servidor_soap.php"
|
19
|
+
end
|
20
|
+
|
21
|
+
def __getobj__
|
22
|
+
@driver
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Request
|
27
|
+
module_function
|
28
|
+
|
29
|
+
# getName(code)
|
30
|
+
def currency_name(code)
|
31
|
+
Client.instance.getName(code)
|
32
|
+
end
|
33
|
+
|
34
|
+
# getZone(code)
|
35
|
+
def zone(code)
|
36
|
+
Client.instance.getZone(code)
|
37
|
+
end
|
38
|
+
|
39
|
+
# getURL(code)
|
40
|
+
def url(code)
|
41
|
+
Client.instance.getURL(code)
|
42
|
+
end
|
43
|
+
|
44
|
+
# isCurrency(code)
|
45
|
+
def currency?(code)
|
46
|
+
Client.instance.isCurrency(code)
|
47
|
+
end
|
48
|
+
|
49
|
+
# getCurrencies
|
50
|
+
def currencies
|
51
|
+
Client.instance.getCurrencies
|
52
|
+
end
|
53
|
+
|
54
|
+
# getValues(code)
|
55
|
+
def values(code)
|
56
|
+
table = {}
|
57
|
+
Client.instance.getValues(code).each do |c|
|
58
|
+
table[c["Id"]] = c["Value"]
|
59
|
+
end
|
60
|
+
return table
|
61
|
+
end
|
62
|
+
|
63
|
+
# getValuesInverse(code)
|
64
|
+
def values_inverse(code)
|
65
|
+
table = {}
|
66
|
+
Client.instance.getValuesInverse(code).each do |c|
|
67
|
+
table[c["Id"]] = c["Value"]
|
68
|
+
end
|
69
|
+
return table
|
70
|
+
end
|
71
|
+
|
72
|
+
# getValue(amount, base, target)
|
73
|
+
def value(amount, base, target)
|
74
|
+
Client.instance.getValue(amount, base, target)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def initialize(init = nil)
|
79
|
+
@cache = {}
|
80
|
+
memoize :currency_name
|
81
|
+
memoize :zone
|
82
|
+
memoize :url
|
83
|
+
memoize :currencies
|
84
|
+
memoize :values
|
85
|
+
memoize :values_inverse
|
86
|
+
if init
|
87
|
+
init = Request.currencies if init == :all
|
88
|
+
init.each {|c| values(c) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Updates the result value.
|
93
|
+
def update(name, *args)
|
94
|
+
raise ArgumentError, name unless @cache.has_key?(name)
|
95
|
+
@cache[name][args] = nil
|
96
|
+
__send__(name, *args)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Calculates the value without quering getValue.
|
100
|
+
def value(amount, base, target)
|
101
|
+
values(base)[target] * amount
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
# Memoize some queries.
|
107
|
+
def memoize(name)
|
108
|
+
@cache[name] = {}
|
109
|
+
(class << self; self; end).__send__(:define_method, name) do |*args|
|
110
|
+
return @cache[name][args] if @cache[name].has_key?(args)
|
111
|
+
@cache[name][args] = Request.send(name, *args)
|
112
|
+
@cache[name][args][:timestamp] = Time.now if name.to_s =~ /values/
|
113
|
+
return @cache[name][args]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
require "xurrency"
|
3
|
+
require "bacon"
|
4
|
+
|
5
|
+
include Xurrency::Request
|
6
|
+
|
7
|
+
describe "Xurrency::Request" do
|
8
|
+
it "should get the currency name" do
|
9
|
+
currency_name("jpy").should == "Japanese Yen"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should get the zone" do
|
13
|
+
zone("jpy").should == "Japan"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should get the url" do
|
17
|
+
url("jpy").should =~ /jpy/
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should determin whether the currency is supported or not" do
|
21
|
+
currency?("jpy").should.be.true
|
22
|
+
currency?("XXX").should.be.false
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should list up supported currencies" do
|
26
|
+
currencies.should.include("jpy")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should get values" do
|
30
|
+
values("jpy").should.has_key("usd")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should get inverse values" do
|
34
|
+
values_inverse("jpy").should.has_key("usd")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should get the value" do
|
38
|
+
value(1, "jpy", "usd").should > 0
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
require "xurrency"
|
3
|
+
require "bacon"
|
4
|
+
|
5
|
+
$xu = Xurrency.new
|
6
|
+
|
7
|
+
describe "Xurrency" do
|
8
|
+
it "should cache queries" do
|
9
|
+
res = $xu.currency_name("jpy")
|
10
|
+
res.should == "Japanese Yen"
|
11
|
+
res.__id__.should == $xu.currency_name("jpy").__id__
|
12
|
+
res.__id__.should.not == Xurrency::Request.currency_name("jpy").__id__
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should update the value" do
|
16
|
+
res = $xu.zone("jpy")
|
17
|
+
res.should == "Japan"
|
18
|
+
res.__id__.should == $xu.zone("jpy").__id__
|
19
|
+
res.__id__.should.not == $xu.update(:zone, "jpy").__id__
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xurrency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "001"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keita Yamaguchi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-04-15 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: soap4r
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description:
|
25
|
+
email: keita.yamaguchi@gmail.com
|
26
|
+
executables:
|
27
|
+
- xurrency
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- .gemified
|
34
|
+
- History.txt
|
35
|
+
- License.txt
|
36
|
+
- README.txt
|
37
|
+
- Rakefile
|
38
|
+
- lib/xurrency.rb
|
39
|
+
- bin/xurrency
|
40
|
+
- spec/request_spec.rb
|
41
|
+
- spec/xurrency_spec.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://rubyforge.org/projects/xurrency/
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
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: xurrency
|
64
|
+
rubygems_version: 1.1.1
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: ruby-xurrency is a wrapper library for API xurrency(http://xurrency.com/api).
|
68
|
+
test_files: []
|
69
|
+
|