Class: Joggle::Twitter::Engine

Inherits:
Object
  • Object
show all
Includes:
Pablotron::Observable
Defined in:
lib/joggle/twitter/engine.rb

Overview

Twitter engine object.

Constant Summary collapse

DEFAULTS =
{
  # update interval, in minutes
  'twitter.engine.update_interval' => 5,
}

Instance Method Summary collapse

Methods included from Pablotron::Observable

#fire, #on, #un

Constructor Details

#initialize(user_store, fetcher, opt = nil) ⇒ Engine

Create new twitter engine.



21
22
23
24
25
# File 'lib/joggle/twitter/engine.rb', line 21

def initialize(user_store, fetcher, opt = nil)
  @opt = DEFAULTS.merge(opt || {})
  @store = user_store
  @fetcher = fetcher
end

Instance Method Details

#ignored?(who) ⇒ Boolean

Is the given Jabber user ignored?

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/joggle/twitter/engine.rb', line 30

def ignored?(who)
  if rec = @store.get_user(who)
    rec['ignored']
  else
    nil
  end
end

#list(who, &block) ⇒ Object

List recent tweets for the given user.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/joggle/twitter/engine.rb', line 96

def list(who, &block)
  store, fetcher = @store, @fetcher

  stoppable_action('twitter_engine_list', who) do
    if user = store.get_user(who)
      fetcher.get(user, true) do |id, who, time, msg|
        block.call(id, who, time, msg)
      end
    end
  end
end

#register(who, user, pass) ⇒ Object

Bind the given Jabber user to the given Twitter username and password.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/joggle/twitter/engine.rb', line 49

def register(who, user, pass)
  store = @store

  stoppable_action('twitter_engine_register_user', who, user, pass) do
    store.add_user(who, { 
      'who'         => who.gsub(/\/.*$/, ''),
      'user'        => user,
      'pass'        => pass,
      'updated_at'  => 0,
      'last_id'     => 0,
      'ignored'     => false,
      'sleep_bgn'   => 0, # sleep start time, in hours
      'sleep_end'   => 0, # sleep end time, in hours
    })
  end
end

#registered?(who) ⇒ Boolean

Is the given Jabber registered?

Returns:

  • (Boolean)


41
42
43
# File 'lib/joggle/twitter/engine.rb', line 41

def registered?(who)
  @store.has_user?(who)
end

#tweet(who, msg) ⇒ Object

Send a tweet as the given Jabber user.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/joggle/twitter/engine.rb', line 80

def tweet(who, msg)
  ret, store, fetcher = nil, @store, @fetcher

  stoppable_action('twitter_engine_tweet', who, msg) do
    if user = store.get_user(who)
      ret = fetcher.tweet(user, msg)
    end
  end

  # return result
  ret
end

#unregister(who) ⇒ Object

Forget registration for the given Jabber user.



69
70
71
72
73
74
75
# File 'lib/joggle/twitter/engine.rb', line 69

def unregister(who)
  store = @store

  stoppable_action('twitter_engine_unregister_user', who) do
    store.delete_user(who)
  end
end

#update(&block) ⇒ Object

Update all users.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/joggle/twitter/engine.rb', line 111

def update(&block) 
  store, updates = @store, []

  # make list of updates
  @store.each_user do |user|
    updates << user if needs_update?(user)
  end

  # iterate over updates and do each one
  updates.each do |user|
    stoppable_action('twitter_engine_update', user) do
      update_user(user) do |id, time, src, msg|
        block.call(user['who'], id, time, src, msg)
      end
    end
  end
end