Class: Article

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

Overview

The Article class contains the links and tags for consumption by twitter

Constant Summary collapse

TAG_CSS =

CSS tag to pull the tags strings

'.tags a'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(link) ⇒ Article

Returns a new instance of Article.



13
14
15
16
# File 'lib/article.rb', line 13

def initialize(link)
  @link = link
  @tweets = []
end

Instance Attribute Details

Returns the value of attribute link.



8
9
10
# File 'lib/article.rb', line 8

def link
  @link
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/article.rb', line 8

def tags
  @tags
end

#tweetsObject

Returns the value of attribute tweets.



7
8
9
# File 'lib/article.rb', line 7

def tweets
  @tweets
end

Instance Method Details

#build_tweetsObject

Recursively build and populate tweets. This will catch any errors which are raised by Tweet#add and attempt to call it again



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/article.rb', line 20

def build_tweets # rubocop:disable Metrics/MethodLength
  tweet = Tweet.new(@link)
  @tags.each do |tag|
    begin
      tweet.add(tag)
    rescue Tweet::TagTooLong
      @tweets.push tweet
      raise StandardError if "#{tag} | #{@link}".length > 140
      tweet = Tweet.new(@link)
    end
  end
  @tweets.push tweet
end

#filter_tags(document) ⇒ Object

Filters finds the link inside the .g-tag-box div, pulls the name, then makes the resulting string uppercase.



49
50
51
52
# File 'lib/article.rb', line 49

def filter_tags(document)
  # Filter down and get the tags.
  @tags = document.css(TAG_CSS).map(&:children).map(&:text)
end

#request_urlObject

Opens @link, then parses using Nokogiri



55
56
57
58
# File 'lib/article.rb', line 55

def request_url
  # Get all data
  Nokogiri::HTML(open(@link))
end

#retrieve_tagsObject

sets @tags by calling the url, then filtering the document to find the tags.



40
41
42
43
44
45
# File 'lib/article.rb', line 40

def retrieve_tags
  # get post
  doc = request_url
  @tags = filter_tags(doc)
  self
end