convertator 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc7a6facbe3c191895e300323d3edef1ade43fb6
4
- data.tar.gz: eaa33fe8665055b55049f7cef821f3402bb06432
3
+ metadata.gz: 022c887fe0bd212569243795a54a18e0b746dc23
4
+ data.tar.gz: 0acc04318bf82853fef66660e2aee623c626f8be
5
5
  SHA512:
6
- metadata.gz: eaa2be165648f8ecd29ff4e29e62409a2021e6a8fc81d10bb6c608827de6f767554537fc02ec4a6891a8cc6f108211084edf8c66f9b5ae2aa16ccff69d4fa4da
7
- data.tar.gz: 727eb42164a427c3ab1c6bde3ba300c93adcc0f3259617cfce71637dd859f036e57de32ffe80786fd6d8c777d0a97a9d8f522096c0950a0bc9755a8cc022e7e0
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
  [![Gem Version](https://badge.fury.io/rb/convertator.svg)](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, :RUB, :USD)
16
+ converter.convert(100, :USD, :RUB)
17
17
  => 0.55e4
18
- converter.convert_digits(100, :RUB, :USD)
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
- => ["15.7142857144", "1.4285714286", "100.0"]
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
- @provider.new_rates
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 / ratio(currency_from, currency_to))
38
+ round(amount * ratio(currency_from, currency_to))
32
39
  end
33
40
 
34
41
  def convert_s(amount, currency_from, currency_to)
35
- round(convert(amount, currency_from, currency_to)).to_digits
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,10 @@
1
+ module Convertator
2
+ class Middleware
3
+ attr_accessor :prev
4
+
5
+ def call
6
+ raise ::NotImplementedError,
7
+ 'Method call should be implemented by every middleware'
8
+ end
9
+ end
10
+ 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 new_rates
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 < BaseProvider
3
+ class StaticProvider < Middleware
4
4
  attr_writer :rates
5
5
 
6
- def initialize
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
@@ -0,0 +1,13 @@
1
+ module Convertator
2
+ module Utils
3
+ def symbolize_keys(array)
4
+ array.each_with_object({}) do |(k, v), memo|
5
+ memo[normalize_currency(k)] = v
6
+ end
7
+ end
8
+
9
+ def normalize_currency(currency)
10
+ currency.to_sym.upcase
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Convertator
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.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:
@@ -1,8 +0,0 @@
1
- module Convertator
2
- class BaseProvider
3
- def new_rates
4
- raise ::NotImplementedError,
5
- 'Method new_rates should be implemented by every rates provider'
6
- end
7
- end
8
- end