exchange_rate_jt 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 66e1fb6a123341314b19f4911176fc17e79262cc
4
+ data.tar.gz: 0b09caa0e5ff4658dd67fcca983e65fc379c0727
5
+ SHA512:
6
+ metadata.gz: cba3207db64b8d3b09c8359ad3e3caf9dc37ba5d72293d4e434d10bf03cd66a8b404d41da5127084725b00008d6206175f049a6e2c9dd8c7a9bd4bc784836a48
7
+ data.tar.gz: 74a76e2bb388abf10f9035a6eecb25ae82c0b4320f6814d18e17a11570794ffc7ae18a79b6eb5eacce76b606c38e5a5339ccbbe05c8efefb84820a9f6c9a62ac
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ store.*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "exchange_rate_jt/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exchange_rate_jt"
8
+ spec.version = ExchangeRateJt::VERSION
9
+ spec.authors = ["Joshua Toper"]
10
+ spec.email = ["work@joshtoper.co.uk"]
11
+
12
+ spec.summary = %q{Minimal gem for calculating exchange rates.}
13
+ spec.description = %q{This is a self-contained gem which calculates exchange rates from a variety of sources. The gem caches all requests so that lookups can be performant.}
14
+ spec.homepage = "http://www.joshtoper.co.uk"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_runtime_dependency 'nokogiri', '1.6.8'
28
+ end
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 exchange_rate_jt.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Joshua Toper
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ExchangeRateJt
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/exchange_rate_jt`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'exchange_rate_jt'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install exchange_rate_jt
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JoshToper/exchange_rate_jt.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ exec "irb -r exchange_rate_jt -I ./lib"
10
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exchange_rate_jt"
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__)
data/bin/setup ADDED
@@ -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
Binary file
@@ -0,0 +1,12 @@
1
+ module ExchangeRateJt
2
+ class Configuration
3
+ attr_accessor :data_store, :data_store_type, :source, :default_currency
4
+
5
+ def initialize
6
+ @data_store = nil
7
+ @data_store_type = nil
8
+ @source = nil
9
+ @default_currency = nil
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ require 'pstore'
2
+
3
+ module ExchangeRateJt
4
+ module DataStore
5
+ class InvalidConnectionTypeError < StandardError; end
6
+ class DataNotFoundError < StandardError; end
7
+
8
+ class PStoreAdaptor
9
+ attr_reader :data_store
10
+
11
+ def initialize(data_store)
12
+ unless data_store.is_a?(PStore)
13
+ raise InvalidConnectionTypeError, 'Invalid connection type'
14
+ end
15
+ @data_store = data_store
16
+ end
17
+
18
+ def persist(key, value)
19
+ data_store.transaction do
20
+ data_store[key] = value
21
+ data_store.commit
22
+ end
23
+ end
24
+
25
+ def fetch(key, value)
26
+ data = data_store.transaction { data_store[key][value] }
27
+ raise DataNotFoundError, 'Data not found' if data.nil?
28
+ data
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'data_store/p_store_adaptor'
2
+
3
+ module ExchangeRateJt
4
+ class InvalidDataStoreTypeError < StandardError; end
5
+
6
+ # DataStoreFactory builds and returns an instance of the
7
+ # appropriate data store handler for the defined data store type
8
+ class DataStoreFactory
9
+ MAPS = { pstore: DataStore::PStoreAdaptor }.freeze
10
+
11
+ def self.build(data_store_type, data_store)
12
+ MAPS[data_store_type].new(data_store)
13
+ rescue
14
+ raise InvalidDataStoreTypeError,
15
+ 'Invalid or no data store type specified'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,61 @@
1
+ require 'bigdecimal'
2
+
3
+ module ExchangeRateJt
4
+ class LookupError < StandardError; end
5
+ class UpdateError < StandardError; end
6
+ class InvalidDateError < StandardError; end
7
+ class FutureDateError < StandardError; end
8
+ class NoExchangeRateDataError < StandardError; end
9
+ class RateNotFoundError < StandardError; end
10
+
11
+ class ExchangeRates
12
+ def initialize(configuration)
13
+ @configuration = configuration
14
+ end
15
+
16
+ def update
17
+ rates = RatesSourceFactory.build(configuration.source).fetch_rates
18
+ rates.each { |date, values| data_store.persist(date, values) }
19
+ true
20
+ rescue => exception
21
+ raise UpdateError, exception.message
22
+ end
23
+
24
+ def at(date, base, counter)
25
+ validate_date(date)
26
+ date = date.strftime('%b_%d_%Y').to_sym.downcase
27
+
28
+ base = fetch_rate(date, base)
29
+ counter = fetch_rate(date, counter)
30
+
31
+ rate = counter / base
32
+
33
+ { status: :success, rate: BigDecimal.new(rate, 5) }
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :configuration
39
+
40
+ def data_store
41
+ @data_store ||= build_data_store
42
+ end
43
+
44
+ def build_data_store
45
+ DataStoreFactory
46
+ .build(configuration.data_store_type, configuration.data_store)
47
+ end
48
+
49
+ def fetch_rate(date, currency)
50
+ return 1 if currency.to_s == configuration.default_currency.to_s
51
+ data_store.fetch(date, currency)
52
+ rescue DataStore::DataNotFoundError
53
+ raise RateNotFoundError, 'Exchange rate data not found'
54
+ end
55
+
56
+ def validate_date(date)
57
+ raise InvalidDateError, 'Invalid date specified' unless date.is_a?(Date)
58
+ raise FutureDateError, 'Date cannot be in the future' if date > Date.today
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,53 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ module ExchangeRateJt
5
+ module RatesSource
6
+ class CouldNotFetchDataError < StandardError; end
7
+ class ParseError < StandardError; end
8
+
9
+ class ECB
10
+ REPOSITORY_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'.freeze
11
+
12
+ def fetch_rates
13
+ doc = fetch_source
14
+ parse(doc)
15
+ end
16
+
17
+ private
18
+
19
+ def fetch_source
20
+ Nokogiri::XML(open(REPOSITORY_URL, read_timeout: 5000))
21
+ rescue Timeout::Error
22
+ handle_fetch_error('the request timed out')
23
+ rescue OpenURI::HTTPError => error
24
+ handle_fetch_error(error)
25
+ end
26
+
27
+ def parse(doc)
28
+ daily_updates = doc.xpath('gesmes:Envelope/xmlns:Cube/xmlns:Cube')
29
+ daily_updates.each_with_object({}) do |daily_update, results_hash|
30
+ strtime = daily_update.attribute('time').value
31
+ date = format_date(strtime)
32
+ results_hash[date] = {}
33
+ daily_update.children.each do |rate|
34
+ curr = rate.attribute('currency').value
35
+ results_hash[date][curr] = rate.attribute('rate').value
36
+ end
37
+ end
38
+ rescue
39
+ raise ParseError, 'There was an error parsing the document'
40
+ end
41
+
42
+ def format_date(time_as_string)
43
+ DateTime.parse(time_as_string).strftime('%b_%d_%Y').downcase.to_sym
44
+ end
45
+
46
+ def handle_fetch_error(error)
47
+ response = error.io
48
+ code = response.status[0]
49
+ raise CouldNotFetchDataError, "There was an problem fetching the data from the source - #{code}"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'rates_source/ecb'
2
+
3
+ module ExchangeRateJt
4
+ class RatesSourceFactory
5
+ MAPS = { ecb: RatesSource::ECB }.freeze
6
+
7
+ def self.build(source)
8
+ MAPS[source].new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ExchangeRateJt
2
+ VERSION = "0.1.7"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'exchange_rate_jt/version'
2
+ require 'exchange_rate_jt/configuration'
3
+ require 'exchange_rate_jt/exchange_rates'
4
+ require 'exchange_rate_jt/data_store_factory'
5
+ require 'exchange_rate_jt/rates_source_factory'
6
+
7
+ module ExchangeRateJt
8
+ class << self
9
+ attr_accessor :configuration
10
+ attr_accessor :exchange_rates
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.exchange_rates
18
+ @exchange_rates ||= ExchangeRates.new(configuration)
19
+ end
20
+
21
+ def self.configure
22
+ yield(configuration)
23
+ end
24
+
25
+ def self.update_exchange_rates
26
+ exchange_rates.update
27
+ end
28
+
29
+ def self.at(date, base, counter)
30
+ exchange_rates.at(date, base, counter)
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exchange_rate_jt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Toper
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-01 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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.6.8
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.8
69
+ description: This is a self-contained gem which calculates exchange rates from a variety
70
+ of sources. The gem caches all requests so that lookups can be performant.
71
+ email:
72
+ - work@joshtoper.co.uk
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - ExchangeRateJt.gemspec
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exchange_rate_jt-0.1.0.gem
88
+ - lib/exchange_rate_jt.rb
89
+ - lib/exchange_rate_jt/configuration.rb
90
+ - lib/exchange_rate_jt/data_store/p_store_adaptor.rb
91
+ - lib/exchange_rate_jt/data_store_factory.rb
92
+ - lib/exchange_rate_jt/exchange_rates.rb
93
+ - lib/exchange_rate_jt/rates_source/ecb.rb
94
+ - lib/exchange_rate_jt/rates_source_factory.rb
95
+ - lib/exchange_rate_jt/version.rb
96
+ homepage: http://www.joshtoper.co.uk
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.6.10
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Minimal gem for calculating exchange rates.
120
+ test_files: []