exchange 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +1 -0
  2. data/.rspec +1 -1
  3. data/Gemfile.lock +3 -3
  4. data/README.rdoc +115 -47
  5. data/benchmark/benchmark.rb +49 -0
  6. data/changelog.rdoc +8 -1
  7. data/lib/exchange.rb +4 -4
  8. data/lib/exchange/base.rb +1 -1
  9. data/lib/exchange/cache.rb +2 -0
  10. data/lib/exchange/cache/base.rb +20 -6
  11. data/lib/exchange/cache/configuration.rb +24 -0
  12. data/lib/exchange/cache/file.rb +24 -9
  13. data/lib/exchange/cache/memcached.rb +3 -3
  14. data/lib/exchange/cache/memory.rb +89 -0
  15. data/lib/exchange/cache/rails.rb +1 -1
  16. data/lib/exchange/cache/redis.rb +4 -4
  17. data/lib/exchange/configurable.rb +53 -0
  18. data/lib/exchange/configuration.rb +32 -26
  19. data/lib/exchange/core_extensions.rb +3 -0
  20. data/lib/exchange/core_extensions/cachify.rb +25 -0
  21. data/lib/exchange/core_extensions/float/error_safe.rb +25 -0
  22. data/lib/{core_extensions → exchange/core_extensions/numeric}/conversability.rb +12 -12
  23. data/lib/exchange/external_api.rb +2 -1
  24. data/lib/exchange/external_api/base.rb +34 -9
  25. data/lib/exchange/external_api/call.rb +6 -8
  26. data/lib/exchange/external_api/configuration.rb +25 -0
  27. data/lib/exchange/external_api/ecb.rb +16 -25
  28. data/lib/exchange/external_api/json.rb +11 -1
  29. data/lib/exchange/external_api/open_exchange_rates.rb +65 -0
  30. data/lib/exchange/external_api/xavier_media.rb +7 -7
  31. data/lib/exchange/iso_4217.rb +32 -5
  32. data/lib/exchange/{currency.rb → money.rb} +112 -110
  33. data/lib/exchange/typecasting.rb +94 -0
  34. data/spec/exchange/cache/base_spec.rb +2 -2
  35. data/spec/exchange/cache/configuration_spec.rb +56 -0
  36. data/spec/exchange/cache/file_spec.rb +10 -8
  37. data/spec/exchange/cache/memcached_spec.rb +9 -18
  38. data/spec/exchange/cache/memory_spec.rb +122 -0
  39. data/spec/exchange/cache/no_cache_spec.rb +5 -15
  40. data/spec/exchange/cache/rails_spec.rb +2 -6
  41. data/spec/exchange/cache/redis_spec.rb +8 -18
  42. data/spec/exchange/configuration_spec.rb +31 -7
  43. data/spec/exchange/core_extensions/array/cachify_spec.rb +12 -0
  44. data/spec/exchange/core_extensions/float/error_safe_spec.rb +49 -0
  45. data/spec/exchange/core_extensions/hash/cachify_spec.rb +12 -0
  46. data/spec/exchange/core_extensions/numeric/cachify_spec.rb +26 -0
  47. data/spec/{core_extensions → exchange/core_extensions/numeric}/conversability_spec.rb +22 -22
  48. data/spec/exchange/core_extensions/string/cachify_spec.rb +59 -0
  49. data/spec/exchange/core_extensions/symbol/cachify_spec.rb +12 -0
  50. data/spec/exchange/external_api/base_spec.rb +10 -7
  51. data/spec/exchange/external_api/call_spec.rb +3 -0
  52. data/spec/exchange/external_api/configuration_spec.rb +52 -0
  53. data/spec/exchange/external_api/ecb_spec.rb +8 -5
  54. data/spec/exchange/external_api/open_exchange_rates_spec.rb +70 -0
  55. data/spec/exchange/external_api/xavier_media_spec.rb +8 -5
  56. data/spec/exchange/iso_4217_spec.rb +208 -20
  57. data/spec/exchange/{currency_spec.rb → money_spec.rb} +102 -82
  58. data/spec/exchange/typecasting_spec.rb +86 -0
  59. metadata +117 -71
  60. data/exchange-0.7.5.gem +0 -0
  61. data/exchange-0.7.6.gem +0 -0
  62. data/lib/exchange/external_api/currency_bot.rb +0 -61
  63. data/spec/exchange/external_api/currency_bot_spec.rb +0 -63
metadata CHANGED
@@ -1,73 +1,84 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: exchange
3
- version: !ruby/object:Gem::Version
4
- version: 0.8.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 59
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Beat Richartz
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-10-09 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-10-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: json
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 1.0.0
22
- type: :runtime
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
29
33
  version: 1.0.0
30
- - !ruby/object:Gem::Dependency
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
31
37
  name: yard
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 0.7.4
38
- type: :development
39
38
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
39
+ requirement: &id002 !ruby/object:Gem::Requirement
41
40
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 11
45
+ segments:
46
+ - 0
47
+ - 7
48
+ - 4
45
49
  version: 0.7.4
46
- - !ruby/object:Gem::Dependency
47
- name: bundler
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: 1.0.0
54
50
  type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: bundler
55
54
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
55
+ requirement: &id003 !ruby/object:Gem::Requirement
57
56
  none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
61
65
  version: 1.0.0
62
- description: ! "The Exchange Gem gives you easy access to currency functions directly
63
- on your Numbers. Imagine a conversion as easy as \n 1.eur.to_usd\n or even better
64
- \n 1.eur.to_usd(:at => Time.now - 84600)\n which gets you an exchange at the
65
- rates of yesterday."
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: |-
69
+ The Exchange Gem gives you easy access to currency functions directly on your Numbers. Imagine a conversion as easy as
70
+ 1.eur.to_usd
71
+ or even better
72
+ 1.eur.to_usd(:at => Time.now - 84600)
73
+ which gets you an exchange at the rates of yesterday.
66
74
  email: exchange_gem@gmail.com
67
75
  executables: []
76
+
68
77
  extensions: []
78
+
69
79
  extra_rdoc_files: []
70
- files:
80
+
81
+ files:
71
82
  - .document
72
83
  - .gitignore
73
84
  - .rspec
@@ -77,51 +88,68 @@ files:
77
88
  - LICENSE.txt
78
89
  - README.rdoc
79
90
  - Rakefile
91
+ - benchmark/benchmark.rb
80
92
  - changelog.rdoc
81
- - exchange-0.7.5.gem
82
- - exchange-0.7.6.gem
83
93
  - exchange.gemspec
84
94
  - iso4217.yml
85
- - lib/core_extensions/conversability.rb
86
95
  - lib/exchange.rb
87
96
  - lib/exchange/base.rb
88
97
  - lib/exchange/cache.rb
89
98
  - lib/exchange/cache/base.rb
99
+ - lib/exchange/cache/configuration.rb
90
100
  - lib/exchange/cache/file.rb
91
101
  - lib/exchange/cache/memcached.rb
102
+ - lib/exchange/cache/memory.rb
92
103
  - lib/exchange/cache/no_cache.rb
93
104
  - lib/exchange/cache/rails.rb
94
105
  - lib/exchange/cache/redis.rb
106
+ - lib/exchange/configurable.rb
95
107
  - lib/exchange/configuration.rb
96
- - lib/exchange/currency.rb
108
+ - lib/exchange/core_extensions.rb
109
+ - lib/exchange/core_extensions/cachify.rb
110
+ - lib/exchange/core_extensions/float/error_safe.rb
111
+ - lib/exchange/core_extensions/numeric/conversability.rb
97
112
  - lib/exchange/external_api.rb
98
113
  - lib/exchange/external_api/base.rb
99
114
  - lib/exchange/external_api/call.rb
100
- - lib/exchange/external_api/currency_bot.rb
115
+ - lib/exchange/external_api/configuration.rb
101
116
  - lib/exchange/external_api/ecb.rb
102
117
  - lib/exchange/external_api/json.rb
118
+ - lib/exchange/external_api/open_exchange_rates.rb
103
119
  - lib/exchange/external_api/xavier_media.rb
104
120
  - lib/exchange/external_api/xml.rb
105
121
  - lib/exchange/gem_loader.rb
106
122
  - lib/exchange/helper.rb
107
123
  - lib/exchange/iso_4217.rb
108
- - spec/core_extensions/conversability_spec.rb
124
+ - lib/exchange/money.rb
125
+ - lib/exchange/typecasting.rb
109
126
  - spec/exchange/cache/base_spec.rb
127
+ - spec/exchange/cache/configuration_spec.rb
110
128
  - spec/exchange/cache/file_spec.rb
111
129
  - spec/exchange/cache/memcached_spec.rb
130
+ - spec/exchange/cache/memory_spec.rb
112
131
  - spec/exchange/cache/no_cache_spec.rb
113
132
  - spec/exchange/cache/rails_spec.rb
114
133
  - spec/exchange/cache/redis_spec.rb
115
134
  - spec/exchange/configuration_spec.rb
116
- - spec/exchange/currency_spec.rb
135
+ - spec/exchange/core_extensions/array/cachify_spec.rb
136
+ - spec/exchange/core_extensions/float/error_safe_spec.rb
137
+ - spec/exchange/core_extensions/hash/cachify_spec.rb
138
+ - spec/exchange/core_extensions/numeric/cachify_spec.rb
139
+ - spec/exchange/core_extensions/numeric/conversability_spec.rb
140
+ - spec/exchange/core_extensions/string/cachify_spec.rb
141
+ - spec/exchange/core_extensions/symbol/cachify_spec.rb
117
142
  - spec/exchange/external_api/base_spec.rb
118
143
  - spec/exchange/external_api/call_spec.rb
119
- - spec/exchange/external_api/currency_bot_spec.rb
144
+ - spec/exchange/external_api/configuration_spec.rb
120
145
  - spec/exchange/external_api/ecb_spec.rb
146
+ - spec/exchange/external_api/open_exchange_rates_spec.rb
121
147
  - spec/exchange/external_api/xavier_media_spec.rb
122
148
  - spec/exchange/gem_loader_spec.rb
123
149
  - spec/exchange/helper_spec.rb
124
150
  - spec/exchange/iso_4217_spec.rb
151
+ - spec/exchange/money_spec.rb
152
+ - spec/exchange/typecasting_spec.rb
125
153
  - spec/spec_helper.rb
126
154
  - spec/support/api_responses/example_ecb_xml_90d.xml
127
155
  - spec/support/api_responses/example_ecb_xml_daily.xml
@@ -130,48 +158,66 @@ files:
130
158
  - spec/support/api_responses/example_json_api.json
131
159
  - spec/support/api_responses/example_xml_api.xml
132
160
  homepage: http://github.com/beatrichartz/exchange
133
- licenses:
161
+ licenses:
134
162
  - MIT
135
163
  post_install_message:
136
164
  rdoc_options: []
137
- require_paths:
165
+
166
+ require_paths:
138
167
  - lib
139
- required_ruby_version: !ruby/object:Gem::Requirement
168
+ required_ruby_version: !ruby/object:Gem::Requirement
140
169
  none: false
141
- requirements:
142
- - - ! '>='
143
- - !ruby/object:Gem::Version
144
- version: '0'
145
- required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ hash: 3
174
+ segments:
175
+ - 0
176
+ version: "0"
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
178
  none: false
147
- requirements:
148
- - - ! '>='
149
- - !ruby/object:Gem::Version
150
- version: '0'
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ hash: 3
183
+ segments:
184
+ - 0
185
+ version: "0"
151
186
  requirements: []
187
+
152
188
  rubyforge_project:
153
189
  rubygems_version: 1.8.24
154
190
  signing_key:
155
191
  specification_version: 3
156
192
  summary: Simple Exchange Rate operations for your ruby app
157
- test_files:
158
- - spec/core_extensions/conversability_spec.rb
193
+ test_files:
159
194
  - spec/exchange/cache/base_spec.rb
195
+ - spec/exchange/cache/configuration_spec.rb
160
196
  - spec/exchange/cache/file_spec.rb
161
197
  - spec/exchange/cache/memcached_spec.rb
198
+ - spec/exchange/cache/memory_spec.rb
162
199
  - spec/exchange/cache/no_cache_spec.rb
163
200
  - spec/exchange/cache/rails_spec.rb
164
201
  - spec/exchange/cache/redis_spec.rb
165
202
  - spec/exchange/configuration_spec.rb
166
- - spec/exchange/currency_spec.rb
203
+ - spec/exchange/core_extensions/array/cachify_spec.rb
204
+ - spec/exchange/core_extensions/float/error_safe_spec.rb
205
+ - spec/exchange/core_extensions/hash/cachify_spec.rb
206
+ - spec/exchange/core_extensions/numeric/cachify_spec.rb
207
+ - spec/exchange/core_extensions/numeric/conversability_spec.rb
208
+ - spec/exchange/core_extensions/string/cachify_spec.rb
209
+ - spec/exchange/core_extensions/symbol/cachify_spec.rb
167
210
  - spec/exchange/external_api/base_spec.rb
168
211
  - spec/exchange/external_api/call_spec.rb
169
- - spec/exchange/external_api/currency_bot_spec.rb
212
+ - spec/exchange/external_api/configuration_spec.rb
170
213
  - spec/exchange/external_api/ecb_spec.rb
214
+ - spec/exchange/external_api/open_exchange_rates_spec.rb
171
215
  - spec/exchange/external_api/xavier_media_spec.rb
172
216
  - spec/exchange/gem_loader_spec.rb
173
217
  - spec/exchange/helper_spec.rb
174
218
  - spec/exchange/iso_4217_spec.rb
219
+ - spec/exchange/money_spec.rb
220
+ - spec/exchange/typecasting_spec.rb
175
221
  - spec/spec_helper.rb
176
222
  - spec/support/api_responses/example_ecb_xml_90d.xml
177
223
  - spec/support/api_responses/example_ecb_xml_daily.xml
data/exchange-0.7.5.gem DELETED
Binary file
data/exchange-0.7.6.gem DELETED
Binary file
@@ -1,61 +0,0 @@
1
- module Exchange
2
- module ExternalAPI
3
- # The Currency Bot API class, handling communication with the Open Source Currency bot API
4
- # You can find further information on the currency bot API here: http://openexchangerates.org
5
- # @author Beat Richartz
6
- # @version 0.1
7
- # @since 0.1
8
- #
9
- class CurrencyBot < Base
10
-
11
- # The base of the Currency Bot exchange API
12
- #
13
- API_URL = 'http://openexchangerates.org/api'
14
-
15
- # The currencies the Currency Bot API can convert
16
- #
17
- CURRENCIES = %W(xcd usd sar rub nio lak nok omr amd cdf kpw cny kes zwd khr pln mvr gtq clp inr bzd myr hkd sek cop dkk byr lyd ron dzd bif ars gip bob xof std ngn pgk aed mwk cup gmd zwl tzs cve btn xaf ugx syp mad mnt lsl top shp rsd htg mga mzn lvl fkp bwp hnl eur egp chf ils pyg lbp ang kzt wst gyd thb npr kmf irr uyu srd jpy brl szl mop bmd xpf etb jod idr mdl mro yer bam awg nzd pen vef try sll aoa tnd tjs sgd scr lkr mxn ltl huf djf bsd gnf isk vuv sdg gel fjd dop xdr mur php mmk krw lrd bbd zmk zar vnd uah tmt iqd bgn gbp kgs ttd hrk rwf clf bhd uzs twd crc aud mkd pkr afn nad bdt azn czk sos iep pab qar svc sbd all jmd bnd cad kwd ghs)
18
-
19
- # Updates the rates by getting the information from Currency Bot for today or a defined historical date
20
- # The call gets cached for a maximum of 24 hours.
21
- # @param [Hash] opts Options to define for the API Call
22
- # @option opts [Time, String] :at a historical date to get the exchange rates for
23
- # @example Update the currency bot API to use the file of March 2, 2010
24
- # Exchange::ExternalAPI::CurrencyBot.new.update(:at => Time.gm(3,2,2010))
25
- #
26
- def update(opts={})
27
- time = helper.assure_time(opts[:at])
28
-
29
- Call.new(api_url(time), :at => time) do |result|
30
- @base = result['base']
31
- @rates = extract_rates(result)
32
- @timestamp = result['timestamp'].to_i
33
- end
34
- end
35
-
36
- private
37
-
38
- # Helper method to extract rates from the api call result
39
- # @param [JSON] parsed The parsed result
40
- # @return [Hash] A hash with rates
41
- # @since 0.7
42
- # @version 0.7
43
- #
44
- def extract_rates parsed
45
- to_hash! parsed['rates'].keys.zip(parsed['rates'].values.map{|v| BigDecimal.new(v.to_s) }).flatten
46
- end
47
-
48
- # A helper function to build an api url for either a specific time or the latest available rates
49
- # @param [Time] time The time to build the api url for
50
- # @return [String] an api url for the time specified
51
- # @since 0.1
52
- # @version 0.2.6
53
- #
54
- def api_url(time=nil)
55
- today = Time.now
56
- [API_URL, time && (time.year != today.year || time.yday != today.yday) ? "historical/#{time.strftime("%Y-%m-%d")}.json" : "latest.json"].join('/') + "?app_id=#{Exchange.configuration.api.app_id}"
57
- end
58
-
59
- end
60
- end
61
- end
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Exchange::ExternalAPI::CurrencyBot" do
4
- before(:all) do
5
- Exchange.configuration = Exchange::Configuration.new{|c|
6
- c.cache = {
7
- :subclass => :no_cache
8
- }
9
- }
10
- end
11
- describe "updating rates" do
12
- subject { Exchange::ExternalAPI::CurrencyBot.new }
13
- before(:each) do
14
- mock_api("http://openexchangerates.org/api/latest.json?app_id=", fixture('api_responses/example_json_api.json'))
15
- end
16
- it "should call the api and yield a block with the result" do
17
- subject.update
18
- subject.base.should == 'USD'
19
- end
20
- it "should set a unix timestamp from the api file" do
21
- subject.update
22
- subject.timestamp.should == 1327748496
23
- end
24
- end
25
- describe "conversion" do
26
- subject { Exchange::ExternalAPI::CurrencyBot.new }
27
- before(:each) do
28
- mock_api("http://openexchangerates.org/api/latest.json?app_id=", fixture('api_responses/example_json_api.json'))
29
- end
30
- it "should convert right" do
31
- subject.convert(78, 'eur', 'usd').round(2).should == BigDecimal.new("103.12")
32
- end
33
- it "should convert negative numbers right" do
34
- subject.convert(-70, 'chf', 'usd').round(2).should == BigDecimal.new("-76.71")
35
- end
36
- it "should convert when given symbols" do
37
- subject.convert(70, :sek, :usd).round(2).should == 10.38
38
- end
39
- end
40
- describe "historic conversion" do
41
- subject { Exchange::ExternalAPI::CurrencyBot.new }
42
- it "should convert and be able to use history" do
43
- mock_api("http://openexchangerates.org/api/historical/2011-09-09.json?app_id=", fixture('api_responses/example_json_api.json'))
44
- subject.convert(72, 'eur', 'usd', :at => Time.gm(2011,9,9)).round(2).should == BigDecimal.new("95.19")
45
- end
46
- it "should convert negative numbers right" do
47
- mock_api("http://openexchangerates.org/api/historical/2011-09-09.json?app_id=", fixture('api_responses/example_json_api.json'))
48
- subject.convert(-70, 'chf', 'usd', :at => Time.gm(2011,9,9)).round(2).should == BigDecimal.new("-76.71")
49
- end
50
- it "should convert when given symbols" do
51
- mock_api("http://openexchangerates.org/api/historical/2011-09-09.json?app_id=", fixture('api_responses/example_json_api.json'))
52
- subject.convert(70, :sek, :usd, :at => Time.gm(2011,9,9)).round(2).should == 10.38
53
- end
54
- it "should convert right when the year is the same, but the yearday is not" do
55
- mock_api("http://openexchangerates.org/api/historical/#{Time.now.year}-0#{Time.now.month > 9 ? Time.now.month - 1 : Time.now.month + 1}-01.json?app_id=", fixture('api_responses/example_json_api.json'))
56
- subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year,Time.now.month > 9 ? Time.now.month - 1 : Time.now.month + 1,1)).round(2).should == 10.38
57
- end
58
- it "should convert right when the yearday is the same, but the year is not" do
59
- mock_api("http://openexchangerates.org/api/historical/#{Time.now.year-1}-03-01.json?app_id=", fixture('api_responses/example_json_api.json'))
60
- subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year - 1,3,1)).round(2).should == 10.38
61
- end
62
- end
63
- end