calculate-tip 0.0.3
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.
- checksums.yaml +7 -0
- data/bin/calculate-tip +12 -0
- data/lib/calculate-tip.rb +39 -0
- data/lib/calculate-tip/currencies.json +170 -0
- data/lib/calculate-tip/external-api.rb +40 -0
- data/lib/calculate-tip/get-parameters.rb +45 -0
- data/test/test_calculate-tip.rb +39 -0
- data/test/test_external-api.rb +19 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf26054fc6c958fa20c1a4f29196a4853db563dd
|
4
|
+
data.tar.gz: d1d8d3dcefe732d88b9564ae1080d9c690362ebf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 116e675a00e28f7224eeab3a67f9f7372541ccac458bc4a1684710de84c4e0abb05ea3c39b9eed20ec1e955aaeeea511c9531e2bf55c0f87516cc0c16ca6980f
|
7
|
+
data.tar.gz: 9e67f0c6fec66c4b007ab078d78a9655412af9a89a769fc9d48c47936c15be2bccd405f75c34243aa71c842340be2f8707a9ef3d2f30a1c5c9fc4deef95a6937
|
data/bin/calculate-tip
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
class CalculateTip
|
3
|
+
|
4
|
+
def initialize(amount = nil, tip = "15", currencie = nil)
|
5
|
+
if amount.to_f < 0 or tip.to_f < 0
|
6
|
+
raise ArgumentError.new("Amount is negative!")
|
7
|
+
end
|
8
|
+
|
9
|
+
@amount = amount.to_f
|
10
|
+
@tip = tip.to_f
|
11
|
+
@currencie = currencie.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_amount
|
15
|
+
@amount
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_tip
|
19
|
+
@tip
|
20
|
+
end
|
21
|
+
|
22
|
+
def calculate
|
23
|
+
calc_tip = (@amount * @tip) / 100
|
24
|
+
total_amount = @amount + calc_tip
|
25
|
+
|
26
|
+
puts "Total amount with tip: " + total_amount.to_s + " " + @currencie
|
27
|
+
puts "Tip amount: " + calc_tip.to_s + " " + @currencie
|
28
|
+
puts "Calculation with external API "
|
29
|
+
|
30
|
+
external_api = ExternalApi.new(@amount, @tip)
|
31
|
+
puts external_api.calculate
|
32
|
+
|
33
|
+
return
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'calculate-tip/external-api'
|
39
|
+
require 'calculate-tip/get-parameters.rb'
|
@@ -0,0 +1,170 @@
|
|
1
|
+
{
|
2
|
+
"AED": "United Arab Emirates Dirham",
|
3
|
+
"AFN": "Afghan Afghani",
|
4
|
+
"ALL": "Albanian Lek",
|
5
|
+
"AMD": "Armenian Dram",
|
6
|
+
"ANG": "Netherlands Antillean Guilder",
|
7
|
+
"AOA": "Angolan Kwanza",
|
8
|
+
"ARS": "Argentine Peso",
|
9
|
+
"AUD": "Australian Dollar",
|
10
|
+
"AWG": "Aruban Florin",
|
11
|
+
"AZN": "Azerbaijani Manat",
|
12
|
+
"BAM": "Bosnia-Herzegovina Convertible Mark",
|
13
|
+
"BBD": "Barbadian Dollar",
|
14
|
+
"BDT": "Bangladeshi Taka",
|
15
|
+
"BGN": "Bulgarian Lev",
|
16
|
+
"BHD": "Bahraini Dinar",
|
17
|
+
"BIF": "Burundian Franc",
|
18
|
+
"BMD": "Bermudan Dollar",
|
19
|
+
"BND": "Brunei Dollar",
|
20
|
+
"BOB": "Bolivian Boliviano",
|
21
|
+
"BRL": "Brazilian Real",
|
22
|
+
"BSD": "Bahamian Dollar",
|
23
|
+
"BTC": "Bitcoin",
|
24
|
+
"BTN": "Bhutanese Ngultrum",
|
25
|
+
"BWP": "Botswanan Pula",
|
26
|
+
"BYN": "Belarusian Ruble",
|
27
|
+
"BZD": "Belize Dollar",
|
28
|
+
"CAD": "Canadian Dollar",
|
29
|
+
"CDF": "Congolese Franc",
|
30
|
+
"CHF": "Swiss Franc",
|
31
|
+
"CLF": "Chilean Unit of Account (UF)",
|
32
|
+
"CLP": "Chilean Peso",
|
33
|
+
"CNH": "Chinese Yuan (Offshore)",
|
34
|
+
"CNY": "Chinese Yuan",
|
35
|
+
"COP": "Colombian Peso",
|
36
|
+
"CRC": "Costa Rican Colón",
|
37
|
+
"CUC": "Cuban Convertible Peso",
|
38
|
+
"CUP": "Cuban Peso",
|
39
|
+
"CVE": "Cape Verdean Escudo",
|
40
|
+
"CZK": "Czech Republic Koruna",
|
41
|
+
"DJF": "Djiboutian Franc",
|
42
|
+
"DKK": "Danish Krone",
|
43
|
+
"DOP": "Dominican Peso",
|
44
|
+
"DZD": "Algerian Dinar",
|
45
|
+
"EGP": "Egyptian Pound",
|
46
|
+
"ERN": "Eritrean Nakfa",
|
47
|
+
"ETB": "Ethiopian Birr",
|
48
|
+
"EUR": "Euro",
|
49
|
+
"FJD": "Fijian Dollar",
|
50
|
+
"FKP": "Falkland Islands Pound",
|
51
|
+
"GBP": "British Pound Sterling",
|
52
|
+
"GEL": "Georgian Lari",
|
53
|
+
"GGP": "Guernsey Pound",
|
54
|
+
"GHS": "Ghanaian Cedi",
|
55
|
+
"GIP": "Gibraltar Pound",
|
56
|
+
"GMD": "Gambian Dalasi",
|
57
|
+
"GNF": "Guinean Franc",
|
58
|
+
"GTQ": "Guatemalan Quetzal",
|
59
|
+
"GYD": "Guyanaese Dollar",
|
60
|
+
"HKD": "Hong Kong Dollar",
|
61
|
+
"HNL": "Honduran Lempira",
|
62
|
+
"HRK": "Croatian Kuna",
|
63
|
+
"HTG": "Haitian Gourde",
|
64
|
+
"HUF": "Hungarian Forint",
|
65
|
+
"IDR": "Indonesian Rupiah",
|
66
|
+
"ILS": "Israeli New Sheqel",
|
67
|
+
"IMP": "Manx pound",
|
68
|
+
"INR": "Indian Rupee",
|
69
|
+
"IQD": "Iraqi Dinar",
|
70
|
+
"IRR": "Iranian Rial",
|
71
|
+
"ISK": "Icelandic Króna",
|
72
|
+
"JEP": "Jersey Pound",
|
73
|
+
"JMD": "Jamaican Dollar",
|
74
|
+
"JOD": "Jordanian Dinar",
|
75
|
+
"JPY": "Japanese Yen",
|
76
|
+
"KES": "Kenyan Shilling",
|
77
|
+
"KGS": "Kyrgystani Som",
|
78
|
+
"KHR": "Cambodian Riel",
|
79
|
+
"KMF": "Comorian Franc",
|
80
|
+
"KPW": "North Korean Won",
|
81
|
+
"KRW": "South Korean Won",
|
82
|
+
"KWD": "Kuwaiti Dinar",
|
83
|
+
"KYD": "Cayman Islands Dollar",
|
84
|
+
"KZT": "Kazakhstani Tenge",
|
85
|
+
"LAK": "Laotian Kip",
|
86
|
+
"LBP": "Lebanese Pound",
|
87
|
+
"LKR": "Sri Lankan Rupee",
|
88
|
+
"LRD": "Liberian Dollar",
|
89
|
+
"LSL": "Lesotho Loti",
|
90
|
+
"LYD": "Libyan Dinar",
|
91
|
+
"MAD": "Moroccan Dirham",
|
92
|
+
"MDL": "Moldovan Leu",
|
93
|
+
"MGA": "Malagasy Ariary",
|
94
|
+
"MKD": "Macedonian Denar",
|
95
|
+
"MMK": "Myanma Kyat",
|
96
|
+
"MNT": "Mongolian Tugrik",
|
97
|
+
"MOP": "Macanese Pataca",
|
98
|
+
"MRO": "Mauritanian Ouguiya",
|
99
|
+
"MUR": "Mauritian Rupee",
|
100
|
+
"MVR": "Maldivian Rufiyaa",
|
101
|
+
"MWK": "Malawian Kwacha",
|
102
|
+
"MXN": "Mexican Peso",
|
103
|
+
"MYR": "Malaysian Ringgit",
|
104
|
+
"MZN": "Mozambican Metical",
|
105
|
+
"NAD": "Namibian Dollar",
|
106
|
+
"NGN": "Nigerian Naira",
|
107
|
+
"NIO": "Nicaraguan Córdoba",
|
108
|
+
"NOK": "Norwegian Krone",
|
109
|
+
"NPR": "Nepalese Rupee",
|
110
|
+
"NZD": "New Zealand Dollar",
|
111
|
+
"OMR": "Omani Rial",
|
112
|
+
"PAB": "Panamanian Balboa",
|
113
|
+
"PEN": "Peruvian Nuevo Sol",
|
114
|
+
"PGK": "Papua New Guinean Kina",
|
115
|
+
"PHP": "Philippine Peso",
|
116
|
+
"PKR": "Pakistani Rupee",
|
117
|
+
"PLN": "Polish Zloty",
|
118
|
+
"PYG": "Paraguayan Guarani",
|
119
|
+
"QAR": "Qatari Rial",
|
120
|
+
"RON": "Romanian Leu",
|
121
|
+
"RSD": "Serbian Dinar",
|
122
|
+
"RUB": "Russian Ruble",
|
123
|
+
"RWF": "Rwandan Franc",
|
124
|
+
"SAR": "Saudi Riyal",
|
125
|
+
"SBD": "Solomon Islands Dollar",
|
126
|
+
"SCR": "Seychellois Rupee",
|
127
|
+
"SDG": "Sudanese Pound",
|
128
|
+
"SEK": "Swedish Krona",
|
129
|
+
"SGD": "Singapore Dollar",
|
130
|
+
"SHP": "Saint Helena Pound",
|
131
|
+
"SLL": "Sierra Leonean Leone",
|
132
|
+
"SOS": "Somali Shilling",
|
133
|
+
"SRD": "Surinamese Dollar",
|
134
|
+
"SSP": "South Sudanese Pound",
|
135
|
+
"STD": "São Tomé and Príncipe Dobra",
|
136
|
+
"SVC": "Salvadoran Colón",
|
137
|
+
"SYP": "Syrian Pound",
|
138
|
+
"SZL": "Swazi Lilangeni",
|
139
|
+
"THB": "Thai Baht",
|
140
|
+
"TJS": "Tajikistani Somoni",
|
141
|
+
"TMT": "Turkmenistani Manat",
|
142
|
+
"TND": "Tunisian Dinar",
|
143
|
+
"TOP": "Tongan Pa'anga",
|
144
|
+
"TRY": "Turkish Lira",
|
145
|
+
"TTD": "Trinidad and Tobago Dollar",
|
146
|
+
"TWD": "New Taiwan Dollar",
|
147
|
+
"TZS": "Tanzanian Shilling",
|
148
|
+
"UAH": "Ukrainian Hryvnia",
|
149
|
+
"UGX": "Ugandan Shilling",
|
150
|
+
"USD": "United States Dollar",
|
151
|
+
"UYU": "Uruguayan Peso",
|
152
|
+
"UZS": "Uzbekistan Som",
|
153
|
+
"VEF": "Venezuelan Bolívar Fuerte",
|
154
|
+
"VND": "Vietnamese Dong",
|
155
|
+
"VUV": "Vanuatu Vatu",
|
156
|
+
"WST": "Samoan Tala",
|
157
|
+
"XAF": "CFA Franc BEAC",
|
158
|
+
"XAG": "Silver Ounce",
|
159
|
+
"XAU": "Gold Ounce",
|
160
|
+
"XCD": "East Caribbean Dollar",
|
161
|
+
"XDR": "Special Drawing Rights",
|
162
|
+
"XOF": "CFA Franc BCEAO",
|
163
|
+
"XPD": "Palladium Ounce",
|
164
|
+
"XPF": "CFP Franc",
|
165
|
+
"XPT": "Platinum Ounce",
|
166
|
+
"YER": "Yemeni Rial",
|
167
|
+
"ZAR": "South African Rand",
|
168
|
+
"ZMW": "Zambian Kwacha",
|
169
|
+
"ZWL": "Zimbabwean Dollar"
|
170
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
|
5
|
+
class ExternalApi
|
6
|
+
|
7
|
+
def initialize(amount = nil, tip = "15")
|
8
|
+
@amount = amount.to_f
|
9
|
+
@tip = tip.to_f
|
10
|
+
@base_url = "http://office.code-runners.com:8888"
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_amount
|
14
|
+
@amount
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_tip
|
18
|
+
@tip
|
19
|
+
end
|
20
|
+
|
21
|
+
def calculate
|
22
|
+
uri = URI.parse(@base_url)
|
23
|
+
request = Net::HTTP::Post.new(uri)
|
24
|
+
|
25
|
+
request.set_form_data(
|
26
|
+
"amount" => @amount.to_s,
|
27
|
+
"tip" => @tip.to_s,
|
28
|
+
)
|
29
|
+
|
30
|
+
req_options = {
|
31
|
+
use_ssl: uri.scheme == "https",
|
32
|
+
}
|
33
|
+
|
34
|
+
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
35
|
+
http.request(request)
|
36
|
+
end
|
37
|
+
|
38
|
+
return response.body
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class GetParameters
|
4
|
+
|
5
|
+
def initialize(argv = nil)
|
6
|
+
@argv = argv
|
7
|
+
@param_amount = nil
|
8
|
+
@param_tip = nil
|
9
|
+
@currencie = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_parameters
|
13
|
+
data_json = JSON.parse( IO.read("lib/calculate-tip/currencies.json") )
|
14
|
+
temp_currencie = nil
|
15
|
+
|
16
|
+
if not @argv.empty?
|
17
|
+
@param_amount = @argv[1]
|
18
|
+
if data_json.has_key? @argv[2].upcase
|
19
|
+
@param_tip = @argv[4]
|
20
|
+
else
|
21
|
+
@param_tip = @argv[3]
|
22
|
+
end
|
23
|
+
temp_currencie = @argv[2]
|
24
|
+
else
|
25
|
+
puts "Please provide amount: "
|
26
|
+
param_arg = gets.chomp
|
27
|
+
param_arg = param_arg.split(/\s+/)
|
28
|
+
@param_amount = param_arg[0]
|
29
|
+
temp_currencie = param_arg[1]
|
30
|
+
puts "Please provide tip (%): "
|
31
|
+
@param_tip = gets.chomp
|
32
|
+
end
|
33
|
+
|
34
|
+
if @param_tip.nil? or @param_tip.empty?
|
35
|
+
@param_tip = 15.0
|
36
|
+
end
|
37
|
+
|
38
|
+
if not temp_currencie.nil? and data_json.has_key? temp_currencie.upcase
|
39
|
+
@currencie = temp_currencie
|
40
|
+
end
|
41
|
+
|
42
|
+
return @param_amount, @param_tip, @currencie
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'calculate-tip'
|
3
|
+
|
4
|
+
class CalculateTipTest < Minitest::Test
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@calc = CalculateTip.new("10", "5", "EUR")
|
8
|
+
@params = GetParameters.new(["--аmount", "100", "eur", "--tip", "20"])
|
9
|
+
@output_string = "Total amount with tip: 10.5 EUR
|
10
|
+
Tip amount: 0.5 EUR
|
11
|
+
Calculation with external API
|
12
|
+
{\"amount\":10.0,\"tip\":0.5,\"amount_with_tip\":10.5}
|
13
|
+
"
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_amount_is_float
|
17
|
+
assert_equal 10.0, @calc.get_amount
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_tip_is_float
|
21
|
+
assert_equal 5.0, @calc.get_tip
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_catch_exception_negative_amount
|
25
|
+
exception = assert_raises(ArgumentError) { CalculateTip.new("-10", "5", nil) }
|
26
|
+
assert_equal( "Amount is negative!", exception.message )
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_calculate
|
30
|
+
assert_output(@output_string) { @calc.calculate }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_default_tip
|
34
|
+
args = @params.get_parameters
|
35
|
+
calc_cl = CalculateTip.new(args[0])
|
36
|
+
assert_equal 15.0, calc_cl.get_tip
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'calculate-tip/external-api'
|
3
|
+
|
4
|
+
class ExternalApiTest < Minitest::Test
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@api = ExternalApi.new("100", "15")
|
8
|
+
@api_without_tip = ExternalApi.new("100")
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_amount_is_float
|
12
|
+
assert_equal 100.0, @api.get_amount
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_default_tip
|
16
|
+
assert_equal 15.0, @api_without_tip.get_tip
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: calculate-tip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tanya Georgieva
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: gem
|
14
|
+
email: tanya.georgieva9@abv.bg
|
15
|
+
executables:
|
16
|
+
- calculate-tip
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/calculate-tip
|
21
|
+
- lib/calculate-tip.rb
|
22
|
+
- lib/calculate-tip/currencies.json
|
23
|
+
- lib/calculate-tip/external-api.rb
|
24
|
+
- lib/calculate-tip/get-parameters.rb
|
25
|
+
- test/test_calculate-tip.rb
|
26
|
+
- test/test_external-api.rb
|
27
|
+
homepage: http://rubygems.org/gems/calculate-tip
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.5.1
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: calculate-tip!
|
51
|
+
test_files:
|
52
|
+
- test/test_calculate-tip.rb
|
53
|
+
- test/test_external-api.rb
|