fxpotato 0.2.2 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 020e1b769d3f87e2da58999d13c1db7a019df85a
4
- data.tar.gz: c2e11872230a35f400930cd523a324b71e18097d
3
+ metadata.gz: f5dc3b4c256444b86d08ce4cf7a31d3e764e5678
4
+ data.tar.gz: 619de4799993b072db5461327ba7ec82cc8fd57b
5
5
  SHA512:
6
- metadata.gz: 7c02d00a4f0e445349a656396c7fef2d2a7bc6dd48717ee06cd44d11be5e442638da2e7319b26dede8d2719a2b9ac79836f77b908c384578f46017bd5bd953f9
7
- data.tar.gz: ac2250a1c7e88d58fc5f9c7479e143bf188123277ffb8ccd8b01bb1c4aed019ef7806076936969c31037824adcdd58660ef0a74cefe872dd57b7f8f74ee3eb6e
6
+ metadata.gz: d0231eaf96ff38a710c06ff0c8ad934652f128666ef7b594c9e43f5c242b1322eeafced918d3e52f48285f362729ef4cb1d1f666d9117a605f8818b803dc2f4f
7
+ data.tar.gz: 445e77fc2ba6d65143ac75b3d8dd1f01e8ef8e5a3550ebb28aa900a7627397d7318fe572bf47bb2165fd977630c121d1bd889130424f4bfbe08376faae93b40d
data/.gitignore CHANGED
@@ -7,4 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ data
10
11
  TODO
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
+ require 'fxpotato'
3
4
 
4
5
  Rake::TestTask.new(:test) do |t|
5
6
  t.libs << 'test'
@@ -7,6 +8,11 @@ Rake::TestTask.new(:test) do |t|
7
8
  t.test_files = FileList['test/**/*_test.rb']
8
9
  end
9
10
 
11
+ desc "Download new exchange rate data"
12
+ task :fetch_new_rates do
13
+ FxPotato.fetch_new_rates
14
+ end
15
+
10
16
  desc "Bump version and release gem"
11
17
  task :bump_release do
12
18
  sh %{ gem bump && rake release }
@@ -0,0 +1,5 @@
1
+ module FxPotato
2
+ DATA_DIRECTORY = File.join(File.expand_path(File.dirname(__FILE__) + "/../../"), "data")
3
+ DATA_FILE = 'rates.xml'
4
+ DATA_SOURCE_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'net/http'
2
+
3
+ module FxPotato
4
+ class RateFetcher
5
+ def self.fetch(source_url, destination)
6
+ uri = URI(source_url)
7
+
8
+ Net::HTTP.start(uri.host, uri.port) do |http|
9
+ request = Net::HTTP::Get.new uri
10
+
11
+ http.request request do |response|
12
+ open destination, 'w' do |io|
13
+ response.read_body do |chunk|
14
+ io.write chunk
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,15 +1,7 @@
1
- require 'nokogiri'
2
-
3
1
  module FxPotato
4
2
  class RateStore
5
- def initialize(xml_data)
6
- @data = Nokogiri::XML(xml_data)
7
- @data.remove_namespaces!
8
- end
9
-
10
- def get(date, currency)
11
- query = "//Cube[@time=\'#{date.to_s}\']//Cube[@currency=\'#{currency}\']"
12
- @data.at_xpath(query)["rate"].to_f
3
+ def self.get(repo, date, currency)
4
+ repo.find(date, currency)
13
5
  end
14
6
  end
15
7
  end
@@ -1,3 +1,3 @@
1
1
  module FxPotato
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -0,0 +1,20 @@
1
+ require 'nokogiri'
2
+
3
+ module FxPotato
4
+ class XmlRepo
5
+ def initialize(data)
6
+ @data = Nokogiri::XML(data || local_data)
7
+ @data.remove_namespaces!
8
+ end
9
+
10
+ def local_data
11
+ source_file = File.join(DATA_DIRECTORY, DATA_FILE)
12
+ File.open(source_file)
13
+ end
14
+
15
+ def find(date, currency)
16
+ query = "//Cube[@time=\'#{date.to_s}\']//Cube[@currency=\'#{currency}\']"
17
+ @data.at_xpath(query)["rate"].to_f
18
+ end
19
+ end
20
+ end
data/lib/fxpotato.rb CHANGED
@@ -1,18 +1,27 @@
1
1
  require 'fxpotato/version'
2
- require 'fxpotato/rate_calculator'
2
+ require 'fxpotato/paths'
3
+ require 'fxpotato/xml_repo'
3
4
  require 'fxpotato/rate_store'
5
+ require 'fxpotato/rate_fetcher'
6
+ require 'fxpotato/rate_calculator'
4
7
 
5
8
  module FxPotato
6
9
  def self.at(date, from_currency, to_currency)
7
10
  raise "Must specify date" if date.nil?
8
11
  raise "Must specify from_currency" if from_currency.nil?
9
12
  raise "Must specify to_currency" if to_currency.nil?
10
- from = @rate_store.get(date, from_currency)
11
- to = @rate_store.get(date, to_currency)
13
+ from = RateStore.get(@repo, date, from_currency)
14
+ to = RateStore.get(@repo, date, to_currency)
12
15
  RateCalculator.calculate(from, to)
13
16
  end
14
17
 
18
+ def self.fetch_new_rates
19
+ Dir.mkdir DATA_DIRECTORY if !File.exists? DATA_DIRECTORY
20
+ destination = File.join(DATA_DIRECTORY, DATA_FILE)
21
+ RateFetcher.fetch(DATA_SOURCE_URL, destination)
22
+ end
23
+
15
24
  module_function
16
- def rate_store; @rate_store ||= RateStore.new() end
17
- def rate_store= v; @rate_store = v end
25
+ def repo; @repo ||= XmlRepo end
26
+ def repo= v; @repo = v end
18
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fxpotato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liam Stupid Name Humphreys
@@ -90,7 +90,6 @@ extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
92
  - ".gitignore"
93
- - ".travis.yml"
94
93
  - CODE_OF_CONDUCT.md
95
94
  - Dockerfile
96
95
  - Gemfile
@@ -101,9 +100,12 @@ files:
101
100
  - bin/setup
102
101
  - fxpotato.gemspec
103
102
  - lib/fxpotato.rb
103
+ - lib/fxpotato/paths.rb
104
104
  - lib/fxpotato/rate_calculator.rb
105
+ - lib/fxpotato/rate_fetcher.rb
105
106
  - lib/fxpotato/rate_store.rb
106
107
  - lib/fxpotato/version.rb
108
+ - lib/fxpotato/xml_repo.rb
107
109
  - shippable.yml
108
110
  homepage: https://github.com/Angry-Potato/fxpotato
109
111
  licenses:
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.15.1