DEV Community

Cover image for Get update Exchange rate from NBC bank with RoR and Wombat
radin reth
radin reth

Posted on Edited on

Get update Exchange rate from NBC bank with RoR and Wombat

I was assigned a task that do exchange currency from US dollar into Khmer riel.

The exchange rate is based on NBC ( National Bank of Cambodia). Unfortunately, the NBC website does not provide provide any Api to get the exchange rate so I am decided to do crawling.

First, I am using sidekiq-cron to perform the crawling every 3 hours. Then, I use wombat gem to perform the crawling. Once, It receives the updated exchange, the system will update and calculate it as Khmer riel.

exchange_rate_spider.rb

## Crawling NBC With OpenSSL verification on(default)
# raise <SSL_connect returned=1 errno=0 state=error: certificate verify failed>

## Disable OpenSSL verfication
# https://github.com/sparklemotion/mechanize/issues/375
# server doesn't provide intermediate certificate to OpenSSL
# Wombat.crawl { mechanize.verify_mode = OpenSSL::SSL::VERIFY_NONE }

module Spider
  class ExchangeRateSpider
    include Wombat::Crawler

    base_url 'https://www.nbc-website.org.kh'
    path '/english/economic_research/exchange_rate_path.php'

    usd_to_khr xpath: '//tr[2]/td/font', &:to_f
  end
end
Enter fullscreen mode Exit fullscreen mode

exchange_rate_job.rb

class ExchangeRateJob < ApplicationJob
  queue_as :default

  def perform
    Setting.instance.update!(usd_to_khr: spider.crawl['usd_to_khr'])
  end

  private

  def spider
    return @spider unless @spider.nil?

    @spider ||= Spider::ExchangeRateSpider.new
    @spider.mechanize.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @spider
  end
end
Enter fullscreen mode Exit fullscreen mode

schedule.yml

exchange_rate_job:
  cron: 'every 3 hours'
  class: 'ExchangeRateJob'
  status: enabled
  description: 'updates exchange rate from NBC'
Enter fullscreen mode Exit fullscreen mode

Thank you for your reading!

Top comments (0)