lbank 0.0.1
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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +8 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/lbank.gemspec +17 -0
- data/lib/lbank.rb +39 -0
- data/lib/lbank/version.rb +3 -0
- data/spec/lbank_spec.rb +22 -0
- data/spec/spec_helper.rb +7 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Laurynas Butkus
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Lbank
|
2
|
+
|
3
|
+
Fetches currency rates from Lithuanian Central Bank website http://lbank.lt.
|
4
|
+
Does currency conversion.
|
5
|
+
Supports historic currency rates.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'lbank'
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Get hash of current currency rates
|
16
|
+
|
17
|
+
Lbank.currency_rates
|
18
|
+
|
19
|
+
Get hash of historic currency rates
|
20
|
+
|
21
|
+
Lbank.currency_rates(Date.new(2012, 8, 4))
|
22
|
+
|
23
|
+
Convert currency
|
24
|
+
|
25
|
+
Lbank.convert_currency(10, 'LTL', 'EUR')
|
26
|
+
|
27
|
+
Convert currency using historic rates
|
28
|
+
|
29
|
+
Lbank.convert_currency(10, 'LTL', 'EUR', Date.new(2012, 8, 4))
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lbank.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/lbank/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Laurynas Butkus"]
|
6
|
+
gem.email = ["laurynas.butkus@gmail.com"]
|
7
|
+
gem.description = %q{lbank.lt currency rates fetcher and converter}
|
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"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "lbank"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Lbank::VERSION
|
17
|
+
end
|
data/lib/lbank.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "lbank/version"
|
2
|
+
require 'open-uri'
|
3
|
+
require 'csv'
|
4
|
+
|
5
|
+
module Lbank
|
6
|
+
|
7
|
+
BASE_CURRENCY = 'LTL'
|
8
|
+
SOURCE = 'http://lbank.lt/exchange/Results.asp'
|
9
|
+
@@cache = {}
|
10
|
+
|
11
|
+
def self.currency_rates(date = nil)
|
12
|
+
date ||= Date.today
|
13
|
+
|
14
|
+
if @@cache[date].nil?
|
15
|
+
url = "#{SOURCE}?Y=%i&M=%i&D=%i&S=csv" % [ date.year, date.month, date.day ]
|
16
|
+
rates = {}
|
17
|
+
|
18
|
+
CSV.parse(open(url)).each do |row|
|
19
|
+
rates[row[1]] = row[2].to_i / row[3].to_f
|
20
|
+
end
|
21
|
+
|
22
|
+
rates[BASE_CURRENCY] = 1.0
|
23
|
+
|
24
|
+
@@cache[date] = rates
|
25
|
+
end
|
26
|
+
|
27
|
+
@@cache[date]
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.convert_currency(amount, from_currency, to_currency, date = nil)
|
31
|
+
rates = self.currency_rates(date)
|
32
|
+
|
33
|
+
from_rate = rates[from_currency.to_s]
|
34
|
+
to_rate = rates[to_currency.to_s]
|
35
|
+
|
36
|
+
amount / from_rate * to_rate
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/spec/lbank_spec.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lbank do
|
4
|
+
|
5
|
+
it "should return currency rates" do
|
6
|
+
currency_rates = subject.currency_rates
|
7
|
+
|
8
|
+
currency_rates['USD'].should be > 0
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should convert currency" do
|
12
|
+
subject.convert_currency(10, 'LTL', 'EUR').should be > 0
|
13
|
+
|
14
|
+
date = Date.new(2012, 8, 4)
|
15
|
+
|
16
|
+
subject.convert_currency(10, 'LTL', 'EUR', date).round(2).should == 2.90
|
17
|
+
subject.convert_currency(10, 'LTL', 'USD', date).round(2).should == 3.55
|
18
|
+
subject.convert_currency(100, 'RUB', 'LTL', date).round(2).should == 8.68
|
19
|
+
subject.convert_currency(100, 'RUB', 'USD', date).round(2).should == 3.08
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lbank
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Laurynas Butkus
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: lbank.lt currency rates fetcher and converter
|
15
|
+
email:
|
16
|
+
- laurynas.butkus@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lbank.gemspec
|
28
|
+
- lib/lbank.rb
|
29
|
+
- lib/lbank/version.rb
|
30
|
+
- spec/lbank_spec.rb
|
31
|
+
- spec/spec_helper.rb
|
32
|
+
homepage: https://github.com/laurynas/lbank
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Fetches currency rates from Lithuanian Central Bank website. Does currency
|
56
|
+
conversion. Supports historic currency rates.
|
57
|
+
test_files:
|
58
|
+
- spec/lbank_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
has_rdoc:
|