Class: Shutwork::Command::Rooms

Inherits:
Base
  • Object
show all
Defined in:
lib/shutwork/command/rooms.rb

Instance Method Summary collapse

Methods inherited from Base

#download_file, #initialize

Methods included from Display

#display_account, #display_file, #display_message, #display_room, #format_datetime, #format_filesize, #to_filesize_human

Constructor Details

This class inherits a constructor from Shutwork::Command::Base

Instance Method Details

#download(room_id) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/shutwork/command/rooms.rb', line 68

def download room_id
  dir = Pathname.new(@download_dir || "download").join(room_id.to_s)
  items = @client.room_files room_id
  files = JSON.parse(items)
  files.each do |file|
    dst = dir.join file["filename"]
    if dst.exist?
      say_status "exist", dst, :cyan
    else
      say_status "download", dst, :yellow
      x = @client.file room_id, file["file_id"]
      url = JSON.parse(x)["download_url"]
      at = Time.at file["upload_time"]
      download_file url, dst, at
    end
  end
end

#listObject



39
40
41
42
43
44
45
46
# File 'lib/shutwork/command/rooms.rb', line 39

def list
  items = @client.rooms
  if @raw
    puts items
  else
    JSON.parse(items).each(&method(:display_room))
  end
end

#parse_args(args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/shutwork/command/rooms.rb', line 11

def parse_args args
  opts = OptionParser.new
  opts.program_name = "shutwork #{self.class.name.split(/::/).last.downcase}"
  opts.on("-r", "--raw", "Show results in raw format") { @raw = true }
  opts.on("-v", "--verbose", "Verbose") { @verbose = true }
  opts.on("--members", "Fetch members instead of messages") { @target = :members }
  opts.on("--files", "Fetch files instead of messages") { @target = :files }
  opts.on("--download", "Download files") { @download = true }
  opts.on("--download-dir DIR", "Set download dir (default: download)") { |dir| @download_dir = dir }
  opts.parse args
end

#run(args = []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shutwork/command/rooms.rb', line 23

def run args = []
  @args = parse_args args
  token = Shutwork::Token.read
  @client = Shutwork::Client.new token: token, verbose: @verbose

  if @args.first
    if @download
      download @args.first
    else
      show @args.first
    end
  else
    list
  end
end

#show(room_id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shutwork/command/rooms.rb', line 48

def show room_id
  items =
    case @target
    when :members
      @display = :display_account
      @client.room_members room_id
    when :files
      @display = :display_file
      @client.room_files room_id
    else
      @display = :display_message
      @client.room_messages room_id
    end
  if @raw
    puts items
  else
    JSON.parse(items).each(&method(@display))
  end
end