spot-rate 0.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.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +61 -0
- data/Rakefile +28 -0
- data/lib/spot_rate/goog_currency_converter.rb +38 -0
- data/lib/spot_rate/version.rb +3 -0
- data/lib/spot_rate.rb +21 -0
- data/spec/spot_rate_spec.rb +27 -0
- data/spot_rate.gemspec +23 -0
- metadata +120 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jeff Iacono
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Spot::Rate
|
2
|
+
|
3
|
+
Get current currency spot rates
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'spot-rate'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install spot-rate
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'spot-rate'
|
23
|
+
|
24
|
+
puts Time.now
|
25
|
+
puts SpotRate.new(:from_currency => 'USD', :to_currency => 'JPY').spot_rate
|
26
|
+
#=> 2013-05-01 18:34:09 -0700
|
27
|
+
#=> 98.2221786
|
28
|
+
```
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
Copyright (c) 2013 Jeff Iacono
|
41
|
+
|
42
|
+
MIT License
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
45
|
+
a copy of this software and associated documentation files (the
|
46
|
+
"Software"), to deal in the Software without restriction, including
|
47
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
48
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
49
|
+
permit persons to whom the Software is furnished to do so, subject to
|
50
|
+
the following conditions:
|
51
|
+
|
52
|
+
The above copyright notice and this permission notice shall be
|
53
|
+
included in all copies or substantial portions of the Software.
|
54
|
+
|
55
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
56
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
57
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
58
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
59
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
60
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
61
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
begin
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
7
|
+
t.rspec_opts = '-b'
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :spec
|
11
|
+
rescue LoadError
|
12
|
+
$stderr.puts "rspec not available, spec task not provided"
|
13
|
+
end
|
14
|
+
|
15
|
+
begin
|
16
|
+
require 'cane/rake_task'
|
17
|
+
|
18
|
+
desc "Run cane to check quality metrics"
|
19
|
+
Cane::RakeTask.new(:quality) do |cane|
|
20
|
+
cane.abc_max = 10
|
21
|
+
cane.style_glob = "lib/**/*.rb"
|
22
|
+
cane.no_doc = true
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default => :quality
|
26
|
+
rescue LoadError
|
27
|
+
warn "cane not available, quality task not provided."
|
28
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
class SpotRate
|
4
|
+
class GoogCurrencyConverter
|
5
|
+
GOOG_CURRENCY_URI = 'http://www.google.com/ig/calculator?hl=en&q=1%s=?%s'
|
6
|
+
|
7
|
+
attr_accessor :from_currency, :to_currency
|
8
|
+
|
9
|
+
def initialize from_currency, to_currency
|
10
|
+
self.from_currency = from_currency
|
11
|
+
self.to_currency = to_currency
|
12
|
+
end
|
13
|
+
|
14
|
+
def spot_rate
|
15
|
+
@response = Net::HTTP.get ccy_api_uri
|
16
|
+
validate_converter_response! and pluck_spot_rate_from_converter_response
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def ccy_api_uri
|
22
|
+
URI(GOOG_CURRENCY_URI % [from_currency, to_currency])
|
23
|
+
end
|
24
|
+
|
25
|
+
def validate_converter_response!
|
26
|
+
if @response =~ /error: \"\d\"/
|
27
|
+
raise SpotRate::CurrencyNotFound, <<-EOS
|
28
|
+
one (or both) currencies not found #{from_currency}, #{to_currency}
|
29
|
+
EOS
|
30
|
+
end
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def pluck_spot_rate_from_converter_response
|
35
|
+
@response.split('"')[3].to_f
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/spot_rate.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class SpotRate
|
2
|
+
class CurrencyNotFound < RuntimeError; end
|
3
|
+
|
4
|
+
attr_accessor :from_currency, :to_currency
|
5
|
+
|
6
|
+
def initialize config = {}
|
7
|
+
self.from_currency = config[:from_currency]
|
8
|
+
self.to_currency = config[:to_currency]
|
9
|
+
end
|
10
|
+
|
11
|
+
def spot_rate
|
12
|
+
default_converter.new(self.from_currency, self.to_currency).spot_rate
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def default_converter
|
18
|
+
require './lib/spot_rate/goog_currency_converter'
|
19
|
+
GoogCurrencyConverter
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require './lib/spot_rate'
|
3
|
+
|
4
|
+
puts Time.now
|
5
|
+
puts SpotRate.new(from_currency: 'USD', to_currency: 'JPY').spot_rate
|
6
|
+
#describe SpotRate do
|
7
|
+
# describe "#spot_rate" do
|
8
|
+
# it "returns a number (no stubbing + requires internet connection)" do
|
9
|
+
# converter = described_class.new(from_currency: 'USD', to_currency: 'JPY')
|
10
|
+
# expect(converter.spot_rate.class).to eq Float
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# it "returns the spot conversion rate from the from_currency to the to_currency" do
|
14
|
+
# Net::HTTP.stub(:get).and_return('{lhs: "1 U.S. dollar",rhs: "98.222 Japanese yen",error: "",icc: true}')
|
15
|
+
# converter = described_class.new(from_currency: 'USD', to_currency: 'JPY')
|
16
|
+
# expect(converter.spot_rate).to eq 98.222
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# it "handles errors when we get an error code back" do
|
20
|
+
# Net::HTTP.stub(:get).and_return('{lhs: "does not matter",rhs: "does not matter",error: "4",icc: true}')
|
21
|
+
# converter = described_class.new
|
22
|
+
# converter.from_currency = 'USD'
|
23
|
+
# converter.to_currency = 'BAD'
|
24
|
+
# expect { converter.spot_rate }.to raise_error(SpotRate::CurrencyNotFound)
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#end
|
data/spot_rate.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/spot_rate/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jeff Iacono"]
|
6
|
+
gem.email = ["jeff.iacono@gmail.com"]
|
7
|
+
gem.description = %q{get current currency spot rates with ruby}
|
8
|
+
gem.summary = %q{current currency spot rates}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "spot-rate"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = SpotRate::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "net-sftp"
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency "cane"
|
22
|
+
gem.add_development_dependency "rspec", [">= 2"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spot-rate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeff Iacono
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: net-sftp
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: cane
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2'
|
78
|
+
description: get current currency spot rates with ruby
|
79
|
+
email:
|
80
|
+
- jeff.iacono@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/spot_rate.rb
|
91
|
+
- lib/spot_rate/goog_currency_converter.rb
|
92
|
+
- lib/spot_rate/version.rb
|
93
|
+
- spec/spot_rate_spec.rb
|
94
|
+
- spot_rate.gemspec
|
95
|
+
homepage: ''
|
96
|
+
licenses: []
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.21
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: current currency spot rates
|
119
|
+
test_files:
|
120
|
+
- spec/spot_rate_spec.rb
|