Class: BlogBase

Inherits:
ApplicationRecord show all
Defined in:
app/models/blog_base.rb

Direct Known Subclasses

Blog, Podcast

Constant Summary collapse

FETCH_SUCCESS =
0
FETCH_FAILED =
1
MAX_FETCHES_STORED =
10

Instance Method Summary collapse

Instance Method Details

#checked!Object


101
102
103
104
# File 'app/models/blog_base.rb', line 101

def checked!
  recent_fetches.push [FETCH_SUCCESS, nil]
  update_health_status
end

#cleanup_html(html) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/blog_base.rb', line 38

def cleanup_html(html)
  # удаляем комментарии
  doc = Nokogiri::HTML::DocumentFragment.parse(html)
  doc.xpath('//comment()').remove
  html = doc.to_html

  text_cleanup_rules_auto.to_s.split("\n").map(&:strip).each_with_index do |line, index|
    match = line.match(/^([^\s]+)\s+([^\s]+)\s+(.*)/)

    if match
      cmd = match[1]
      args = [match[2], match[3].strip]

      case cmd
      when 'remove'

        case args[0]
        when 'regexp'
          regexp = eval(args[1])

          html = html.remove regexp
        when 'css'
          doc = Nokogiri::HTML::DocumentFragment.parse(html)
          doc.css(args[1]).each(&:remove)

          html = doc.to_html
        else
          raise "unknown remove type '#{args[0]}': '#{line}'"
        end

      when 'select'
        case args[0]
        when 'css'
          doc = Nokogiri::HTML::DocumentFragment.parse(html)

          el = doc.css(args[1])[0]

          if el.present?
            html = doc.css(args[1])[0].to_html
          else
            raise "Can't find anything by css selector '#{args[1]}'"
          end
        else
          raise "unknown remove type '#{args[0]}': '#{line}'"
        end

      else
        raise "unknown cmd '#{cmd}'"
      end
    else
      raise "failed to parse line ##{index}: #{line}"
    end
  end

  html
end

#failed_to_check!(exception = nil) ⇒ Object


106
107
108
109
# File 'app/models/blog_base.rb', line 106

def failed_to_check!(exception = nil)
  recent_fetches.push [FETCH_FAILED, exception.try(:to_s)]
  update_health_status
end

#home_urlObject


95
96
97
98
99
# File 'app/models/blog_base.rb', line 95

def home_url
  Rails.cache.fetch("BlogView##{id}#home_url", expires_in: 1.hour) do
    "http://#{URI(posts.first.try(:source_url) || "http://#{Rails.configuration.x.domain}").host}"
  end
end

#text_cleanup_rules_autoObject


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/blog_base.rb', line 18

def text_cleanup_rules_auto
  rules = text_cleanup_rules || ''

  if feed_url.try(:match, /ftr.fivefilters.org/)
    rules << "\n" if rules.present?
    rules << 'remove regexp /This entry passed through the Full-Text RSS service.+/im'
    rules << "\n"
    rules << 'remove regexp /Let\'s block ads!/'
    rules << "\n"
    rules << 'remove regexp /\(Why\?\)/'
  end

  if feed_url.try(:match, /feeds.feedburner.com/)
    rules << "\n" if rules.present?
    rules << 'remove css .feedflare'
  end

  rules
end