Class: Feedbase::Feed

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/feedbase/feed.rb

Instance Method Summary collapse

Instance Method Details

#last_downloadObject



62
63
64
# File 'lib/feedbase/feed.rb', line 62

def last_download
  @last_download ||= FeedDownload.filter(feed_id: feed_id).first
end

#last_etagObject



66
67
68
# File 'lib/feedbase/feed.rb', line 66

def last_etag
  last_download && last_download.etag
end

#last_modifiedObject



70
71
72
# File 'lib/feedbase/feed.rb', line 70

def last_modified
  last_download && last_download.last_modified
end

#make_alpha_title(s) ⇒ Object



74
75
76
77
# File 'lib/feedbase/feed.rb', line 74

def make_alpha_title(s)
  return if s.nil?
  s.gsub(/^(The|A|An)\s/, '')
end

#refresh(force = false) ⇒ Object

returns number of items created



16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
59
60
# File 'lib/feedbase/feed.rb', line 16

def refresh(force=false)
  # check headers and etag and last modified
  raise "Missing feed_url" if feed_url.nil?
  ff = Feedbase::FetchFeed.new(feed_url)
  headers = ff.headers
  if !force 
    if last_etag && (headers[:etag] == last_etag)
      puts "-- #{feed_url} -- ETag cache hit"
      return
    end
  end
  data = ff.fetch 
  params = data[:feed_params].merge(:alpha_title => make_alpha_title(data[:feed_params][:title])) 
  if params[:feed_url] != self[:feed_url]
    if x = self.class.filter(:feed_url => params[:feed_url]).first
      raise Redirected.new("Redirected to existing feed: #{x.feed_url}")
    end
  end
  params.delete(:feed_url) 
  begin Sequel::DatabaseError
    update params
  rescue StandardError # PGError
    puts "The offending record is #{self.inspect}"
    raise
  end

  Feedbase::FeedDownload.create({feed_id: feed_id}.merge(data[:download_params])) 
  items_created = data[:items].
    select {|item| Feedbase::Item[:guid => item[:guid]].nil?}.
    map { |item|
      params = {
        feed_id: feed_id,
        title: item[:title].encode("utf-8"), 
        guid: item[:guid], 
        link: item[:link],
        content: item[:content],
        author: item[:author],
        word_count: item[:word_count],
        pub_date: item[:pub_date]
      }
      Feedbase::Item.create params
    }
  # caller can extract an item count from this
  items_created
end