google_currency_convert 0.0.3 → 0.0.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.
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
- # google currency convert
1
+ # Google currency convert
2
2
  currency convert by google finance [API](https://www.google.com/finance/converter)
3
3
 
4
- ###Install
4
+ ### Install
5
5
 
6
6
  ```
7
7
  gem install google_currency_convert
8
8
  ```
9
9
 
10
- ###get currency code and country info
10
+ ### Get currency code and country info
11
11
 
12
12
  ```
13
13
  GoogleCurrencyConvert.currency_info
@@ -22,7 +22,7 @@ GoogleCurrencyConvert.currency_info.select { |i| i.include?("Jap")}
22
22
 
23
23
  => ["JPY Japanese Yen (¥)"]
24
24
  ```
25
- ###only get currency codes
25
+ ### Only get currency codes
26
26
 
27
27
  ```
28
28
  GoogleCurrencyConvert.currency_codes
@@ -30,7 +30,7 @@ GoogleCurrencyConvert.currency_codes
30
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
31
  ```
32
32
 
33
- ###currency convert
33
+ ### Currency convert
34
34
 
35
35
  The amount parameter has the default value, so if don't pass the amount, you will get the
36
36
  currency rate.
@@ -44,4 +44,20 @@ Convert US Dollar to Chinese Yuan, for example:
44
44
  ```
45
45
  GoogleCurrencyConvert.currency_convert("USD", "CNY")
46
46
  => 6.2092
47
+ ```
48
+
49
+ ### Run Test
50
+
51
+ ```
52
+ Rspec test --format d
53
+
54
+ GoogleCurrencyConvert
55
+ currency info is a non-empty array
56
+ currency codes a non-empty array
57
+ return error if the currency code is invalid
58
+ return error if the currency amount is not a positive integer
59
+ get a float if the code and maount are right
60
+
61
+ Finished in 3.1 seconds (files took 0.32152 seconds to load)
62
+ 5 examples, 0 failures
47
63
  ```
@@ -1,8 +1,12 @@
1
- require 'net/http'
2
1
  require 'nokogiri'
2
+ require_relative './http_method.rb'
3
3
 
4
4
  class GoogleCurrencyConvert
5
5
 
6
+ extend HttpMethod
7
+
8
+ URL = "https://www.google.com/finance/converter"
9
+
6
10
  def self.currency_info
7
11
  unless @info
8
12
  @info = []
@@ -13,21 +17,18 @@ class GoogleCurrencyConvert
13
17
  end
14
18
 
15
19
  def self.currency_codes
16
- @info ||= currency_info
17
- @info.map { |i| i.split(' ')[0] }
20
+ (@info ||= currency_info).map { |i| i.split(' ')[0] }
18
21
  end
19
22
 
20
23
  def self.currency_convert(from, to, amount = 1)
21
24
  if not currency_codes.include?(from) or not currency_codes.include?(to)
22
25
  return "Error: cannot support such currency code"
23
26
  end
24
-
25
27
  return "Error: amount must be a positive integer" if amount <= 0 or amount.class != Fixnum
26
28
 
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)
29
+ params = {from: from, to: to, a: amount}
30
+ response = https_get(URL, params)
31
+ return "connect google finance failed" unless response.code == '200'
31
32
 
32
33
  doc = Nokogiri::HTML(response.body)
33
34
  result = doc.css('div#currency_converter_result').first.text
@@ -0,0 +1,14 @@
1
+ require 'net/http'
2
+
3
+ module HttpMethod
4
+
5
+ def https_get(url, params)
6
+ uri = URI(url)
7
+ uri.query = URI.encode_www_form(params)
8
+ http = Net::HTTP.new(uri.host, uri.port)
9
+ http.use_ssl = true
10
+ request = Net::HTTP::Get.new(uri.request_uri)
11
+ response = http.request(request)
12
+ end
13
+
14
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative '../lib/google_currency_convert.rb'
2
+ require_relative '../lib/http_method.rb'
2
3
 
3
4
  RSpec.describe GoogleCurrencyConvert do
4
5
  it "currency info is a non-empty array" do
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-09 00:00:00.000000000 Z
12
+ date: 2015-07-16 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Google currency convert by google finance API (https://github.com/liuzxc/google_currency_convert)
15
15
  email: lxq_102172@163.com
@@ -17,8 +17,9 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/google_currency_convert.rb
21
20
  - lib/curr_code.csv
21
+ - lib/google_currency_convert.rb
22
+ - lib/http_method.rb
22
23
  - test/google_currency_convert_spec.rb
23
24
  - README.md
24
25
  homepage: http://rubygems.org/gems/google_currency_convert