convertator 0.1.1 → 0.2.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: fec0ef4b0e1409a1223912e8b22f0ec8094822ff
4
- data.tar.gz: 6f8abbd25071d4e7ed68628b6f15f3a8a58f1b45
3
+ metadata.gz: bc7a6facbe3c191895e300323d3edef1ade43fb6
4
+ data.tar.gz: eaa33fe8665055b55049f7cef821f3402bb06432
5
5
  SHA512:
6
- metadata.gz: '01595668a682cc1b476657e4ad0a6402b2370c9c2fcdf7d2fa4140033a7cdf18542dee4a5735dab70d3879cbca3fa29343e49b9ab1b4b07fb85fd3a03db01e61'
7
- data.tar.gz: f37171f28f4eda3a988b307d5e964720896d51cb51fa2dfda46bbebdc647fb9ae2e3814d48077d0e3c0cc752b5e35bdf8e5a216487840674a6ed78fbf453bf69
6
+ metadata.gz: eaa2be165648f8ecd29ff4e29e62409a2021e6a8fc81d10bb6c608827de6f767554537fc02ec4a6891a8cc6f108211084edf8c66f9b5ae2aa16ccff69d4fa4da
7
+ data.tar.gz: 727eb42164a427c3ab1c6bde3ba300c93adcc0f3259617cfce71637dd859f036e57de32ffe80786fd6d8c777d0a97a9d8f522096c0950a0bc9755a8cc022e7e0
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+ Metrics/BlockLength:
4
+ Enabled: false
data/README.md CHANGED
@@ -1,11 +1,14 @@
1
1
  # Convertator
2
2
 
3
- A simple rate exchange converter.
3
+ A simple currency converter which can use different sources to fetch rates and provides a rack-like middleware feature.
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/convertator.svg)](https://badge.fury.io/rb/convertator)
6
6
 
7
7
  ## Usage
8
8
 
9
+
10
+ **Simple example**
11
+
9
12
  ```ruby
10
13
  require 'convertator/converter'
11
14
 
@@ -16,6 +19,21 @@ converter.convert_digits(100, :RUB, :USD)
16
19
  => "5500.0"
17
20
  ```
18
21
 
22
+
23
+ **Multi convertion**
24
+
25
+ ```ruby
26
+ Convertator::Converter.new.convert_multi_s(100, :GBP, [:AMD, :RUB, :GBP])
27
+ => ["15.7142857144", "1.4285714286", "100.0"]
28
+ ```
29
+
30
+
31
+ **Define accuracy and handler**
32
+
33
+ ```ruby
34
+ Convertator::Converter.new(:static, 7)
35
+ ```
36
+
19
37
  ## Installation
20
38
 
21
39
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "convertator"
3
+ require 'bundler/setup'
4
+ require 'convertator'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "convertator"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
data/convertator.gemspec CHANGED
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'convertator/version'
@@ -9,8 +10,8 @@ Gem::Specification.new do |spec|
9
10
  spec.authors = ['Ivan Zinovyev']
10
11
  spec.email = ['vanyazin@gmail.com']
11
12
 
12
- spec.summary = %q{Rate exchange converter}
13
- spec.homepage = "https://github.com/zinovyev/convertator"
13
+ spec.summary = 'Simple currency converter'
14
+ spec.homepage = 'https://github.com/zinovyev/convertator'
14
15
  spec.license = 'MIT'
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -23,5 +24,6 @@ Gem::Specification.new do |spec|
23
24
  spec.add_development_dependency 'bundler', '~> 1.14'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
25
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'rubocop', '~> 0.48.1'
26
28
  spec.add_development_dependency 'pry', '~> 0.10.3'
27
29
  end
@@ -2,7 +2,7 @@ module Convertator
2
2
  class BaseProvider
3
3
  def new_rates
4
4
  raise ::NotImplementedError,
5
- "Method new_rates should be implemented by every rates provider"
5
+ 'Method new_rates should be implemented by every rates provider'
6
6
  end
7
7
  end
8
8
  end
@@ -13,29 +13,49 @@ module Convertator
13
13
  end
14
14
 
15
15
  def rates
16
- rates ||= @provider.new_rates
16
+ @provider.new_rates
17
17
  end
18
18
 
19
19
  def rate(currency)
20
20
  currency = normalize_currency(currency)
21
- raise UnknownCurrencyError unless rates[currency]
22
- BigDecimal.new(rates[currency], @accuracy)
21
+ rate = rates[currency]
22
+ raise UnknownCurrencyError, "Unknown currency #{currency}" unless rate
23
+ round(BigDecimal.new(rate, @accuracy))
23
24
  end
24
25
 
25
26
  def ratio(currency_from, currency_to)
26
- rate(currency_from) / rate(currency_to)
27
+ round(rate(currency_from) / rate(currency_to))
27
28
  end
28
29
 
29
30
  def convert(amount, currency_from, currency_to)
30
- amount / ratio(currency_from, currency_to)
31
+ round(amount / ratio(currency_from, currency_to))
31
32
  end
32
33
 
33
- def convert_digits(amount, currency_from, currency_to)
34
- convert(amount, currency_from, currency_to).to_digits
34
+ def convert_s(amount, currency_from, currency_to)
35
+ round(convert(amount, currency_from, currency_to)).to_digits
36
+ end
37
+
38
+ def convert_multi(amount, currency_from, currencies_to)
39
+ currencies_to.map do |currency_to|
40
+ convert(amount, currency_from, currency_to)
41
+ end
42
+ end
43
+
44
+ def convert_multi_s(amount, currency_from, currencies_to)
45
+ currencies_to.map do |currency_to|
46
+ convert_s(amount, currency_from, currency_to)
47
+ end
35
48
  end
36
49
 
37
50
  private
38
51
 
52
+ def round(value)
53
+ BigDecimal.save_rounding_mode do
54
+ BigDecimal.mode(BigDecimal::ROUND_MODE, :half_up)
55
+ value.round(@accuracy)
56
+ end
57
+ end
58
+
39
59
  def load_provider(name)
40
60
  try_require(name)
41
61
  klass = Object.const_get(
@@ -4,9 +4,9 @@ require 'rexml/document'
4
4
  module Convertator
5
5
  module Providers
6
6
  class CbrProvider
7
- SERVICE_URI = 'http://www.cbr.ru/scripts/xml_daily.asp'
7
+ SERVICE_URI = 'http://www.cbr.ru/scripts/xml_daily.asp'.freeze
8
8
 
9
- def new_rates
9
+ def new_rates
10
10
  default_rates.merge parse(fetch)
11
11
  end
12
12
 
@@ -14,7 +14,7 @@ module Convertator
14
14
 
15
15
  def default_rates
16
16
  {
17
- RUB: "1",
17
+ RUB: '1'
18
18
  }
19
19
  end
20
20
 
@@ -31,7 +31,7 @@ module Convertator
31
31
 
32
32
  def fetch
33
33
  uri = URI(SERVICE_URI)
34
- ::Net::HTTP.get(uri)
34
+ ::Net::HTTP.get(uri)
35
35
  end
36
36
  end
37
37
  end
@@ -1,3 +1,3 @@
1
1
  module Convertator
2
- VERSION = "0.1.1"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/convertator.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "convertator/version"
1
+ require 'convertator/version'
2
2
 
3
3
  module Convertator
4
4
  # Your code goes here...
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: convertator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Zinovyev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-05 00:00:00.000000000 Z
11
+ date: 2017-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.48.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.48.1
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pry
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +89,7 @@ extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
77
91
  - ".rspec"
92
+ - ".rubocop.yml"
78
93
  - ".travis.yml"
79
94
  - CODE_OF_CONDUCT.md
80
95
  - Gemfile
@@ -113,5 +128,5 @@ rubyforge_project:
113
128
  rubygems_version: 2.6.8
114
129
  signing_key:
115
130
  specification_version: 4
116
- summary: Rate exchange converter
131
+ summary: Simple currency converter
117
132
  test_files: []