Class: Rupee::Quote

Inherits:
Object
  • Object
show all
Defined in:
lib/rupee/quote.rb

Overview

An object representing a security quote from an online source. With a Rupee::Quote object, you can retrieve the most recent information on a particular security using either the get method or the helper methods for each parameter (e.g. price, change):

require "rupee/quote"

wfc = Rupee::Quote.new(:wfc)

wfc.get :price, :change, :pct_chg
# => {:price=>24.96, :change=>0.17, :pct_chg =>0.686}

wfc.price
# => 24.96

wfc.change
# => 0.17

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ticker, opts = {}) ⇒ Quote

Creates a new Rupee::Quote object.

wfc = Rupee::Quote.new(:wfc)

is equivalent to

wfc = Rupee::Quote.new(:wfc, :source => :bloomberg, :frequency => 15)

Configuration options

  • :source - The name of the source (default is :bloomberg).

    • :bloomberg - The Bloomberg quote service

    • :google - The Google Finance quote service

    • :yahoo - The Yahoo! quote service

  • :frequency - How often the quote will seek new values from the quote source, in seconds (default is 15).



48
49
50
51
52
53
54
# File 'lib/rupee/quote.rb', line 48

def initialize(ticker, opts = {})
  opts = { :source => :bloomberg, :frequency => 15 }.merge opts
  @ticker = ticker.upcase
  @source = Source.find(opts[:source])
  @frequency = opts[:frequency]
  @next_pull = Time.now
end

Instance Attribute Details

#frequencyObject

The frequency in seconds that a quote’s information should be updated



28
29
30
# File 'lib/rupee/quote.rb', line 28

def frequency
  @frequency
end

#next_pullObject (readonly)

The time at which the next pull from the online quote source will occur



30
31
32
# File 'lib/rupee/quote.rb', line 30

def next_pull
  @next_pull
end

#sourceObject

The name of the quote source



26
27
28
# File 'lib/rupee/quote.rb', line 26

def source
  @source
end

#tickerObject

A ticker symbol



24
25
26
# File 'lib/rupee/quote.rb', line 24

def ticker
  @ticker
end

Instance Method Details

#bid_askObject

The bid-ask spread



120
121
122
# File 'lib/rupee/quote.rb', line 120

def bid_ask
  diff bid, ask
end

#get(*params) ⇒ Object Also known as: []

Retrieves the current information for a security

Rupee::Quote.new(:wfc).get :price, :change, :pct_chg
# => {:price=>24.96, :change=>0.17, :pct_chg =>0.686}


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rupee/quote.rb', line 60

def get(*params)
  now = Time.now
  params = [:price] if params.empty?

  if now >= @next_pull
    @next_pull = now + @frequency
    @results = {}
    @ticker = "#{@ticker.to_s}:US" if @source == Source::BLOOMBERG && !@ticker.to_s.include?(":")
    url = URI.parse(@source.url % @ticker)
    res = Net::HTTP.start(url.host, url.port) do |http|
      http.get url.request_uri
    end

    case res
    when Net::HTTPSuccess
      @source.params.each do |param, regex|
        if res.body =~ regex
          full_text, match = $&, $1

          # Special handling for <tt>:change</tt> because quote services
          # insist on using pretty pictures instead of minus signs
          if param == :change
            if (Source::BLOOMBERG || Source::YAHOO) && full_text =~ /down/
              match = "-#{match}"
            end
          end
          @results[param] = parse(match)
        else
          @results[param] = nil
        end
      end
    else
      res.error!
    end
  end

  if params.length == 1
    @results[params[0]]
  else
    @results.keep_if { |r| params.include?(r) }
  end
end

#rangeObject Also known as: trading_range

The daily trading range



125
126
127
# File 'lib/rupee/quote.rb', line 125

def range
  diff low, high
end