Module: Earthquake::Core

Included in:
Earthquake
Defined in:
lib/earthquake/core.rb

Instance Method Summary collapse

Instance Method Details

#_initObject



34
35
36
37
38
39
# File 'lib/earthquake/core.rb', line 34

def _init
  load_config
  load_plugins
  inits.each { |block| class_eval(&block) }
  inits.clear
end

#_onceObject



30
31
32
# File 'lib/earthquake/core.rb', line 30

def _once
  onces.each { |block| class_eval(&block) }
end

#async(&block) ⇒ Object



188
189
190
# File 'lib/earthquake/core.rb', line 188

def async(&block)
  Thread.start(&block)
end

#browse(url) ⇒ Object



202
203
204
# File 'lib/earthquake/core.rb', line 202

def browse(url)
  Launchy::Browser.run(url)
end

#configObject



6
7
8
# File 'lib/earthquake/core.rb', line 6

def config
  @config ||= {}
end

#error(e) ⇒ Object



192
193
194
# File 'lib/earthquake/core.rb', line 192

def error(e)
  notify "[ERROR] #{e.message}\n#{e.backtrace.join("\n")}"
end

#init(&block) ⇒ Object



18
19
20
# File 'lib/earthquake/core.rb', line 18

def init(&block)
  inits << block
end

#initsObject



14
15
16
# File 'lib/earthquake/core.rb', line 14

def inits
  @inits ||= []
end

#item_queueObject



10
11
12
# File 'lib/earthquake/core.rb', line 10

def item_queue
  @item_queue ||= []
end

#load_configObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/earthquake/core.rb', line 51

def load_config
  config[:dir]              ||= File.expand_path('~/.earthquake')
  config[:time_format]      ||= Time::DATE_FORMATS[:short]
  config[:plugin_dir]       ||= File.join(config[:dir], 'plugin')
  config[:file]             ||= File.join(config[:dir], 'config')
  config[:prompt]           ||= ''
  config[:consumer_key]     ||= 'RmzuwQ5g0SYObMfebIKJag'
  config[:consumer_secret]  ||= 'V98dYYmWm9JoG7qfOF0jhJaVEVW3QhGYcDJ9JQSXU'

  [config[:dir], config[:plugin_dir]].each do |dir|
    unless File.exists?(dir)
      FileUtils.mkdir_p(dir)
    end
  end

  if File.exists?(config[:file])
    load config[:file]
  else
    File.open(config[:file], 'w')
  end

  get_access_token unless self.config[:token] && self.config[:secret]
end

#load_pluginsObject



75
76
77
78
79
80
81
82
83
# File 'lib/earthquake/core.rb', line 75

def load_plugins
  Dir[File.join(config[:plugin_dir], '*.rb')].each do |lib|
    begin
      require_dependency lib
    rescue Exception => e
      error e
    end
  end
end

#mutexObject



178
179
180
# File 'lib/earthquake/core.rb', line 178

def mutex
  @mutex ||= Mutex.new
end

#notify(message, options = {:title => 'earthquake'}) ⇒ Object Also known as: n



196
197
198
199
# File 'lib/earthquake/core.rb', line 196

def notify(message, options = {:title => 'earthquake'})
  message = message.is_a?(String) ? message : message.inspect
  Notify.notify options[:title], message
end

#once(&block) ⇒ Object



26
27
28
# File 'lib/earthquake/core.rb', line 26

def once(&block)
  onces << block
end

#oncesObject



22
23
24
# File 'lib/earthquake/core.rb', line 22

def onces
  @once ||= []
end

#reconnectObject



121
122
123
124
# File 'lib/earthquake/core.rb', line 121

def reconnect
  item_queue.clear
  start_stream(:host  => 'userstream.twitter.com', :path  => '/2/user.json', :ssl => true)
end

#reloadObject



41
42
43
44
45
46
47
48
49
# File 'lib/earthquake/core.rb', line 41

def reload
  loaded = ActiveSupport::Dependencies.loaded.dup
  ActiveSupport::Dependencies.clear
  loaded.each { |lib| require_dependency lib }
rescue Exception => e
  error e
ensure
  _init
end

#restore_historyObject



171
172
173
174
175
176
# File 'lib/earthquake/core.rb', line 171

def restore_history
  history_file = File.join(config[:dir], 'history')
  if File.exists?(history_file)
    File.read(history_file).split(/\n/).each { |line| Readline::HISTORY << line }
  end
end

#start(options = {}) ⇒ Object



85
86
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
114
115
116
117
118
119
# File 'lib/earthquake/core.rb', line 85

def start(options = {})
  config.merge!(options)
  _init
  _once
  restore_history

  EventMachine::run do
    Thread.start do
      while buf = Readline.readline(config[:prompt], true)
        unless Readline::HISTORY.count == 1
          Readline::HISTORY.pop if buf.empty? || Readline::HISTORY[-1] == Readline::HISTORY[-2]
        end
        sync {
          reload
          store_history
          input(buf.strip)
        }
      end
    end

    Thread.start do
      loop do
        if Readline.line_buffer.nil? || Readline.line_buffer.empty?
          reload
          sync { output }
        end
        sleep 1
      end
    end

    reconnect

    trap('INT') { stop }
  end
end

#start_stream(options) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/earthquake/core.rb', line 126

def start_stream(options)
  stop_stream

  options = {
    :oauth => config.slice(:consumer_key, :consumer_secret).merge(
      :access_key => config[:token], :access_secret => config[:secret]
    )
  }.merge(options)

  @stream = ::Twitter::JSONStream.connect(options)

  @stream.each_item do |item|
    item_queue << JSON.parse(item)
  end

  @stream.on_error do |message|
    notify "error: #{message}"
  end

  @stream.on_reconnect do |timeout, retries|
    notify "reconnecting in: #{timeout} seconds"
  end

  @stream.on_max_reconnects do |timeout, retries|
    notify "Failed after #{retries} failed reconnects"
  end
end

#stopObject



158
159
160
161
# File 'lib/earthquake/core.rb', line 158

def stop
  stop_stream
  EventMachine.stop_event_loop
end

#stop_streamObject



154
155
156
# File 'lib/earthquake/core.rb', line 154

def stop_stream
  @stream.stop if @stream
end

#store_historyObject



163
164
165
166
167
168
169
# File 'lib/earthquake/core.rb', line 163

def store_history
  history_size = config[:history_size] || 1000
  File.open(File.join(config[:dir], 'history'), 'w') do |file|
    lines = Readline::HISTORY.to_a[([Readline::HISTORY.size - history_size, 0].max)..-1]
    file.print(lines.join("\n"))
  end
end

#sync(&block) ⇒ Object



182
183
184
185
186
# File 'lib/earthquake/core.rb', line 182

def sync(&block)
  mutex.synchronize do
    block.call
  end
end