Class: Joggle::Runner::PStore

Inherits:
Object
  • Object
show all
Defined in:
lib/joggle/runner/pstore.rb

Overview

Basic PStore-backed runner. Creates all necessary objects from given config and binds them together.

Constant Summary collapse

PATHS =
{
  'store' => ENV['JOGGLE_STORE_PATH'] || '~/.joggle/joggle.pstore',
  'log'   => ENV['JOGGLE_LOG_PATH'] || '~/.joggle/joggle.log',
}
DEFAULTS =
{
  # store configuration
  'runner.store.path'       => File.expand_path(PATHS['store']),

  # log configuration
  'runner.log.path'         => File.expand_path(PATHS['log']),
  # FIXME: change to INFO
  'runner.log.level'        => 'INFO',
  'runner.log.format'       => '%Y-%m-%dT%H:%M:%S',

  # cache configuration
  'runner.cache.headers'    => {
    'user-agent' => "Joggle/#{Joggle::VERSION}",
  },
}
PATH_KEYS =
%w{store log}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = nil) ⇒ PStore

Create new PStore runner object from the given options.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/joggle/runner/pstore.rb', line 60

def initialize(opt = nil)
  @opt = DEFAULTS.merge(opt || {})

  # make sure paths exist
  PATH_KEYS.each do |key|
    FileUtils.mkdir_p(File.dirname(@opt["runner.#{key}.path"]), {
      # restict access to owner
      :mode => 0700
    })
  end

  # create logger
  @log = Logger.new(@opt['runner.log.path'])
  @log.level = Logger.const_get(@opt['runner.log.level'].upcase)
  @log.datetime_format = @opt['runner.log.format']
  @log.info('Log started.')

  # create backing store
  path = @opt['runner.store.path'] 
  @log.debug("Creating backing store \"#{path}\".")
  pstore = ::PStore.new(path)
  @store = Store::PStore::All.new(pstore)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def cache
  @cache
end

#clientObject (readonly)

Returns the value of attribute client.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def client
  @client
end

#engineObject (readonly)

Returns the value of attribute engine.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def engine
  @engine
end

#fetcherObject (readonly)

Returns the value of attribute fetcher.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def fetcher
  @fetcher
end

#logObject (readonly)

Returns the value of attribute log.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def log
  @log
end

#storeObject (readonly)

Returns the value of attribute store.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def store
  @store
end

#tweeterObject (readonly)

Returns the value of attribute tweeter.



46
47
48
# File 'lib/joggle/runner/pstore.rb', line 46

def tweeter
  @tweeter
end

Class Method Details

.run(opt = nil) ⇒ Object

Create and run PStore runner object.



51
52
53
# File 'lib/joggle/runner/pstore.rb', line 51

def self.run(opt = nil)
  new(opt).run
end

Instance Method Details

#on_engine_command(e, who, cmd, arg) ⇒ Object

Log engine_command events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



202
203
204
205
# File 'lib/joggle/runner/pstore.rb', line 202

def on_engine_command(e, who, cmd, arg)
  pre = '<Engine>'
  @log.debug("#{pre} Command: #{who}: cmd = #{cmd}, arg = #{arg}.")
end

#on_engine_ignored_message(e, who, msg) ⇒ Object

Log engine_ignored_message events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



235
236
237
238
# File 'lib/joggle/runner/pstore.rb', line 235

def on_engine_ignored_message(e, who, msg)
  pre = '<Engine>'
  @log.info("#{pre} IGNORED: #{who}: #{msg}.")
end

#on_engine_ignored_subscription(e, who) ⇒ Object

Log engine_ignored_subscription events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



246
247
248
249
# File 'lib/joggle/runner/pstore.rb', line 246

def on_engine_ignored_subscription(e, who)
  pre = '<Engine>'
  @log.info("#{pre} IGNORED: #{who} (subscription)")
end

#on_engine_message(e, who, msg) ⇒ Object

Log engine_message events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



224
225
226
227
# File 'lib/joggle/runner/pstore.rb', line 224

def on_engine_message(e, who, msg)
  pre = '<Engine>'
  @log.info("#{pre} Message: #{who}: #{msg}.")
end

#on_engine_reply(e, who, msg) ⇒ Object

Log engine_reply events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



180
181
182
183
# File 'lib/joggle/runner/pstore.rb', line 180

def on_engine_reply(e, who, msg)
  pre = '<Engine>'
  @log.info("#{pre} Reply: #{who}: #{msg}.")
end

#on_engine_reply_error(e, who, msg, err) ⇒ Object

Log engine_reply_error events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



191
192
193
194
# File 'lib/joggle/runner/pstore.rb', line 191

def on_engine_reply_error(e, who, msg, err) 
  pre = '<Engine>'
  @log.warn("#{pre} Reply Error: Couldn't send reply \"#{msg}\" to #{who}: #{err}.")
end

#on_engine_update_error(e, err) ⇒ Object

Log engine_update_error events.

Note: This method is a listener for Joggle::Engine objects; you should never call it directly.



169
170
171
172
# File 'lib/joggle/runner/pstore.rb', line 169

def on_engine_update_error(e, err) 
  pre = '<Engine>'
  @log.warn("#{pre} Twitter update failed: #{err}.")
end

#on_twitter_engine_register_user(e, who, user, pass) ⇒ Object

Log twitter_engine_register_user events.

Note: This method is a listener for Twitter::Engine objects; you should never call it directly.



125
126
127
128
# File 'lib/joggle/runner/pstore.rb', line 125

def on_twitter_engine_register_user(e, who, user, pass)
  pre = '<Twitter::Engine>'
  @log.info("#{pre} Registering user: #{who} (xmpp) => #{user} (twitter).")
end

#on_twitter_engine_tweet(e, who, msg) ⇒ Object

Log twitter_engine_tweet events.

Note: This method is a listener for Twitter::Engine objects; you should never call it directly.



147
148
149
150
# File 'lib/joggle/runner/pstore.rb', line 147

def on_twitter_engine_tweet(e, who, msg)
  pre = '<Twitter::Engine>'
  @log.info("#{pre} Tweet: #{who}: #{msg}.")
end

#on_twitter_engine_unregister_user(e, who) ⇒ Object

Log twitter_engine_unregister_user events.

Note: This method is a listener for Twitter::Engine objects; you should never call it directly.



136
137
138
139
# File 'lib/joggle/runner/pstore.rb', line 136

def on_twitter_engine_unregister_user(e, who)
  pre = '<Twitter::Engine>'
  @log.info("#{pre} Unregistering user: #{who} (xmpp).")
end

#on_twitter_engine_update(e, user) ⇒ Object

Log twitter_engine_update events.

Note: This method is a listener for Twitter::Engine objects; you should never call it directly.



158
159
160
161
# File 'lib/joggle/runner/pstore.rb', line 158

def on_twitter_engine_update(e, user)
  pre = '<Twitter::Engine>'
  @log.info("#{pre} Updating: #{user['who']}.")
end

#runObject

Run this runner.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/joggle/runner/pstore.rb', line 87

def run
  # create cache
  @log.debug('Creating cache.')
  @cache = Joggle::Pablotron::Cache.new(@store, @opt['runner.cache.headers'])

  # create fetcher
  @log.debug('Creating twitter fetcher.')
  @fetcher = Twitter::Fetcher.new(@store, @cache, @opt)

  # create twitter engine
  @log.debug('Creating twitter engine.')
  @tweeter = Twitter::Engine.new(@store, @fetcher, @opt)
  @tweeter.on(self)

  # create jabber client
  @log.debug('Creating jabber client.')
  @client = Jabber::Client.new(@opt['jabber.user'], @opt['jabber.pass'], @opt)
  @client.on(self)

  # create new joggle engine
  @log.debug('Creating engine.')
  @engine = Engine.new(@client, @tweeter)
  @engine.on(self)

  @log.debug('Running engine.')
  @engine.run
end