Class: AstrologicalForecast::Prediction

Inherits:
Object
  • Object
show all
Defined in:
lib/astrological_forecast/prediction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars) ⇒ Prediction

Returns a new instance of Prediction.



7
8
9
10
11
12
# File 'lib/astrological_forecast/prediction.rb', line 7

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/astrological_forecast/prediction.rb', line 5

def name
  @name
end

#timeObject

Returns the value of attribute time.



5
6
7
# File 'lib/astrological_forecast/prediction.rb', line 5

def time
  @time
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/astrological_forecast/prediction.rb', line 5

def type
  @type
end

Instance Method Details

#fetch_forecast!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/astrological_forecast/prediction.rb', line 26

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

#fetch_more!(url_more = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/astrological_forecast/prediction.rb', line 18

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

#fetch_page!Object



14
15
16
# File 'lib/astrological_forecast/prediction.rb', line 14

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