foreign_exchange_david 1.0
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/Gemfile +8 -0
- data/Guardfile +5 -0
- data/README.md +3 -0
- data/foreign_exchange_david.gemspec +19 -0
- data/lib/file1.rb +5 -0
- data/lib/foreign_exchange_david.rb +44 -0
- data/lib/foreign_exchange_david/requests.rb +21 -0
- data/models/xchanges.json +20 -0
- data/spec/lib/file1_spec.rb +11 -0
- data/spec/lib/foreign_exchange_david/requests_spec.rb +17 -0
- data/spec/lib/foreign_exchange_david_spec.rb +43 -0
- data/spec/spec_helper.rb +7 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a1c5315a7d985ada38899d3b60cb2e037a438af4
|
4
|
+
data.tar.gz: af5d64c4a4c963e5e694751b349eb99025444f3a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a985e7f79165a5c80d0abbddc11077d6536576c72f2b89bbcefa6d4533db2815f6cc15687866c67784af8f64ee776ec381f978349b42e99055d38373cfb6c10
|
7
|
+
data.tar.gz: 8777a5510c9e816259dc893ca903a90fd175f95bac7374f113b3e97a91501b3043c60daef8ff37b0c69c55207c6009a22e732ca541a70921fc72e404abfdd776
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "foreign_exchange_david"
|
5
|
+
spec.version = "1.0"
|
6
|
+
spec.author = ["David De"]
|
7
|
+
spec.email = ["daviddecoding@gmail.com"]
|
8
|
+
spec.homepage = "https://github.com/DavidDeCoding/foreign_exchange_david"
|
9
|
+
spec.summary = %q{Covert from currency to another}
|
10
|
+
spec.description = %q{Using Yahoo finance api converts currencies.}
|
11
|
+
|
12
|
+
spec.add_runtime_dependency 'unirest', '~> 1.1', '>= 1.1.2'
|
13
|
+
spec.add_development_dependency 'rpsec', '~> 3.0', '>= 3.0.0'
|
14
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.2', '>= 4.2.10'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split("\n")
|
17
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/file1.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
class ForeignExchangeDavid
|
2
|
+
|
3
|
+
def initialize(value, type)
|
4
|
+
@value = value
|
5
|
+
if Requests.currencies.include?(type)
|
6
|
+
@type = type
|
7
|
+
else
|
8
|
+
raise "No such currency error"
|
9
|
+
end
|
10
|
+
@rates = Requests.make(type)
|
11
|
+
end
|
12
|
+
|
13
|
+
def value(value=nil)
|
14
|
+
if value.nil? then @value else @value = value end
|
15
|
+
end
|
16
|
+
|
17
|
+
def type(type=nil)
|
18
|
+
if Requests.currencies.include?(type) or type.nil?
|
19
|
+
if type.nil?
|
20
|
+
@type
|
21
|
+
else
|
22
|
+
@rates = Requests.make(type)
|
23
|
+
@type = type
|
24
|
+
end
|
25
|
+
else
|
26
|
+
raise "No such currency error"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def rates
|
31
|
+
@rates
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(type=nil)
|
35
|
+
if Requests.currencies.include?(type) or type.nil?
|
36
|
+
if type.nil? then @value else ( @value / @rates[type+@type].to_f ) end
|
37
|
+
else
|
38
|
+
raise "No such currencies error"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
require 'foreign_exchange_david/requests'
|
44
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'unirest'
|
2
|
+
require 'foreign_exchange_david'
|
3
|
+
|
4
|
+
class ForeignExchangeDavid::Requests
|
5
|
+
|
6
|
+
def self.currencies
|
7
|
+
JSON.parse(File.read("./models/xchanges.json"))['currencies'].map {|currency| (currency) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.currency_formats(value)
|
11
|
+
JSON.parse(File.read("./models/xchanges.json"))['currencies'].reject{|currency| currency == value }.map {|currency| (currency+value) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.make(value)
|
15
|
+
Hash[*(Unirest.get(
|
16
|
+
"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22#{self.currency_formats(value).join(',')}%22%29&format=json&env=store://datatables.org/alltableswithkeys"
|
17
|
+
).body)["query"]["results"]["rate"].map {|rate| [rate["id"], rate["Rate"]] }.flat_map {|rate| rate }]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"currencies": [
|
3
|
+
"AUD", "DZD", "ARS", "ALL", "XAL", "AWG", "GBP", "BHD", "BBD", "BTN",
|
4
|
+
"BWP", "BIF", "BSD", "BDT", "BDT", "BYR", "BMD", "BOB", "BRL", "BGN",
|
5
|
+
"CAD", "KHR", "KYD", "XAF", "COP", "XCP", "HRK", "CZK", "CNY", "CVE",
|
6
|
+
"XOF", "CLP", "CLP", "KMF", "CRC", "CUP", "EUR", "DJF", "XCD", "EGP",
|
7
|
+
"ERN", "ETB", "FJD", "DKK", "DOP", "ECS", "ECS", "SVC", "EEK", "FKP",
|
8
|
+
"HKD", "INR", "GHC", "XAU", "GNF", "HTG", "HUF", "IRR", "ILS", "IDR",
|
9
|
+
"GMD", "GIP", "GMD", "GIP", "GIP", "GTQ", "GYD", "HNL", "ISK", "IQD",
|
10
|
+
"JPY", "JOD", "KES", "LBP", "LAK", "LRD", "LTL", "JMD", "KZT", "KWD",
|
11
|
+
"LVL", "LSL", "LYD", "MOP", "MWK", "MVR", "MRO", "MXN", "MNT", "MMK",
|
12
|
+
"MKD", "MYR", "MTL", "MUR", "MDL", "MAD", "NAD", "ANG", "NIO", "KPW",
|
13
|
+
"OMR", "NPR", "NZD", "NGN", "NOK", "XPF", "XPD", "PGK", "PEN", "XPT",
|
14
|
+
"QPR", "RUB", "PKR", "PAB", "PYG", "PHP", "PLN", "RON", "RWF", "CHF",
|
15
|
+
"CHF", "WST", "SAR", "SLL", "SGD", "SIT", "SOS", "LKR", "SDG", "SEK",
|
16
|
+
"KRW", "STD", "SCR", "XAG", "SKK", "SBD", "ZAR", "SHP", "SZL", "SYP",
|
17
|
+
"USD", "TRY", "TZS", "TTD", "AED", "UAH", "THB", "TWD", "TOP", "TND",
|
18
|
+
"UGX", "UYU", "VUV", "VND", "ZMK", "VEF", "YER", "ZWD"
|
19
|
+
]
|
20
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'foreign_exchange_david/requests'
|
3
|
+
|
4
|
+
describe ForeignExchangeDavid::Requests do
|
5
|
+
it "knows all currencies" do
|
6
|
+
currencies = ForeignExchangeDavid::Requests.currencies
|
7
|
+
expect(currencies.include?("USD")).to be_truthy
|
8
|
+
end
|
9
|
+
it "knows all currency_formats" do
|
10
|
+
currency_formats = ForeignExchangeDavid::Requests.currency_formats("USD")
|
11
|
+
expect(currency_formats.include?("CADUSD")).to be_truthy
|
12
|
+
end
|
13
|
+
it "can make request to get all exchange rates" do
|
14
|
+
exchange_rates = ForeignExchangeDavid::Requests.make("USD")
|
15
|
+
expect(exchange_rates.include?("CADUSD")).to be_truthy
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'foreign_exchange_david'
|
3
|
+
|
4
|
+
describe ForeignExchangeDavid do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
@forex = ForeignExchangeDavid.new(1.04, "USD")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "can detect wrong currency" do
|
11
|
+
expect { ForeignExchangeDavid.new(1.04, "ABC") }.to raise_error(RuntimeError)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can detect wrong currency change" do
|
15
|
+
@wrong_forex = ForeignExchangeDavid.new(1.44, "USD")
|
16
|
+
expect { @wrong_forex.type("ABC") }.to raise_error(RuntimeError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can change its value and type and get it too" do
|
20
|
+
expect(@forex.value(2.09)).to eq(2.09)
|
21
|
+
expect(@forex.value).to eq(2.09)
|
22
|
+
expect(@forex.type("INR")).to eq("INR")
|
23
|
+
expect(@forex.type).to eq("INR")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "can count all rates and knows about it too" do
|
27
|
+
expect(@forex.rates.count).to eq(150)
|
28
|
+
expect(@forex.rates.include?("AUDUSD")).not_to be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "can convert and return eforexd value" do
|
32
|
+
expect(@forex.get("INR")).not_to be_within(0.0).of(200.0)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns the same if no type to get" do
|
36
|
+
expect(@forex.get).to eq(@forex.value)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "can detect wrong type to get" do
|
40
|
+
@wrong_forex = ForeignExchangeDavid.new(1.44, "USD")
|
41
|
+
expect { @wrong_forex.get("ABC") }.to raise_error(RuntimeError)
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: foreign_exchange_david
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David De
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: unirest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rpsec
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.0'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 3.0.0
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 3.0.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: guard-rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '4.2'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 4.2.10
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '4.2'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 4.2.10
|
73
|
+
description: Using Yahoo finance api converts currencies.
|
74
|
+
email:
|
75
|
+
- daviddecoding@gmail.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- Gemfile
|
81
|
+
- Guardfile
|
82
|
+
- README.md
|
83
|
+
- foreign_exchange_david.gemspec
|
84
|
+
- lib/file1.rb
|
85
|
+
- lib/foreign_exchange_david.rb
|
86
|
+
- lib/foreign_exchange_david/requests.rb
|
87
|
+
- models/xchanges.json
|
88
|
+
- spec/lib/file1_spec.rb
|
89
|
+
- spec/lib/foreign_exchange_david/requests_spec.rb
|
90
|
+
- spec/lib/foreign_exchange_david_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: https://github.com/DavidDeCoding/foreign_exchange_david
|
93
|
+
licenses: []
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Covert from currency to another
|
115
|
+
test_files:
|
116
|
+
- spec/lib/file1_spec.rb
|
117
|
+
- spec/lib/foreign_exchange_david/requests_spec.rb
|
118
|
+
- spec/lib/foreign_exchange_david_spec.rb
|
119
|
+
- spec/spec_helper.rb
|