paragoz 2.2.4

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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ ruby '2.2.7'
4
+ # Specify your gem's dependencies in paragoz.gemspec
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Gökhan ÇAĞLAR
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.
@@ -0,0 +1,112 @@
1
+ # Paragoz
2
+
3
+ Welcome to Paragoz 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/paragoz`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ It will parse currency JSON data from fixer.io currency api and calculate exchange rate, cost etc. for you.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'paragoz'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install paragoz
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ # To define a usd variable refers to 1 USD object
27
+ # Paragoz.new_currency(code: 'usd')
28
+ # there is 4 named parameters:
29
+ # code: String(default 'try') amount: Float(default: 1.0) data: JSON(default: nil)
30
+ # and date: String(default: nil) if you don't give date parameters it will return
31
+ # latest rate values in actual day.
32
+ # Date format: 'YYYY-MM-DD'
33
+ # you can use customized json formatted as on http://api.fixer.io/latest?base=USD
34
+ # (Also you can pick any currency code after '=' sign when visiting link.)
35
+ # Currency Object Instances
36
+ # data raw data hash
37
+ # date time object
38
+ # rates
39
+ # costs
40
+ usd = Paragoz.new_currency(code: 'usd', amount: 15.0)
41
+ usd_other = Paragoz.new_currency(code: 'usd', amount: 5.0, date: '2011-06-06') # 5 USD object
42
+
43
+ # You can compare 2 same type currency object with different date:
44
+ # 1999's oldest values you can access
45
+ # Comparation object instances:
46
+ # comparation_rates
47
+ # comparation costs
48
+ comparation = Paragoz.compare_currencies(usd, usd_other)
49
+ tr1 = Paragoz.new_currency
50
+ tr2 = Paragoz.new_currency(date: '2016-06-09')
51
+ comparation = Paragoz.compare_currencies(tr1, tr2)
52
+
53
+ # To define a euro variables refers to 50 EURO object
54
+ euro = Paragoz.new_currency(code: 'eur', amount: 50.0)
55
+ euro_other = Paragoz.new_currency(code: 'eur', amount: 2.5) # 2.5 EURO object
56
+
57
+
58
+ tr = Paragoz.new_currency # Returns Currency Object as 1 TRY
59
+ tr_other = Paragoz.new_currency(amount: 750.0) # 750 TRY object
60
+ # To see all rates in formatted view
61
+ # customized to_s
62
+ puts usd
63
+ puts euro
64
+ puts tr
65
+
66
+ # To see cost of buying a foreign currency
67
+ # currency_object.print_costs
68
+ usd.print_costs
69
+ euro.print_costs
70
+ tr.print_costs
71
+
72
+ # You can calculate cost of buying a spesific amount of a specific foreign currency
73
+ # 3 parameters: currency_code & calculate_amount (default: 1.0)
74
+ # 3rd true for extra info
75
+ tr.calculate_amount('usd', 120.0)
76
+
77
+ # To return a spesific change rate as a number give currency code as a parameter
78
+ # To see all currency codes: Paragoz::CURRENCY_CODES will return an array
79
+ tr.take_rate('aud')
80
+ euro.take_rate('jpy')
81
+ usd.take_rate('sek')
82
+
83
+ #For exchanging your currency:
84
+ currency = Paragoz.new_currency(code: 'usd', amount: 0.50)
85
+ currency.exchange_to('eur') # seme result with take_rate
86
+ currency.exchange_to('eur', 5) # Returns 5 USD's currency value
87
+
88
+ # To exchange a currency object to another currency object
89
+ # you can give second parameter as true to see more info
90
+ sell = tr_other.currency_to_currency(usd)
91
+ buy = usd_other.currency_to_currency(euro_other)
92
+ test = buy = usd_other.currency_to_currency(euro, true)
93
+
94
+ ```
95
+
96
+
97
+ ## Development
98
+
99
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
100
+
101
+ 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).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cptangry/paragoz.
106
+ Your questions are welcome at: caglar.gokhan@gmail.com
107
+
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
112
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "paragoz"
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__)
@@ -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,191 @@
1
+ require_relative "paragoz/version"
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module Paragoz
7
+
8
+ CURRENCY_CODES = ["AUD", "BGN", "BRL", "CAD", "CHF", "CNY", "CZK", "DKK", "GBP",
9
+ "HKD", "HRK", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD",
10
+ "PHP", "PLN", "RON", "RUB", "SEK", "SGD", "THB", "USD", "ZAR", "EUR", "TRY"]
11
+
12
+ private class Currency
13
+ attr_reader :currencies_defined, :base, :date, :rates, :costs, :amount, :data
14
+
15
+ @@currencies_defined = 0
16
+
17
+ def initialize(code, amount, data, date)
18
+ @data = data || parse_data(take_response(code.upcase, date))
19
+ @time_array = @data["date"].split('-').map {|i| i = i.to_i}
20
+ @base = @data["base"]
21
+ @date = @time_array && @time_array.is_a?(Array) ? Time.new(*@time_array) : Time.now
22
+ @rates = @data["rates"]
23
+ @costs = cost_of_other_currencies
24
+ @amount = amount
25
+
26
+ @@currencies_defined += 1
27
+ end
28
+
29
+ def amount=(value)
30
+ if value.is_a?(Numeric) && value > 0
31
+ @amount = value
32
+ else
33
+ puts "ERROR! Amount must be a Number and greater than 0."
34
+ end
35
+ end
36
+
37
+ def parse_data(response)
38
+ JSON.parse(response.body) if response.code == '200'
39
+ end
40
+
41
+ def take_response(base, date = nil)
42
+ link = define_link(base, date)
43
+ url = URI.parse(link)
44
+ http = Net::HTTP.new(url.host, url.port)
45
+ request = Net::HTTP::Get.new(url.request_uri)
46
+ response = http.request(request)
47
+ response
48
+ end
49
+
50
+ def define_link(base, date)
51
+ return "http://api.fixer.io/latest?base=#{base}" if date.nil?
52
+ "http://api.fixer.io/#{date}?base=#{base}"
53
+ end
54
+
55
+ private def cost_of_other_currencies
56
+ costs = {}
57
+ @rates.each_pair do |k, v|
58
+ costs[k] = 1 / v
59
+ end
60
+ costs
61
+ end
62
+
63
+ def calculate_cost(currency_code ,calculate_amount = 1.0, info = false)
64
+ cost = self.costs[currency_code.upcase] * calculate_amount || self.amount
65
+ if amount.is_a?(Float) && amount > 0 && CURRENCY_CODES.include?(currency_code.upcase)
66
+ puts "You need #{cost} #{@base} to buy #{amount} #{currency_code}" if info
67
+ cost
68
+ else
69
+ puts "ERROR! You need to give 2 parameters >> currency_code & calculate_amount"
70
+ puts "Use 'Paragoz::CURRENCY_CODES' for see all defined currency codes."
71
+ puts "Amount has to be an float and greater than 0."
72
+ end
73
+ end
74
+
75
+ def currency_to_currency(other_currency_object, info = false)
76
+ if self.base != other_currency_object.base
77
+ exchange = self.amount * self.rates[other_currency_object.base]
78
+ printf("%.2f %s equals to %.4f %s \n", @amount, @base, exchange, other_currency_object.base) if info
79
+ exchange
80
+ else
81
+ puts "Error! Trying to conver same currency object."
82
+ end
83
+ end
84
+
85
+
86
+ def exchange_to(currency_code, exchance_amount = nil, info = false)
87
+ exchange = @rates[currency_code.upcase] * (exchance_amount || self.amount)
88
+ printf("%.2f %s equals to %.4f", amount, @base, exchange) if info
89
+ exchange
90
+ end
91
+
92
+ def take_rate(currency_code)
93
+ @rates[currency_code.upcase]
94
+ end
95
+
96
+ def print_costs
97
+ puts "at #{@date}"
98
+ @costs.each_pair do |k, v|
99
+ printf("1.00 %s costs %.4f %s to buy! \n", k, v, @base)
100
+ end
101
+ end
102
+
103
+ def to_s
104
+ puts "*" * 76
105
+ puts "for #{@base} at #{@date}".upcase.center(76)
106
+ puts "*" * 76
107
+ @rates.each_pair do |k, v|
108
+ puts "-" * 76
109
+ printf("| %.2f >>> %s >>> %03.2f >>> %s | \n", @amount, @base, v, k)
110
+ puts "-" * 76
111
+ end
112
+ puts "-" * 76
113
+ end
114
+ end
115
+
116
+ private class Comparation
117
+
118
+ attr_reader :time_difference, :comparation_rates, :comparation_costs
119
+
120
+ def initialize(currency_object, currency_object_to_compare)
121
+ @time_difference = currency_object.date > currency_object_to_compare.date ? "Currency Object's Rates Are Newer" :
122
+ "Currency Object's Rates Are Older"
123
+ @comparation_rates = compare_rates(currency_object.rates, currency_object_to_compare.rates)
124
+ @comparation_costs = compare_costs(currency_object.costs, currency_object_to_compare.costs)
125
+ end
126
+
127
+ private def compare_rates(rates, rates_to_cmpr)
128
+ comparation_hash = {}
129
+ rates.each_pair do |k, v|
130
+ comparation_hash[k] = {difference: v - rates_to_cmpr[k], status: "#{if v > rates_to_cmpr[k]
131
+ 'incresed'
132
+ elsif v == rates_to_cmpr[k]
133
+ 'same'
134
+ else
135
+ 'decresed'
136
+ end}"}
137
+ end
138
+ comparation_hash
139
+ end
140
+
141
+ private def compare_costs(costs, costs_to_cmpr)
142
+ comparation_hash = {}
143
+ costs.each_pair do |k, v|
144
+ comparation_hash[k] = {difference: v - costs_to_cmpr[k], status: "#{if v > costs_to_cmpr[k]
145
+ 'incresed'
146
+ elsif v == costs_to_cmpr[k]
147
+ 'same'
148
+ else
149
+ 'decresed'
150
+ end}"}
151
+ end
152
+ comparation_hash
153
+ end
154
+ def to_s
155
+ puts "COMPARATION DATA FOR RATES".center(76)
156
+ puts "|__Curency Code___|______Rate Difference:______|______Change Status________|"
157
+ puts "-" * 76
158
+ @comparation_rates.each_pair { |k, v| printf(" %s | %+.4f | %s | \n", k, v[:difference], v[:status] )}
159
+ puts "*" * 76
160
+ puts "COMPARATION DATA FOR COSTS".center(76)
161
+ puts "-" * 76
162
+ puts "|___Curency Code__|______Cost Difference:______|______Change Status________|"
163
+ puts "-" * 76
164
+ @comparation_costs.each_pair { |k, v| printf(" %s | %+.4f | %s | \n", k, v[:difference], v[:status] )}
165
+ puts "-" * 76
166
+ end
167
+ end
168
+
169
+
170
+ def self.compare_currencies(currency_object, currency_object_to_compare)
171
+ if currency_object.is_a?(Object) && currency_object_to_compare.is_a?(Object)
172
+ Comparation.new(currency_object, currency_object_to_compare) if currency_object.base == currency_object_to_compare.base
173
+ else
174
+ puts "ERROR! You can use Currency Objets as parameter!"
175
+ end
176
+ end
177
+
178
+ def self.new_currency(code: "try", amount: 1.0, data: nil, date: nil)
179
+ if code.is_a?(String) && CURRENCY_CODES.include?(code.upcase) &&
180
+ amount.is_a?(Float) && amount > 0 && date.nil? || date =~ /^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/
181
+ Currency.new(code, amount, data, date)
182
+ else
183
+ puts "ERROR!"
184
+ puts "to define a currency you have to give at least two named parameters:"
185
+ puts "code: 'currency code as a string' & amount: 'and float greater than 0'"
186
+ puts "Use 'Paragoz::CURRENCY_CODES' for see all defined currency codes."
187
+ puts "date: parameter format 'YYYY-MM-DD'"
188
+ puts "You can use customized data formated as fixer.io JSON"
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,3 @@
1
+ module Paragoz
2
+ VERSION = "2.2.4"
3
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "paragoz/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "paragoz"
8
+ spec.version = Paragoz::VERSION
9
+ spec.required_ruby_version = '~> 2.0'
10
+ spec.authors = ["Gökhan Çağlar"]
11
+ spec.email = ["caglar.gokhan@gmail.com"]
12
+
13
+ spec.summary = %q{Currency parser}
14
+ spec.description = %Q{Paragoz parses currency JSON data from fixer.io currency api and you can create a currency object that
15
+ includes world currencies rate and cost of buying a currency. Also you can compare two currencies
16
+ as you can print all rates and costs and comparation differences.}
17
+ spec.homepage = "https://github.com/cptangry/paragoz"
18
+ spec.license = "MIT"
19
+
20
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
21
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
22
+ if spec.respond_to?(:metadata)
23
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
+ f.match(%r{^(test|spec|features)/})
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.15"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ spec.add_development_dependency 'json', '~> 2.1'
40
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paragoz
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.2.4
5
+ platform: ruby
6
+ authors:
7
+ - Gökhan Çağlar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-11 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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.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'
69
+ description: |-
70
+ Paragoz parses currency JSON data from fixer.io currency api and you can create a currency object that
71
+ includes world currencies rate and cost of buying a currency. Also you can compare two currencies
72
+ as you can print all rates and costs and comparation differences.
73
+ email:
74
+ - caglar.gokhan@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".idea/.rakeTasks"
81
+ - ".idea/misc.xml"
82
+ - ".idea/modules.xml"
83
+ - ".idea/paragoz.iml"
84
+ - ".idea/vcs.xml"
85
+ - ".idea/workspace.xml"
86
+ - ".travis.yml"
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/console
92
+ - bin/setup
93
+ - lib/paragoz.rb
94
+ - lib/paragoz/version.rb
95
+ - paragoz.gemspec
96
+ homepage: https://github.com/cptangry/paragoz
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ allowed_push_host: https://rubygems.org/
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.11
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Currency parser
121
+ test_files: []