moneyc 1.0.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: 6a639091173a1ccfa70372a5e5e6ed64f785b0cb
4
+ data.tar.gz: 92f2f8dbd94a0362fb9a201dc5f678073102400c
5
+ SHA512:
6
+ metadata.gz: 70d23f8677c62b449d7b5f5a028a955399097413936fe1248090f8e95ae69ddfee9cfab74f387a1c26dbb776b1a09c7b4b6478f3a494e19c42970738815d36f4
7
+ data.tar.gz: e9e83ebb21be17b4df5d4e5b1ae5bc5ebf3326d0ee92c0b48eebf4066f20f1b42257793e600e6dd57986afce5d59fb02ceebe0ce56ce438511104a0aa728f09b
@@ -0,0 +1,40 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
36
+
37
+ ## Editor
38
+ # vim files
39
+ *~
40
+ .*~
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format documentation
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.2.0
5
+ script:
6
+ - bundle exec rspec spec
7
+ addons:
8
+ code_climate:
9
+ repo_token: efcf991d55c57701f5ca053efbba2dd47d79f858169ab22d96e5874b1d240650
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport'
4
+
5
+ group :test do
6
+ gem 'codeclimate-test-reporter', require: nil
7
+ gem 'timecop'
8
+ end
9
+
10
+ gemspec
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ moneyc (1.0.0)
5
+ activesupport (~> 4.0)
6
+ rake (~> 10.4, >= 10.4)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (4.2.4)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
17
+ codeclimate-test-reporter (0.4.7)
18
+ simplecov (>= 0.7.1, < 1.0.0)
19
+ coderay (1.1.0)
20
+ diff-lcs (1.2.5)
21
+ docile (1.1.5)
22
+ i18n (0.7.0)
23
+ json (1.8.3)
24
+ method_source (0.8.2)
25
+ minitest (5.8.1)
26
+ pry (0.10.1)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.8.1)
29
+ slop (~> 3.4)
30
+ rake (10.4.2)
31
+ rspec (3.3.0)
32
+ rspec-core (~> 3.3.0)
33
+ rspec-expectations (~> 3.3.0)
34
+ rspec-mocks (~> 3.3.0)
35
+ rspec-core (3.3.2)
36
+ rspec-support (~> 3.3.0)
37
+ rspec-expectations (3.3.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.3.0)
40
+ rspec-mocks (3.3.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.3.0)
43
+ rspec-support (3.3.0)
44
+ simplecov (0.10.0)
45
+ docile (~> 1.1.0)
46
+ json (~> 1.8)
47
+ simplecov-html (~> 0.10.0)
48
+ simplecov-html (0.10.0)
49
+ slop (3.6.0)
50
+ thread_safe (0.3.5)
51
+ timecop (0.8.0)
52
+ tzinfo (1.2.2)
53
+ thread_safe (~> 0.1)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ activesupport
60
+ bundler (~> 1.7)
61
+ codeclimate-test-reporter
62
+ moneyc!
63
+ pry (~> 0.10)
64
+ rspec (~> 3.1)
65
+ timecop
@@ -0,0 +1,49 @@
1
+ # Currency Converter
2
+
3
+ [![Build Status](https://travis-ci.org/gearnode/moneyc.svg)](https://travis-ci.org/gearnode/moneyc)
4
+ [![Code Climate](https://codeclimate.com/github/gearnode/moneyc/badges/gpa.svg)](https://codeclimate.com/github/gearnode/moneyc)
5
+ [![Test Coverage](https://codeclimate.com/github/gearnode/moneyc/badges/coverage.svg)](https://codeclimate.com/github/gearnode/moneyc/coverage)
6
+ [![Dependency Status](https://gemnasium.com/gearnode/currency_converter.svg)](https://gemnasium.com/gearnode/currency_converter)
7
+ [![Inline docs](http://inch-ci.org/github/gearnode/currency_converter.svg?branch=master)](http://inch-ci.org/github/gearnode/currency_converter)
8
+
9
+ Moneyc is a library that allows you to get the exchange rate and conversion of one currency to another. This library uses the API of the European Central Bank via Fixer.io.
10
+
11
+ Get historical rates for any day since 1999 (2000 January 3).
12
+
13
+ Links:
14
+
15
+ - [Source Code](https://github.com/gearnode/moneyc)
16
+ - [Documentation](http://rubydoc.info/github/gearnode/moneyc/master)
17
+
18
+ ## Installation
19
+
20
+ ``` ruby
21
+ gem 'moneyc'
22
+ ```
23
+ or
24
+ ``` shell
25
+ gem install moneyc
26
+ ```
27
+
28
+
29
+ ## Use it
30
+
31
+ ``` ruby
32
+ require 'moneyc'
33
+
34
+ # Convert 7.55 EUR to USD
35
+ Moneyc::Converter.new(money: 7.55, from: 'EUR', to: 'USD').convert
36
+ # => Float
37
+
38
+ # Convert with specific date 7.55 EUR to USD
39
+ Moneyc::Converter.new(money: 7.55, from: 'EUR', to: 'USD', at: Time.now).convert
40
+ # => Float
41
+
42
+ # Get today rate (EUR to USD)
43
+ Moneyc::Converter.new(from: 'EUR', to: 'USD').rate
44
+ # => Integer or Float
45
+
46
+ # Get specific date rate (EUR to USD)
47
+ Moneyc::Converter.new(from: 'EUR', to: 'USD', at: Time.now).rate
48
+ # => Integer or Float
49
+ ```
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
3
+ task :default => :spec
4
+
5
+ Dir.glob('tasks/*.rake').each { |r| load r}
@@ -0,0 +1,17 @@
1
+ require 'net/http'
2
+ require 'date'
3
+ require 'json'
4
+ require 'active_support/all'
5
+
6
+ module Moneyc
7
+ AVAILABLE_CURRENCY = [ :AUD, :BGN, :BRL, :CAD, :CHF,
8
+ :CNY, :CZK, :DKK, :GBP, :HKD,
9
+ :HRK, :HUF, :IDR, :ILS, :INR,
10
+ :JPY, :KRW, :MXN, :MYR, :NOK,
11
+ :NZD, :PHP, :PLN, :RON, :RUB,
12
+ :SEK, :SGD, :THB, :TRY, :USD,
13
+ :ZAR, :EUR ]
14
+
15
+ autoload :FixerHTTPClient, 'moneyc/fixer_http_client'
16
+ autoload :Converter, 'moneyc/converter'
17
+ end
@@ -0,0 +1,46 @@
1
+ module Moneyc
2
+ class Converter
3
+
4
+ def initialize(options = {})
5
+ @money_in_reference_currency = options[:money]
6
+ @reference_currency = options.fetch(:from)
7
+ @target_currency = options[:to].present? ? options[:to] : @reference_currency
8
+ @conversion_date = options[:at].present? ? options[:at] : Time.now
9
+ end
10
+
11
+ def rate
12
+ reference_and_target_is_same? ? 1 : retrieve_taget_currency_rate
13
+ end
14
+
15
+ def convert
16
+ if @money_in_reference_currency.present?
17
+ @money_in_reference_currency.to_f * rate
18
+ else
19
+ raise ConverterError.new("You need enter an value to convert")
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def reference_and_target_is_same?
26
+ @reference_currency == @target_currency
27
+ end
28
+
29
+ def retrieve_taget_currency_rate
30
+ if http_response.present? && http_response[@target_currency.to_sym].present?
31
+ http_response[@target_currency.to_sym]
32
+ else
33
+ raise ConverterError.new("#{@target_currency} or #{@reference_currency} is not available currency")
34
+ end
35
+ end
36
+
37
+ def http_response
38
+ FixerHTTPClient.new(
39
+ conversion_date: @conversion_date,
40
+ reference_currency: @reference_currency
41
+ ).fetch_currency_rate
42
+ end
43
+
44
+ class ConverterError < StandardError; end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ module Moneyc
2
+ class FixerHTTPClient
3
+ API_PROTOCOL = 'http://'
4
+ BASE_API_URL = 'api.fixer.io'
5
+
6
+ def initialize(conversion_date:, reference_currency:)
7
+ @conversion_date = conversion_date.strftime('%Y-%m-%d')
8
+ @reference_currency = reference_currency.to_s
9
+ end
10
+
11
+ def fetch_currency_rate
12
+ construct_uri
13
+ retrieve_rate
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :uri
19
+
20
+ def construct_uri
21
+ @uri = URI("#{API_PROTOCOL}#{BASE_API_URL}/#{@conversion_date}/?base=#{@reference_currency}")
22
+ end
23
+
24
+ def retrieve_rate
25
+ JSON.parse(call_api, { symbolize_names: true})[:rates]
26
+ end
27
+
28
+ def call_api
29
+ http_client.get(uri)
30
+ end
31
+
32
+ def http_client
33
+ Net::HTTP
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Moneyc
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'moneyc/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "moneyc"
7
+ gem.version = Moneyc::VERSION
8
+ gem.authors = ["Bryan FRIMIN"]
9
+ gem.email = ["bfrimin@student.42.fr"]
10
+ gem.description = %q{Convert your currency with simple lib}
11
+ gem.summary = %q{Convert et retrive rate of your currency with simple lib}
12
+ gem.homepage = "https://github.com/gearnode/moneyc"
13
+ gem.license = "MIT"
14
+
15
+ gem.required_ruby_version = '>= 2.2'
16
+ gem.required_rubygems_version = '>= 1.8.11'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(spec)/})
21
+ gem.require_paths = ["lib"]
22
+
23
+ gem.add_runtime_dependency 'rake', '~> 10.4', '>= 10.4'
24
+
25
+ gem.add_development_dependency 'bundler', '~> 1.7'
26
+ gem.add_development_dependency 'rspec', '~> 3.1'
27
+ gem.add_development_dependency 'pry', '~> 0.10'
28
+ gem.add_dependency 'activesupport', '~> 4.0'
29
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe Moneyc::Converter do
4
+
5
+ describe '#rate' do
6
+ subject{ described_class.new(from: reference_currency, to: target_currency, at: date).rate }
7
+
8
+ let(:date){ Time.new('2015', '8', '20') }
9
+
10
+ context 'when reference and target currency is same' do
11
+ let(:reference_currency){ 'USD' }
12
+ let(:target_currency){ 'USD' }
13
+
14
+ it 'returns 1' do
15
+ expect(subject).to eq(1)
16
+ end
17
+ end
18
+
19
+ context 'when reference and target currency is not same' do
20
+ let(:reference_currency){ 'USD' }
21
+ let(:target_currency){ 'EUR' }
22
+
23
+ it 'returns 0.89421' do
24
+ expect(subject).to eq(0.89421)
25
+ end
26
+ end
27
+
28
+ context 'when target currency is not present' do
29
+ let(:reference_currency){ 'USD' }
30
+ let(:target_currency){ '' }
31
+
32
+ it 'returns 1' do
33
+ expect(subject).to eq(1)
34
+ end
35
+ end
36
+
37
+ context 'when conversion date is not present' do
38
+ let(:reference_currency){ 'USD' }
39
+ let(:target_currency){ 'EUR' }
40
+ let(:date){ nil }
41
+
42
+ before do
43
+ Timecop.freeze(Time.new('2015', '8', '20'))
44
+ end
45
+
46
+ it 'returns rate at now date' do
47
+ expect(subject).to eq(0.89421)
48
+ end
49
+
50
+ after do
51
+ Timecop.return
52
+ end
53
+ end
54
+
55
+ context 'when reference or target currency is not available' do
56
+ let(:reference_currency){ 'USD' }
57
+ let(:target_currency){ 'ISNOTACURRENCY' }
58
+
59
+ it 'raises a error' do
60
+ expect{ subject }.to raise_error(Moneyc::Converter::ConverterError)
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '#convert' do
66
+ subject{ described_class.new(money: 7, from: reference_currency, to: target_currency, at: date).convert }
67
+
68
+ let(:date){ Time.new('2015', '8', '20') }
69
+
70
+ context 'when reference and target is same' do
71
+ let(:reference_currency){ 'USD' }
72
+ let(:target_currency){ 'USD' }
73
+
74
+ it 'returns 7' do
75
+ expect(subject).to eq(7)
76
+ end
77
+ end
78
+
79
+ context 'when reference and target currency is not same' do
80
+ let(:reference_currency){ 'USD' }
81
+ let(:target_currency){ 'EUR' }
82
+
83
+ it 'returns rate * total in target reference at 2015-8-20' do
84
+ expect(subject).to eq(6.259469999999999)
85
+ end
86
+ end
87
+
88
+ context 'when target currency is not present' do
89
+ let(:reference_currency){ 'USD' }
90
+ let(:target_currency){ '' }
91
+
92
+ it 'returns total in reference currency' do
93
+ expect(subject).to eq(7)
94
+ end
95
+ end
96
+
97
+ context 'when conversion date is not present' do
98
+ let(:reference_currency){ 'USD' }
99
+ let(:target_currency){ 'EUR' }
100
+ let(:date){ nil }
101
+
102
+ before do
103
+ Timecop.freeze(Time.new('2015', '8', '20'))
104
+ end
105
+
106
+ it 'return rate * total in target currency at now date' do
107
+ expect(subject).to eq(6.259469999999999)
108
+ end
109
+
110
+ after do
111
+ Timecop.return
112
+ end
113
+ end
114
+
115
+ context 'when reference or target currency is not available' do
116
+ let(:reference_currency){ 'ISNOTACURRENCY' }
117
+ let(:target_currency){ 'USD' }
118
+
119
+ it 'raises error' do
120
+ expect{ subject }.to raise_error(Moneyc::Converter::ConverterError)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Moneyc::FixerHTTPClient do
4
+
5
+ describe '#fetch_currency_rate' do
6
+ subject{ described_class.new(conversion_date: conversion_date, reference_currency: reference_currency).fetch_currency_rate }
7
+
8
+ let(:conversion_date){ Time.new('2015', '10', '24') }
9
+
10
+ context 'when reference_currency is available' do
11
+ let(:reference_currency){ :USD }
12
+
13
+ it 'returns hash' do
14
+ expect(subject).to be_a(Hash)
15
+ end
16
+
17
+ it 'return hash with eur' do
18
+ expect(subject[:EUR]).not_to be_nil
19
+ end
20
+ end
21
+
22
+ context 'when reference_currency is not available' do
23
+ let(:reference_currency){ :WDFGYUJI }
24
+
25
+ it 'returns nil' do
26
+ expect(subject).to be_nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'timecop'
5
+ require 'moneyc'
6
+
7
+ RSpec.configure do |config|
8
+
9
+ config.expect_with :rspec do |expectations|
10
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
11
+ end
12
+
13
+ config.mock_with :rspec do |mocks|
14
+ mocks.verify_partial_doubles = true
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ desc 'Open an irb session preloaded with this library'
2
+ task :console do
3
+ sh 'irb -rubygems -I lib -r moneyc.rb'
4
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moneyc
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bryan FRIMIN
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '10.4'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '10.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '10.4'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.1'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.1'
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.10'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.10'
75
+ - !ruby/object:Gem::Dependency
76
+ name: activesupport
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '4.0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '4.0'
89
+ description: Convert your currency with simple lib
90
+ email:
91
+ - bfrimin@student.42.fr
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".travis.yml"
99
+ - Gemfile
100
+ - Gemfile.lock
101
+ - README.md
102
+ - Rakefile
103
+ - lib/moneyc.rb
104
+ - lib/moneyc/converter.rb
105
+ - lib/moneyc/fixer_http_client.rb
106
+ - lib/moneyc/version.rb
107
+ - moneyc.gemspec
108
+ - spec/lib/money/converter_spec.rb
109
+ - spec/lib/money/fixer_http_client_spec.rb
110
+ - spec/spec_helper.rb
111
+ - tasks/debug.rake
112
+ homepage: https://github.com/gearnode/moneyc
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '2.2'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: 1.8.11
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.7
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Convert et retrive rate of your currency with simple lib
136
+ test_files:
137
+ - spec/lib/money/converter_spec.rb
138
+ - spec/lib/money/fixer_http_client_spec.rb
139
+ - spec/spec_helper.rb