money-distributed 0.0.2.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/lib/money-distributed.rb +3 -0
- data/lib/money/distributed/fetcher/base.rb +38 -0
- data/lib/money/distributed/fetcher/file.rb +28 -0
- data/lib/money/distributed/redis.rb +36 -0
- data/lib/money/distributed/storage.rb +87 -0
- data/lib/money/distributed/version.rb +5 -0
- data/money-distributed.gemspec +33 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d55309ec08b53b939eeec77dfbda51efa101790
|
4
|
+
data.tar.gz: b74ea6ba1dfabd013e3427cf60b0337e02e1cbb3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1d49fdc344121a1ff8a0acc036cddc0a2aba830d81dc54fc3369da09532273aa05506c6da701c15c721269cd8ffd0b566c0382f8f799f1b43e69d1e57409ce80
|
7
|
+
data.tar.gz: f35cf3cd105862c89f9cae8fe3d65247a1c05231dde7990b5160f19135a0a89ef59a795be5bbe33e1534fccc7df48f0da6e4c48124f2e70e93130cabcd3d491e
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sergey Alexandrovich
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'money'
|
2
|
+
|
3
|
+
class Money
|
4
|
+
module Distributed
|
5
|
+
module Fetcher
|
6
|
+
# Base class for reates fetchers
|
7
|
+
module Base
|
8
|
+
def initialize(bank = nil)
|
9
|
+
@bank = bank || Money.default_bank
|
10
|
+
end
|
11
|
+
|
12
|
+
def fetch
|
13
|
+
rates = exchange_rates
|
14
|
+
currencies = rates.keys
|
15
|
+
|
16
|
+
currencies.each { |cur| add_rate(cur, cur, 1) }
|
17
|
+
|
18
|
+
currencies.combination(2).each do |curr1, curr2|
|
19
|
+
rate = rates[curr2] / rates[curr1]
|
20
|
+
add_rate(curr1, curr2, rate)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def add_rate(from_iso, to_iso, rate)
|
27
|
+
@bank.add_rate(from_iso, to_iso, rate.round(4))
|
28
|
+
return if from_iso == to_iso
|
29
|
+
@bank.add_rate(to_iso, from_iso, (1 / rate).round(4))
|
30
|
+
end
|
31
|
+
|
32
|
+
def exchange_rates
|
33
|
+
raise NotImplementedError
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'money/distributed/fetcher/base'
|
4
|
+
|
5
|
+
class Money
|
6
|
+
module Distributed
|
7
|
+
module Fetcher
|
8
|
+
# Fetcher that loads rates from a file
|
9
|
+
class File
|
10
|
+
include Base
|
11
|
+
|
12
|
+
def initialize(file_path, bank = nil)
|
13
|
+
super(bank)
|
14
|
+
@file_path = file_path
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def exchange_rates
|
20
|
+
open(@file_path).read.split("\n").each_with_object({}) do |line, h|
|
21
|
+
code_rate = line.split(' ')
|
22
|
+
h[code_rate[0]] = BigDecimal.new(code_rate[1])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'connection_pool'
|
3
|
+
|
4
|
+
class Money
|
5
|
+
module Distributed
|
6
|
+
# Wrapper over different parameters that can be provided for redis
|
7
|
+
class Redis
|
8
|
+
def initialize(redis)
|
9
|
+
@redis_proc = build_redis_proc(redis)
|
10
|
+
end
|
11
|
+
|
12
|
+
def exec(&block)
|
13
|
+
@redis_proc.call(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# rubocop: disable Metrics/MethodLength
|
19
|
+
def build_redis_proc(redis)
|
20
|
+
case redis
|
21
|
+
when ::Redis
|
22
|
+
proc { |&b| b.call(redis) }
|
23
|
+
when ConnectionPool
|
24
|
+
proc { |&b| redis.with { |r| b.call(r) } }
|
25
|
+
when Hash
|
26
|
+
build_redis_proc(::Redis.new(redis))
|
27
|
+
when Proc
|
28
|
+
redis
|
29
|
+
else
|
30
|
+
raise ArgumentError, 'Redis, ConnectionPool, Hash or Proc is required'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
# rubocop: enable Metrics/MethodLength
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'money/distributed/redis'
|
3
|
+
|
4
|
+
class Money
|
5
|
+
module Distributed
|
6
|
+
# Storage for `Money::Bank::VariableExchange` that stores rates in Redis
|
7
|
+
class Storage
|
8
|
+
INDEX_KEY_SEPARATOR = '_TO_'.freeze
|
9
|
+
REDIS_KEY = 'money_rates'.freeze
|
10
|
+
|
11
|
+
def initialize(redis, cache_ttl = nil)
|
12
|
+
@redis = Money::Distributed::Redis.new(redis)
|
13
|
+
|
14
|
+
@cache = {}
|
15
|
+
@cache_ttl = cache_ttl
|
16
|
+
@cache_updated_at = nil
|
17
|
+
|
18
|
+
@mutex = Mutex.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_rate(iso_from, iso_to, rate)
|
22
|
+
@redis.exec do |r|
|
23
|
+
r.hset(REDIS_KEY, key_for(iso_from, iso_to), rate)
|
24
|
+
end
|
25
|
+
clear_cache
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_rate(iso_from, iso_to)
|
29
|
+
cached_rates[key_for(iso_from, iso_to)]
|
30
|
+
end
|
31
|
+
|
32
|
+
def each_rate
|
33
|
+
enum = Enumerator.new do |yielder|
|
34
|
+
cached_rates.each do |key, rate|
|
35
|
+
iso_from, iso_to = key.split(INDEX_KEY_SEPARATOR)
|
36
|
+
yielder.yield iso_from, iso_to, rate
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
block_given? ? enum.each(&block) : enum
|
41
|
+
end
|
42
|
+
|
43
|
+
def transaction
|
44
|
+
# We don't need transactions, we all thread safe here
|
45
|
+
yield
|
46
|
+
end
|
47
|
+
|
48
|
+
def marshal_dump
|
49
|
+
[self.class, @cache_ttl]
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def key_for(iso_from, iso_to)
|
55
|
+
[iso_from, iso_to].join(INDEX_KEY_SEPARATOR).upcase
|
56
|
+
end
|
57
|
+
|
58
|
+
def cached_rates
|
59
|
+
@mutex.synchronize do
|
60
|
+
retrieve_rates if @cache.empty? || cache_outdated?
|
61
|
+
@cache
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def cache_outdated?
|
66
|
+
return false unless @cache_ttl
|
67
|
+
@cache_updated_at.nil? ||
|
68
|
+
@cache_updated_at < Time.now - @cache_ttl
|
69
|
+
end
|
70
|
+
|
71
|
+
def clear_cache
|
72
|
+
@mutex.synchronize do
|
73
|
+
@cache.clear
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def retrieve_rates
|
78
|
+
@redis.exec do |r|
|
79
|
+
r.hgetall(REDIS_KEY).each_with_object(@cache) do |(key, val), h|
|
80
|
+
h[key] = BigDecimal.new(val)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
@cache_updated_at = Time.now
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'money/distributed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'money-distributed'
|
8
|
+
spec.version = Money::Distributed::VERSION
|
9
|
+
spec.authors = ['DarthSim']
|
10
|
+
spec.email = ['darthsim@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Money gem extension for distributed systems'
|
13
|
+
spec.homepage = 'https://github.com/DarthSim/money-distributed'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '>= 3.0.0'
|
27
|
+
spec.add_development_dependency 'timecop'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.44.1'
|
29
|
+
|
30
|
+
spec.add_dependency 'money', '>= 6.6.0'
|
31
|
+
spec.add_dependency 'redis'
|
32
|
+
spec.add_dependency 'connection_pool'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: money-distributed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DarthSim
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: timecop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.44.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.44.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: money
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 6.6.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 6.6.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: redis
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: connection_pool
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- darthsim@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE
|
138
|
+
- lib/money-distributed.rb
|
139
|
+
- lib/money/distributed/fetcher/base.rb
|
140
|
+
- lib/money/distributed/fetcher/file.rb
|
141
|
+
- lib/money/distributed/redis.rb
|
142
|
+
- lib/money/distributed/storage.rb
|
143
|
+
- lib/money/distributed/version.rb
|
144
|
+
- money-distributed.gemspec
|
145
|
+
homepage: https://github.com/DarthSim/money-distributed
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.5.1
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Money gem extension for distributed systems
|
169
|
+
test_files: []
|