Class: CurrencySpy::Nbp

Inherits:
Scraper show all
Defined in:
lib/currency_spy/nbp.rb

Overview

A class designed to fetch currency information from the website of The Polish National Bank (Narodowy Bank Polski).

Constant Summary

Constants inherited from Scraper

Scraper::RATE_DATA

Instance Attribute Summary

Attributes inherited from Scraper

#available_codes, #currency_code, #institution, #url

Instance Method Summary collapse

Methods inherited from Scraper

#fetch_rates, #page

Constructor Details

#initializeNbp

Constructor method. Initializes the following:

  • a url of the source

  • and the name of the source

  • a list of currency codes available



10
11
12
13
14
15
# File 'lib/currency_spy/nbp.rb', line 10

def initialize
  super
  @url = 'http://www.nbp.pl/home.aspx?f=/kursy/kursyc.html'
  @institution = "Narodowy Bank Polski"
  @available_codes = %w(EUR USD GBP CHF AUS AUD CAD CZK DKK NOK SEK)
end

Instance Method Details

#buy_rateObject

Get the buying rate from the parsed website



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/currency_spy/nbp.rb', line 18

def buy_rate
  regexp = Regexp.new(currency_code)
  page.search('//tr[@valign="middle"]').each do |tr|
    tr.search('td').each do |td|
      if (regexp.match(td.content))
        return td.next_element.content.sub(',','.').to_f
      end
    end
  end
  return nil
end

#rate_timeObject

Get the time for this rate (based on the information on the website)



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/currency_spy/nbp.rb', line 44

def rate_time
  regexp = Regexp.new(/\d\d\d\d-\d\d-\d\d/)
  res = nil
  page.search('//p[@class="nag"]').each do |p|
    p.search('b').each do |b|
      if (regexp.match(b.content))
        res = b.content
      end
    end
  end
  return DateTime.strptime(res, "%Y-%m-%d")
end

#sell_rateObject

Get the selling rate from the parsed website



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/currency_spy/nbp.rb', line 31

def sell_rate
  regexp = Regexp.new(currency_code)
  page.search('//tr[@valign="middle"]').each do |tr|
    tr.search('td').each do |td|
      if (regexp.match(td.content))
        return td.next_element.next_element.content.sub(',','.').to_f
      end
    end
  end
  return nil
end