module RbaRates
Constants
- CASH_CSV_URL
- CASH_SERIES_ID
- EXCHANGE_CSV_URL
- PAIR_ALIASES
- SERIES_LABELS
- STYLE_TITLE
Public Instance Methods
Source
# File lib/rba_rates.rb, line 90 def fetch_cash_csv(fetch: nil) body = fetch ? fetch.call(CASH_CSV_URL) : RabbotHttp.fetch(CASH_CSV_URL) parse_cash_csv(body) rescue StandardError nil end
Source
# File lib/rba_rates.rb, line 59 def fetch_exchange_csv(fetch: nil) body = fetch ? fetch.call(EXCHANGE_CSV_URL) : RabbotHttp.fetch(EXCHANGE_CSV_URL) parse_exchange_csv(body) rescue StandardError nil end
Source
# File lib/rba_rates.rb, line 150 def format_cash(result) return format_missing("cash rate") unless result lines = [ IrcFormat.report_header(tag: "RBA", title: "Cash rate target", style: STYLE_TITLE), "#{result[:rate]}% — as at #{result[:date]}", IrcFormat.report_footer("target cash rate", "Reserve Bank of Australia") ] lines.join("\n") end
Source
# File lib/rba_rates.rb, line 161 def format_missing(what) lines = [ IrcFormat.report_header(tag: "RBA", title: what, style: STYLE_TITLE), "Rate data unavailable right now.", IrcFormat.report_footer("RBA statistics", "Reserve Bank of Australia") ] lines.join("\n") end
Source
# File lib/rba_rates.rb, line 139 def format_pair(result) return format_missing("exchange rate") unless result lines = [ IrcFormat.report_header(tag: "RBA", title: result[:label], style: STYLE_TITLE), "#{result[:value]} — as at #{result[:date]}", IrcFormat.report_footer("daily indicative rate", "Reserve Bank of Australia") ] lines.join("\n") end
Source
# File lib/rba_rates.rb, line 121 def lookup_pair(pair, fetch: nil) series_id = resolve_series(pair) return nil unless series_id table = fetch_exchange_csv(fetch: fetch) return nil unless table value = table[:values][series_id] return nil unless value { series_id: series_id, label: SERIES_LABELS.fetch(series_id, series_id), value: value, date: table[:date] } end
Source
# File lib/rba_rates.rb, line 97 def parse_cash_csv(body) rows = CSV.parse(body.to_s) series_row = rows.find { |row| row[0].to_s.strip == "Series ID" } return nil unless series_row cash_index = series_row.index(CASH_SERIES_ID) return nil unless cash_index data_rows = rows.select do |row| row[0].to_s.match?(/\A(?:\d{2}-[A-Za-z]{3}-\d{4}|[A-Za-z]{3}-\d{4})\z/) end return nil if data_rows.empty? latest = data_rows.reverse.find { |row| !row[cash_index].to_s.strip.empty? } return nil unless latest { date: latest[0].to_s.strip, rate: latest[cash_index].to_s.strip } rescue StandardError nil end
Source
# File lib/rba_rates.rb, line 66 def parse_exchange_csv(body) rows = CSV.parse(body.to_s) series_row = rows.find { |row| row[0].to_s.strip == "Series ID" } return nil unless series_row series_ids = series_row[1..] data_rows = rows.select { |row| row[0].to_s.match?(/\A\d{2}-[A-Za-z]{3}-\d{4}\z/) } return nil if data_rows.empty? latest = data_rows.last date = latest[0].to_s.strip values = {} series_ids.each_with_index do |series_id, index| next if series_id.to_s.strip.empty? value = latest[index + 1].to_s.strip values[series_id.to_s.strip] = value unless value.empty? end { date: date, values: values } rescue StandardError nil end
Source
# File lib/rba_rates.rb, line 52 def resolve_series(pair) key = pair.to_s.strip.downcase.gsub(/[^a-z]/, "") return nil if key.empty? PAIR_ALIASES[key] end