rupee 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest CHANGED
File without changes
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ Gemfile.lock
6
6
  doc
7
7
  pkg
8
8
  tmp
9
+ wiki
data/.rspec CHANGED
File without changes
data/README.md CHANGED
@@ -72,7 +72,20 @@ Fargo using the following (note that you only need to `require` the `quote`
72
72
  module):
73
73
 
74
74
  require "rupee/quote"
75
- Rupee::Quote.bloomberg "WFC", :price, :change, :pct_change
75
+ wfc = Rupee::Quote.new("WFC")
76
+
77
+ wfc.get :price, :change, :pct_change
78
+ #=> {:price=>24.96, :change=>0.17, :pct_change =>0.686}
79
+
80
+ wfc.price
81
+ #=> 24.96
82
+
83
+ wfc.change
84
+ #=> 0.17
85
+
86
+ `wfc.get` will return a hash of the requested information for the security.
87
+ Each valid parameter will also have its own utility method. The results will
88
+ update every `wfc.frequency` seconds (defaults to 15).
76
89
 
77
90
  Got it? Good. This will surely help you collect some rupees in real life.
78
91
 
@@ -24,10 +24,10 @@ module Rupee
24
24
 
25
25
  # Build the default sources that come with Rupee
26
26
  def build_sources
27
- @sources ||= []
27
+ @sources ||= {}
28
28
 
29
29
  # Bloomberg
30
- @sources << Source.new(:bloomberg, [:bberg, :bb, :b],
30
+ @sources[:bloomberg] = Source.new(:bloomberg, [:bberg, :bb, :b],
31
31
  :price => /(?:PRICE|VALUE): <span class="amount">([0-9.,NA-]{1,})/,
32
32
  :change => /Change<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
33
33
  :pct_change => /Change<\/td>\n<td class="value[^>]+>[0-9.,NA-]{1,} \(([0-9NA.,-]{1,})\%/,
@@ -41,8 +41,8 @@ module Rupee
41
41
  :volume => /Volume<\/td>\n<td class="value[^>]+>([0-9.,NA-]{1,})/,
42
42
  :mkt_cap => /Market Cap[^<]+<\/td>\n<td class="value">([0-9.,NA-]{1,})/,
43
43
  :p_e => /Price\/Earnings[^<]+<\/td>\n<td class="value">([0-9.,NA-]{1,})/)
44
- @sources << Source.new(:yahoo)
45
- @sources << Source.new(:google)
44
+ @sources[:yahoo] = Source.new(:yahoo)
45
+ @sources[:google] = Source.new(:google)
46
46
  @default_source = :bloomberg
47
47
  end
48
48
  end
data/lib/rupee/quote.rb CHANGED
@@ -5,43 +5,86 @@ autoload :URI, "uri"
5
5
  module Rupee
6
6
  # The quote and data import functionality in Rupee
7
7
  class Quote
8
- class << self
9
- # Retrieves the current price of a security
10
- def get(url, *params)
11
- results = {}
12
- params = [:price] if params.empty?
8
+ # A ticker symbol
9
+ attr_accessor :ticker
10
+
11
+ # The name of the quote source
12
+ attr_accessor :source
13
+
14
+ # The cached HTML
15
+ attr :html
16
+
17
+ attr_accessor :frequency
18
+
19
+ attr :next_pull
20
+
21
+ def initialize(ticker, opts = {})
22
+ opts = { :source => :bloomberg, :frequency => 15 }.merge opts
23
+ @ticker = ticker
24
+ @source = Quote.sources[opts[:source]]
25
+ @frequency = opts[:frequency]
26
+ @next_pull = Time.now
27
+ end
28
+
29
+ # Retrieves the current price of a security
30
+ def get(*params)
31
+ now = Time.now
32
+ params = [:price] if params.empty?
33
+
34
+ if now >= @next_pull
35
+ @next_pull = now + @frequency
36
+ url = BLOOMBERG_URL % ticker
37
+ @results = {}
13
38
  url = URI.parse(url)
14
- html = Net::HTTP.start(url.host, url.port) do |http|
39
+ @html = Net::HTTP.start(url.host, url.port) do |http|
15
40
  http.get url.request_uri
16
41
  end.body
17
42
 
18
- params.each do |p|
19
- results[p] = @sources[0].params[p].match(html)[1]
43
+ @source.params.each do |param, regex|
44
+ begin
45
+ @results[param] = parse(regex.match(html)[1])
46
+ rescue
47
+ @results[param] = nil
48
+ end
20
49
  end
50
+ end
21
51
 
22
- results
52
+ if params.length == 1
53
+ @results[params[0]]
54
+ else
55
+ @results.keep_if { |r| params.include?(r) }
23
56
  end
57
+ end
24
58
 
25
- # Retrieves the current price of a security from Bloomberg
26
- def bloomberg(ticker, *params)
27
- get BLOOMBERG_URL % ticker, *params
59
+ [:price, :change, :pct_change, :date, :time, :bid, :ask, :open, :high,
60
+ :low, :volume, :mkt_cap, :p_e].each do |method_name|
61
+ define_method method_name do
62
+ get method_name
28
63
  end
64
+ end
29
65
 
30
- private
66
+ private
31
67
 
32
- # The URL for Bloomberg's quotes service
33
- BLOOMBERG_URL = "http://www.bloomberg.com/apps/quote?ticker=%s"
68
+ # The URL for Bloomberg's quotes service
69
+ BLOOMBERG_URL = "http://www.bloomberg.com/apps/quote?ticker=%s"
34
70
 
35
- # Returns an intepretation of an abbreviated source name
36
- def shorten_source(source)
37
- case source.downcase.to_sym
38
- when :"", :bloomberg, :bberg, :bb, :b
39
- :bloomberg
40
- when :google, :goog, :g
41
- :google
42
- when :yahoo!, :yahoo, :yhoo, :y!, :y
43
- :yahoo
44
- end
71
+ # Returns an intepretation of an abbreviated source name
72
+ def shorten_source(source)
73
+ case source.downcase.to_sym
74
+ when :"", :bloomberg, :bberg, :bb, :b
75
+ :bloomberg
76
+ when :google, :goog, :g
77
+ :google
78
+ when :yahoo!, :yahoo, :yhoo, :y!, :y
79
+ :yahoo
80
+ end
81
+ end
82
+
83
+ def parse(result)
84
+ begin
85
+ Float(result)
86
+ rescue
87
+ result
45
88
  end
46
89
  end
47
90
  end
data/lib/rupee/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Rupee
2
2
  # The current version
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
@@ -2,21 +2,21 @@ require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
3
  describe Rupee::Quote do
4
4
  it "should automatically have a Bloomberg source" do
5
- Rupee::Quote.sources[0].name.should == :bloomberg
5
+ Rupee::Quote.sources.should include :bloomberg
6
6
  end
7
7
 
8
8
  describe "when pulling quotes" do
9
9
  describe "without any parameters specified" do
10
10
  before :each do
11
- @wfc = Rupee::Quote.bloomberg("WFC")
11
+ @wfc = Rupee::Quote.new("WFC")
12
12
  end
13
13
 
14
14
  it "should default to pulling the price" do
15
- @wfc.should include :price
15
+ @wfc.get.should == @wfc.price
16
16
  end
17
17
 
18
18
  it "should return a price" do
19
- @wfc[:price].to_f.should be_a_kind_of Float
19
+ @wfc.price.should be_a_kind_of Float
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rupee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &71003600 !ruby/object:Gem::Requirement
16
+ requirement: &72721210 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *71003600
24
+ version_requirements: *72721210
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &71002450 !ruby/object:Gem::Requirement
27
+ requirement: &72720960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *71002450
35
+ version_requirements: *72720960
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: autotest
38
- requirement: &70999390 !ruby/object:Gem::Requirement
38
+ requirement: &72720730 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '4.0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70999390
46
+ version_requirements: *72720730
47
47
  description: rupee aims to provide user-friendly tools for use in financial gems and
48
48
  applications.
49
49
  email: