Sha256: e4620fe60039c28c41a5e8939d7942828f3bb29574d7b7fdb993bc641f81ffab

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Calagator
  class Source < Calagator::ApplicationRecord
    class Importer < Struct.new(:source, :events)
      def initialize(params)
        self.source = Source.find_or_create_by(params)
      end

      def import
        return unless source.valid?

        self.events = source.create_events!

        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 => e
        add_error "Unknown error: #{e}"
      end

      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

      private

      def add_error(message)
        source.errors.add :base, message
        nil
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
koalagator-3.0.0 app/models/calagator/source/importer.rb
koalagator-2.1.1 app/models/calagator/source/importer.rb
koalagator-2.1.0 app/models/calagator/source/importer.rb
calagator2-2.1.1 app/models/calagator/source/importer.rb
calagator2-2.1.0 app/models/calagator/source/importer.rb