Sha256: 1daa4b3d9bd95467e17863d63438df1d22e0f94bff05ac02af2881416e9a3edb

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require 'open-uri'
require 'fileutils'
require 'aranha/parsers/source_address'

module Aranha
  module Parsers
    class Base
      LOG_DIR_ENVVAR = 'ARANHA_PARSERS_LOG_DIR'

      attr_reader :source_address

      def initialize(url)
        @source_address = ::Aranha::Parsers::SourceAddress.new(url)
        log_content(source_address.serialize, '-source-address')
      end

      delegate :url, to: :source_address

      def content
        s = source_address.content
        log_content(s)
        s
      end

      private

      def log_content(content, suffix = '')
        path = log_file(suffix)

        return unless path

        File.open(path, 'wb') { |file| file.write(content) }
      end

      def log_file(suffix)
        dir = log_parsers_dir
        return nil unless dir

        f = ::File.join(dir, "#{self.class.name.parameterize}#{suffix}.log")
        FileUtils.mkdir_p(File.dirname(f))
        f
      end

      def log_parsers_dir
        return ENV[LOG_DIR_ENVVAR] if ENV[LOG_DIR_ENVVAR]
        return ::Rails.root.join('log', 'parsers') if rails_root_exist?

        nil
      end

      def rails_root_exist?
        ::Rails.root
        true
      rescue NameError
        false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aranha-parsers-0.5.0 lib/aranha/parsers/base.rb
aranha-parsers-0.4.0 lib/aranha/parsers/base.rb
aranha-parsers-0.3.0 lib/aranha/parsers/base.rb