Updated Gems, improved quote retrieval
[smr.git] / gui / lib / smr / reapers / onvista.rb
blob88f35d0ffdd0fc4071d308718e5500abb2e851ce
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
16 require 'open-uri'
17 require 'nokogiri'
19 module Smr::Reapers # :nodoc:
21     class Onvista
22         include Smr::Reapers
23         QUERY_URL = 'http://www.onvista.de/suche?searchValue=%s'
25         def initialize(security)
26             raise 'Security object required' unless security.is_a? Security
27             raise 'Security.type not supported by this reaper' unless Onvista.type_supported? security.type
28             @security = security
29         end
31         ##
32         # List of Security#types this reaper can retrieve data for.
33         def Onvista.security_types
34             [ :unknown, :stock ]
35         end
37         ##
38         # tell whether :type can be handled by this reaper.
39         def Onvista.type_supported?(type)
40             Onvista.security_types.include? type.to_sym
41         end
43         ##
44         # Retrieve current Quote for Security and safe! it on success.
45         def quote
46             src = open(QUERY_URL % @security.symbol)
47             page = Nokogiri::HTML(src) do |config|
48                 config.strict.noblanks
49             end
51             # check whether :symbol was found actually
52             return false unless  page.css('div.WERTPAPIER_DETAILS').text.include? @security.symbol
54             # look for quote and supplementary details
55             page.css('article.HANDELSPLAETZE table tr').each do |dataline|
56                 next if dataline.child.name == 'th'           # skip table header
57                 next if dataline.text.include? 'geschlossen'  # skip currently closed exchange
58 #p '---'
59 #dataline.element_children.each{ |column| p '==> %s %s: %s' %  [ column.name, column[:class], column.text ] }
60                 q = Quote.new(:id_security=>@security.id)
62                 if match = /\d{1,},\d{2,}\s[A-Z]{3}/.match(dataline.element_children[2].text) then
63                     (quotestr, currency) = match.to_s.split(' ')
64                     q.quote = quotestr.gsub(',','.').to_f
65                     q.time = Time.parse(dataline.element_children[5].text + dataline.element_children[6].text)
66 #                   (q.bid, q.ask) = dataline.element_children[7].text.split('/')
67                     q.exchange = dataline[:class]
68                 else next end  # skip to next line when column 2 not looks like a quote
70                 # the first valid quote is whats taken
71                 return q
72             end
74             false
75         end
76     end
78 end # module