Sha256: b511ee6f4a41be01ccbdcfb1209d20d7dc98f3fe8cd6eeea853baf83a5af3f21

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'nokogiri'

module Weneedfeed
  class Scraping
    class << self
      # @return [Faraday::Connection]
      def faraday_connection
        @faraday_connection ||= ::Faraday.new do |connection|
          connection.use ::Weneedfeed::FaradayResponseMiddleware
        end
      end
    end

    # @param [String, nil] description
    # @param [String, nil] item_description_selector
    # @param [String] item_link_selector
    # @param [String, nil] item_time_selector
    # @param [String] item_title_selector
    # @param [String] item_selector
    # @param [String] title
    # @param [String] url
    def initialize(
      description:,
      item_description_selector:,
      item_link_selector:,
      item_time_selector:,
      item_title_selector:,
      item_selector:,
      title:,
      url:
    )
      @description = description
      @item_description_selector = item_description_selector
      @item_link_selector = item_link_selector
      @item_time_selector = item_time_selector
      @item_title_selector = item_title_selector
      @item_selector = item_selector
      @title = title
      @url = url
    end

    # @return [Weneedfeed::Page]
    def call
      ::Weneedfeed::Page.new(
        description: @description,
        node: parsed_body,
        item_description_selector: @item_description_selector,
        item_selector: @item_selector,
        item_link_selector: @item_link_selector,
        item_time_selector: @item_time_selector,
        item_title_selector: @item_title_selector,
        title: @title,
        url: @url
      )
    end

    private

    # @return [Nokogiri::Node]
    def parsed_body
      ::Nokogiri::XML.parse(response.body)
    end

    # @return [Faraday::Response]
    def response
      self.class.faraday_connection.get(@url)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
weneedfeed-0.8.0 lib/weneedfeed/scraping.rb