Class: Hikkmemo::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ Session

Returns a new instance of Session.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hikkmemo/session.rb', line 15

def initialize(path, opts = {})
  opts[:log_to]       ||= :files
  opts[:boards]       ||= {}
  opts[:history_size] ||= 10
  @prompt       = opts[:prompt] || 'hikkmemo/%b>'
  @prompt_color = opts[:prompt_color] || :default
  @theme   = opts[:theme]   || :solid
  @colors  = opts[:colors]  || :default
  @log_msg = opts[:log_msg] || '%t %k (%b) %m'
  @msg_sz  = opts[:msg_sz]  || 100

  @path    = File.expand_path(path).chomp('/')
  @history = RingBuffer.new(opts[:history_size])
  @aux_cnt = 0
  @workers = {}
  @cmds    = {}
  @board   = opts[:boards].keys[0].to_s

  cl = ->(k,b,m) { print "\r#{log_msg(k,b,m)}\n#{prompt}"}
  fl = ->(k,b,m) { File.open("#{@path}/#{b}.log", 'a') {|f| f.puts "#{time} #{k} #{m}" } }
  @logger = {
    :console => cl,
    :files   => fl,
    :console_and_files => ->(k,b,m) { cl.(k,b,m); fl.(k,b,m) }
  }[opts[:log_to]]

  opts[:boards].each {|b,r| serve(b.to_s, r) }
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



13
14
15
# File 'lib/hikkmemo/session.rb', line 13

def history
  @history
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/hikkmemo/session.rb', line 13

def path
  @path
end

#workersObject (readonly)

Returns the value of attribute workers.



13
14
15
# File 'lib/hikkmemo/session.rb', line 13

def workers
  @workers
end

Instance Method Details

#cmd(name, &block) ⇒ Object



123
124
125
# File 'lib/hikkmemo/session.rb', line 123

def cmd(name, &block)
  @cmds[name] = block
end

#hook(board = nil, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/hikkmemo/session.rb', line 113

def hook(board = nil, &block)
  if board
    with_board_worker (board.to_s) {|w| w.on_add_post(&block) }
  else
    @workers.each do |b,w|
      w.on_add_post {|p| block.call(p,b) }
    end
  end
end

#interactObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hikkmemo/session.rb', line 75

def interact
  loop do
    inp = Readline.readline(prompt, true)
    next unless inp
    cmd = inp.split
    case cmd[0]
    when 'exit' then break
    when 'help'
      puts '=========================================================================='
      puts '#  b = board'
      puts '#  p = post id'
      puts '#  t = thread id'
      puts '# ?x = optional x'
      puts '=========================================================================='
      puts 'history n      -- last n events'
      puts 'context b      -- set board in context (for cmds like "post" and "thread")'
      puts 'post    p   ?b -- print post'
      puts 'posts   n   ?b -- last n posts'
      puts 'tposts  n t ?b -- last n posts of thread'
      puts 'thread  t   ?b -- print all posts of thread'
    when 'history' then @history.last_n(cmd[1].to_i).each {|m| puts m }
    when 'context' then cmd_context cmd[1]
    when 'post'    then cmd_post    cmd[1].to_i, cmd[2] || @board
    when 'posts'   then cmd_posts   cmd[1].to_i, cmd[2] || @board
    when 'tposts'  then cmd_tposts  cmd[1].to_i, cmd[2].to_i, cmd[3] || @board
    when 'thread'  then cmd_thread  cmd[1].to_i, cmd[2] || @board
    else
      fn = @cmds[cmd[0]]
      fn && fn.(cmd.drop(1))
    end
    puts ''
  end
end

#notice(msg) ⇒ Object



109
110
111
# File 'lib/hikkmemo/session.rb', line 109

def notice(msg)
  print "\r#{time} ~ #{msg}\n#{prompt}".color(:cyan)
end

#serve(board, reader) ⇒ Object



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
# File 'lib/hikkmemo/session.rb', line 44

def serve(board, reader)
  FileUtils.mkdir_p [@path, "#{@path}/#{board}/"]
  File.open("#{@path}/#{board}.db", 'a') {}
  db = Sequel.sqlite("#{@path}/#{board}.db")

  db.create_table? :posts do
    Integer :id, :primary_key => true
    foreign_key :thread, :threads
    DateTime :date
    String :author
    String :subject
    String :message
    String :image
    String :embed
  end

  db.create_table? :threads do
    Integer :id, :primary_key => true
    Integer :last_post
    DateTime :date
  end

  worker = @workers[board] = Worker.new(db, reader)
  worker.on_add_thread {|t| log('+', board, "[#{t[:id]}]") }
  worker.on_add_post do |p|
    log('+', board, "#{p[:id]}[#{p[:thread]}] - '#{p[:message][0..@msg_sz].tr("\n",'')}...'")
    (img = p[:image]) && img.split(',').each {|img| download_image(board, img) }
  end
  worker.run
end