open_exchange_rates 0.2.0

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in open_exchange_rates.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Vlado Cingel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # OpenExchangeRates
2
+
3
+ Ruby gem for currency conversion based on [Open Exchange Rates API](http://openexchangerates.org) - free / open source hourly-updated currency data for everybody
4
+
5
+ ## Accuracy
6
+
7
+ Please see [https://github.com/currencybot/open-exchange-rates/#accuracy](https://github.com/currencybot/open-exchange-rates/#accuracy) and/or [http://openexchangerates.org/documentation/#specification](http://openexchangerates.org/documentation/#specification)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'open_exchange_rates'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install open_exchange_rates
22
+
23
+ ## Usage
24
+
25
+ Start by creating OpenExchangeRates::Rates instance
26
+
27
+ fx = OpenExchangeRates::Rates.new
28
+
29
+ Convert between currencies using current rates
30
+
31
+ fx.convert(123.45, :from => "USD", :to => "EUR") # => 99.87
32
+
33
+ Convert between currencies on specific date
34
+
35
+ fx.convert(123.45, :from => "USD", :to => "EUR", :on => "2012-05-10") # => 95.47
36
+
37
+ Get current exchange rate
38
+
39
+ fx.exchange_rate(:from => "USD", :to => "EUR") # => 0.808996
40
+
41
+ Get exchange rate on specific date
42
+
43
+ fx.exchange_rate(:from => "USD", :to => "EUR", :on => "2012-05-10") # => 0.773329
44
+
45
+ ### Default currency
46
+
47
+ If you omit **:from** or **:to** option conversion will be related to base currency. USD is set as base currency (plan is to add this as config option in the near future).
48
+
49
+ fx.convert(123.45, :to => "EUR") # => 99.87 EUR
50
+ fx.convert(123.45, :from => "EUR") # => 152.51 USD
51
+
52
+ fx.exchange_rate(:to => "EUR") # => 0.808996
53
+ fx.exchange_rate(:from => "EUR") # => 1.235414
54
+
55
+
56
+ ## TODO
57
+
58
+ - ability to set default currency (USD is currently always set as base currency)
59
+ - ability to pass Date as :on option (only 'yyyy-mm-dd' works currently)
60
+ - write some docs
61
+ - write more test for specific situations (invalid date, ...)
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
70
+
71
+ ## Licence and Terms
72
+
73
+ Licence - [http://openexchangerates.org/license](http://openexchangerates.org/license)
74
+ Terms - [http://openexchangerates.org/terms](http://openexchangerates.org/terms)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ require "yajl"
2
+
3
+ module OpenExchangeRates
4
+ class Parser < Yajl::Parser
5
+ end
6
+ end
@@ -0,0 +1,56 @@
1
+ require "open-uri"
2
+
3
+ module OpenExchangeRates
4
+ class Rates
5
+
6
+ def exchange_rate(options = {})
7
+ from_curr = options[:from].to_s.upcase
8
+ to_curr = options[:to].to_s.upcase
9
+
10
+ response = options[:on] ? on(options[:on]) : latest
11
+ rates = response.rates
12
+
13
+ from_curr = response.base_currency if from_curr.empty?
14
+ to_curr = response.base_currency if to_curr.empty?
15
+
16
+ if from_curr == to_curr
17
+ rate = 1
18
+ elsif from_curr == response.base_currency
19
+ rate = rates[to_curr]
20
+ elsif to_curr == response.base_currency
21
+ rate = 1 / rates[from_curr]
22
+ else
23
+ rate = rates[to_curr] * (1 / rates[from_curr])
24
+ end
25
+ round(rate, 6)
26
+ end
27
+
28
+ def convert(amount, options = {})
29
+ round(amount*exchange_rate(options))
30
+ end
31
+
32
+ def latest(reload = false)
33
+ @latest_response = reload ? parse_latest : (@latest_response ||= parse_latest)
34
+ OpenExchangeRates::Response.new(@latest_response)
35
+ end
36
+
37
+ def on(date_string)
38
+ OpenExchangeRates::Response.new(parse_on(date_string))
39
+ end
40
+
41
+ def round(amount, decimals = 2)
42
+ (amount * 10**decimals).round.to_f / 10**decimals
43
+ end
44
+
45
+ def parse_latest
46
+ @latets_parser ||= OpenExchangeRates::Parser.new
47
+ @latets_parser.parse(open(OpenExchangeRates::LATEST_URL))
48
+ end
49
+
50
+ def parse_on(date_string)
51
+ @on_parser = OpenExchangeRates::Parser.new
52
+ @on_parser.parse(open("#{OpenExchangeRates::BASE_URL}/historical/#{date_string}.json"))
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ module OpenExchangeRates
2
+ class Response
3
+
4
+ attr_reader :raw_data
5
+
6
+ def initialize(raw_data)
7
+ @raw_data = raw_data
8
+ end
9
+
10
+ def rates
11
+ raw_data["rates"]
12
+ end
13
+
14
+ def base_currency
15
+ raw_data["base"]
16
+ end
17
+ alias :base :base_currency
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module OpenExchangeRates
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "open_exchange_rates/version"
3
+ require "open_exchange_rates/parser"
4
+ require "open_exchange_rates/response"
5
+ require "open_exchange_rates/rates"
6
+
7
+ module OpenExchangeRates
8
+ BASE_URL = "http://openexchangerates.org"
9
+ LATEST_URL = "#{BASE_URL}/latest.json"
10
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/open_exchange_rates/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Vlado Cingel"]
6
+ gem.email = ["vladocingel@gmail.com"]
7
+ gem.description = %q{Ruby library for Open Exchange Rates API - free / open source hourly-updated currency data for everybody}
8
+ gem.summary = %q{Ruby library for Open Exchange Rates API}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "open_exchange_rates"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = OpenExchangeRates::VERSION
17
+
18
+ gem.add_dependency('yajl-ruby')
19
+
20
+ gem.add_development_dependency('rr')
21
+ gem.add_development_dependency('rake')
22
+ end
@@ -0,0 +1,167 @@
1
+ {
2
+ "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! More info: http://openexchangerates.org/terms/",
3
+ "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Full license info: http://openexchangerates.org/license/",
4
+ "timestamp": 1336690853,
5
+ "base": "USD",
6
+ "rates": {
7
+ "AED": 3.6732,
8
+ "AFN": 48.380001,
9
+ "ALL": 107.559998,
10
+ "AMD": 393.070007,
11
+ "ANG": 1.79,
12
+ "AOA": 94.949997,
13
+ "ARS": 4.439,
14
+ "AUD": 0.99458,
15
+ "AWG": 1.7901,
16
+ "AZN": 0.786,
17
+ "BAM": 1.51505,
18
+ "BBD": 2,
19
+ "BDT": 81.889999,
20
+ "BGN": 1.514,
21
+ "BHD": 0.37706,
22
+ "BIF": 1402.5,
23
+ "BMD": 1,
24
+ "BND": 1.25065,
25
+ "BOB": 6.91,
26
+ "BRL": 1.954,
27
+ "BSD": 1,
28
+ "BTN": 53.384998,
29
+ "BWP": 7.5188,
30
+ "BYR": 8135,
31
+ "BZD": 1.9135,
32
+ "CAD": 1.00305,
33
+ "CDF": 923.920715,
34
+ "CHF": 0.92865,
35
+ "CLF": 0.02154,
36
+ "CLP": 487.049988,
37
+ "CNH": 6.2995,
38
+ "CNY": 6.3152,
39
+ "COP": 1766.300049,
40
+ "CRC": 503.700012,
41
+ "CUP": 1,
42
+ "CVE": 85.379997,
43
+ "CZK": 19.51585,
44
+ "DJF": 179.710007,
45
+ "DKK": 5.7477,
46
+ "DOP": 39.075001,
47
+ "DZD": 74.379997,
48
+ "EGP": 6.0418,
49
+ "ETB": 17.609699,
50
+ "EUR": 0.773329,
51
+ "FJD": 1.8162,
52
+ "FKP": 0.6191,
53
+ "GBP": 0.619963,
54
+ "GEL": 1.62505,
55
+ "GHS": 1.8808,
56
+ "GIP": 0.61915,
57
+ "GMD": 29.690001,
58
+ "GNF": 6990,
59
+ "GTQ": 7.7745,
60
+ "GYD": 203.199997,
61
+ "HKD": 7.76345,
62
+ "HNL": 19.055,
63
+ "HRK": 5.80025,
64
+ "HTG": 41.849998,
65
+ "HUF": 223.854996,
66
+ "IDR": 9218,
67
+ "IEP": 0.608809,
68
+ "ILS": 3.8129,
69
+ "INR": 53.255001,
70
+ "IQD": 1165.5,
71
+ "IRR": 12278,
72
+ "ISK": 125.620003,
73
+ "JMD": 87.239998,
74
+ "JOD": 0.7095,
75
+ "JPY": 79.885002,
76
+ "KES": 83.599998,
77
+ "KGS": 46.9706,
78
+ "KHR": 4025.300049,
79
+ "KMF": 380.30899,
80
+ "KPW": 900,
81
+ "KRW": 1142.400024,
82
+ "KWD": 0.27867,
83
+ "KZT": 147.850006,
84
+ "LAK": 7980.299805,
85
+ "LBP": 1504,
86
+ "LKR": 127.849998,
87
+ "LRD": 74.400002,
88
+ "LSL": 8.035,
89
+ "LTL": 2.6704,
90
+ "LVL": 0.5397,
91
+ "LYD": 1.2521,
92
+ "MAD": 8.5844,
93
+ "MDL": 11.9,
94
+ "MGA": 2080,
95
+ "MKD": 47.465,
96
+ "MMK": 827.5,
97
+ "MNT": 1312.5,
98
+ "MOP": 7.9961,
99
+ "MRO": 292.079987,
100
+ "MUR": 29.23,
101
+ "MVR": 15.3,
102
+ "MWK": 250.5,
103
+ "MXN": 13.55145,
104
+ "MYR": 3.067,
105
+ "MZN": 27.424999,
106
+ "NAD": 8.002,
107
+ "NGN": 157.570007,
108
+ "NIO": 23.290001,
109
+ "NOK": 5.8685,
110
+ "NPR": 85.860001,
111
+ "NZD": 1.275998,
112
+ "OMR": 0.38505,
113
+ "PAB": 1,
114
+ "PEN": 2.649,
115
+ "PGK": 2.0362,
116
+ "PHP": 42.459999,
117
+ "PKR": 90.949997,
118
+ "PLN": 3.274,
119
+ "PYG": 4345,
120
+ "QAR": 3.6415,
121
+ "RON": 3.4164,
122
+ "RSD": 86.7705,
123
+ "RUB": 30.1635,
124
+ "RWF": 608.155273,
125
+ "SAR": 3.7503,
126
+ "SBD": 7.070963,
127
+ "SCR": 14.11295,
128
+ "SDG": 2.675,
129
+ "SEK": 6.9449,
130
+ "SGD": 1.2518,
131
+ "SHP": 0.61915,
132
+ "SLL": 4357.5,
133
+ "SOS": 1627.300049,
134
+ "SRD": 3.2875,
135
+ "STD": 18923.300781,
136
+ "SVC": 8.7475,
137
+ "SYP": 62.549999,
138
+ "SZL": 8.035,
139
+ "THB": 31.125,
140
+ "TJS": 4.7593,
141
+ "TMT": 2.8525,
142
+ "TND": 1.5597,
143
+ "TOP": 1.648917,
144
+ "TRY": 1.7868,
145
+ "TTD": 6.39355,
146
+ "TWD": 29.363001,
147
+ "TZS": 1572.5,
148
+ "UAH": 8.035,
149
+ "UGX": 2482.5,
150
+ "USD": 1,
151
+ "UYU": 19.91,
152
+ "UZS": 1856.150024,
153
+ "VEF": 4.2951,
154
+ "VND": 20881,
155
+ "VUV": 92.199997,
156
+ "WST": 2.265296,
157
+ "XAF": 507.078705,
158
+ "XCD": 2.7,
159
+ "XDR": 0.650166,
160
+ "XOF": 506.399994,
161
+ "XPF": 92.230003,
162
+ "YER": 214.714996,
163
+ "ZAR": 8.03925,
164
+ "ZMK": 5135,
165
+ "ZWL": 322.355011
166
+ }
167
+ }
data/test/latest.json ADDED
@@ -0,0 +1,167 @@
1
+ {
2
+ "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! More info: http://openexchangerates.org/terms/",
3
+ "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Full license info: http://openexchangerates.org/license/",
4
+ "timestamp": 1338460116,
5
+ "base": "USD",
6
+ "rates": {
7
+ "AED": 3.6733,
8
+ "AFN": 48.419998,
9
+ "ALL": 112,
10
+ "AMD": 406.130005,
11
+ "ANG": 1.79,
12
+ "AOA": 95.610001,
13
+ "ARS": 4.47,
14
+ "AUD": 1.026057,
15
+ "AWG": 1.7899,
16
+ "AZN": 0.7857,
17
+ "BAM": 1.58,
18
+ "BBD": 2,
19
+ "BDT": 82.084999,
20
+ "BGN": 1.5787,
21
+ "BHD": 0.37698,
22
+ "BIF": 1416,
23
+ "BMD": 1,
24
+ "BND": 1.28465,
25
+ "BOB": 6.91,
26
+ "BRL": 2.0167,
27
+ "BSD": 1,
28
+ "BTN": 56.130001,
29
+ "BWP": 7.8431,
30
+ "BYR": 8400,
31
+ "BZD": 1.9135,
32
+ "CAD": 1.02753,
33
+ "CDF": 920.661682,
34
+ "CHF": 0.96804,
35
+ "CLF": 0.02288,
36
+ "CLP": 517.75,
37
+ "CNH": 6.2995,
38
+ "CNY": 6.3436,
39
+ "COP": 1824.5,
40
+ "CRC": 500.5,
41
+ "CUP": 1,
42
+ "CVE": 89.050003,
43
+ "CZK": 20.682949,
44
+ "DJF": 180.949997,
45
+ "DKK": 5.9872,
46
+ "DOP": 39.099998,
47
+ "DZD": 76.894997,
48
+ "EGP": 6.0459,
49
+ "ETB": 17.6849,
50
+ "EUR": 0.805737,
51
+ "FJD": 1.8664,
52
+ "FKP": 0.6449,
53
+ "GBP": 0.644683,
54
+ "GEL": 1.62485,
55
+ "GHS": 1.8827,
56
+ "GIP": 0.6449,
57
+ "GMD": 30.26,
58
+ "GNF": 7150,
59
+ "GTQ": 7.8045,
60
+ "GYD": 203.699997,
61
+ "HKD": 7.76355,
62
+ "HNL": 19.055,
63
+ "HRK": 6.0995,
64
+ "HTG": 41.970001,
65
+ "HUF": 242.395004,
66
+ "IDR": 9548,
67
+ "IEP": 0.634055,
68
+ "ILS": 3.88115,
69
+ "INR": 56.205002,
70
+ "IQD": 1165.5,
71
+ "IRR": 12275,
72
+ "ISK": 130.449997,
73
+ "JMD": 87.43,
74
+ "JOD": 0.7105,
75
+ "JPY": 78.839996,
76
+ "KES": 86.900002,
77
+ "KGS": 46.9352,
78
+ "KHR": 4050,
79
+ "KMF": 396.077393,
80
+ "KPW": 900,
81
+ "KRW": 1178.199951,
82
+ "KWD": 0.2808,
83
+ "KZT": 148.020004,
84
+ "LAK": 8032.5,
85
+ "LBP": 1503.5,
86
+ "LKR": 132.25,
87
+ "LRD": 74.25,
88
+ "LSL": 8.51,
89
+ "LTL": 2.78165,
90
+ "LVL": 0.5623,
91
+ "LYD": 1.2802,
92
+ "MAD": 8.8936,
93
+ "MDL": 12.02,
94
+ "MGA": 2122,
95
+ "MKD": 48.860001,
96
+ "MMK": 841.5,
97
+ "MNT": 1316,
98
+ "MOP": 7.99655,
99
+ "MRO": 290.25,
100
+ "MUR": 30,
101
+ "MVR": 15.4,
102
+ "MWK": 269.484985,
103
+ "MXN": 14.11515,
104
+ "MYR": 3.17175,
105
+ "MZN": 27.5,
106
+ "NAD": 8.515,
107
+ "NGN": 159.850006,
108
+ "NIO": 23.445,
109
+ "NOK": 6.0581,
110
+ "NPR": 89.809998,
111
+ "NZD": 1.322664,
112
+ "OMR": 0.38465,
113
+ "PAB": 1,
114
+ "PEN": 2.708,
115
+ "PGK": 2.0429,
116
+ "PHP": 43.43,
117
+ "PKR": 93.800003,
118
+ "PLN": 3.54265,
119
+ "PYG": 4535,
120
+ "QAR": 3.6413,
121
+ "RON": 3.6025,
122
+ "RSD": 94.440498,
123
+ "RUB": 33.050598,
124
+ "RWF": 609.275513,
125
+ "SAR": 3.7505,
126
+ "SBD": 7.048334,
127
+ "SCR": 14.336,
128
+ "SDG": 2.6769,
129
+ "SEK": 7.2326,
130
+ "SGD": 1.285105,
131
+ "SHP": 0.6449,
132
+ "SLL": 4350,
133
+ "SOS": 1620,
134
+ "SRD": 3.275,
135
+ "STD": 19545,
136
+ "SVC": 8.7475,
137
+ "SYP": 63.900002,
138
+ "SZL": 8.504,
139
+ "THB": 31.84,
140
+ "TJS": 4.7659,
141
+ "TMT": 2.8615,
142
+ "TND": 1.6138,
143
+ "TOP": 1.795477,
144
+ "TRY": 1.8478,
145
+ "TTD": 6.3963,
146
+ "TWD": 29.811001,
147
+ "TZS": 1588.5,
148
+ "UAH": 8.082,
149
+ "UGX": 2485,
150
+ "USD": 1,
151
+ "UYU": 20.299999,
152
+ "UZS": 1871.439941,
153
+ "VEF": 4.2927,
154
+ "VND": 20840,
155
+ "VUV": 95.25,
156
+ "WST": 2.398426,
157
+ "XAF": 528.103271,
158
+ "XCD": 2.7,
159
+ "XDR": 0.661611,
160
+ "XOF": 529,
161
+ "XPF": 95.650002,
162
+ "YER": 215.5,
163
+ "ZAR": 8.50305,
164
+ "ZMK": 5345,
165
+ "ZWL": 322.355011
166
+ }
167
+ }
@@ -0,0 +1,156 @@
1
+ require "test_helper"
2
+
3
+ class TestOpenExchangeRates < Test::Unit::TestCase
4
+
5
+ def test_exchange_rate
6
+ fx = OpenExchangeRates::Rates.new
7
+ stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(File.open("latest.json")) }
8
+
9
+ # 1 USD = 6.0995 HRK
10
+ # 1 USD = 1.026057 AUD
11
+ assert_equal 1, fx.exchange_rate(:from => "USD", :to => "USD")
12
+ assert_equal 1, fx.exchange_rate(:from => "AUD", :to => "AUD")
13
+ assert_equal 1, fx.exchange_rate(:from => "HRK", :to => "HRK")
14
+
15
+ assert_equal 6.0995, fx.exchange_rate(:from => "USD", :to => "HRK")
16
+ assert_equal 1.026057, fx.exchange_rate(:from => "USD", :to => "AUD")
17
+
18
+ assert_equal 0.163948, fx.exchange_rate(:from => "HRK", :to => "USD")
19
+ assert_equal 0.974605, fx.exchange_rate(:from => "AUD", :to => "USD")
20
+
21
+ assert_equal 5.944602, fx.exchange_rate(:from => "AUD", :to => "HRK")
22
+ assert_equal 0.168220, fx.exchange_rate(:from => "HRK", :to => "AUD")
23
+ end
24
+
25
+ def test_exchange_rate_on_specific_date
26
+ fx = OpenExchangeRates::Rates.new
27
+ stub(fx).parse_on { OpenExchangeRates::Parser.new.parse(File.open("2012-05-10.json")) }
28
+
29
+ # 1 USD = 5.80025 HRK
30
+ # 1 USD = 0.99458 AUD
31
+ assert_equal 1, fx.exchange_rate(:from => "USD", :to => "USD", :on => "2012-05-10")
32
+ assert_equal 1, fx.exchange_rate(:from => "AUD", :to => "AUD", :on => "2012-05-10")
33
+ assert_equal 1, fx.exchange_rate(:from => "HRK", :to => "HRK", :on => "2012-05-10")
34
+
35
+ assert_equal 5.80025, fx.exchange_rate(:from => "USD", :to => "HRK", :on => "2012-05-10")
36
+ assert_equal 0.99458, fx.exchange_rate(:from => "USD", :to => "AUD", :on => "2012-05-10")
37
+
38
+ assert_equal 0.172406, fx.exchange_rate(:from => "HRK", :to => "USD", :on => "2012-05-10")
39
+ assert_equal 1.005450, fx.exchange_rate(:from => "AUD", :to => "USD", :on => "2012-05-10")
40
+
41
+ assert_equal 5.831859, fx.exchange_rate(:from => "AUD", :to => "HRK", :on => "2012-05-10")
42
+ assert_equal 0.171472, fx.exchange_rate(:from => "HRK", :to => "AUD", :on => "2012-05-10")
43
+ end
44
+
45
+ def test_convert
46
+ fx = OpenExchangeRates::Rates.new
47
+ stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(File.open("latest.json")) }
48
+
49
+ # 1 USD = 6.0995 HRK
50
+ # 1 USD = 1.026057 AUD
51
+ assert_equal 609.95, fx.convert(100, :from => "USD", :to => "HRK")
52
+ assert_equal 16.39, fx.convert(100, :from => "HRK", :to => "USD")
53
+ assert_equal 120.32, fx.convert(123.4567, :from => "AUD", :to => "USD")
54
+ assert_equal 733.90, fx.convert(123.4567, :from => "AUD", :to => "HRK")
55
+ assert_equal 20.77, fx.convert(123.4567, :from => "HRK", :to => "AUD")
56
+ end
57
+
58
+ def test_convert_on_specific_date
59
+ fx = OpenExchangeRates::Rates.new
60
+ stub(fx).parse_on { OpenExchangeRates::Parser.new.parse(File.open("2012-05-10.json")) }
61
+
62
+ # 1 USD = 5.80025 HRK
63
+ # 1 USD = 0.99458 AUD
64
+ assert_equal 580.03, fx.convert(100, :from => "USD", :to => "HRK", :on => "2012-10-05")
65
+ assert_equal 17.24, fx.convert(100, :from => "HRK", :to => "USD", :on => "2012-10-05")
66
+ assert_equal 124.13, fx.convert(123.4567, :from => "AUD", :to => "USD", :on => "2012-10-05")
67
+ assert_equal 719.98, fx.convert(123.4567, :from => "AUD", :to => "HRK", :on => "2012-10-05")
68
+ assert_equal 21.17, fx.convert(123.4567, :from => "HRK", :to => "AUD", :on => "2012-10-05")
69
+ end
70
+
71
+ def test_convert_if_from_option_is_missing
72
+ fx = OpenExchangeRates::Rates.new
73
+ stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(File.open("latest.json")) }
74
+
75
+ # from defaults to base currency (USD)
76
+ # 1 USD = 6.0995 HRK
77
+ # 1 USD = 1.026057 AUD
78
+ assert_equal 609.95, fx.convert(100, :to => "HRK")
79
+ assert_equal 102.61, fx.convert(100, :to => "AUD")
80
+ end
81
+
82
+ def test_convert_if_to_option_is_missing
83
+ fx = OpenExchangeRates::Rates.new
84
+ stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(File.open("latest.json")) }
85
+
86
+ # to defaults to base currency (USD)
87
+ # 1 USD = 6.0995 HRK
88
+ # 1 USD = 1.026057 AUD
89
+ assert_equal 16.39, fx.convert(100, :from => "HRK")
90
+ assert_equal 97.46, fx.convert(100, :from => "AUD")
91
+ end
92
+
93
+ def test_latest
94
+ fx = OpenExchangeRates::Rates.new
95
+ latest_rates = fx.latest
96
+
97
+ assert_equal "USD", latest_rates.base_currency
98
+ assert_equal "USD", latest_rates.base
99
+ assert latest_rates.rates.is_a?(Hash)
100
+
101
+ stub(fx).parse_latest { OpenExchangeRates::Parser.new.parse(File.open("latest.json")) }
102
+
103
+ # latest results are cached
104
+ cached_rates = fx.latest
105
+ assert_equal latest_rates.rates["USD"], cached_rates.rates["USD"]
106
+ assert_equal latest_rates.rates["AUD"], cached_rates.rates["AUD"]
107
+ assert_equal latest_rates.rates["HRK"], cached_rates.rates["HRK"]
108
+
109
+ # latest results are reloaded
110
+ stubbed_rates = fx.latest(:reload)
111
+ assert_equal latest_rates.rates["USD"], stubbed_rates.rates["USD"]
112
+ assert_not_equal latest_rates.rates["AUD"], stubbed_rates.rates["AUD"]
113
+ assert_not_equal latest_rates.rates["HRK"], stubbed_rates.rates["HRK"]
114
+
115
+ assert_equal 1, stubbed_rates.rates["USD"]
116
+ assert_equal 1.026057, stubbed_rates.rates["AUD"]
117
+ assert_equal 6.0995, stubbed_rates.rates["HRK"]
118
+ end
119
+
120
+ def test_on
121
+ fx = OpenExchangeRates::Rates.new
122
+ on_rates = fx.on("2012-05-10")
123
+
124
+ assert_equal "USD", on_rates.base_currency
125
+ assert_equal "USD", on_rates.base
126
+ assert on_rates.rates.is_a?(Hash)
127
+
128
+ assert_equal 1, on_rates.rates["USD"]
129
+ assert_equal 0.99458, on_rates.rates["AUD"]
130
+ assert_equal 5.80025, on_rates.rates["HRK"]
131
+ end
132
+
133
+ def test_round
134
+ fx = OpenExchangeRates::Rates.new
135
+
136
+ assert_equal 12.35, fx.round(12.345)
137
+ assert_equal 1.23, fx.round(1.2345)
138
+ assert_equal 1.2, fx.round(1.2345, 1)
139
+ assert_equal 12.3457, fx.round(12.345678, 4)
140
+ end
141
+
142
+ def test_multiple_calls
143
+ fx = OpenExchangeRates::Rates.new
144
+ assert_nothing_raised do
145
+ fx.convert(123, :from => "EUR", :to => "AUD", :on => "2012-03-10")
146
+ fx.convert(100, :from => "USD", :to => "EUR")
147
+ fx.convert(123.45, :from => "EUR", :to => "USD", :on => "2012-04-10")
148
+ fx.convert(12, :from => "USD", :to => "EUR")
149
+ fx.convert(123.4567, :from => "EUR", :to => "USD", :on => "2012-05-10")
150
+ fx.exchange_rate(:from => "USD", :to => "EUR")
151
+ fx.exchange_rate(:from => "USD", :to => "EUR", :on => "2012-04-10")
152
+ fx.exchange_rate(:from => "USD", :to => "AUD", :on => "2012-05-10")
153
+ end
154
+ end
155
+
156
+ end
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "rr"
4
+ require "open_exchange_rates"
5
+
6
+ class Test::Unit::TestCase
7
+ include RR::Adapters::TestUnit
8
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_exchange_rates
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Vlado Cingel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-01 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: yajl-ruby
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rr
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: Ruby library for Open Exchange Rates API - free / open source hourly-updated currency data for everybody
63
+ email:
64
+ - vladocingel@gmail.com
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - .gitignore
73
+ - Gemfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - lib/open_exchange_rates.rb
78
+ - lib/open_exchange_rates/parser.rb
79
+ - lib/open_exchange_rates/rates.rb
80
+ - lib/open_exchange_rates/response.rb
81
+ - lib/open_exchange_rates/version.rb
82
+ - open_exchange_rates.gemspec
83
+ - test/2012-05-10.json
84
+ - test/latest.json
85
+ - test/rates_test.rb
86
+ - test/test_helper.rb
87
+ homepage: ""
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.12
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Ruby library for Open Exchange Rates API
120
+ test_files:
121
+ - test/2012-05-10.json
122
+ - test/latest.json
123
+ - test/rates_test.rb
124
+ - test/test_helper.rb
125
+ has_rdoc: