Class: Time
Class Method Summary collapse
- .can_parse?(date) ⇒ Boolean
-
.translate_for_parsing(date_as_string) ⇒ Object
We have a lot of timestamps in portuguese and Ruby can’t parse’em So, this function will translate portuguese date related terms to english for correct parsing, because we can’t afford so many feed entries with wrong timestamps.
Instance Method Summary collapse
- #to_db_format(format = :default) ⇒ Object (also: #to_s)
Class Method Details
.can_parse?(date) ⇒ Boolean
9 10 11 12 13 14 15 16 |
# File 'lib/spix_parser/core_ext.rb', line 9 def self.can_parse?(date) begin Time.parse(date) rescue StandardError => e return false end true end |
.translate_for_parsing(date_as_string) ⇒ Object
We have a lot of timestamps in portuguese and Ruby can’t parse’em So, this function will translate portuguese date related terms to english for correct parsing, because we can’t afford so many feed entries with wrong timestamps
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/spix_parser/core_ext.rb', line 21 def self.translate_for_parsing(date_as_string) # First, add leading zero to days of month below 10 formatted_date = date_as_string.sub(/\A[a-zA-Z]+\,\s{1}(\d)[^\d]/, '0\1 ') day_names = {"Domingo" => "Sunday", "Segunda" => "Monday", "Terça" => "Tuesday", "Quarta" => "Wednesday", "Quinta" => "Thursday", "Sexta" => "Friday", "Sábado" => "Saturday", "Sabado" => "Saturday"} abbr_day_names = {"Dom" => "Sun", "Seg" => "Mon", "Ter" => "Tue", "Qua" => "Wed", "Qui" => "Thu", "Sex" => "Fri", "Sáb" => "Sat", "Sab" => "Sat"} month_names = {"Janeiro" => "January", "Fevereiro" => "February", "Março" => "March", "Marco" => "March", "Abril" => "April", "Maio" => "May", "Junho" => "June", "Julho" => "July", "Agosto" => "August", "Setembro" => "September", "Outubro" => "October", "Novembro" => "November", "Dezembro" => "December"} abbr_month_names = {"Jan" => "Jan", "Fev" => "Feb", "Abr" => "Apr", "Mai" => "May", "Ago" => "Aug", "Set" => "Sep", "Out" => "Oct", "Dez" => "Dec"} day_names.each do |key, value| formatted_date.sub!(key, value) end abbr_day_names.each do |key, value| formatted_date.sub!(key, value) end month_names.each do |key, value| formatted_date.sub!(key, value) end abbr_month_names.each do |key, value| formatted_date.sub!(key, value) end formatted_date end |
Instance Method Details
#to_db_format(format = :default) ⇒ Object Also known as: to_s
3 4 5 |
# File 'lib/spix_parser/core_ext.rb', line 3 def to_db_format(format=:default) format == :default ? to_s_default : strftime("%Y-%m-%d %H:%M:%S").strip end |