convertator 0.2.0 → 0.3.0
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 +4 -4
- data/README.md +20 -10
- data/lib/convertator/converter.rb +20 -7
- data/lib/convertator/middleware.rb +10 -0
- data/lib/convertator/middlewares/file_cache_middleware.rb +50 -0
- data/lib/convertator/providers/cbr_provider.rb +2 -2
- data/lib/convertator/providers/static_provider.rb +3 -7
- data/lib/convertator/utils.rb +13 -0
- data/lib/convertator/version.rb +1 -1
- metadata +4 -2
- data/lib/convertator/base_provider.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 022c887fe0bd212569243795a54a18e0b746dc23
|
4
|
+
data.tar.gz: 0acc04318bf82853fef66660e2aee623c626f8be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 154ac3ec7baebf2b6fa19b707ddd990c9e300591e0a540ac040cb96d4aa096adf3fd27db89401722de79362ea8c139f9618bd1c5dff907928449f2fe6fb5382f
|
7
|
+
data.tar.gz: 44c39d3a310f2aeb0a850f0488536275a10766cdcdd3254ce2196cc9d37966a827023060b3fc2b12dd89a50be05aac0c4cb534f092743dbeef91119c9634fd72
|
data/README.md
CHANGED
@@ -4,8 +4,8 @@ A simple currency converter which can use different sources to fetch rates and p
|
|
4
4
|
|
5
5
|
[](https://badge.fury.io/rb/convertator)
|
6
6
|
|
7
|
-
## Usage
|
8
7
|
|
8
|
+
## Usage
|
9
9
|
|
10
10
|
**Simple example**
|
11
11
|
|
@@ -13,27 +13,39 @@ A simple currency converter which can use different sources to fetch rates and p
|
|
13
13
|
require 'convertator/converter'
|
14
14
|
|
15
15
|
converter = Convertator::Converter.new
|
16
|
-
converter.convert(100, :
|
16
|
+
converter.convert(100, :USD, :RUB)
|
17
17
|
=> 0.55e4
|
18
|
-
converter.convert_digits(100, :
|
18
|
+
converter.convert_digits(100, :USD, :RUB)
|
19
19
|
=> "5500.0"
|
20
20
|
```
|
21
21
|
|
22
|
-
|
23
22
|
**Multi convertion**
|
24
23
|
|
25
24
|
```ruby
|
26
|
-
Convertator::Converter.new.convert_multi_s(100, :GBP, [:AMD, :RUB, :GBP])
|
27
|
-
=> ["
|
25
|
+
Convertator::Converter.new(:static).convert_multi_s(100, :GBP, [:AMD, :RUB, :GBP])
|
26
|
+
=> ["47.12398", "3336.69827", "100.0"]
|
28
27
|
```
|
29
28
|
|
30
|
-
|
31
29
|
**Define accuracy and handler**
|
32
30
|
|
33
31
|
```ruby
|
34
32
|
Convertator::Converter.new(:static, 7)
|
35
33
|
```
|
36
34
|
|
35
|
+
**Use file cache middleware**
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
irb(main):002:0> require 'convertator/middlewares/file_cache_middleware'
|
39
|
+
=> true
|
40
|
+
irb(main):003:0> converter = Convertator::Converter.new do |c|
|
41
|
+
irb(main):004:1* c.add Convertator::Middlewares::FileCacheMiddleware.new('/tmp/convertator.cache')
|
42
|
+
irb(main):005:1> end
|
43
|
+
=> #<Convertator::Converter:0x005642ede40a58>
|
44
|
+
converter.convert_s(100, :GBP, :RUB)
|
45
|
+
=> 7000.0
|
46
|
+
```
|
47
|
+
|
48
|
+
|
37
49
|
## Installation
|
38
50
|
|
39
51
|
Add this line to your application's Gemfile:
|
@@ -50,9 +62,6 @@ Or install it yourself as:
|
|
50
62
|
|
51
63
|
$ gem install convertator
|
52
64
|
|
53
|
-
## Usage
|
54
|
-
|
55
|
-
TODO: Write usage instructions here
|
56
65
|
|
57
66
|
## Development
|
58
67
|
|
@@ -60,6 +69,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
60
69
|
|
61
70
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
62
71
|
|
72
|
+
|
63
73
|
## Contributing
|
64
74
|
|
65
75
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/convertator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
@@ -10,10 +10,17 @@ module Convertator
|
|
10
10
|
def initialize(provider = :cbr, accuracy = 10)
|
11
11
|
@provider = load_provider(provider)
|
12
12
|
@accuracy = accuracy
|
13
|
+
@chain = [] << @provider
|
14
|
+
yield(self) if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def add(middleware)
|
18
|
+
middleware.prev = @chain.last
|
19
|
+
@chain << middleware
|
13
20
|
end
|
14
21
|
|
15
22
|
def rates
|
16
|
-
@
|
23
|
+
symbolize_keys(@chain.last.call)
|
17
24
|
end
|
18
25
|
|
19
26
|
def rate(currency)
|
@@ -28,11 +35,11 @@ module Convertator
|
|
28
35
|
end
|
29
36
|
|
30
37
|
def convert(amount, currency_from, currency_to)
|
31
|
-
round(amount
|
38
|
+
round(amount * ratio(currency_from, currency_to))
|
32
39
|
end
|
33
40
|
|
34
41
|
def convert_s(amount, currency_from, currency_to)
|
35
|
-
|
42
|
+
convert(amount, currency_from, currency_to).to_digits
|
36
43
|
end
|
37
44
|
|
38
45
|
def convert_multi(amount, currency_from, currencies_to)
|
@@ -49,6 +56,16 @@ module Convertator
|
|
49
56
|
|
50
57
|
private
|
51
58
|
|
59
|
+
def symbolize_keys(array)
|
60
|
+
array.each_with_object({}) do |(k, v), memo|
|
61
|
+
memo[normalize_currency(k)] = v
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def normalize_currency(currency)
|
66
|
+
currency.to_sym.upcase
|
67
|
+
end
|
68
|
+
|
52
69
|
def round(value)
|
53
70
|
BigDecimal.save_rounding_mode do
|
54
71
|
BigDecimal.mode(BigDecimal::ROUND_MODE, :half_up)
|
@@ -71,9 +88,5 @@ module Convertator
|
|
71
88
|
"#{name.downcase}_provider"
|
72
89
|
)
|
73
90
|
end
|
74
|
-
|
75
|
-
def normalize_currency(currency)
|
76
|
-
currency.to_sym.upcase
|
77
|
-
end
|
78
91
|
end
|
79
92
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'convertator/middleware'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Convertator
|
5
|
+
module Middlewares
|
6
|
+
class FileCacheMiddleware < ::Convertator::Middleware
|
7
|
+
DEFAULT_TTL = 3600
|
8
|
+
|
9
|
+
def initialize(file, ttl = DEFAULT_TTL)
|
10
|
+
@file = file_open(file)
|
11
|
+
@ttl = ttl
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
@file.flock(::File::LOCK_EX)
|
16
|
+
if file_zero? || file_old?
|
17
|
+
data = @prev.call
|
18
|
+
file_write(data.to_json)
|
19
|
+
else
|
20
|
+
data = JSON.parse(file_read)
|
21
|
+
end
|
22
|
+
@file.flock(::File::LOCK_UN)
|
23
|
+
data
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def file_read
|
29
|
+
::File.read(@file.path)
|
30
|
+
end
|
31
|
+
|
32
|
+
def file_zero?
|
33
|
+
File.zero? @file.path
|
34
|
+
end
|
35
|
+
|
36
|
+
def file_open(file)
|
37
|
+
::File.new(file, 'w+')
|
38
|
+
end
|
39
|
+
|
40
|
+
def file_write(data)
|
41
|
+
@file.rewind
|
42
|
+
@file.write data
|
43
|
+
end
|
44
|
+
|
45
|
+
def file_old?
|
46
|
+
@ttl < (::Time.new.to_i - @file.mtime.to_i)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -3,10 +3,10 @@ require 'rexml/document'
|
|
3
3
|
|
4
4
|
module Convertator
|
5
5
|
module Providers
|
6
|
-
class CbrProvider
|
6
|
+
class CbrProvider < Middleware
|
7
7
|
SERVICE_URI = 'http://www.cbr.ru/scripts/xml_daily.asp'.freeze
|
8
8
|
|
9
|
-
def
|
9
|
+
def call
|
10
10
|
default_rates.merge parse(fetch)
|
11
11
|
end
|
12
12
|
|
@@ -1,14 +1,10 @@
|
|
1
1
|
module Convertator
|
2
2
|
module Providers
|
3
|
-
class StaticProvider <
|
3
|
+
class StaticProvider < Middleware
|
4
4
|
attr_writer :rates
|
5
5
|
|
6
|
-
def
|
7
|
-
@rates
|
8
|
-
end
|
9
|
-
|
10
|
-
def new_rates
|
11
|
-
@rates
|
6
|
+
def call
|
7
|
+
@rates || {}
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
data/lib/convertator/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: convertator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Zinovyev
|
@@ -100,10 +100,12 @@ files:
|
|
100
100
|
- bin/setup
|
101
101
|
- convertator.gemspec
|
102
102
|
- lib/convertator.rb
|
103
|
-
- lib/convertator/base_provider.rb
|
104
103
|
- lib/convertator/converter.rb
|
104
|
+
- lib/convertator/middleware.rb
|
105
|
+
- lib/convertator/middlewares/file_cache_middleware.rb
|
105
106
|
- lib/convertator/providers/cbr_provider.rb
|
106
107
|
- lib/convertator/providers/static_provider.rb
|
108
|
+
- lib/convertator/utils.rb
|
107
109
|
- lib/convertator/version.rb
|
108
110
|
homepage: https://github.com/zinovyev/convertator
|
109
111
|
licenses:
|