yahoofx 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  ## Foreign exchange (forex) rate scraper
4
4
 
5
- Gets real time foreign currency exchange rates from Yahoo Finance.
5
+ Gets foreign currency exchange rates from Yahoo Finance.
6
+
7
+ ### Installation
8
+
9
+ Simply the gem: `gem install yahoofx`
10
+
11
+ ### Usage
6
12
 
7
13
  Includes shell command that returns the rate. Usage: yahoofx
8
14
  [currency pair]
@@ -10,3 +16,15 @@ Includes shell command that returns the rate. Usage: yahoofx
10
16
  E.g. `yahoofx eurusd` outputs 1.2925 if the price of 1 EUR is 1.2925 USD
11
17
 
12
18
  To use this in your ruby code, this would return the rate as a Float: `Yahoofx::Pair.new(:eur, :usd).bid`
19
+
20
+ ## License
21
+
22
+ The MIT License (MIT)
23
+
24
+ Copyright © 2012 Lau Taarnskov
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
27
+
28
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
29
+
30
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/yahoofx CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'yahoofx'
3
- raise "Usage: yahoofx [currencypair] eg. yahoofx eurusd" unless ARGV[0].length==6
4
- p Yahoofx::Pair.new(ARGV[0][0..2], ARGV[0][3..5]).bid
3
+ raise "Usage: yahoofx [currencypair] eg. 'yahoofx eurusd' or 'yahoofx 100 eur in usd'" unless ARGV[0] && (ARGV[0].length==6 || ARGV.join(" ") =~ /in/)
4
+ p Yahoofx::Converter.new.answer(ARGV.join(" "))
@@ -0,0 +1,31 @@
1
+ module Yahoofx
2
+ class Converter
3
+ def initialize(rate_class = Yahoofx::Pair)
4
+ @rate_class = rate_class
5
+ end
6
+
7
+ # Either returns a currency rate or converts an amount from one currency to another.
8
+ # Example to convert currency: "119.99 EUR in USD"
9
+ # Example to show currency rate: "EURUSD"
10
+ def answer(p)
11
+ if p.length==6
12
+ return @rate_class.new(p[0..2], p[3..5]).bid
13
+ else
14
+ convert p
15
+ end
16
+ end
17
+
18
+ def convert(input_string)
19
+ parsed = parse(input_string)
20
+ @rate_class.new(parsed[:from_currency], parsed[:to_currency]).bid*parsed[:from_amount]
21
+ end
22
+
23
+ # Example parameter: 100 EUR in USD
24
+ def parse(input_string)
25
+ arr = input_string.split(/[\p{Space}]+/)
26
+ { :from_amount => arr[0].to_f,
27
+ :from_currency => arr[1],
28
+ :to_currency => arr[3] }
29
+ end
30
+ end
31
+ end
data/lib/yahoofx/pair.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'net/http'
2
2
 
3
3
  module Yahoofx
4
- class Pair
4
+ class Pair
5
5
  # First and second currency code. The parameters could be "USD" and "EUR" for instance
6
6
  def initialize(first, second)
7
7
  @first, @second = first.to_s, second.to_s
@@ -21,7 +21,7 @@ module Yahoofx
21
21
  end
22
22
 
23
23
  def url_for_currency_pair
24
- URI("http://finance.yahoo.com/q?s=#{@first}#{@second}%3Dx&ql=1")
24
+ URI("http://finance.yahoo.com/q?s=#{@first.downcase}#{@second.downcase}%3Dx&ql=1")
25
25
  end
26
26
 
27
27
  def request_feed
data/lib/yahoofx.rb CHANGED
@@ -1 +1,2 @@
1
1
  require 'yahoofx/pair'
2
+ require 'yahoofx/converter'
@@ -0,0 +1,25 @@
1
+ require_relative "../test_helper"
2
+
3
+ describe Yahoofx::Converter do
4
+ before do
5
+ mock_eur_dkk_bid = MiniTest::Mock.new
6
+ mock_eur_dkk_bid.expect(:bid, 7.4556)
7
+ mock_rate_pair_class = MiniTest::Mock.new
8
+ mock_rate_pair_class.expect(:new, mock_eur_dkk_bid, ['EUR', 'DKK'])
9
+ @converter = Yahoofx::Converter.new(mock_rate_pair_class)
10
+ end
11
+
12
+ it "should convert amount from one to the other" do
13
+ assert_equal 745.56, @converter.convert("100 EUR in DKK").to_f
14
+ end
15
+
16
+ describe "when calling answer method" do
17
+ it "should convert amount when it looks like a conversion" do
18
+ assert_equal 745.56, @converter.answer("100 EUR in DKK").to_f
19
+ end
20
+
21
+ it "should show currency rate when two valid currency codes are provided" do
22
+ assert_equal 7.4556, @converter.answer("EURDKK").to_f
23
+ end
24
+ end
25
+ end
@@ -1,6 +1,6 @@
1
1
  require_relative "../test_helper"
2
2
 
3
- describe Yahoofx do
3
+ describe Yahoofx::Pair do
4
4
  before do
5
5
  page = IO.read(File.dirname(__FILE__) + '/../fixtures/eurusd.html')
6
6
  stub_request(:get, "http://finance.yahoo.com/q?ql=1&s=eurusd=x").
data/yahoofx.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'yahoofx'
3
- s.version = '0.0.2'
3
+ s.version = '0.1.0'
4
4
  s.executables << 'yahoofx'
5
5
  s.summary = "Gets currency rates from Yahoo Finance"
6
6
  s.description = "The gem scrapes Yahoo Finance and returns foreign exchange rates. Also comes with a shell command."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahoofx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
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: 2012-12-08 00:00:00.000000000 Z
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: webmock
@@ -40,10 +40,12 @@ files:
40
40
  - Rakefile
41
41
  - bin/yahoofx
42
42
  - lib/yahoofx.rb
43
+ - lib/yahoofx/converter.rb
43
44
  - lib/yahoofx/pair.rb
44
45
  - test/fixtures/eurusd.html
45
46
  - test/test_helper.rb
46
- - test/unit/yahoofx_test.rb
47
+ - test/unit/converter_test.rb
48
+ - test/unit/pair_test.rb
47
49
  - yahoofx.gemspec
48
50
  homepage: https://github.com/lau/yahoofx
49
51
  licenses:
@@ -73,4 +75,5 @@ summary: Gets currency rates from Yahoo Finance
73
75
  test_files:
74
76
  - test/fixtures/eurusd.html
75
77
  - test/test_helper.rb
76
- - test/unit/yahoofx_test.rb
78
+ - test/unit/converter_test.rb
79
+ - test/unit/pair_test.rb