Class: Whistle::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Resource

Returns a new instance of Resource.



26
27
28
29
30
31
32
33
34
35
# File 'lib/resource.rb', line 26

def initialize(url)
  @uri = returning URI.parse(url) do |u|
    @username, @password = u.user, u.password
    @username = CGI.unescape(@username) unless @username.blank? # Convert %40 in username to @ (gmail) 
    u.remove_userinfo!
  end
  @url = @uri.to_s
  @store = ($store[@url] ||= {})
  @type = (@store[:type] ||= guess_type) 
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



24
25
26
# File 'lib/resource.rb', line 24

def url
  @url
end

Instance Method Details

#tailObject



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/resource.rb', line 37

def tail
  case @type
    when :svn
      revisions = []
      silence_stream(STDOUT) do # RSCM outputs everything to stdout?
        scm = returning RSCM::Subversion.new(@url) do |svn|
          svn.username, svn.password = @username, @password
        end
        since = @store[:last] || begin
          rs = scm.revisions(7.days.ago)
          diff_from = rs[rs.length-2]
          return if diff_from.nil? # There's no revision in last days to show updates since
          diff_from.identifier # Grab max last two
        end
        revisions = scm.revisions(since)
      end
      last_revision = revisions[revisions.length-1]
      @store[:last] = last_revision.identifier.to_i if last_revision
      revisions.map do |r| 
        message = "Revision #{r.identifier} commited by #{r.developer} #{Time.now.distance_in_words_since(r.time)} ago: "
        message << "\n"
        message << r.message.chomp.strip
        message << "\n"
        message << r.map {|f| " #{f.status[0,1]} #{f.path}" }.join("\n")
        message << "\n"
        message
      end
    when :feed
      f = FeedParser::Feed.new(open(@url,:http_basic_authentication => [@username,@password]).read)
      
      @store[:seen] ||= {}
      @store[:seen].delete_if {|link,date| (!f.items.map{|i| i.link}.include?(link)) && date && (date < 1.month.ago) }
      
      old, new = f.items.partition {|i| @store[:seen].has_key?(i.link) }
      new = new.first(5) if @store[:seen].empty? # Limit first tail to 5 entries
      
      # Mark all items as seen
      f.items.each { |i| @store[:seen][i.link] = i.date }
      
      return new.reverse.map do |i|
        safe_title = i.title.encode_entities(:basic)
        title = ''
        if @uri.host == 'mail.google.com'
          #title = "<span style=\"background:#ccccff; color: #000000;\">#{@username}</span>"
          title << "#{@username}: #{safe_title}"
        else
          title = safe_title
          title << " (#{i.creator})" if i.creator
        end  
        link = i.link.encode_entities(:basic) # xmpp4r had problems with unencoded &
        pre,post = title.split(/\:/,2)
        if post.nil?
          body = "#{title} <a href=\"#{link}\">#</a>"
        else
          body = "#{pre}: <a href=\"#{link}\">#{post.strip}</a>"
        end
        "<html><body>#{body}</body></html>"
      end
  end
end