exchange 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.document +5 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +17 -0
  5. data/Gemfile.lock +41 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.rdoc +262 -0
  8. data/Rakefile +46 -0
  9. data/VERSION +1 -0
  10. data/exchange.gemspec +102 -0
  11. data/lib/core_extensions/conversability.rb +32 -0
  12. data/lib/exchange.rb +11 -0
  13. data/lib/exchange/cache.rb +4 -0
  14. data/lib/exchange/cache/base.rb +51 -0
  15. data/lib/exchange/cache/memcached.rb +52 -0
  16. data/lib/exchange/cache/rails.rb +45 -0
  17. data/lib/exchange/cache/redis.rb +54 -0
  18. data/lib/exchange/configuration.rb +72 -0
  19. data/lib/exchange/currency.rb +237 -0
  20. data/lib/exchange/external_api.rb +4 -0
  21. data/lib/exchange/external_api/base.rb +114 -0
  22. data/lib/exchange/external_api/call.rb +76 -0
  23. data/lib/exchange/external_api/currency_bot.rb +47 -0
  24. data/lib/exchange/external_api/xavier_media.rb +47 -0
  25. data/spec/core_extensions/conversability_spec.rb +64 -0
  26. data/spec/exchange/cache/base_spec.rb +29 -0
  27. data/spec/exchange/cache/memcached_spec.rb +72 -0
  28. data/spec/exchange/cache/rails_spec.rb +67 -0
  29. data/spec/exchange/cache/redis_spec.rb +76 -0
  30. data/spec/exchange/configuration_spec.rb +47 -0
  31. data/spec/exchange/currency_spec.rb +219 -0
  32. data/spec/exchange/external_api/base_spec.rb +31 -0
  33. data/spec/exchange/external_api/call_spec.rb +68 -0
  34. data/spec/exchange/external_api/currency_bot_spec.rb +61 -0
  35. data/spec/exchange/external_api/xavier_media_spec.rb +59 -0
  36. data/spec/spec_helper.rb +28 -0
  37. data/spec/support/api_responses/example_historic_json.json +167 -0
  38. data/spec/support/api_responses/example_json_api.json +167 -0
  39. data/spec/support/api_responses/example_xml_api.xml +156 -0
  40. metadata +191 -0
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Exchange::ExternalAPI::CurrencyBot" do
4
+ before(:all) do
5
+ Exchange::Configuration.cache = false
6
+ end
7
+ describe "updating rates" do
8
+ subject { Exchange::ExternalAPI::CurrencyBot.new }
9
+ before(:each) do
10
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json", fixture('api_responses/example_json_api.json'))
11
+ end
12
+ it "should call the api and yield a block with the result" do
13
+ subject.update
14
+ subject.base.should == 'USD'
15
+ end
16
+ it "should set a unix timestamp from the api file" do
17
+ subject.update
18
+ subject.timestamp.should == 1327748496
19
+ end
20
+ end
21
+ describe "conversion" do
22
+ subject { Exchange::ExternalAPI::CurrencyBot.new }
23
+ before(:each) do
24
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json", fixture('api_responses/example_json_api.json'))
25
+ end
26
+ it "should convert right" do
27
+ subject.convert(80, 'eur', 'usd').should == 105.76
28
+ end
29
+ it "should convert negative numbers right" do
30
+ subject.convert(-70, 'chf', 'usd').should == -76.71
31
+ end
32
+ it "should convert when given symbols" do
33
+ subject.convert(70, :sek, :usd).should == 10.38
34
+ end
35
+ end
36
+ describe "historic conversion" do
37
+ subject { Exchange::ExternalAPI::CurrencyBot.new }
38
+ before(:each) do
39
+ end
40
+ it "should convert and be able to use history" do
41
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/historical/2011-09-09.json", fixture('api_responses/example_json_api.json'))
42
+ subject.convert(70, 'eur', 'usd', :at => Time.gm(2011,9,9)).should == 92.54
43
+ end
44
+ it "should convert negative numbers right" do
45
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/historical/2011-09-09.json", fixture('api_responses/example_json_api.json'))
46
+ subject.convert(-70, 'chf', 'usd', :at => Time.gm(2011,9,9)).should == -76.71
47
+ end
48
+ it "should convert when given symbols" do
49
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/historical/2011-09-09.json", fixture('api_responses/example_json_api.json'))
50
+ subject.convert(70, :sek, :usd, :at => Time.gm(2011,9,9)).should == 10.38
51
+ end
52
+ it "should convert right when the year is the same, but the yearday is not" do
53
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/historical/#{Time.now.year}-0#{Time.now.month > 10 ? Time.now.month - 1 : Time.now.month + 1}-01.json", fixture('api_responses/example_json_api.json'))
54
+ subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year,Time.now.month > 9 ? Time.now.month - 1 : Time.now.month + 1,1)).should == 10.38
55
+ end
56
+ it "should convert right when the yearday is the same, but the year is not" do
57
+ mock_api("https://raw.github.com/currencybot/open-exchange-rates/master/historical/#{Time.now.year-1}-03-01.json", fixture('api_responses/example_json_api.json'))
58
+ subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year - 1,3,1)).should == 10.38
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Exchange::ExternalAPI::XavierMedia" do
4
+ before(:all) do
5
+ Exchange::Configuration.cache = false
6
+ end
7
+ describe "updating rates" do
8
+ subject { Exchange::ExternalAPI::XavierMedia.new }
9
+ before(:each) do
10
+ mock_api("http://api.finance.xaviermedia.com/api/#{Time.now.strftime("%Y/%m/%d")}.xml", fixture('api_responses/example_xml_api.xml'))
11
+ end
12
+ it "should call the api and yield a block with the result" do
13
+ subject.update
14
+ subject.base.should == 'EUR'
15
+ end
16
+ it "should set a unix timestamp from the api file" do
17
+ subject.update
18
+ subject.timestamp.should == 1322697600
19
+ end
20
+ end
21
+ describe "conversion" do
22
+ subject { Exchange::ExternalAPI::XavierMedia.new }
23
+ before(:each) do
24
+ mock_api("http://api.finance.xaviermedia.com/api/#{Time.now.strftime("%Y/%m/%d")}.xml", fixture('api_responses/example_xml_api.xml'))
25
+ end
26
+ it "should convert right" do
27
+ subject.convert(80, 'eur', 'usd').should == 107.94
28
+ end
29
+ it "should convert negative numbers right" do
30
+ subject.convert(-70, 'chf', 'usd').should == -77.01
31
+ end
32
+ it "should convert when given symbols" do
33
+ subject.convert(70, :sek, :usd).should == 10.35
34
+ end
35
+ end
36
+ describe "historic conversion" do
37
+ subject { Exchange::ExternalAPI::XavierMedia.new }
38
+ it "should convert and be able to use history" do
39
+ mock_api("http://api.finance.xaviermedia.com/api/2011/09/09.xml", fixture('api_responses/example_xml_api.xml'))
40
+ subject.convert(70, 'eur', 'usd', :at => Time.gm(2011,9,9)).should == 94.44
41
+ end
42
+ it "should convert negative numbers right" do
43
+ mock_api("http://api.finance.xaviermedia.com/api/2011/09/09.xml", fixture('api_responses/example_xml_api.xml'))
44
+ subject.convert(-70, 'chf', 'usd', :at => Time.gm(2011,9,9)).should == -77.01
45
+ end
46
+ it "should convert when given symbols" do
47
+ mock_api("http://api.finance.xaviermedia.com/api/2011/09/09.xml", fixture('api_responses/example_xml_api.xml'))
48
+ subject.convert(70, :sek, :usd, :at => Time.gm(2011,9,9)).should == 10.35
49
+ end
50
+ it "should convert right when the year is the same, but the yearday is not" do
51
+ mock_api("http://api.finance.xaviermedia.com/api/#{Time.now.year}/0#{Time.now.month > 10 ? Time.now.month - 1 : Time.now.month + 1}/01.xml", fixture('api_responses/example_xml_api.xml'))
52
+ subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year,Time.now.month > 9 ? Time.now.month - 1 : Time.now.month + 1,1)).should == 10.35
53
+ end
54
+ it "should convert right when the yearday is the same, but the year is not" do
55
+ mock_api("http://api.finance.xaviermedia.com/api/#{Time.now.year-1}/03/01.xml", fixture('api_responses/example_xml_api.xml'))
56
+ subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year - 1,3,1)).should == 10.35
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+ require 'shoulda'
15
+ require 'exchange'
16
+
17
+ module HelperMethods
18
+ def fixture(name)
19
+ File.read(File.dirname(__FILE__) + "/support/#{name}")
20
+ end
21
+
22
+ def mock_api(adress, response, count=1)
23
+ @uri_mock = mock('uri', :open => mock('opened_uri', :read => response))
24
+ URI.should_receive(:parse).with(adress).at_most(count).times.and_return(@uri_mock)
25
+ end
26
+ end
27
+
28
+ RSpec.configuration.include(HelperMethods)
@@ -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, and please share/watch/fork if you think data like this should be free!",
3
+ "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
4
+ "timestamp": 1327748496,
5
+ "base": "USD",
6
+ "rates": {
7
+ "AED": 3.6731,
8
+ "AFN": 44.25,
9
+ "ALL": 105.029999,
10
+ "AMD": 386.630005,
11
+ "ANG": 1.79,
12
+ "AOA": 95.285004,
13
+ "ARS": 4.334,
14
+ "AUD": 0.938218,
15
+ "AWG": 1.79,
16
+ "AZN": 0.7863,
17
+ "BAM": 1.48265,
18
+ "BBD": 2,
19
+ "BDT": 84.480003,
20
+ "BGN": 1.4929,
21
+ "BHD": 0.37696,
22
+ "BIF": 1294.75,
23
+ "BMD": 1,
24
+ "BND": 1.2514,
25
+ "BOB": 6.91,
26
+ "BRL": 1.7412,
27
+ "BSD": 1,
28
+ "BTN": 49.310001,
29
+ "BWP": 7.278,
30
+ "BYR": 8350,
31
+ "BZD": 1.9485,
32
+ "CAD": 1.00275,
33
+ "CDF": 921.919495,
34
+ "CHF": 0.91355,
35
+ "CLF": 0.02164,
36
+ "CLP": 484.779999,
37
+ "CNY": 6.3182,
38
+ "COP": 1808,
39
+ "CRC": 503.149994,
40
+ "CUP": 1,
41
+ "CVE": 84.230003,
42
+ "CZK": 19.007999,
43
+ "DJF": 178.509995,
44
+ "DKK": 5.6281,
45
+ "DOP": 38.875,
46
+ "DZD": 76.019997,
47
+ "EGP": 6.04075,
48
+ "ETB": 17.35375,
49
+ "EUR": 0.766401,
50
+ "FJD": 1.7699,
51
+ "FKP": 0.6358,
52
+ "GBP": 0.635627,
53
+ "GEL": 1.66895,
54
+ "GHS": 1.6835,
55
+ "GIP": 0.63575,
56
+ "GMD": 30.549999,
57
+ "GNF": 7237.5,
58
+ "GTQ": 7.8145,
59
+ "GYD": 203.949997,
60
+ "HKD": 7.7561,
61
+ "HNL": 19.055,
62
+ "HRK": 5.72725,
63
+ "HTG": 40.349998,
64
+ "HUF": 222.050003,
65
+ "IDR": 8973,
66
+ "IEP": 0.595735,
67
+ "ILS": 3.7492,
68
+ "INR": 49.314999,
69
+ "IQD": 1165.310059,
70
+ "IRR": 11308,
71
+ "ISK": 123.040001,
72
+ "JMD": 86.25,
73
+ "JOD": 0.7083,
74
+ "JPY": 76.68,
75
+ "KES": 85.099998,
76
+ "KGS": 46.648899,
77
+ "KHR": 4189.799805,
78
+ "KMF": 372.139038,
79
+ "KPW": 900,
80
+ "KRW": 1121.02002,
81
+ "KWD": 0.27814,
82
+ "KZT": 148.425003,
83
+ "LAK": 7998.299805,
84
+ "LBP": 1505,
85
+ "LKR": 113.834999,
86
+ "LRD": 73.339996,
87
+ "LSL": 7.76,
88
+ "LTL": 2.614,
89
+ "LVL": 0.529,
90
+ "LYD": 1.2485,
91
+ "MAD": 8.4415,
92
+ "MDL": 11.785,
93
+ "MGA": 2190,
94
+ "MKD": 46.939999,
95
+ "MMK": 6.51,
96
+ "MNT": 1362.5,
97
+ "MOP": 7.98705,
98
+ "MRO": 290.130005,
99
+ "MUR": 29.5,
100
+ "MVR": 15.41,
101
+ "MWK": 165.5,
102
+ "MXN": 12.9155,
103
+ "MYR": 3.0425,
104
+ "MZN": 27.087,
105
+ "NAD": 7.756,
106
+ "NGN": 160.949997,
107
+ "NIO": 23.065001,
108
+ "NOK": 5.80155,
109
+ "NPR": 80,
110
+ "NZD": 1.212342,
111
+ "OMR": 0.3851,
112
+ "PAB": 1,
113
+ "PEN": 2.69,
114
+ "PGK": 2.0619,
115
+ "PHP": 42.849998,
116
+ "PKR": 90.150002,
117
+ "PLN": 3.1972,
118
+ "PYG": 4710,
119
+ "QAR": 3.6415,
120
+ "RON": 3.2843,
121
+ "RSD": 80.640503,
122
+ "RUB": 30.131001,
123
+ "RWF": 604.380066,
124
+ "SAR": 3.7503,
125
+ "SBD": 7.11124,
126
+ "SCR": 13.99045,
127
+ "SDG": 2.6778,
128
+ "SEK": 6.7462,
129
+ "SGD": 1.25075,
130
+ "SHP": 0.63575,
131
+ "SLL": 4407.5,
132
+ "SOS": 1627,
133
+ "SRD": 3.275,
134
+ "STD": 18602.5,
135
+ "SVC": 8.7475,
136
+ "SYP": 57.450001,
137
+ "SZL": 7.762,
138
+ "THB": 31.084999,
139
+ "TJS": 4.7581,
140
+ "TMT": 2.8425,
141
+ "TND": 1.4945,
142
+ "TOP": 1.708066,
143
+ "TRY": 1.7767,
144
+ "TTD": 6.4053,
145
+ "TWD": 29.74,
146
+ "TZS": 1595.5,
147
+ "UAH": 8.038,
148
+ "UGX": 2343.5,
149
+ "USD": 1,
150
+ "UYU": 19.52,
151
+ "UZS": 1808.170044,
152
+ "VEF": 4.2927,
153
+ "VND": 20943,
154
+ "VUV": 90.5,
155
+ "WST": 2.309777,
156
+ "XAF": 496.185364,
157
+ "XCD": 2.7,
158
+ "XDR": 0.646403,
159
+ "XOF": 497.450012,
160
+ "XPF": 90.980003,
161
+ "YER": 213.255005,
162
+ "ZAR": 7.7622,
163
+ "ZMK": 5150,
164
+ "ZWD": 0,
165
+ "ZWL": 322.355011
166
+ }
167
+ }
@@ -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, and please share/watch/fork if you think data like this should be free!",
3
+ "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
4
+ "timestamp": 1327748496,
5
+ "base": "USD",
6
+ "rates": {
7
+ "AED": 3.6731,
8
+ "AFN": 44.25,
9
+ "ALL": 105.029999,
10
+ "AMD": 386.630005,
11
+ "ANG": 1.79,
12
+ "AOA": 95.285004,
13
+ "ARS": 4.334,
14
+ "AUD": 0.938218,
15
+ "AWG": 1.79,
16
+ "AZN": 0.7863,
17
+ "BAM": 1.48265,
18
+ "BBD": 2,
19
+ "BDT": 84.480003,
20
+ "BGN": 1.4929,
21
+ "BHD": 0.37696,
22
+ "BIF": 1294.75,
23
+ "BMD": 1,
24
+ "BND": 1.2514,
25
+ "BOB": 6.91,
26
+ "BRL": 1.7412,
27
+ "BSD": 1,
28
+ "BTN": 49.310001,
29
+ "BWP": 7.278,
30
+ "BYR": 8350,
31
+ "BZD": 1.9485,
32
+ "CAD": 1.00275,
33
+ "CDF": 921.919495,
34
+ "CHF": 0.91255,
35
+ "CLF": 0.02164,
36
+ "CLP": 484.779999,
37
+ "CNY": 6.3182,
38
+ "COP": 1808,
39
+ "CRC": 503.149994,
40
+ "CUP": 1,
41
+ "CVE": 84.230003,
42
+ "CZK": 19.007999,
43
+ "DJF": 178.509995,
44
+ "DKK": 5.6281,
45
+ "DOP": 38.875,
46
+ "DZD": 76.019997,
47
+ "EGP": 6.04075,
48
+ "ETB": 17.35375,
49
+ "EUR": 0.756401,
50
+ "FJD": 1.7699,
51
+ "FKP": 0.6358,
52
+ "GBP": 0.635627,
53
+ "GEL": 1.66895,
54
+ "GHS": 1.6835,
55
+ "GIP": 0.63575,
56
+ "GMD": 30.549999,
57
+ "GNF": 7237.5,
58
+ "GTQ": 7.8145,
59
+ "GYD": 203.949997,
60
+ "HKD": 7.7561,
61
+ "HNL": 19.055,
62
+ "HRK": 5.72725,
63
+ "HTG": 40.349998,
64
+ "HUF": 222.050003,
65
+ "IDR": 8973,
66
+ "IEP": 0.595735,
67
+ "ILS": 3.7492,
68
+ "INR": 49.314999,
69
+ "IQD": 1165.310059,
70
+ "IRR": 11308,
71
+ "ISK": 123.040001,
72
+ "JMD": 86.25,
73
+ "JOD": 0.7083,
74
+ "JPY": 76.68,
75
+ "KES": 85.099998,
76
+ "KGS": 46.648899,
77
+ "KHR": 4189.799805,
78
+ "KMF": 372.139038,
79
+ "KPW": 900,
80
+ "KRW": 1121.02002,
81
+ "KWD": 0.27814,
82
+ "KZT": 148.425003,
83
+ "LAK": 7998.299805,
84
+ "LBP": 1505,
85
+ "LKR": 113.834999,
86
+ "LRD": 73.339996,
87
+ "LSL": 7.76,
88
+ "LTL": 2.614,
89
+ "LVL": 0.529,
90
+ "LYD": 1.2485,
91
+ "MAD": 8.4415,
92
+ "MDL": 11.785,
93
+ "MGA": 2190,
94
+ "MKD": 46.939999,
95
+ "MMK": 6.51,
96
+ "MNT": 1362.5,
97
+ "MOP": 7.98705,
98
+ "MRO": 290.130005,
99
+ "MUR": 29.5,
100
+ "MVR": 15.41,
101
+ "MWK": 165.5,
102
+ "MXN": 12.9155,
103
+ "MYR": 3.0425,
104
+ "MZN": 27.087,
105
+ "NAD": 7.756,
106
+ "NGN": 160.949997,
107
+ "NIO": 23.065001,
108
+ "NOK": 5.80155,
109
+ "NPR": 80,
110
+ "NZD": 1.212342,
111
+ "OMR": 0.3851,
112
+ "PAB": 1,
113
+ "PEN": 2.69,
114
+ "PGK": 2.0619,
115
+ "PHP": 42.849998,
116
+ "PKR": 90.150002,
117
+ "PLN": 3.1972,
118
+ "PYG": 4710,
119
+ "QAR": 3.6415,
120
+ "RON": 3.2843,
121
+ "RSD": 80.640503,
122
+ "RUB": 30.131001,
123
+ "RWF": 604.380066,
124
+ "SAR": 3.7503,
125
+ "SBD": 7.11124,
126
+ "SCR": 13.99045,
127
+ "SDG": 2.6778,
128
+ "SEK": 6.7462,
129
+ "SGD": 1.25075,
130
+ "SHP": 0.63575,
131
+ "SLL": 4407.5,
132
+ "SOS": 1627,
133
+ "SRD": 3.275,
134
+ "STD": 18602.5,
135
+ "SVC": 8.7475,
136
+ "SYP": 57.450001,
137
+ "SZL": 7.762,
138
+ "THB": 31.084999,
139
+ "TJS": 4.7581,
140
+ "TMT": 2.8425,
141
+ "TND": 1.4945,
142
+ "TOP": 1.708066,
143
+ "TRY": 1.7767,
144
+ "TTD": 6.4053,
145
+ "TWD": 29.74,
146
+ "TZS": 1595.5,
147
+ "UAH": 8.038,
148
+ "UGX": 2343.5,
149
+ "USD": 1,
150
+ "UYU": 19.52,
151
+ "UZS": 1808.170044,
152
+ "VEF": 4.2927,
153
+ "VND": 20943,
154
+ "VUV": 90.5,
155
+ "WST": 2.309777,
156
+ "XAF": 496.185364,
157
+ "XCD": 2.7,
158
+ "XDR": 0.646403,
159
+ "XOF": 497.450012,
160
+ "XPF": 90.980003,
161
+ "YER": 213.255005,
162
+ "ZAR": 7.7622,
163
+ "ZMK": 5150,
164
+ "ZWD": 0,
165
+ "ZWL": 322.355011
166
+ }
167
+ }