light_currency_convert 0.1

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: f2f09610bdc212f9e394ddf3664cdd5dd6a83ec8
4
+ data.tar.gz: 76409ef745cba6106e6d6dce6bf452bb813e1746
5
+ SHA512:
6
+ metadata.gz: 1621895e011c30fce1e7fa1024d849b40f9e5414cacb064f9365069d981873f795c76d61a1230003a2cd0aa5a4240a7d7c744ef5c65384511e1309552a7265ef
7
+ data.tar.gz: 1787219d94e71c2380bfbb1c3494dadd91852fd87366b027ebe7fc035685e8550d17c282a75e0e9f372df95a098f7906a2c4b72df34081ffe1a865fc82ccca46
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at t0ma.popov.90@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in light_currency_convert.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 toma
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,93 @@
1
+ # LightCurrencyConvert
2
+
3
+ LightCurrency convert is a gem for currency convertion by given a currency course list.
4
+
5
+ ## Install
6
+ Install the gem throught the gem command:
7
+ ````bash
8
+ gem install light_currency_convert
9
+ ````
10
+ Or add to a Gemfile:
11
+ ````ruby
12
+ gem light_currency_convert
13
+ ````
14
+ and run bundler:
15
+ ````bash
16
+ bundle install
17
+ ````
18
+
19
+ ## Usage
20
+ ### Command line
21
+ Use can use the command line tool to convert one cyrrency to an other by giving the amount, froml_currency, to_currency to the command line tool:
22
+
23
+ ````bash
24
+ convert_currency 100 USD BGN
25
+ ````
26
+ And you will get the following output:
27
+ ````bash
28
+ 173.49
29
+ ````
30
+ If only two arguments are given the program will output the course between the currencies.
31
+
32
+ ````bash
33
+ convert_currency USD BGN
34
+ 1.0 USD => 1.73488 BGN
35
+ ````
36
+
37
+ The program uses the following default currency courses(that could not be up to date):
38
+
39
+ ````json
40
+ {
41
+ "1 GBP" : "2.45986 BGN",
42
+ "1 EUR" : "1.95462 BGN",
43
+ "1 USD" : "1.73488 BGN",
44
+ "1 TRY" : "0.59272 BGN",
45
+ "1 CAD" : "1.33794 BGN",
46
+ "1 CHF" : "1.80742 BGN",
47
+ "1 RUB" : "0.02634 BGN",
48
+ "1 AUD" : "1.27747 BGN",
49
+ "1 DKK" : "0.26287 BGN",
50
+ "100 CZK" : "7.24 BGN"
51
+ }
52
+ ````
53
+
54
+ To supply yor own your own currency file by using the -s flag.
55
+
56
+ ````bash
57
+ convert_currency 1 USD BGN -s ./currencies.json
58
+ ````
59
+
60
+ The json file must be formated as show in the example json.
61
+
62
+ ### In a ruby project
63
+
64
+ First require the gem.
65
+ ````ruby
66
+ require 'light_currency_convert'
67
+ ````
68
+ Create a currencies collection object:
69
+ ````ruby
70
+ collection = LightCurrencyConverter::CurrencyCollection.new(json_string)
71
+ ````
72
+ Create a currencies processor object and run the process method:
73
+ ````ruby
74
+ processor = LightCurrencyConverter::Processor.new('USD', 'BGN', 100, collection)
75
+ processor.convert
76
+ => 173.49
77
+ ````
78
+
79
+ ## Development
80
+
81
+ 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.
82
+
83
+ 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).
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tmpv/light_currency_convert. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
88
+
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
93
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "light_currency_convert"
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
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "light_currency_convert"
5
+ require 'optparse'
6
+
7
+ options = {}
8
+ OptionParser.new do |opts|
9
+ opts.banner = <<-EOT
10
+ Usage: [OPTIONS] AMOUNT FROM TO
11
+ or: [OPTIONS] FROM TO
12
+ Currency convert toll.
13
+ When an AMOUN FROM and TO are given outputs a new amount in TO currency.
14
+ When only FROM and TO are given outputs the course between the currencies.
15
+ EOT
16
+
17
+ opts.on('-s', '--sourcefile FILE', 'Source file for currencies if non is given a default currency list is used') { |v| options[:source_name] = v }
18
+ end.parse!
19
+
20
+ if ARGV.count == 2
21
+ from, to = *ARGV
22
+ elsif ARGV.count == 3
23
+ amount, from, to = *ARGV
24
+ else
25
+ puts 'No valid arguments'
26
+ end
27
+
28
+ default_json = '{ "1 GBP" : "2.45986 BGN", "1 EUR" : "1.95462 BGN", "1 USD" : "1.73488 BGN", "1 TRY" : "0.59272 BGN", "1 CAD" : "1.33794 BGN", "1 CHF" : "1.80742 BGN", "1 RUB" : "0.02634 BGN", "1 AUD" : "1.27747 BGN", "1 DKK" : "0.26287 BGN", "100 CZK" : "7.24 BGN" }'
29
+
30
+ source_str = options[:sourcefile] ? File.read(options[:sourcefile]) : default_json
31
+ currency_collection = LightCurrencyConverter::CurrencyCollection.new(source_str)
32
+ processor = LightCurrencyConverter::Processor.new(from, to, amount, currency_collection)
33
+
34
+ result = processor.process
35
+
36
+ puts result
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
@@ -0,0 +1,53 @@
1
+ require 'active_support'
2
+ module LightCurrencyConverter
3
+ class CurrencyCollection
4
+ include Enumerable
5
+
6
+ attr_accessor :currencies, :graph
7
+
8
+ def initialize(json_string)
9
+ json_entries = ::ActiveSupport::JSON.decode(json_string)
10
+ @currencies = json_entries.to_a.map { |e| CurrencyCourse.new(*e) }
11
+ @graph = Graph.new
12
+ populate_graph
13
+ end
14
+
15
+ def each
16
+ @currencies.each { |e| yield(e) }
17
+ end
18
+
19
+ def [](*args)
20
+ @currencies[*args]
21
+ end
22
+
23
+ def find_currency_course(from, to)
24
+ simple_find(from, to) || graph_find(from, to)
25
+ end
26
+
27
+ def base_find(currency_one, currency_two)
28
+ detect { |e| e.from.currency == currency_one && e.to.currency == currency_two }
29
+ end
30
+
31
+ def simple_find(from, to)
32
+ base_find(from, to) || base_find(to, from)
33
+ end
34
+
35
+ def graph_find(from, to)
36
+ graph.search_path(from, to, [], results = [])
37
+ return nil if results.empty?
38
+
39
+ result = graph.minimize_result(results)
40
+ full_path = [result[0], *(result[1..-2].map{|x| [x, x] }.flatten), result[-1]].each_slice(2)
41
+ courses = full_path.map { |x| simple_find(*x) }
42
+ IndirectCurrencyCourse.new(courses, full_path)
43
+ end
44
+
45
+ private
46
+
47
+ def populate_graph
48
+ each do |currency_entry|
49
+ @graph.add_edge(currency_entry.from.currency, currency_entry.to.currency)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ module LightCurrencyConverter
2
+ class CurrencyCourse
3
+ attr_accessor :from, :to
4
+
5
+ def initialize(from, to)
6
+ @from = CurrencyEntry.new(*from.split)
7
+ @to = CurrencyEntry.new(*to.split)
8
+ end
9
+
10
+ def convert(amount, from, to)
11
+ if from == self.from.currency && to == self.to.currency
12
+ normal_convert(amount)
13
+ elsif to == self.from.currency && from == self.to.currency
14
+ invers_convert(amount)
15
+ else
16
+ raise ArgumentError, 'No match for the currencies'
17
+ end
18
+ end
19
+
20
+ def normal_convert(amount)
21
+ ((amount.to_f / from.value.to_f) * to.value.to_f).round(2)
22
+ end
23
+
24
+ def invers_convert(amount)
25
+ ((amount.to_f / to.value.to_f) * from.value.to_f).round(2)
26
+ end
27
+
28
+ def to_s
29
+ "#{from.value} #{from.currency} => #{to.value} #{to.currency}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ module LightCurrencyConverter
2
+ class CurrencyEntry
3
+ attr_accessor :value, :currency
4
+
5
+ def initialize(value, currency)
6
+ @value = value.to_f
7
+ @currency = currency
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ module LightCurrencyConverter
2
+ class Graph
3
+ attr_accessor :graph
4
+
5
+ def initialize(&block)
6
+ @graph = Hash.new { |hash, key| hash[key] = [] }
7
+ yield self if block_given?
8
+ end
9
+
10
+ def add_edge(a, b)
11
+ @graph[a] << b
12
+ @graph[b] << a
13
+ end
14
+
15
+ def minimize_result(results)
16
+ results.reduce(Array.new(1000)) do |end_array, element|
17
+ (element.size < end_array.size) ? element : end_array
18
+ end
19
+ end
20
+
21
+ def search_path(a, b, result, lr)
22
+ result += [a]
23
+ lr << result if a == b
24
+ @graph[a].each do |v|
25
+ search_path(v, b, result, lr) unless result.include?(v)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ module LightCurrencyConverter
2
+ class IndirectCurrencyCourse
3
+ attr_accessor :courses, :path
4
+
5
+ def initialize(courses, path)
6
+ @path = path
7
+ @courses = courses
8
+ end
9
+
10
+ def convert(amount, *args)
11
+ @courses.zip(path).reduce(amount) do |result, course_with_convert_couple|
12
+ course_with_convert_couple[0].convert(result, *course_with_convert_couple[1])
13
+ end
14
+ end
15
+
16
+ def to_s
17
+ flat_path = path.to_a.flatten
18
+ to_value = convert(1, flat_path[0], flat_path[-1])
19
+ "1 #{flat_path[0]} => #{to_value} #{flat_path[-1]}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ module LightCurrencyConverter
2
+ class Processor
3
+ attr_accessor :from, :to, :amount, :currencies, :course
4
+
5
+ def initialize(from, to, amount, currecnies)
6
+ @from = from
7
+ @to = to
8
+ @amount = amount
9
+ @currecnies = currecnies
10
+ @course = currecnies.find_currency_course(from, to)
11
+ end
12
+
13
+ def process
14
+ amount.nil? ? show_course : convert
15
+ rescue NoCourseFundError
16
+ 'No course correlation has been found.'
17
+ end
18
+
19
+ def convert
20
+ raise NoCourseFundError unless @course
21
+ @course.convert(amount, from, to)
22
+ end
23
+
24
+ def show_course
25
+ raise NoCourseFundError unless @course
26
+ @course.to_s
27
+ end
28
+ end
29
+ end
30
+
31
+ class NoCourseFundError < Exception; end
@@ -0,0 +1,3 @@
1
+ module LightCurrencyConvert
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'light_currency_convert/version'
2
+ require 'light_currency_convert/currency_entity'
3
+ require 'light_currency_convert/currency_course'
4
+ require 'light_currency_convert/indirect_currency_course'
5
+ require 'light_currency_convert/currency_collection'
6
+ require 'light_currency_convert/graph'
7
+ require 'light_currency_convert/processor'
8
+
9
+ module LightCurrencyConvert
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'light_currency_convert/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "light_currency_convert"
8
+ spec.version = LightCurrencyConvert::VERSION
9
+ spec.authors = ["toma"]
10
+ spec.email = ["t0ma.popov.90@gmail.com"]
11
+
12
+ spec.summary = %q{This is a little gem for currency conversion. }
13
+ spec.description = %q{The gem adds a simple command line tool for currency conversion by given currencies in a json file.}
14
+ spec.homepage = "https://github.com/Tmpv/light_currency_converter"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = ['convert_currency']
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "activesupport", "~> 4.2.5"
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: light_currency_convert
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - toma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-16 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.2.5
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.2.5
69
+ description: The gem adds a simple command line tool for currency conversion by given
70
+ currencies in a json file.
71
+ email:
72
+ - t0ma.popov.90@gmail.com
73
+ executables:
74
+ - convert_currency
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/convert_currency
88
+ - bin/setup
89
+ - lib/light_currency_convert.rb
90
+ - lib/light_currency_convert/currency_collection.rb
91
+ - lib/light_currency_convert/currency_course.rb
92
+ - lib/light_currency_convert/currency_entity.rb
93
+ - lib/light_currency_convert/graph.rb
94
+ - lib/light_currency_convert/indirect_currency_course.rb
95
+ - lib/light_currency_convert/processor.rb
96
+ - lib/light_currency_convert/version.rb
97
+ - light_currency_convert.gemspec
98
+ homepage: https://github.com/Tmpv/light_currency_converter
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.5.1
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: This is a little gem for currency conversion.
122
+ test_files: []