Class: Calagator::Source

Inherits:
ActiveRecord::Base
  • Object
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.



63
64
65
# File 'app/models/calagator/source.rb', line 63

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

#to_eventsObject

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

Raises:

  • (ActiveRecord::RecordInvalid)


56
57
58
59
60
# File 'app/models/calagator/source.rb', line 56

def to_events
  raise ActiveRecord::RecordInvalid, self unless valid?
  self.imported_at = Time.now
  Source::Parser.to_events(url: url, source: self)
end

#url=(value) ⇒ Object

Normalize the URL.



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

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