Class: Calagator::Source::Importer

Inherits:
Struct
  • Object
show all
Defined in:
app/models/calagator/source/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Importer

Returns a new instance of Importer.



5
6
7
# File 'app/models/calagator/source/importer.rb', line 5

def initialize params
  self.source = Source.find_or_create_by(params)
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events

Returns:

  • (Object)

    the current value of events



4
5
6
# File 'app/models/calagator/source/importer.rb', line 4

def events
  @events
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



4
5
6
# File 'app/models/calagator/source/importer.rb', line 4

def source
  @source
end

Instance Method Details

#failure_messageObject



29
30
31
32
33
34
35
# File 'app/models/calagator/source/importer.rb', line 29

def failure_message
  if events.nil?
    "Unable to import: #{source.errors.full_messages.to_sentence}"
  else
    "Unable to find any upcoming events to import from this source"
  end
end

#importObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/calagator/source/importer.rb', line 9

def import
  return unless source.valid?
  self.events = source.create_events!

  self.events.present?

rescue Source::Parser::NotFound
  add_error "No events found at remote site. Is the event identifier in the URL correct?"
rescue Source::Parser::HttpAuthenticationRequiredError
  add_error "Couldn't import events, remote site requires authentication."
rescue OpenURI::HTTPError
  add_error "Couldn't download events, remote site may be experiencing connectivity problems."
rescue Errno::EHOSTUNREACH
  add_error "Couldn't connect to remote site."
rescue SocketError
  add_error "Couldn't find IP address for remote site. Is the URL correct?"
rescue Exception => e
  add_error "Unknown error: #{e}"
end