Sha256: 64852883609271b7737e441c6b07c7325697ed2aa96f453a8e3d4cd5aebb7c6b

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true
module Aranha
  class Address < ActiveRecord::Base
    include ::Eac::InequalityQueries

    add_inequality_queries(:created_at)

    class << self
      def set_start_point(url, processor)
        start_points[url] = processor
      end

      def add_start_points
        start_points.each do |url, processor|
          add(url, processor)
        end
      end

      def add(url, processor)
        a = find_or_initialize_by(url: url)
        a.processor = processor
        a.save!
      end

      def clear_expired
        q = by_created_at_lt(Time.zone.now - 12.hours)
        Rails.logger.info("Addresses expired: #{q.count}")
        q.destroy_all
      end

      private

      def start_points
        @start_points ||= {}
      end
    end

    validates :url, presence: true, uniqueness: true
    validates :processor, presence: true

    scope :unprocessed, lambda {
      where(processed_at: nil)
    }

    def to_s
      "#{processor}|#{url}"
    end

    def process
      ActiveRecord::Base.transaction do
        processor.constantize.new(url).process
        self.processed_at = Time.zone.now
        save!
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aranha-0.0.4 app/models/aranha/address.rb
aranha-0.0.3 app/models/aranha/address.rb
aranha-0.0.2 app/models/aranha/address.rb
aranha-0.0.1 app/models/aranha/address.rb