Advertisement
Guest User

service sample

a guest
Jan 11th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.62 KB | None | 0 0
  1. # frozen_string_literal: true
  2. require 'csv'
  3. require 'open-uri'
  4.  
  5. CSV_URL='http://sdw-wsrest.ecb.europa.eu/service/data/EXR/D.USD.EUR.SP00.A?format=csvdata'
  6.  
  7. class ParseRates
  8.   def self.perform(file: open(CSV_URL), date_column: 'TIME_PERIOD', value_column: 'OBS_VALUE')
  9.     last_rate_date = EurUsdRate.order(:date).last&.date || ''
  10.     CSV.foreach(file, :headers => true) do |row|
  11.       rh = row.to_hash
  12.       next if last_rate_date.to_s >= rh[date_column]
  13.       EurUsdRate.create(date: rh[date_column], value: rh[value_column].to_f)
  14.     end
  15.   rescue => e
  16.     puts "CSV parse & saving failed: #{e.message}"
  17.   end
  18. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement