Module: Twinkit

Defined in:
lib/twinkit.rb

Defined Under Namespace

Classes: Twink

Class Method Summary collapse

Class Method Details



11
12
13
14
15
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
# File 'lib/twinkit.rb', line 11

def self.get_links(name)
  tweets = Twitter.user_timeline(name)
  tweet_text = ""

  # grab tweet text from user
  tweets.each do |t| 
    tweet_text << " , " << t.text 
  end

  # parse links from tweets
  twink =  /((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w!:.?+=&%@!\-\/]))?)/ 
  link = tweet_text.scan(twink)
  link_array = []

  link.each do |l|

    begin
      s = Twink.new(l[0])
      # link_and_title = [] << l[0]
      URI.parse(l[0]).open do |f|
        body = f.read
        doc = Nokogiri::HTML.parse(body)
        s.title = doc.title
        # link_and_title << doc.title
      end
      s.redir_url = Net::HTTP.get_response(URI.parse(l[0]))['location']
      # link_and_title << Net::HTTP.get_response(URI.parse(l[0]))['location']
      link_array << s
    rescue => e
      case e 
      when URI::InvalidURIError
        p "invalid URL #{l[0]}"
      end
    end      
  end
  
  return link_array
end