Module: Needy

Defined in:
lib/needy.rb

Defined Under Namespace

Classes: Git, IChat

Constant Summary collapse

@@when =
{}
@@threads =
[]

Instance Method Summary collapse

Instance Method Details

#feed(abbrev, url) ⇒ Object



33
34
35
36
37
38
# File 'lib/needy.rb', line 33

def feed abbrev, url
  thr do
    feed = FeedTools::Feed.open(url)
    set abbrev, feed.updated || feed.items.map(&:published).max
  end
end

#git(abbrev, dir, branch, &block) ⇒ Object



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

def git abbrev, dir, branch, &block
  thr do
    g = Needy::Git.new(dir, branch)
    yield(g) if block
    set abbrev, g.when
  end
end

#gmail(args, &block) ⇒ Object



20
21
22
23
24
# File 'lib/needy.rb', line 20

def gmail args, &block
  self.imap_server 'imap.gmail.com', { :port => 993,
                                       :ssl  => true,
                                       :folder => '[Gmail]/Sent Mail' }.merge(args), &block
end

#ichats(dir = "#{ENV['HOME']}/Documents/iChats") {|i| ... } ⇒ Object

Yields:

  • (i)


62
63
64
65
# File 'lib/needy.rb', line 62

def ichats dir = "#{ENV['HOME']}/Documents/iChats"
  i = Needy::IChat.new(dir, self)
  yield(i)
end

#imap_server(host, args) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/needy.rb', line 12

def imap_server host, args
  args = { :port => 143, :ssl  => false }.merge(args)
  @@server = Net::IMAP.new host, args[:port], args[:ssl]
  @@server. args[:username], args[:password]
  @@server.select args[:folder] if args[:folder]
  thr { yield }
end

#recipient(abbrev, *others) ⇒ Object



26
27
28
29
30
31
# File 'lib/needy.rb', line 26

def recipient abbrev, *others
  recips = [abbrev.to_s] + others.map(&:to_s)
  query = recips.map{|whom| "TO #{whom}"}.reduce{|memo,q| "OR #{q} #{memo}"}
  last_id = @@server.search(query).max
  set abbrev, Time.parse(@@server.fetch(last_id, 'envelope')[0].attr['ENVELOPE'].date)
end

#set(abbrev, val) ⇒ Object



58
59
60
# File 'lib/needy.rb', line 58

def set abbrev, val
  @@when[abbrev.to_s] = val unless (@@when[abbrev.to_s] and @@when[abbrev.to_s] > val)
end

#thr(&block) ⇒ Object



54
55
56
# File 'lib/needy.rb', line 54

def thr &block
  @@threads.push(Thread.new(&block))
end

#whensObject



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

def whens
  @@threads.each{|t| t.join}
  @@threads = []
  @@when
end