fafx 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93feca2af8fb931f0c8e62b0f3aea2bf24eaaf9c
4
+ data.tar.gz: 4bcdfbf6645958ed69b014d6d5f737728b6e36b0
5
+ SHA512:
6
+ metadata.gz: 1e52c5a8b4c5c7895ff69da56cb93c8807e8fd8b47dbcbc8636254e9772c99c216b7518eb05295ed02750e34a6202c303de81e3518d598ed4d5676a99e393f9f
7
+ data.tar.gz: 240f24101ace83b8f0829d77bfb14896490b227ac4e8aa61a38b8374f38d464dfc14c60c3e877f44596d60e7b76b92bf3e778477e0e7e34ce048fc00cb50f0c6
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.16.1
6
+ script:
7
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fafx.gemspec
6
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fafx (0.1.0)
5
+ nokogiri (~> 1.8.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ mini_portile2 (2.3.0)
12
+ nokogiri (1.8.2)
13
+ mini_portile2 (~> 2.3.0)
14
+ rake (10.5.0)
15
+ rspec (3.7.0)
16
+ rspec-core (~> 3.7.0)
17
+ rspec-expectations (~> 3.7.0)
18
+ rspec-mocks (~> 3.7.0)
19
+ rspec-core (3.7.1)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-expectations (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-mocks (3.7.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-support (3.7.1)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.16)
34
+ fafx!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Frank Kair
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # FAFX
2
+
3
+ [![Build Status](https://travis-ci.org/FrankKair/fafx.svg?branch=master)](https://travis-ci.org/FrankKair/fafx)
4
+
5
+ ### Installation
6
+
7
+ `$ gem install fafx`
8
+
9
+ ### CLI
10
+
11
+ `$ fafx`
12
+
13
+ ```
14
+ Usage: fafx [options]
15
+ --recent Lists most recent rates
16
+ --currencies Lists available currencies
17
+ --dates Lists available dates
18
+ --update Fetches new data from the web
19
+ ```
20
+
21
+ ### Example of client usage
22
+
23
+ ```ruby
24
+ require 'Date'
25
+ require 'fafx'
26
+
27
+ puts Fafx::ExchangeRate.at(Date.today, 'GBP', 'USD')
28
+ puts Fafx::ExchangeRate.currencies_available
29
+ puts Fafx::ExchangeRate.dates_available
30
+ puts Fafx::ExchangeRate.most_recent
31
+ ```
32
+
33
+ This code outputs:
34
+
35
+ ```
36
+ 1.2951995012468827
37
+
38
+ USD
39
+ JPY
40
+ BGN
41
+ CZK
42
+ ...
43
+
44
+ 2018-09-07
45
+ 2018-09-06
46
+ 2018-09-05
47
+ 2018-09-04
48
+ ...
49
+
50
+ {"USD"=>1.1615, "JPY"=>128.74, "BGN"=>1.9558, "CZK"=>25.697 ... }
51
+ ```
52
+
53
+ ### Updating the exchange rates data
54
+
55
+ You can update the exchange rates values via the CLI using:
56
+
57
+ `$ fafx --update`
58
+
59
+ One can also use the `$ rake update_data` task as well.
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'fafx'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
8
+
9
+ task :update_data do
10
+ Fafx::DataFetcher.save_to_disk
11
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'fafx'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'fafx'
4
+
5
+ ARGV[0] = '--help' if ARGV.empty?
6
+ puts "invalid option: #{ARGV[0]}" unless ARGV[0].include?('--') || ARGV[0].include?('-')
7
+
8
+ options = {}
9
+ opt_parser = OptionParser.new do |opt|
10
+ opt.on('--recent', 'Lists most recent rates') { |o| options[:recent] = o }
11
+ opt.on('--currencies', 'Lists available currencies') { |o| options[:currencies] = o }
12
+ opt.on('--dates', 'Lists available dates') { |o| options[:dates] = o }
13
+ opt.on('--update', 'Fetches new data from the web') { |o| options[:update] = o }
14
+ end
15
+
16
+ begin
17
+ opt_parser.parse!
18
+ options.keys.each do |k|
19
+ case k
20
+ when :recent
21
+ Fafx::ExchangeRate.most_recent.each_pair do |pair|
22
+ puts "#{pair.first} #{pair.last}"
23
+ end
24
+ when :currencies
25
+ puts Fafx::ExchangeRate.currencies_available
26
+ when :dates
27
+ puts Fafx::ExchangeRate.dates_available
28
+ when :update
29
+ Fafx::DataFetcher.save_to_disk
30
+ end
31
+ end
32
+ rescue Exception => e
33
+ puts e.to_s unless e.message == 'exit'
34
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fafx/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fafx'
8
+ spec.version = Fafx::VERSION
9
+ spec.authors = ['Frank Kair']
10
+ spec.email = ['frankkair@gmail.com']
11
+
12
+ spec.summary = 'Exchange Rates from the European Central Bank'
13
+ spec.description = 'Lib and CLI to get exchange rates from the European Central Bank'
14
+ spec.homepage = "https://github.com/frankkair/fafx"
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ # 'public gem pushes.'
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ # spec.bindir = "exe"
30
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.executables = ['fafx']
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.16'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_runtime_dependency 'nokogiri', '~> 1.8.2'
38
+ end
@@ -0,0 +1,4 @@
1
+ require 'fafx/version'
2
+ require 'fafx/data_fetcher'
3
+ require 'fafx/er'
4
+ require 'fafx/exchange_rate'
@@ -0,0 +1,31 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ module Fafx
5
+ module DataFetcher
6
+ def save_to_disk
7
+ url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'
8
+ doc = Nokogiri::XML(open(url))
9
+ rates = process_currencies_xml(doc)
10
+ dir = "#{File.join(File.dirname(__FILE__))}/rates.yaml"
11
+ File.open(dir, 'w') { |f| f << rates.to_yaml }
12
+ end
13
+
14
+ private
15
+
16
+ def process_currencies_xml(doc)
17
+ rates = {}
18
+ doc.css('Cube>Cube[time]').each do |day|
19
+ time = day[:time]
20
+ rates[time] = {}
21
+ day.css('Cube').each do |currency|
22
+ # TODO: Think about float value - type safety
23
+ rates[time][currency[:currency]] = currency[:rate].to_f
24
+ end
25
+ end
26
+ rates
27
+ end
28
+
29
+ module_function :save_to_disk, :process_currencies_xml
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require 'yaml'
2
+
3
+ # TODO: Create a currency class?
4
+ module Fafx
5
+ class ER
6
+ attr_reader :rates, :dates, :currencies
7
+ def initialize
8
+ data = load_data
9
+ @rates = data
10
+ @dates = data.keys
11
+ @currencies = @rates[@dates.first].keys
12
+ end
13
+
14
+ def rates_at(date, curr)
15
+ raise KeyError, 'Date not available' unless @dates.include?(date)
16
+ raise KeyError, "#{curr} not found" unless @currencies.include?(curr)
17
+ @rates[date][curr]
18
+ end
19
+
20
+ private
21
+
22
+ # -> Hash[String => Float]
23
+ def load_data
24
+ rates = "#{File.join(File.dirname(__FILE__))}/rates.yaml"
25
+ DataFetcher.save_to_disk unless File.exist?(rates)
26
+ YAML.load_file(rates)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module Fafx
2
+ module ExchangeRate
3
+ def at(date, base, other)
4
+ er = ER.new
5
+ base = er.rates_at(date.to_s, base)
6
+ other = er.rates_at(date.to_s, other)
7
+ other / base
8
+ end
9
+
10
+ def currencies_available
11
+ ER.new.currencies
12
+ end
13
+
14
+ def dates_available
15
+ ER.new.dates
16
+ end
17
+
18
+ def most_recent
19
+ er = ER.new
20
+ k = er.dates.first
21
+ er.rates[k]
22
+ end
23
+ module_function :at, :currencies_available, :dates_available, :most_recent
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Fafx
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fafx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frank Kair
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.2
69
+ description: Lib and CLI to get exchange rates from the European Central Bank
70
+ email:
71
+ - frankkair@gmail.com
72
+ executables:
73
+ - fafx
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/fafx
87
+ - bin/setup
88
+ - fafx.gemspec
89
+ - lib/fafx.rb
90
+ - lib/fafx/data_fetcher.rb
91
+ - lib/fafx/er.rb
92
+ - lib/fafx/exchange_rate.rb
93
+ - lib/fafx/version.rb
94
+ homepage: https://github.com/frankkair/fafx
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Exchange Rates from the European Central Bank
118
+ test_files: []