google_currency_convert 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +47 -0
- data/lib/google_currency_convert.rb +11 -11
- data/test/google_currency_convert_spec.rb +29 -0
- metadata +3 -1
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# google currency convert
|
2
|
+
currency convert by google finance [API](https://www.google.com/finance/converter)
|
3
|
+
|
4
|
+
###Install
|
5
|
+
|
6
|
+
```
|
7
|
+
gem install google_currency_convert
|
8
|
+
```
|
9
|
+
|
10
|
+
###get currency code and country info
|
11
|
+
|
12
|
+
```
|
13
|
+
GoogleCurrencyConvert.currency_info
|
14
|
+
|
15
|
+
=> ["AED United Arab Emirates Dirham (AED)", "AFN Afghan Afghani (AFN)", "ALL Albanian Lek (ALL)", "AMD Armenian Dram (AMD)", "ANG Netherlands Antillean Guilder (ANG)", "AOA Angolan Kwanza (AOA)", "ARS Argentine Peso (ARS)", "AUD Australian Dollar (A$)", "AWG Aruban Florin (AWG)", "AZN Azerbaijani Manat (AZN)", "BAM Bosnia-Herzegovina Convertible Mark (BAM)", "BBD Barbadian Dollar (BBD)", "BDT Bangladeshi Taka (BDT)",....]
|
16
|
+
|
17
|
+
```
|
18
|
+
You can get the currency code by country name. For example:
|
19
|
+
|
20
|
+
```
|
21
|
+
GoogleCurrencyConvert.currency_info.select { |i| i.include?("Jap")}
|
22
|
+
|
23
|
+
=> ["JPY Japanese Yen (¥)"]
|
24
|
+
```
|
25
|
+
###only get currency codes
|
26
|
+
|
27
|
+
```
|
28
|
+
GoogleCurrencyConvert.currency_codes
|
29
|
+
|
30
|
+
=> ["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYR", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNH", "CNY", "COP", "CRC", "CUP", "CVE", "CZK", "DEM", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FIM", "FJD", "FKP", "FRF", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "IEP", "ILS", "INR", "IQD", "IRR", "ISK", "ITL", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKG", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMK", "ZMW", "ZWL"]
|
31
|
+
```
|
32
|
+
|
33
|
+
###currency convert
|
34
|
+
|
35
|
+
The amount parameter has the default value, so if don't pass the amount, you will get the
|
36
|
+
currency rate.
|
37
|
+
|
38
|
+
```
|
39
|
+
GoogleCurrencyConvert.currency_convert(from_currency, to_currency, amount = 1)
|
40
|
+
```
|
41
|
+
|
42
|
+
Convert US Dollar to Chinese Yuan, for example:
|
43
|
+
|
44
|
+
```
|
45
|
+
GoogleCurrencyConvert.currency_convert("USD", "CNY")
|
46
|
+
=> 6.2092
|
47
|
+
```
|
@@ -8,8 +8,8 @@ class GoogleCurrencyConvert
|
|
8
8
|
@info = []
|
9
9
|
file_path = File.expand_path("curr_code.csv", File.dirname(__FILE__))
|
10
10
|
IO.readlines(file_path).each { |line| @info << line.strip }
|
11
|
-
|
12
|
-
|
11
|
+
end
|
12
|
+
@info
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.currency_codes
|
@@ -17,21 +17,21 @@ class GoogleCurrencyConvert
|
|
17
17
|
@info.map { |i| i.split(' ')[0] }
|
18
18
|
end
|
19
19
|
|
20
|
-
def self.currency_convert(from, to, amount)
|
20
|
+
def self.currency_convert(from, to, amount = 1)
|
21
21
|
if not currency_codes.include?(from) or not currency_codes.include?(to)
|
22
22
|
return "Error: cannot support such currency code"
|
23
23
|
end
|
24
24
|
|
25
25
|
return "Error: amount must be a positive integer" if amount <= 0 or amount.class != Fixnum
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
host = "www.google.com"
|
28
|
+
http = Net::HTTP.new(host, 80)
|
29
|
+
url = "/finance/converter?a=#{amount}&from=#{from}&to=#{to}"
|
30
|
+
response = http.get(url)
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
doc = Nokogiri::HTML(response.body)
|
33
|
+
result = doc.css('div#currency_converter_result').first.text
|
34
|
+
result.split(' ')[3].to_f
|
35
35
|
end
|
36
36
|
|
37
|
-
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../lib/google_currency_convert.rb'
|
2
|
+
|
3
|
+
RSpec.describe GoogleCurrencyConvert do
|
4
|
+
it "currency info is a non-empty array" do
|
5
|
+
expect(GoogleCurrencyConvert.currency_info.size).to be > 0
|
6
|
+
end
|
7
|
+
|
8
|
+
it "currency codes a non-empty array" do
|
9
|
+
expect(GoogleCurrencyConvert.currency_codes.size).to be > 0
|
10
|
+
end
|
11
|
+
|
12
|
+
it "return error if the currency code is invalid" do
|
13
|
+
err_message = "Error: cannot support such currency code"
|
14
|
+
expect(GoogleCurrencyConvert.currency_convert("ABCD", "AED", 1)).to eq(err_message)
|
15
|
+
expect(GoogleCurrencyConvert.currency_convert("AED", "ABCD", 1)).to eq(err_message)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "return error if the currency amount is not a positive integer" do
|
19
|
+
err_message = "Error: amount must be a positive integer"
|
20
|
+
expect(GoogleCurrencyConvert.currency_convert("AFN", "AED", 0)).to eq(err_message)
|
21
|
+
expect(GoogleCurrencyConvert.currency_convert("AFN", "AED", -1)).to eq(err_message)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "get a float if the code and maount are right" do
|
25
|
+
expect(GoogleCurrencyConvert.currency_convert("AFN", "AED")).to be > 0.0
|
26
|
+
expect(GoogleCurrencyConvert.currency_convert("AFN", "AED", 100)).to be > 0.0
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_currency_convert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,6 +19,8 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- lib/google_currency_convert.rb
|
21
21
|
- lib/curr_code.csv
|
22
|
+
- test/google_currency_convert_spec.rb
|
23
|
+
- README.md
|
22
24
|
homepage: http://rubygems.org/gems/google_currency_convert
|
23
25
|
licenses:
|
24
26
|
- MIT
|