Sha256: 6e50c60038f7a888b654de33e8975c263745156fea5d760ab8b3ecf390c28a9c

Contents?: true

Size: 1.52 KB

Versions: 11

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module AstrologicalForecast
  class Prediction
    attr_accessor :type, :name, :time

    def initialize(vars)
      @type = vars[:type]
      @name = vars[:name_en]
      @time = vars[:time]
      @url = "#{::BASE_URL}horoscope/astrologic/#{@type}/#{@name}/#{@time}.html"
    end

    def fetch_page!
      @fetch_page ||= URI.parse(@url).open
    end

    def fetch_more!(url_more = nil)
      url_more = @url.gsub(type, 'more') if (type == 'general') & (time == 'today')
      url_more = @url.gsub(type, 'more') if (type == 'general') & (time == 'tomorrow')
      return nil if url_more.nil?

      @fetch_more ||= URI.parse(url_more).read
    end

    def fetch_forecast!
      @fetch_forecast ||= begin
        document = Nokogiri::HTML(fetch_page!)
        record = document.css('div.contentOnly p')[0].children.text.strip
        record = "#{record.delete(".")} или не существует!" if record.match?(/\Извините/)

        predict =
          {
            head: document.css('div.contentOnly #type2 h2')
                          .children.text
                          .strip.gsub(/\s{2,}/, ' '),
            forecast: record
          }
      end

      if fetch_more!.nil?
        predict
      else
        document = Nokogiri::HTML(fetch_more!)
        predict = predict.merge(details: document.css('div.contentOnly p')[0].children.text.strip)
        {
          head: predict[:head],
          forecast: "#{predict[:forecast]}\n#{predict[:details]}"
        }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
astrological_forecast-2.0.5 lib/astrological_forecast/prediction.rb
astrological_forecast-2.0.4 lib/astrological_forecast/prediction.rb
astrological_forecast-2.0.2 lib/astrological_forecast/prediction.rb
astrological_forecast-2.0.1 lib/astrological_forecast/prediction.rb
astrological_forecast-1.3.5 lib/astrological_forecast/prediction.rb
astrological_forecast-1.3.4 lib/astrological_forecast/prediction.rb
astrological_forecast-1.3.3 lib/astrological_forecast/prediction.rb
astrological_forecast-1.2.3 lib/astrological_forecast/prediction.rb
astrological_forecast-1.2.2 lib/astrological_forecast/prediction.rb
astrological_forecast-1.2.1 lib/astrological_forecast/prediction.rb
astrological_forecast-1.2.0 lib/astrological_forecast/prediction.rb