class ExchangeRatesNBP::Clients::TableList

Constants

PATTERN

Public Class Methods

new(year, table_type) click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 6
def initialize(year, table_type)
  @year = year
  @table_type = table_type

  validate_year
end

Public Instance Methods

fetch_closest_to(date) click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 13
def fetch_closest_to(date)
  loop do
    table_id = table_id_for(date)
    return table_id unless table_id.nil?
    date -= 1
    return fetch_data_for_previous_year if date.year != @year
  end
end

Private Instance Methods

all_table_ids() click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 45
def all_table_ids
  if @year == Date.today.year
    response = request_table(FILE_NAME_CURRENT_YEAR)
  else
    response = request_table(FILE_NAME_YEAR_PATTERN.sub('{year}',
                                                        @year.to_s))

    if response.status == 404
      response = request_table(FILE_NAME_CURRENT_YEAR)
    end
  end

  response.to_s.scan(PATTERN)
end
fetch_data_for_previous_year() click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 29
def fetch_data_for_previous_year
  last_day = Date.new(@year - 1, 12, 31)
  self.class.new(@year - 1, @table_type).fetch_closest_to(last_day)
end
request_table(suffix) click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 68
def request_table(suffix)
  HTTP.get(BASE_URL + suffix, encoding: 'utf-8')
end
selected_table_ids() click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 39
def selected_table_ids
  @selected_table_ids ||= all_table_ids.select do |table_id|
    table_id =~ /^#{@table_type}/
  end
end
table_id_for(date) click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 34
def table_id_for(date)
  date_string = date.strftime('%y%m%d')
  selected_table_ids.detect { |l| l =~ /#{date_string}$/ }
end
table_list_url() click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 60
def table_list_url
  if @year == Date.today.year
    FILE_NAME_CURRENT_YEAR
  else
    FILE_NAME_YEAR_PATTERN.sub('{year}', @year.to_s)
  end
end
validate_year() click to toggle source
# File lib/exchange_rates_nbp/clients/table_list.rb, line 24
def validate_year
  raise 'Data for nbp.pl is available from 2002 onwards' if @year < 2002
  raise "Can't fetch data for the future" if @year > Date.today.year
end