Class: Calagator::Source::Parser

Inherits:
Struct
  • Object
show all
Defined in:
app/models/calagator/source/parser/http_authentication_required_error.rb,
app/models/calagator/source/parser.rb,
app/models/calagator/source/parser/not_found.rb

Overview

Exception raised if user requests parsing of a URL that requires authentication but none was provided.

Direct Known Subclasses

Facebook, Hcal, Ical, Meetup, Plancast

Defined Under Namespace

Classes: Facebook, Hcal, HttpAuthenticationRequiredError, Ical, Meetup, NotFound, Plancast

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



12
13
14
# File 'app/models/calagator/source/parser.rb', line 12

def source
  @source
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



12
13
14
# File 'app/models/calagator/source/parser.rb', line 12

def url
  @url
end

Class Method Details

.<=>(other) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'app/models/calagator/source/parser.rb', line 55

def self.<=>(other)
  # use site-specific parsers first, then generics alphabetically
  if self.url_pattern && !other.url_pattern
    -1
  elsif !self.url_pattern && other.url_pattern
    1
  else
    self.label <=> other.label
  end
end

.inherited(subclass) ⇒ Object



34
35
36
# File 'app/models/calagator/source/parser.rb', line 34

def self.inherited(subclass)
  parsers << subclass
end

.labelsObject

Returns an Array of sorted string labels for the parsers.



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

def self.labels
  parsers.map { |p| p.label.to_s }.sort_by(&:downcase)
end

.read_url(url) ⇒ Object



45
46
47
48
49
# File 'app/models/calagator/source/parser.rb', line 45

def self.read_url(url)
  RestClient.get(url).to_str
rescue RestClient::Unauthorized
  raise Source::Parser::HttpAuthenticationRequiredError.new
end

.to_events(url: nil, source: nil) ⇒ Object

Return an Array of unsaved Event instances.



14
15
16
17
18
19
20
21
# File 'app/models/calagator/source/parser.rb', line 14

def self.to_events(url: nil, source: nil)
  # Return events from the first parser that suceeds
  events = matched_parsers(url).lazy.collect { |parser|
    parser.new(url, source).to_events
  }.detect(&:present?)

  events || []
end

Instance Method Details

#to_eventsObject

Raises:

  • (NotImplementedError)


51
52
53
# File 'app/models/calagator/source/parser.rb', line 51

def to_events
  raise NotImplementedError
end