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

Defined Under Namespace

Classes: Facebook, Hcal, HttpAuthenticationRequiredError, Ical, Meetup, 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



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

def source
  @source
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



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

def url
  @url
end

Class Method Details

.<=>(other) ⇒ Object



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

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



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

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

.labelsObject

Returns an Array of sorted string labels for the parsers.



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

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

.read_url(url) ⇒ Object



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

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.



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

def self.to_events(url: nil, source: nil)
  # Return events from the first parser that suceeds
  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)


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

def to_events
  raise NotImplementedError
end