Sha256: 219c0bdeb8efedd1b5f2261abe08f37b83ac443b01e3520cdb827df3edb55d95

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require "maxwell/converter"

module Maxwell
  class Base
    class << self
      def attr_scrape(*attr_scrapes)
        @acquirer_class = Class.new do
          attr_accessor *attr_scrapes
          @attr_scrapes = attr_scrapes

          def self.attr_scrapes
            @attr_scrapes
          end

          def initialize(nokogiri_obj)
            @html = nokogiri_obj
          end

          def result
            self.class.attr_scrapes.map { |k| [k, send(k)]  }.to_h
          end
        end
      end

      def regist_strategy(link_selectore=nil, &strategy_blk)
        @link_selectore = link_selectore
        @strategy_blk = strategy_blk
      end

      def regist_handler(&handler_blk)
        @handler_blk = handler_blk
      end

      def use_poltergeist(value)
        @use_poltergeist = value
      end
    end

    def execute(root_url)
      if self.link_selectore
        html = Maxwell::Converter.call(root_url, use_poltergeist)
        html.css(self.link_selectore).each do |a|
          execute_for_result a[:href]
        end
      else
        execute_for_result root_url
      end
    end

    def use_poltergeist
      self.class.instance_eval("@use_poltergeist")
    end

    def link_selectore
      self.class.instance_eval("@link_selectore")
    end

    def strategy_blk
      self.class.instance_eval("@strategy_blk")
    end

    def handler_blk
      self.class.instance_eval("@handler_blk")
    end

    def acquirer_class
      self.class.instance_eval("@acquirer_class")
    end

    private
      def execute_for_result(tip_url)
        acquirer = acquirer_class.new(Maxwell::Converter.call(tip_url, use_poltergeist))
        acquirer.instance_eval &self.strategy_blk

        acquirer.result.tap do |result|
          self.handler_blk.call(result) if self.handler_blk
        end
      end
  end
end

class ::String
  def trim
    delete("\r\n\t")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maxwell-0.3.0 lib/maxwell.rb