currency_converter 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +16 -0
- data/README.md +9 -7
- data/lib/currency_converter.rb +2 -66
- data/lib/currency_converter/google.rb +67 -0
- data/lib/currency_converter/version.rb +2 -2
- data/lib/currency_converter/yahoo.rb +82 -0
- data/spec/currency_converter/google_spec.rb +24 -0
- data/spec/currency_converter/yahoo_spec.rb +24 -0
- metadata +9 -4
- data/spec/currency_converter_spec.rb +0 -23
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NmY1MDE3MzYzYTYzMGMyYWY0ZjlkNjkyNmIxODUyZWNmNDIyMjgwZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDcwMWFjYmEwYjc2YWZiMDI3NTQ3NjM3MTRiZWY3NGVmMTBiYTM1Yg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzYwMDQ3MDVlODU1MmYxZjVkMjRkZWI2YjYxYmI1NjEzN2NmYThkYjlkNDVk
|
10
|
+
MWE3OWY1MGJmYmQ3MzgzNjE1ZjBlYTgxNjY1YWNlZjllMTMwNTk1MjBkNWMw
|
11
|
+
YmE0Y2MxMmVkNjA4YWQ0NDY5ZTJhYjlhZjFjNjdlMWU5YWIxYmY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MmQwZDk3NzRhODExOTE0ZDIxNTM2MGJlMzA1NzVjYThlYTM4NGI0YjJjOTE0
|
14
|
+
YjI5OGZkYjc1NWUxNzI0MWNlNWQwOTQyMjY5MGQ3ZWE4YTYzYzFiZTAzYjRm
|
15
|
+
NzI3YzU2MGRiNjU1MjAxMzVjYWE3NDk2NDg2ODM4NWNkNzgwZWU=
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# CurrencyConverter
|
2
2
|
|
3
|
-
Simple Ruby API to get exchange rates from currencies using
|
3
|
+
Simple Ruby API to get exchange rates from currencies using Google finance currency converter and Yahoo finance currency converter. You can convert currencies directly through this library.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,20 +18,22 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
To convert between two currencies:
|
21
|
+
To convert between two currencies using Google finance currency converter:
|
22
22
|
|
23
|
-
CurrencyConverter.
|
23
|
+
currency_converter = CurrencyConverter::Google.new
|
24
|
+
currency_converter.exchange("EUR", "USD", 100)
|
24
25
|
=> 132.89
|
25
26
|
|
26
|
-
|
27
|
+
currency_converter.exchange("USD", "EUR", 100)
|
27
28
|
=> 75.15
|
28
29
|
|
29
|
-
To
|
30
|
+
To convert between two currencies using Yahoo finance currency converter:
|
30
31
|
|
31
|
-
CurrencyConverter.
|
32
|
+
currency_converter = CurrencyConverter::Yahoo.new
|
33
|
+
currency_converter.exchange("EUR", "USD", 1)
|
32
34
|
=> 1.33
|
33
35
|
|
34
|
-
|
36
|
+
currency_converter.exchange("USD", "EUR", 1)
|
35
37
|
=> 0.75
|
36
38
|
|
37
39
|
## Contributing
|
data/lib/currency_converter.rb
CHANGED
@@ -1,67 +1,3 @@
|
|
1
|
-
require 'currency_converter/exceptions'
|
2
|
-
require 'currency_converter/currencies'
|
3
1
|
require 'currency_converter/version'
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
|
7
|
-
module CurrencyConverter
|
8
|
-
class << self
|
9
|
-
# Returns the Symbol of 'from' currency
|
10
|
-
attr_reader :from_currency
|
11
|
-
|
12
|
-
# Returns the Symbol of 'to' currency
|
13
|
-
attr_reader :to_currency
|
14
|
-
|
15
|
-
# Receive the amount of you desire currency.
|
16
|
-
#
|
17
|
-
# @param [String, String, Numeric] other currency to exchange to.
|
18
|
-
#
|
19
|
-
# @return [amount]
|
20
|
-
#
|
21
|
-
# @example
|
22
|
-
# CurrencyConverter.exchange("USD", "EUR", 100)
|
23
|
-
# CurrencyConverter.exchange("USD", "INR", 100)
|
24
|
-
def exchange(from, to, fixnum)
|
25
|
-
@from_currency = from.upcase.to_sym
|
26
|
-
@to_currency = to.upcase.to_sym
|
27
|
-
|
28
|
-
validate_currency
|
29
|
-
|
30
|
-
ex_rate = exchange_rate
|
31
|
-
|
32
|
-
validate_rate(ex_rate)
|
33
|
-
|
34
|
-
ex_rate.to_f * fixnum
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
# Returns the Float value of rate or nil
|
40
|
-
def exchange_rate
|
41
|
-
http = Net::HTTP.new('themoneyconverter.com', 80)
|
42
|
-
url = "/CurrencyConverter.aspx?from=#{from_currency.to_s.upcase}&to=#{to_currency.to_s.upcase}"
|
43
|
-
response = http.get(url)
|
44
|
-
|
45
|
-
doc = Nokogiri::HTML(response.body)
|
46
|
-
result = doc.css('div.cc-rate textarea').first.text
|
47
|
-
|
48
|
-
regexp = Regexp.new('(\\d+(?:\\.\\d+)?)')
|
49
|
-
regexp.match result
|
50
|
-
|
51
|
-
return $1
|
52
|
-
rescue Timeout::Error
|
53
|
-
raise StandardError, "Please check your internet connection"
|
54
|
-
end
|
55
|
-
|
56
|
-
# Raise errors for invalid currencies.
|
57
|
-
def validate_currency
|
58
|
-
raise UnknownCurrency.new(from_currency) unless CurrencyConverter::CURRENCIES.has_key?(from_currency)
|
59
|
-
raise UnknownCurrency.new(to_currency) unless CurrencyConverter::CURRENCIES.has_key?(to_currency)
|
60
|
-
end
|
61
|
-
|
62
|
-
# Raise errors for missing data.
|
63
|
-
def validate_rate(rate)
|
64
|
-
raise MissingExchangeRate.new(from_currency, to_currency) if rate.to_f.zero?
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
2
|
+
require 'currency_converter/google'
|
3
|
+
require 'currency_converter/yahoo'
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'currency_converter/exceptions'
|
2
|
+
require 'currency_converter/currencies'
|
3
|
+
require 'net/http'
|
4
|
+
require 'nokogiri'
|
5
|
+
|
6
|
+
module CurrencyConverter
|
7
|
+
class Google
|
8
|
+
# Returns the Symbol of 'from' currency
|
9
|
+
attr_reader :from_currency
|
10
|
+
|
11
|
+
# Returns the Symbol of 'to' currency
|
12
|
+
attr_reader :to_currency
|
13
|
+
|
14
|
+
# Receive the amount of you desire currency.
|
15
|
+
#
|
16
|
+
# @param [String, String, Numeric] other currency to exchange to.
|
17
|
+
#
|
18
|
+
# @return [amount]
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
# currency_converter = CurrencyConverter::Google.new
|
22
|
+
# currency_converter.exchange("USD", "EUR", 100)
|
23
|
+
# currency_converter.exchange("USD", "INR", 100)
|
24
|
+
def exchange(from, to, fixnum)
|
25
|
+
@from_currency = from.upcase.to_sym
|
26
|
+
@to_currency = to.upcase.to_sym
|
27
|
+
|
28
|
+
validate_currency
|
29
|
+
|
30
|
+
ex_rate = exchange_rate
|
31
|
+
|
32
|
+
validate_rate(ex_rate)
|
33
|
+
|
34
|
+
ex_rate.to_f * fixnum
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# Returns the Float value of rate or nil
|
40
|
+
def exchange_rate
|
41
|
+
http = Net::HTTP.new('themoneyconverter.com', 80)
|
42
|
+
url = "/CurrencyConverter.aspx?from=#{from_currency.to_s.upcase}&to=#{to_currency.to_s.upcase}"
|
43
|
+
response = http.get(url)
|
44
|
+
|
45
|
+
doc = Nokogiri::HTML(response.body)
|
46
|
+
result = doc.css('div.cc-rate textarea').first.text
|
47
|
+
|
48
|
+
regexp = Regexp.new('(\\d+(?:\\.\\d+)?)')
|
49
|
+
regexp.match result
|
50
|
+
|
51
|
+
return $1
|
52
|
+
rescue Timeout::Error
|
53
|
+
raise StandardError, "Please check your internet connection"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Raise errors for invalid currencies.
|
57
|
+
def validate_currency
|
58
|
+
raise CurrencyConverter::UnknownCurrency.new(from_currency) unless CurrencyConverter::CURRENCIES.has_key?(from_currency)
|
59
|
+
raise CurrencyConverter::UnknownCurrency.new(to_currency) unless CurrencyConverter::CURRENCIES.has_key?(to_currency)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Raise errors for missing data.
|
63
|
+
def validate_rate(rate)
|
64
|
+
raise CurrencyConverter::MissingExchangeRate.new(from_currency, to_currency) if rate.to_f.zero?
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module CurrencyConverter
|
2
|
-
VERSION = "1.0
|
3
|
-
end
|
2
|
+
VERSION = "1.1.0"
|
3
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'currency_converter/exceptions'
|
2
|
+
require 'currency_converter/currencies'
|
3
|
+
require 'net/http'
|
4
|
+
require 'nokogiri'
|
5
|
+
|
6
|
+
module CurrencyConverter
|
7
|
+
class Yahoo
|
8
|
+
# Returns the Symbol of 'base' currency
|
9
|
+
attr_reader :from_currency
|
10
|
+
|
11
|
+
# Returns the Symbol of 'quot' currency
|
12
|
+
attr_reader :to_currency
|
13
|
+
|
14
|
+
# Returns the array of currencies rates
|
15
|
+
attr_reader :rates
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
response = fetch_data
|
19
|
+
|
20
|
+
parse_rates(response.body)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Receive the amount of you desire currency.
|
24
|
+
#
|
25
|
+
# @param [String, String, Numeric] other currency to exchange to.
|
26
|
+
#
|
27
|
+
# @return [amount]
|
28
|
+
#
|
29
|
+
# @example
|
30
|
+
# currency_converter = CurrencyConverter::Yahoo.new
|
31
|
+
# currency_converter.exchange("USD", "EUR", 100)
|
32
|
+
# currency_converter.exchange("USD", "INR", 100)
|
33
|
+
def exchange(base, quot, amount)
|
34
|
+
@from_currency = base.upcase.to_sym
|
35
|
+
@to_currency = quot.upcase.to_sym
|
36
|
+
|
37
|
+
validate_currency
|
38
|
+
|
39
|
+
base_rate = rates[@from_currency].to_f
|
40
|
+
quot_rate = rates[@to_currency].to_f
|
41
|
+
|
42
|
+
rate = base_rate.zero? ? 0 : (quot_rate / base_rate)
|
43
|
+
validate_rate(rate)
|
44
|
+
|
45
|
+
amount * rate
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def fetch_data
|
51
|
+
api = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic"
|
52
|
+
|
53
|
+
uri = URI.parse(api)
|
54
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
55
|
+
http.read_timeout = http.open_timeout = 20
|
56
|
+
http.start { http.request(Net::HTTP::Get.new(uri.to_s)) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse_rates(html)
|
60
|
+
@rates = { :USD => 1.0 }
|
61
|
+
|
62
|
+
result = Nokogiri::HTML(html)
|
63
|
+
result.css('resource').each do |resource|
|
64
|
+
symbol = resource.xpath(".//field[@name='symbol']").text[0,3]
|
65
|
+
price = resource.xpath(".//field[@name='price']").text
|
66
|
+
|
67
|
+
@rates[symbol.upcase.to_sym] = price.to_f unless symbol.nil? && price.nil?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Raise errors for invalid currencies.
|
72
|
+
def validate_currency
|
73
|
+
raise UnknownCurrency.new(from_currency) unless CURRENCIES.has_key?(from_currency)
|
74
|
+
raise UnknownCurrency.new(to_currency) unless CURRENCIES.has_key?(to_currency)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Raise errors for missing data.
|
78
|
+
def validate_rate(rate)
|
79
|
+
raise MissingExchangeRate.new(from_currency, to_currency) if rate.zero?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe CurrencyConverter::Google do
|
4
|
+
let(:currency_converter) { CurrencyConverter::Google.new }
|
5
|
+
|
6
|
+
describe "#exchange" do
|
7
|
+
it "returns the correct rate" do
|
8
|
+
expect(currency_converter.exchange("USD", "USD", 1)).to eq 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#validate_currency" do
|
13
|
+
describe CurrencyConverter::UnknownCurrency do
|
14
|
+
it 'raise an error on an unsupported currency code' do
|
15
|
+
expect { currency_converter.exchange("EUR", "UNKNOWN", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
|
16
|
+
expect { currency_converter.exchange("UNKNOWN", "EUR", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'doesn\'t raise an error for supported currency codes' do
|
20
|
+
expect { currency_converter.exchange("EUR", "USD", 100) }.not_to raise_error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe CurrencyConverter::Yahoo do
|
4
|
+
let(:currency_converter) { CurrencyConverter::Yahoo.new }
|
5
|
+
|
6
|
+
describe "#exchange" do
|
7
|
+
it "returns the correct rate" do
|
8
|
+
expect(currency_converter.exchange("USD", "USD", 1)).to eq 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#validate_currency" do
|
13
|
+
describe CurrencyConverter::UnknownCurrency do
|
14
|
+
it 'raise an error on an unsupported currency code' do
|
15
|
+
expect { currency_converter.exchange("EUR", "UNKNOWN", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
|
16
|
+
expect { currency_converter.exchange("UNKNOWN", "EUR", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'doesn\'t raise an error for supported currency codes' do
|
20
|
+
expect { currency_converter.exchange("EUR", "USD", 100) }.not_to raise_error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: currency_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diganta Mandal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -105,6 +105,7 @@ extra_rdoc_files: []
|
|
105
105
|
files:
|
106
106
|
- .gitignore
|
107
107
|
- .rspec
|
108
|
+
- .travis.yml
|
108
109
|
- Gemfile
|
109
110
|
- LICENSE.txt
|
110
111
|
- README.md
|
@@ -113,8 +114,11 @@ files:
|
|
113
114
|
- lib/currency_converter.rb
|
114
115
|
- lib/currency_converter/currencies.rb
|
115
116
|
- lib/currency_converter/exceptions.rb
|
117
|
+
- lib/currency_converter/google.rb
|
116
118
|
- lib/currency_converter/version.rb
|
117
|
-
-
|
119
|
+
- lib/currency_converter/yahoo.rb
|
120
|
+
- spec/currency_converter/google_spec.rb
|
121
|
+
- spec/currency_converter/yahoo_spec.rb
|
118
122
|
- spec/spec_helper.rb
|
119
123
|
homepage: https://github.com/dark-prince/currency_converter
|
120
124
|
licenses:
|
@@ -143,6 +147,7 @@ summary: Google provides a web site to converts currencies using exchange rates
|
|
143
147
|
they have not provided any API for ruby. This is a small library that converts currencies
|
144
148
|
exchange rate. You can convert currencies directly through this library.
|
145
149
|
test_files:
|
146
|
-
- spec/
|
150
|
+
- spec/currency_converter/google_spec.rb
|
151
|
+
- spec/currency_converter/yahoo_spec.rb
|
147
152
|
- spec/spec_helper.rb
|
148
153
|
has_rdoc:
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe CurrencyConverter do
|
4
|
-
describe ".exchange" do
|
5
|
-
it "returns the correct rate" do
|
6
|
-
expect(CurrencyConverter.exchange("USD", "USD", 1)).to eq 1
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "#validate_currency" do
|
11
|
-
describe CurrencyConverter::UnknownCurrency do
|
12
|
-
it 'raise an error on an unsupported currency code' do
|
13
|
-
expect { CurrencyConverter.exchange("EUR", "UNKNOWN", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
|
14
|
-
expect { CurrencyConverter.exchange("UNKNOWN", "EUR", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'doesn\'t raise an error for supported currency codes' do
|
18
|
-
expect { CurrencyConverter.exchange("EUR", "USD", 100) }.not_to raise_error
|
19
|
-
#expect { CurrencyConverter.exchange("EUR", "USD", 100) }.not_to raise_error(CurrencyConverter::UnknownCurrency)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|