money-fixer-io 0.0.0

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: d81fd4bcd190187d09ae4dfb34353f2085ac5248
4
+ data.tar.gz: b62fb58ab92382d54fd48507ea52df941ae82db6
5
+ SHA512:
6
+ metadata.gz: fcdb71085d4d6bc9e0ae99629180a56930bae9dd5df43b39c926c2dcd40382cf5f419a2e1196b5d1397e4ecae72cf98087a96bd31d9c4601d4fc0ea034918b0e
7
+ data.tar.gz: 290d4f2f43125aebed80691728bd7433c7e1faace82c758b97d3f6dfe4e54ab61651795ffd7cbb4c136b1fef9e32ac9cf0efc2526644f1b6a7678f41f552bc3c
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,83 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ Exclude:
5
+ - db/schema.rb
6
+ - spec/dummy/db/schema.rb
7
+
8
+ # https://github.com/AtomLinter/linter-rubocop/issues/2
9
+ Style/FileName:
10
+ Enabled: false
11
+
12
+ Metrics/CyclomaticComplexity:
13
+ Max: 10
14
+
15
+ Metrics/LineLength:
16
+ Max: 160
17
+
18
+ Metrics/MethodLength:
19
+ Max: 50
20
+
21
+ Metrics/AbcSize:
22
+ Max: 25
23
+
24
+ Metrics/ClassLength:
25
+ Max: 250
26
+
27
+ # .find_each is not the same as .each
28
+ Rails/FindEach:
29
+ Enabled: false
30
+
31
+ Style/AccessModifierIndentation:
32
+ EnforcedStyle: outdent
33
+
34
+ Style/AlignParameters:
35
+ EnforcedStyle: with_fixed_indentation
36
+
37
+ Style/ClassAndModuleChildren:
38
+ EnforcedStyle: compact
39
+
40
+ Style/ConditionalAssignment:
41
+ Enabled: false
42
+
43
+ Style/Documentation:
44
+ Enabled: false
45
+
46
+ Style/EmptyLines:
47
+ Enabled: false
48
+
49
+ # Will report offences for many places that are much more readable without using a guard clause
50
+ Style/GuardClause:
51
+ Enabled: false
52
+
53
+ Style/MultilineMethodCallIndentation:
54
+ EnforcedStyle: indented
55
+
56
+ Style/MultilineOperationIndentation:
57
+ EnforcedStyle: indented
58
+
59
+ Style/StringLiterals:
60
+ EnforcedStyle: double_quotes
61
+
62
+ Style/StringLiteralsInInterpolation:
63
+ Enabled: false
64
+
65
+ Style/NilComparison:
66
+ Enabled: false
67
+
68
+ Style/SignalException:
69
+ EnforcedStyle: only_raise
70
+
71
+ Style/MultilineOperationIndentation:
72
+ EnforcedStyle: indented
73
+
74
+ Style/SpaceInsideHashLiteralBraces:
75
+ EnforcedStyle: no_space
76
+
77
+ Style/TrivialAccessors:
78
+ ExactNameMatch: true
79
+ Enabled: true
80
+
81
+ # Disabled on purpose: https://github.com/bbatsov/rubocop/issues/1758
82
+ Style/ClosingParenthesisIndentation:
83
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "https://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "2.1.1"
13
+ gem "simplecov", ">= 0"
14
+ gem "rubocop", "0.43.0"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.4.0)
5
+ ast (2.3.0)
6
+ builder (3.2.2)
7
+ descendants_tracker (0.0.4)
8
+ thread_safe (~> 0.3, >= 0.3.1)
9
+ diff-lcs (1.1.3)
10
+ docile (1.1.5)
11
+ faraday (0.9.2)
12
+ multipart-post (>= 1.2, < 3)
13
+ git (1.3.0)
14
+ github_api (0.14.5)
15
+ addressable (~> 2.4.0)
16
+ descendants_tracker (~> 0.0.4)
17
+ faraday (~> 0.8, < 0.10)
18
+ hashie (>= 3.4)
19
+ oauth2 (~> 1.0)
20
+ hashie (3.4.6)
21
+ highline (1.7.8)
22
+ jeweler (2.1.1)
23
+ builder
24
+ bundler (>= 1.0)
25
+ git (>= 1.2.5)
26
+ github_api
27
+ highline (>= 1.6.15)
28
+ nokogiri (>= 1.5.10)
29
+ rake
30
+ rdoc
31
+ semver
32
+ json (1.8.3)
33
+ jwt (1.5.6)
34
+ mini_portile2 (2.1.0)
35
+ multi_json (1.12.1)
36
+ multi_xml (0.5.5)
37
+ multipart-post (2.0.0)
38
+ nokogiri (1.6.8)
39
+ mini_portile2 (~> 2.1.0)
40
+ pkg-config (~> 1.1.7)
41
+ oauth2 (1.2.0)
42
+ faraday (>= 0.8, < 0.10)
43
+ jwt (~> 1.0)
44
+ multi_json (~> 1.3)
45
+ multi_xml (~> 0.5)
46
+ rack (>= 1.2, < 3)
47
+ parser (2.3.1.4)
48
+ ast (~> 2.2)
49
+ pkg-config (1.1.7)
50
+ powerpack (0.1.1)
51
+ rack (2.0.1)
52
+ rainbow (2.1.0)
53
+ rake (11.3.0)
54
+ rdoc (3.12.2)
55
+ json (~> 1.4)
56
+ rspec (2.8.0)
57
+ rspec-core (~> 2.8.0)
58
+ rspec-expectations (~> 2.8.0)
59
+ rspec-mocks (~> 2.8.0)
60
+ rspec-core (2.8.0)
61
+ rspec-expectations (2.8.0)
62
+ diff-lcs (~> 1.1.2)
63
+ rspec-mocks (2.8.0)
64
+ rubocop (0.43.0)
65
+ parser (>= 2.3.1.1, < 3.0)
66
+ powerpack (~> 0.1)
67
+ rainbow (>= 1.99.1, < 3.0)
68
+ ruby-progressbar (~> 1.7)
69
+ unicode-display_width (~> 1.0, >= 1.0.1)
70
+ ruby-progressbar (1.8.1)
71
+ semver (1.0.1)
72
+ simplecov (0.12.0)
73
+ docile (~> 1.1.0)
74
+ json (>= 1.8, < 3)
75
+ simplecov-html (~> 0.10.0)
76
+ simplecov-html (0.10.0)
77
+ thread_safe (0.3.5)
78
+ unicode-display_width (1.1.1)
79
+
80
+ PLATFORMS
81
+ ruby
82
+
83
+ DEPENDENCIES
84
+ bundler (~> 1.0)
85
+ jeweler (= 2.1.1)
86
+ rdoc (~> 3.12)
87
+ rspec (~> 2.8.0)
88
+ rubocop (= 0.43.0)
89
+ simplecov
90
+
91
+ BUNDLED WITH
92
+ 1.12.5
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 kaspernj
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = money-fixer-io
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to money-fixer-io
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2016 kaspernj. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require "rubygems"
4
+ require "bundler"
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require "rake"
13
+
14
+ require "jeweler"
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "money-fixer-io"
18
+ gem.homepage = "http://github.com/kaspernj/money-fixer-io"
19
+ gem.license = "MIT"
20
+ gem.summary = %(fixer.io support for money)
21
+ gem.description = %(fixer.io support for money)
22
+ gem.email = "k@spernj.org"
23
+ gem.authors = ["kaspernj"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require "rspec/core"
29
+ require "rspec/core/rake_task"
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList["spec/**/*_spec.rb"]
32
+ end
33
+
34
+ desc "Code coverage detail"
35
+ task :simplecov do
36
+ ENV["COVERAGE"] = "true"
37
+ Rake::Task["spec"].execute
38
+ end
39
+
40
+ task default: :spec
41
+
42
+ require "rdoc/task"
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?("VERSION") ? File.read("VERSION") : ""
45
+
46
+ rdoc.rdoc_dir = "rdoc"
47
+ rdoc.title = "money-fixer-io #{version}"
48
+ rdoc.rdoc_files.include("README*")
49
+ rdoc.rdoc_files.include("lib/**/*.rb")
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,148 @@
1
+ require "money"
2
+ require "money/rates_store/rate_removal_support"
3
+ require "open-uri"
4
+
5
+ class Money::Bank::FixerIo < Money::Bank::VariableExchange
6
+ SERVICE_HOST = "api.fixer.io".freeze
7
+ SERVICE_PATH = "/".freeze
8
+
9
+ # @return [Hash] Stores the currently known rates.
10
+ attr_reader :rates
11
+
12
+ class << self
13
+ # @return [Integer] Returns the Time To Live (TTL) in seconds.
14
+ attr_reader :ttl_in_seconds
15
+
16
+ # @return [Time] Returns the time when the rates expire.
17
+ attr_reader :rates_expiration
18
+
19
+ ##
20
+ # Set the Time To Live (TTL) in seconds.
21
+ #
22
+ # @param [Integer] the seconds between an expiration and another.
23
+ def ttl_in_seconds=(value)
24
+ @ttl_in_seconds = value
25
+ refresh_rates_expiration! if ttl_in_seconds
26
+ end
27
+
28
+ ##
29
+ # Set the rates expiration TTL seconds from the current time.
30
+ #
31
+ # @return [Time] The next expiration.
32
+ def refresh_rates_expiration!
33
+ @rates_expiration = Time.now + ttl_in_seconds
34
+ end
35
+ end
36
+
37
+ def initialize(*)
38
+ super
39
+ @store.extend Money::RatesStore::RateRemovalSupport
40
+ end
41
+
42
+ ##
43
+ # Clears all rates stored in @rates
44
+ #
45
+ # @return [Hash] The empty @rates Hash.
46
+ #
47
+ # @example
48
+ # @bank = GoogleCurrency.new #=> <Money::Bank::GoogleCurrency...>
49
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
50
+ # @bank.flush_rates #=> {}
51
+ def flush_rates
52
+ store.clear_rates
53
+ end
54
+
55
+ ##
56
+ # Clears the specified rate stored in @rates.
57
+ #
58
+ # @param [String, Symbol, Currency] from Currency to convert from (used
59
+ # for key into @rates).
60
+ # @param [String, Symbol, Currency] to Currency to convert to (used for
61
+ # key into @rates).
62
+ #
63
+ # @return [Float] The flushed rate.
64
+ #
65
+ # @example
66
+ # @bank = GoogleCurrency.new #=> <Money::Bank::GoogleCurrency...>
67
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
68
+ # @bank.flush_rate(:USD, :EUR) #=> 0.776337241
69
+ def flush_rate(from, to)
70
+ store.remove_rate(from, to)
71
+ end
72
+
73
+ ##
74
+ # Returns the requested rate.
75
+ #
76
+ # It also flushes all the rates when and if they are expired.
77
+ #
78
+ # @param [String, Symbol, Currency] from Currency to convert from
79
+ # @param [String, Symbol, Currency] to Currency to convert to
80
+ #
81
+ # @return [Float] The requested rate.
82
+ #
83
+ # @example
84
+ # @bank = GoogleCurrency.new #=> <Money::Bank::GoogleCurrency...>
85
+ # @bank.get_rate(:USD, :EUR) #=> 0.776337241
86
+ def get_rate(from, to, args = {})
87
+ expire_rates
88
+
89
+ if args[:exchanged_at]
90
+ fetch_rate(from, to, exchanged_at: args.fetch(:exchanged_at))
91
+ raise "stub"
92
+ else
93
+ store.get_rate(from, to) || store.add_rate(from, to, fetch_rate(from, to))
94
+ end
95
+ end
96
+
97
+ ##
98
+ # Flushes all the rates if they are expired.
99
+ #
100
+ # @return [Boolean]
101
+ def expire_rates
102
+ if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now
103
+ flush_rates
104
+ self.class.refresh_rates_expiration!
105
+ true
106
+ else
107
+ false
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ ##
114
+ # Queries for the requested rate and returns it.
115
+ #
116
+ # @param [String, Symbol, Currency] from Currency to convert from
117
+ # @param [String, Symbol, Currency] to Currency to convert to
118
+ #
119
+ # @return [BigDecimal] The requested rate.
120
+ def fetch_rate(from, to, args = {})
121
+ from = Currency.wrap(from)
122
+ to = Currency.wrap(to)
123
+
124
+ data = JSON.parse(build_uri(from, to, args).read)
125
+ rate = data.fetch("rates").fetch(to)
126
+ rate = 1 / extract_rate(build_uri(to, from).read) if rate < 0.1
127
+ rate
128
+ end
129
+
130
+ ##
131
+ # Build a URI for the given arguments.
132
+ #
133
+ # @param [Currency] from The currency to convert from.
134
+ # @param [Currency] to The currency to convert to.
135
+ #
136
+ # @return [URI::HTTP]
137
+ def build_uri(from, _to, _args = {})
138
+ query = "base=#{from.iso_code}"
139
+ query << "&date=#{args.fetch(:exchange_at).strftime("%Y-%m-%D")}"
140
+
141
+ uri = URI::HTTP.build(
142
+ host: SERVICE_HOST,
143
+ path: SERVICE_PATH,
144
+ query: query
145
+ )
146
+ uri
147
+ end
148
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe "MoneyFixerIo" do
4
+ it "fails" do
5
+ raise "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require "simplecov"
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_adapter "test_frameworks"
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require "rspec"
21
+ require "money-fixer-io"
22
+
23
+ # Requires supporting files with custom matchers and macros, etc,
24
+ # in ./support/ and its subdirectories.
25
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
26
+
27
+ RSpec.configure do |config|
28
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money-fixer-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kaspernj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.8.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.43.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.43.0
97
+ description: fixer.io support for money
98
+ email: k@spernj.org
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ files:
105
+ - ".document"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - lib/money-fixer-io.rb
115
+ - spec/money-fixer-io_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: http://github.com/kaspernj/money-fixer-io
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.5.1
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: fixer.io support for money
141
+ test_files: []