Class: Omnom::Source::Reddit::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/omnom/source/reddit/default.rb

Direct Known Subclasses

Images

Instance Attribute Summary

Attributes inherited from Base

#config, #feed_key, #key, #options, #settings, #source_id

Instance Method Summary collapse

Methods inherited from Base

config, configure, cron, every, feed_url, full_key, guid_namespace, icon, icon_from_url, inherited, #initialize, key, required_config, required_options, #update, url

Methods included from ParserMethods

#html_to_text

Constructor Details

This class inherits a constructor from Omnom::Source::Base

Instance Method Details

#after_initializeObject



8
9
10
# File 'lib/omnom/source/reddit/default.rb', line 8

def after_initialize
  @url = "#{@url}#{@options[:subreddit]}" if @options[:subreddit].present?
end

#get_raw_postsObject



12
13
14
# File 'lib/omnom/source/reddit/default.rb', line 12

def get_raw_posts
  @page.search('#siteTable > .link')
end

#post_attributes(node) ⇒ Object



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
61
62
# File 'lib/omnom/source/reddit/default.rb', line 16

def post_attributes(node)
  thumbnail_node = node.find('a.thumbnail > img')
  thumbnail_url = thumbnail_node.attr('src') if thumbnail_node.present?

  comments_node = node.drill(
      [:find, '.flat-list.buttons'],
      [:text_matches, /^(\d+) comments$/i],
      :first
  )
  if comments_node
    comments_count = comments_node.matches[1]
    comments_url = comments_node.url
  else
    comments_node = node.drill(
      [:find, '.flat-list.buttons'],
      [:text_equals, 'comment'],
      :first
    )
    if comments_node
      comments_count = 0
      comments_url = comments_node.url
    end
  end

  title_node = node.find('p.title a.title')

  author_node = node.find('.tagline author')
  if author_node
    author_name = author_node.text
    author_url = author_node.url
  end

  {
    title: title_node.text,
    guid: node.attr('data-fullname'),
    url: title_node.url,
    published_at: node.find('.tagline time').time(attribute: 'datetime'),
    thumbnail_url: thumbnail_url,
    author_name: author_name,
    author_url: author_url,
    comments_count: comments_count,
    comments_url: comments_url,
    other: {
      likes_count: node.find('.score.unvoted').text.to_i
    }
  }
end