# frozen_string_literal: true
require "net/http"
require "provider/adapters/adapter"
class Provider < Sequel::Model(:providers)
module Adapters
# Bank Indonesia.
# Fetches daily transaction exchange rates for 26 currencies against the Indonesian rupiah (IDR).
# The page is SharePoint-based; we POST a search per currency and parse the HTML result table.
# Rates are buy/sell; we compute the mid-rate as (sell + buy) / 2.
class BI < Adapter
BASE_URL = "https://www.bi.go.id/en/statistik/informasi-kurs/transaksi-bi/default.aspx"
USER_AGENT = "Mozilla/5.0 (compatible; Frankfurter/2.0; +https://frankfurter.dev)"
class << self
def backfill_range = 90
end
def fetch(after: nil, upto: nil)
end_date = upto || Date.today
from_str = after.strftime("%d-%b-%Y")
to_str = end_date.strftime("%d-%b-%Y")
page = load_page
prefix = extract_prefix(page)
tokens = extract_tokens(page)
cookies = @cookies
currencies = extract_currencies(page)
dataset = []
first = true
currencies.each do |currency|
sleep(1) unless first
first = false
html = search(prefix:, tokens:, cookies:, currency:, from_str:, to_str:)
dataset.concat(parse(html, currency:))
end
dataset
end
def parse(html, currency:)
table = html[%r{gvSearchResult2"[^>]*>(.*?)}mi, 1]
return [] unless table
records = []
table.scan(%r{