Class: Reddit::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/reddit/scraper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#authorObject

Returns the value of attribute author.



2
3
4
# File 'lib/reddit/scraper.rb', line 2

def author
  @author
end

#commentsObject

Returns the value of attribute comments.



2
3
4
# File 'lib/reddit/scraper.rb', line 2

def comments
  @comments
end

#timestampObject

Returns the value of attribute timestamp.



2
3
4
# File 'lib/reddit/scraper.rb', line 2

def timestamp
  @timestamp
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/reddit/scraper.rb', line 2

def title
  @title
end

#upvotesObject

Returns the value of attribute upvotes.



2
3
4
# File 'lib/reddit/scraper.rb', line 2

def upvotes
  @upvotes
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/reddit/scraper.rb', line 2

def url
  @url
end

Class Method Details

.scrapeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/reddit/scraper.rb', line 3

def self.scrape
  doc = Nokogiri::HTML(open("https://www.reddit.com/r/ruby", 'User-Agent' => 'ruby-reddit'))
  posts = []
  postsList = doc.css('#siteTable > div.thing.link').first(10)
  postsList.each do |post|
    title = post.css('.title a.title').text.strip
    author = post.css('.author').text.strip
    timestamp = post.css('.live-timestamp').text.strip
    comments = post.attr('data-comments-count')
    upvotes = post.attr('data-score')
    url = post.css('a.comments').attr('href')
    newPost = {
      title: title,
      author: author,
      timestamp: timestamp,
      comments: comments,
      upvotes: upvotes,
      url: url,
    }
    posts << newPost
  end
  posts
end