Typo:Stock Plugin


Intro

Have you ever wanted to have realtime quotes in your blog? I know I have. That’s why I developed typo:stock.

How?

Get it out of svn:


wget http://modzer0.cs.uaf.edu/repos/hank/code/typo_stock_controller.rb

and dump it into typo/components/plugins/textfilters

Then, if you are running production mode, restart your webserver.

Use it.

You can style it like I’ve done here using typostock_down, typostock_neutral, and typostock_up in your css. The class depends on the difference between the current price and the bought price. If no bought price is given, it evaluates to neutral.

What do I need?

It uses SOAP via WSDL which as far as I can tell comes standard with Ruby. So, hopefully, you don’t need anything but a working Typo.

What does it look like…on the inside?

Here’s the script in fulltext:


require 'syntax/convertors/html'
class Plugins::Textfilters::StockController < TextFilterPlugin::MacroPre
  plugin_display_name "Stocks"
  plugin_description "Insert inline stock quotes"

  def self.macrofilter(controller,content,attrib,params,text="")

    security = text.to_s

    # WSDL Factory Setup
    require 'soap/wsdlDriver'
    factory = SOAP::WSDLDriverFactory.new("http://ws.invesbot.com/stockquotes.asmx?WSDL")
    soap = factory.create_rpc_driver
    soapResponse = soap.GetQuote(:symbol => security)
    price = soapResponse.getQuoteResult.stockQuote.price
    current_price = price.strip_html.to_f
    current_price = price.strip_html.to_f

    # If the bought price is given from attrib, use it.  Else, nil.
    bought = (attrib['bought'] and !attrib['bought'].blank?) ? attrib['bought'].to_f : nil
    # If bought is nil, moving is neutral, else base it on the current_price
    moving = bought.nil? ? 'neutral' : ['neutral','up','down'][current_price <=> bought]
    soap.reset_stream
    retvalue = "<span class=\"typostock_#{moving}\">#{security}: #{current_price}"
    retvalue << " (#{bought})" if not bought.nil?
    retvalue << "</span>"
    return retvalue
  end

end

UPDATE:

So, I realized that my blog was moving very slow, and it eventually ended up generating so many fcgi processes on the server that it crashed – which was weird. Turns out the GoogleBot was looping generating insane requests, possibly attributed to the SOAP calls this plugin does. The sadness.



Comments are closed