lbank 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e97309035b55a0607c7e25877d7b93d47efd7798
4
+ data.tar.gz: 2f2ab7d5612edd9509d501dc0f15b308275dced4
5
+ SHA512:
6
+ metadata.gz: 47701d086875ab0ba6e85b98e942378ff2940f33c1f391ca6b4738294b897eb746be03616fa5a14c785ce399651e5c488b237b5fe6d4a029a347619ad5513cc2
7
+ data.tar.gz: 154a81cf41ab3b91b817ce3d92d66e349daeccd85ed7afa8325b93f33d6680dc8f8e2bf245e5364174c5bc36944108e9265e000da14cfb709dbacadaf179d9a2
@@ -0,0 +1 @@
1
+ 2.3.1
@@ -1,7 +1,5 @@
1
1
  rvm:
2
- - 1.9.3
3
- - 1.9.2
4
- - jruby
5
- - rbx
2
+ - ruby-head
3
+ - 2.2.2
6
4
  script: "bundle exec rake spec"
7
5
 
data/Gemfile CHANGED
@@ -2,10 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake'
6
- gem 'tzinfo'
7
- gem 'activesupport'
8
-
9
5
  group :test do
10
- gem "rspec"
11
- end
6
+ gem 'rake'
7
+ end
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Lbank
2
2
 
3
- [![Build Status](http://travis-ci.org/laurynas/lbank.png)](http://travis-ci.org/laurynas/lbank)
3
+ [![Build Status](https://secure.travis-ci.org/laurynas/lbank.png)](http://travis-ci.org/laurynas/lbank)
4
4
 
5
5
  Fetches currency rates from Lithuanian Central Bank website http://lbank.lt.
6
6
  Does currency conversion.
@@ -2,16 +2,24 @@
2
2
  require File.expand_path('../lib/lbank/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Laurynas Butkus"]
6
- gem.email = ["laurynas.butkus@gmail.com"]
5
+ gem.authors = ['Laurynas Butkus']
6
+ gem.email = ['laurynas.butkus@gmail.com']
7
7
  gem.description = %q{lbank.lt currency rates fetcher and converter}
8
8
  gem.summary = %q{Fetches currency rates from Lithuanian Central Bank website. Does currency conversion. Supports historic currency rates.}
9
- gem.homepage = "https://github.com/laurynas/lbank"
9
+ gem.homepage = 'https://github.com/laurynas/lbank'
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
13
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "lbank"
15
- gem.require_paths = ["lib"]
14
+ gem.name = 'lbank'
15
+ gem.require_paths = ['lib']
16
16
  gem.version = Lbank::VERSION
17
+
18
+ gem.add_dependency 'faraday'
19
+ gem.add_dependency 'faraday_middleware'
20
+ gem.add_dependency 'activesupport'
21
+ gem.add_dependency 'multi_xml'
22
+
23
+ gem.add_development_dependency 'rspec', '~> 3.5'
24
+ gem.add_development_dependency 'rspec-its', '~> 1.2'
17
25
  end
@@ -1,46 +1,64 @@
1
- require "lbank/version"
2
- require 'open-uri'
3
- require 'csv'
1
+ require 'lbank/version'
2
+ require 'lbank/memory_cache'
4
3
  require 'active_support/time'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require 'bigdecimal'
5
7
 
6
8
  module Lbank
9
+ RATE_TYPE = 'LT'
10
+ TIMEZONE = 'Europe/Vilnius'
11
+ URL = 'http://www.lb.lt/webservices/FxRates/FxRates.asmx/getFxRates'
12
+ LTL = 'LTL'
13
+ LTL_RATE = BigDecimal('3.45280')
7
14
 
8
- BASE_CURRENCY = 'LTL'
9
- TIMEZONE = 'Europe/Vilnius'
10
- SOURCE = 'http://lbank.lt/exchange/Results.asp'
15
+ module_function
11
16
 
12
- @@cache = {}
17
+ def cache
18
+ @cache ||= MemoryCache.new
19
+ end
20
+
21
+ def cache=(store)
22
+ @cache = store
23
+ end
13
24
 
14
- def self.currency_rates(time = nil)
15
- time ||= Time.now
25
+ def currency_rates(time = nil)
26
+ time ||= Time.now
16
27
  bank_time = time.to_time.in_time_zone(TIMEZONE)
17
- date = [ bank_time.year, bank_time.month, bank_time.day ]
28
+ date = bank_time.strftime('%Y-%m-%d')
18
29
 
19
- if @@cache[date].nil?
20
- url = "#{SOURCE}?Y=%i&M=%i&D=%i&S=csv" % date
21
- rates = {}
30
+ cache.fetch(cache_key(date)) do
31
+ response = connection.post(URL, tp: RATE_TYPE, dt: date)
32
+ fx_rates = response.body['FxRates']['FxRate']
22
33
 
23
- data = open(url).read
34
+ rates = { fx_rates[0]['CcyAmt'][0]['Ccy'] => 1 }
35
+ rates[LTL] ||= LTL_RATE
24
36
 
25
- CSV.parse(data).each do |row|
26
- rates[row[1]] = row[2].to_i / row[3].to_f
37
+ fx_rates.each_with_object(rates) do |rate, result|
38
+ base, foreign = rate['CcyAmt']
39
+ result[foreign['Ccy']] = BigDecimal(foreign['Amt']) / BigDecimal(base['Amt'])
27
40
  end
28
-
29
- rates[BASE_CURRENCY] = 1.0
30
-
31
- @@cache[date] = rates
32
41
  end
33
-
34
- @@cache[date]
35
42
  end
36
43
 
37
- def self.convert_currency(amount, from_currency, to_currency, date = nil)
38
- rates = self.currency_rates(date)
39
-
44
+ def convert_currency(amount, from_currency, to_currency, date = nil)
45
+ rates = currency_rates(date)
40
46
  from_rate = rates[from_currency.to_s]
41
- to_rate = rates[to_currency.to_s]
47
+ to_rate = rates[to_currency.to_s]
42
48
 
43
49
  amount / from_rate * to_rate
44
50
  end
45
51
 
52
+ def cache_key(date)
53
+ "lbank-#{date}"
54
+ end
55
+
56
+ def connection
57
+ @connection ||= Faraday.new do |builder|
58
+ builder.request :url_encoded
59
+ builder.response :raise_error
60
+ builder.response :xml, content_type: /\bxml$/
61
+ builder.adapter Faraday.default_adapter
62
+ end
63
+ end
46
64
  end
@@ -0,0 +1,21 @@
1
+ class Lbank::MemoryCache
2
+ def initialize
3
+ @store = {}
4
+ end
5
+
6
+ def store(key, value)
7
+ @store[key] = value
8
+ end
9
+
10
+ def read(key)
11
+ @store[key]
12
+ end
13
+
14
+ def fetch(key, &block)
15
+ return self[key] if self[key]
16
+ self[key] = block.call
17
+ end
18
+
19
+ alias :[]= :store
20
+ alias :[] :read
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Lbank
2
- VERSION = "0.0.3"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -1,22 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Lbank do
4
+ describe '#currency_rates' do
5
+ subject { described_class.currency_rates }
4
6
 
5
- it "should return currency rates" do
6
- currency_rates = subject.currency_rates
7
-
8
- currency_rates['USD'].should be > 0
7
+ its(['USD']) { is_expected.to be > 0 }
9
8
  end
10
9
 
11
- it "should convert currency" do
12
- subject.convert_currency(10, 'LTL', 'EUR').should be > 0
10
+ describe '#convert_currency' do
11
+ it 'converts currency' do
12
+ expect(subject.convert_currency(10, 'USD', 'EUR')).to be > 0
13
+ expect(subject.convert_currency(10, 'EUR', 'LTL')).to eq 34.528
13
14
 
14
- date = Date.new(2012, 8, 4)
15
+ date = Date.new(2012, 8, 4)
15
16
 
16
- round(subject.convert_currency(10, 'LTL', 'EUR', date)).should == 2.90
17
- round(subject.convert_currency(10, 'LTL', 'USD', date)).should == 3.55
18
- round(subject.convert_currency(100, 'RUB', 'LTL', date)).should == 8.68
19
- round(subject.convert_currency(100, 'RUB', 'USD', date)).should == 3.08
17
+ expect(subject.convert_currency(10, 'EUR', 'LTL')).to eq 34.528
18
+ expect(round(subject.convert_currency(1, 'USD', 'EUR', date))).to eq 0.82
19
+ expect(round(subject.convert_currency(100, 'RUB', 'USD', date))).to eq 3.08
20
+ end
20
21
  end
21
22
 
22
23
  private
@@ -24,5 +25,4 @@ describe Lbank do
24
25
  def round(number, precision = 2)
25
26
  ("%.#{precision}f" % number).to_f
26
27
  end
27
-
28
- end
28
+ end
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
+ require 'rspec/its'
3
4
  require 'lbank'
4
5
 
5
6
  RSpec.configure do |config|
metadata CHANGED
@@ -1,16 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lbank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Laurynas Butkus
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-04 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2016-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_xml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
14
97
  description: lbank.lt currency rates fetcher and converter
15
98
  email:
16
99
  - laurynas.butkus@gmail.com
@@ -18,44 +101,44 @@ executables: []
18
101
  extensions: []
19
102
  extra_rdoc_files: []
20
103
  files:
21
- - .gitignore
22
- - .rspec
23
- - .travis.yml
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-version"
107
+ - ".travis.yml"
24
108
  - Gemfile
25
109
  - LICENSE
26
110
  - README.md
27
111
  - Rakefile
28
112
  - lbank.gemspec
29
113
  - lib/lbank.rb
114
+ - lib/lbank/memory_cache.rb
30
115
  - lib/lbank/version.rb
31
116
  - spec/lbank_spec.rb
32
117
  - spec/spec_helper.rb
33
118
  homepage: https://github.com/laurynas/lbank
34
119
  licenses: []
120
+ metadata: {}
35
121
  post_install_message:
36
122
  rdoc_options: []
37
123
  require_paths:
38
124
  - lib
39
125
  required_ruby_version: !ruby/object:Gem::Requirement
40
- none: false
41
126
  requirements:
42
- - - ! '>='
127
+ - - ">="
43
128
  - !ruby/object:Gem::Version
44
129
  version: '0'
45
130
  required_rubygems_version: !ruby/object:Gem::Requirement
46
- none: false
47
131
  requirements:
48
- - - ! '>='
132
+ - - ">="
49
133
  - !ruby/object:Gem::Version
50
134
  version: '0'
51
135
  requirements: []
52
136
  rubyforge_project:
53
- rubygems_version: 1.8.24
137
+ rubygems_version: 2.5.1
54
138
  signing_key:
55
- specification_version: 3
139
+ specification_version: 4
56
140
  summary: Fetches currency rates from Lithuanian Central Bank website. Does currency
57
141
  conversion. Supports historic currency rates.
58
142
  test_files:
59
143
  - spec/lbank_spec.rb
60
144
  - spec/spec_helper.rb
61
- has_rdoc: