money-json-rates 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/money/bank/json_rates.rb +162 -0
  3. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 65ad4a66cac1f91eace625bf55473fbae8356b2f
4
+ data.tar.gz: a04375b33cfda6acff2cf067f0880c42cdaa0b48
5
+ SHA512:
6
+ metadata.gz: 98957c324ccc5d62d1a34e578c47af66c371a156dea0e86afa099dcdde3e145c1ac1a6b9d55465620e44712852d00e69755f94589e62bb0f4061b0154a392ecb
7
+ data.tar.gz: 94af90123b47dbee64d935833c2fc34200d0468d3d20cacd4b16864dd0df91492d313debf5925483a785d1d560cadc32a0b417f5766b100dbd52a4fc8c586990
@@ -0,0 +1,162 @@
1
+ require 'money'
2
+ require 'open-uri'
3
+
4
+ class Money
5
+ module Bank
6
+
7
+ class JsonRatesRequestError < StandardError ; end
8
+
9
+ class NoApiKey < StandardError
10
+ def message
11
+ "Blank api_key! You should get your api_key on jsonrates.com and specify it like JsonRates.api_key = YOUR_API_KEY"
12
+ end
13
+ end
14
+
15
+ class JsonRates < Money::Bank::VariableExchange
16
+
17
+ SERVICE_HOST = "jsonrates.com"
18
+ SERVICE_PATH = "/get"
19
+
20
+ # @return [Hash] Stores the currently known rates.
21
+ attr_reader :rates
22
+
23
+ attr_accessor :cache, :api_key
24
+
25
+ class << self
26
+ # @return [Integer] Returns the Time To Live (TTL) in seconds.
27
+ attr_reader :ttl_in_seconds
28
+
29
+ # @return [Time] Returns the time when the rates expire.
30
+ attr_reader :rates_expiration
31
+
32
+ ##
33
+ # Set the Time To Live (TTL) in seconds.
34
+ #
35
+ # @param [Integer] the seconds between an expiration and another.
36
+ def ttl_in_seconds=(value)
37
+ @ttl_in_seconds = value
38
+ refresh_rates_expiration! if ttl_in_seconds
39
+ end
40
+
41
+ ##
42
+ # Set the rates expiration TTL seconds from the current time.
43
+ #
44
+ # @return [Time] The next expiration.
45
+ def refresh_rates_expiration!
46
+ @rates_expiration = Time.now + ttl_in_seconds
47
+ end
48
+ end
49
+
50
+ ##
51
+ # Clears all rates stored in @rates
52
+ #
53
+ # @return [Hash] The empty @rates Hash.
54
+ #
55
+ # @example
56
+ # @bank = JsonRates.new #=> <Money::Bank::JsonRates...>
57
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
58
+ # @bank.flush_rates #=> {}
59
+ def flush_rates
60
+ @mutex.synchronize{
61
+ @rates = {}
62
+ }
63
+ end
64
+
65
+ ##
66
+ # Clears the specified rate stored in @rates.
67
+ #
68
+ # @param [String, Symbol, Currency] from Currency to convert from (used
69
+ # for key into @rates).
70
+ # @param [String, Symbol, Currency] to Currency to convert to (used for
71
+ # key into @rates).
72
+ #
73
+ # @return [Float] The flushed rate.
74
+ #
75
+ # @example
76
+ # @bank = JsonRates.new #=> <Money::Bank::JsonRates...>
77
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
78
+ # @bank.flush_rate(:USD, :EUR) #=> 0.776337241
79
+ def flush_rate(from, to)
80
+ key = rate_key_for(from, to)
81
+ @mutex.synchronize{
82
+ @rates.delete(key)
83
+ }
84
+ end
85
+
86
+ ##
87
+ # Returns the requested rate.
88
+ #
89
+ # It also flushes all the rates when and if they are expired.
90
+ #
91
+ # @param [String, Symbol, Currency] from Currency to convert from
92
+ # @param [String, Symbol, Currency] to Currency to convert to
93
+ #
94
+ # @return [Float] The requested rate.
95
+ #
96
+ # @example
97
+ # @bank = JsonRates.new #=> <Money::Bank::JsonRates...>
98
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
99
+ def get_rate(from, to)
100
+ expire_rates
101
+
102
+ @mutex.synchronize{
103
+ @rates[rate_key_for(from, to)] ||= fetch_rate(from, to)
104
+ }
105
+ end
106
+
107
+ ##
108
+ # Flushes all the rates if they are expired.
109
+ #
110
+ # @return [Boolean]
111
+ def expire_rates
112
+ if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now
113
+ flush_rates
114
+ self.class.refresh_rates_expiration!
115
+ true
116
+ else
117
+ false
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ ##
124
+ # Queries for the requested rate and returns it.
125
+ #
126
+ # @param [String, Symbol, Currency] from Currency to convert from
127
+ # @param [String, Symbol, Currency] to Currency to convert to
128
+ #
129
+ # @return [BigDecimal] The requested rate.
130
+ def fetch_rate(from, to)
131
+ from, to = Currency.wrap(from), Currency.wrap(to)
132
+ data = build_uri(from, to).read
133
+ extract_rate(data)
134
+ end
135
+
136
+ ##
137
+ # Build a URI for the given arguments.
138
+ #
139
+ # @param [Currency] from The currency to convert from.
140
+ # @param [Currency] to The currency to convert to.
141
+ #
142
+ # @return [URI::HTTP]
143
+ def build_uri(from, to)
144
+ raise NoApiKey if api_key.blank?
145
+ uri = URI::HTTP.build(
146
+ :host => SERVICE_HOST,
147
+ :path => SERVICE_PATH,
148
+ :query => "from=#{from.iso_code}&to=#{to.iso_code}&apiKey=#{api_key}"
149
+ )
150
+ end
151
+
152
+ ##
153
+ # Takes the response from jsonrates.com and extract the rate.
154
+ # @return [BigDecimal]
155
+ def extract_rate(data)
156
+ request_hash = JSON.parse(data)
157
+ raise JsonRatesRequestError, request_hash['error'] if request_hash['error'].present?
158
+ BigDecimal.new(request_hash['rate'])
159
+ end
160
+ end
161
+ end
162
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money-json-rates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Skuratovsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: money
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ description: MoneyJsonRates extends Money::Bank::Base and gives access to the current
42
+ exchange rates using http://jsonrates.com/ api.
43
+ email:
44
+ - skuratowsky@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/money/bank/json_rates.rb
50
+ homepage: http://github.com/askuratovsky/money-json-rates
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.4.3
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Access the jsonrates.com for gem money
74
+ test_files: []