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

Hcal, Ical

Defined Under Namespace

Classes: Hcal, HttpAuthenticationRequiredError, Ical, NotFound

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



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

def source
  @source
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



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

def url
  @url
end

Class Method Details

.<=>(other) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



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

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

.labelsObject

Returns an Array of sorted string labels for the parsers.



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

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

.read_url(url) ⇒ Object



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

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

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

Return an Array of unsaved Event instances.



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

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

  events || []
end

Instance Method Details

#to_eventsObject

Raises:

  • (NotImplementedError)


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

def to_events
  raise NotImplementedError
end