Class: Calagator::Source

Inherits:
ApplicationRecord show all
Includes:
DecodeHtmlEntitiesHack
Defined in:
app/models/calagator/source.rb,
app/models/calagator/source/importer.rb

Defined Under Namespace

Classes: Importer, Parser

Instance Method Summary collapse

Methods included from DecodeHtmlEntitiesHack

#decode_html_entities, included

Instance Method Details

#create_events!Object

Create events for this source. Returns the events created. URL must be set for this source for this to work.



41
42
43
44
# File 'app/models/calagator/source.rb', line 41

def create_events!
  save!
  to_events.select { |event| event.valid? && !event.old? }.each(&:save!)
end

#nameObject

Return the name of the source, which can be its title or URL.



66
67
68
# File 'app/models/calagator/source.rb', line 66

def name
  [title, url].detect(&:present?)
end

#to_eventsObject

Returns an Array of Event objects that were read from this source.

Raises:

  • (ActiveRecord::RecordInvalid)


58
59
60
61
62
63
# File 'app/models/calagator/source.rb', line 58

def to_events
  raise ActiveRecord::RecordInvalid, self unless valid?

  self.imported_at = Time.now.in_time_zone
  Source::Parser.to_events(url: url, source: self)
end

#url=(value) ⇒ Object

Normalize the URL.



47
48
49
50
51
52
53
54
55
# File 'app/models/calagator/source.rb', line 47

def url=(value)
  url = URI.parse(value.strip)
  unless %w[http https ftp].include?(url.scheme) || url.scheme.nil?
    url.scheme = 'http'
  end
  self[:url] = url.scheme.nil? ? 'http://' + value.strip : url.to_s
rescue URI::InvalidURIError
  false
end