yahoo_finance_currency 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ Yahoo Currency 0.1.1
2
+ =====================
3
+
4
+ Features
5
+ --------
6
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Shane Emmons
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ Yahoo Currency
2
+ ===============
3
+
4
+ This gem extends Money::Bank::VariableExchange with Money::Bank::YahooCurrency
5
+ and gives you access to the current Yahoo! Finance exchange rates.
6
+
7
+ Usage
8
+ -----
9
+
10
+ require 'money'
11
+ require 'money/bank/yahoo_currency'
12
+
13
+ # set default bank to instance of YahooCurrency
14
+ Money.default_bank = Money::Bank::YahooCurrency.new
15
+
16
+ # create a new money object, and use the standard #exchange_to method
17
+ n = 1.to_money(:USD)
18
+ n.exchange_to(:EUR)
19
+
20
+ An `UnknownRate` will be thrown if `#exchange_to` is called with a `Currency`
21
+ that `Money` knows, but Yahoo! Finance does not.
22
+
23
+ An `UnknownCurrency` will be thrown if `#exchange_to` is called with a
24
+ `Currency` that `Money` does not know.
25
+
26
+ Copyright
27
+ ---------
28
+
29
+ See {file:LICENSE} for details.
@@ -0,0 +1,93 @@
1
+ require 'money'
2
+ require 'open-uri'
3
+
4
+ class Money
5
+ module Bank
6
+ class YahooFinanceCurrency < Money::Bank::VariableExchange
7
+ # @return [Hash] Stores the currently known rates.
8
+ attr_reader :rates
9
+
10
+ ##
11
+ # Clears all rates stored in @rates
12
+ #
13
+ # @return [Hash] The empty @rates Hash.
14
+ #
15
+ # @example
16
+ # @bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
17
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
18
+ # @bank.flush_rates #=> {}
19
+ def flush_rates
20
+ @mutex.synchronize{
21
+ @rates = {}
22
+ }
23
+ end
24
+
25
+ ##
26
+ # Clears the specified rate stored in @rates.
27
+ #
28
+ # @param [String, Symbol, Currency] from Currency to convert from (used
29
+ # for key into @rates).
30
+ # @param [String, Symbol, Currency] to Currency to convert to (used for
31
+ # key into @rates).
32
+ #
33
+ # @return [Float] The flushed rate.
34
+ #
35
+ # @example
36
+ # @bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
37
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
38
+ # @bank.flush_rate(:USD, :EUR) #=> 0.776337241
39
+ def flush_rate(from, to)
40
+ key = rate_key_for(from, to)
41
+ @mutex.synchronize{
42
+ @rates.delete(key)
43
+ }
44
+ end
45
+
46
+ ##
47
+ # Returns the requested rate from @rates if it exists, otherwise calls
48
+ # +#get_yahoo_rate+.
49
+ #
50
+ # @param [String, Symbol, Currency] from Currency to convert from
51
+ # @param [String, Symbol, Currency] to Currency to convert to
52
+ #
53
+ # @return [Float] The requested rate.
54
+ #
55
+ # @example
56
+ # @bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
57
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
58
+ def get_rate(from, to)
59
+ @mutex.synchronize{
60
+ @rates[rate_key_for(from, to)] ||= get_yahoo_rate(from, to)
61
+ }
62
+ end
63
+
64
+ ##
65
+ # Returns the requested rate after querying Yahoo! Finance.
66
+ #
67
+ #
68
+ # @param [String, Symbol, Currency] from Currency to convert from
69
+ # @param [String, Symbol, Currency] to Currency to convert to
70
+ #
71
+ # @return [Float] The requested rate.
72
+ #
73
+ # @example
74
+ # @bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
75
+ # @bank.get_yahoo_rate(:USD, :EUR) #=> 0.776337241
76
+ def get_yahoo_rate(from, to)
77
+ from, to = Currency.wrap(from), Currency.wrap(to)
78
+
79
+ if from.iso_code == to.iso_code
80
+ return 1.0
81
+ else
82
+ url = "http://download.finance.yahoo.com/d/quotes.csv?s=#{from.iso_code}#{to.iso_code}=X&f=l1"
83
+ data = URI.parse(url).read.to_f
84
+ if data.zero? or to.iso_code == "ALL"
85
+ raise UnknownRate
86
+ else
87
+ return data
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoo_finance_currency
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Vicent Gozalbes
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-08 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ version: 2.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: money
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 3
45
+ - 1
46
+ - 5
47
+ version: 3.1.5
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: YahooFinanceCurrency extends Money::Bank::Base and gives you access to the current Yahoo! Finance exchange rates.
51
+ email:
52
+ - vicent@peertransfer.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.md
60
+ - CHANGELOG.md
61
+ files:
62
+ - lib/money/bank/yahoo_finance_currency.rb
63
+ - LICENSE
64
+ - README.md
65
+ - CHANGELOG.md
66
+ has_rdoc: true
67
+ homepage: https://github.com/peertransfer/yahoo_currency
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 1
90
+ - 3
91
+ - 7
92
+ version: 1.3.7
93
+ requirements: []
94
+
95
+ rubyforge_project: ""
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Access the Yahoo! Finance exchange rate data.
100
+ test_files: []
101
+