Module: SleepRoom

Defined in:
lib/sleeproom/cli.rb,
lib/sleeproom/utils.rb,
lib/sleeproom/record.rb,
lib/sleeproom/version.rb,
lib/sleeproom/record/tasks.rb,
lib/sleeproom/record/record.rb,
lib/sleeproom/record/api/api.rb,
lib/sleeproom/record/api/room.rb,
lib/sleeproom/record/websocket.rb,
lib/sleeproom/record/api/room_api.rb,
lib/sleeproom/record/write_status.rb,
lib/sleeproom/record/api/streaming_api.rb

Defined Under Namespace

Modules: Record Classes: CLI, Error

Constant Summary collapse

VERSION =
"0.4.4"

Class Method Summary collapse

Class Method Details

.config_dirString

Returns:

  • (String)


31
32
33
# File 'lib/sleeproom/utils.rb', line 31

def self.config_dir
  File.join(user_path, ".config", "sleeproom")
end

.config_path(config) ⇒ String

Parameters:

  • filename (String)

Returns:

  • (String)

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sleeproom/utils.rb', line 37

def self.config_path(config)
  name = {
    status: "status.yml",
    base: "base.yml",
    record: "record.yml",
    pid: "tmp/pid.yml"
  }
  file = name[config].to_s
  raise Error if file.empty?
  return File.join(config_dir, file) if config == :base

  if load_config(:base)[:config_path] == "USER_CONFIG"
    File.join(config_dir, file)
  else
    File.join(load_config(:base)[:config_path], file)
  end
end

.create_config_file(config, settings) ⇒ Object



64
65
66
67
68
69
# File 'lib/sleeproom/utils.rb', line 64

def self.create_config_file(config, settings)
  path = config_path(config)
  return false if File.exist?(path)
  mkdir(File.dirname(path)) unless Dir.exist?(File.dirname(path))
  write_config_file(config, settings)
end

.create_pid(pid) ⇒ Object



190
191
192
193
# File 'lib/sleeproom/utils.rb', line 190

def self.create_pid(pid)
  SleepRoom.create_config_file(:pid, pid)
  SleepRoom.write_config_file(:pid, pid)
end

.create_record(record = {default: []}) ⇒ Object



186
187
188
# File 'lib/sleeproom/utils.rb', line 186

def self.create_record(record = {default: []})
  SleepRoom.create_config_file(:record, record)
end

.create_status(status = []) ⇒ Object



182
183
184
# File 'lib/sleeproom/utils.rb', line 182

def self.create_status(status = [])
  SleepRoom.create_config_file(:status, status)
end

.error(string) ⇒ nil

Parameters:

  • string (String)

Returns:

  • (nil)


209
210
211
# File 'lib/sleeproom/utils.rb', line 209

def self.error(string)
  log(:error, string)
end

.file_logger(type, log) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sleeproom/utils.rb', line 227

def self.file_logger(type, log)
  path = configatron.logger.file.path
  mkdir(File.dirname(path)) unless Dir.exist?(File.dirname(path))
  logger = Logger.new(path)
  case type
  when :info
    logger.info(log)
  when :warning
    logger.warning(log)
  when :error
    logger.error(log)
  end
end

.info(string) ⇒ nil

Parameters:

  • string (String)

Returns:

  • (nil)


197
198
199
# File 'lib/sleeproom/utils.rb', line 197

def self.info(string)
  log(:info, string)
end

.init_baseObject



84
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/sleeproom/utils.rb', line 84

def self.init_base
  base = {
    web: {
      use: true,
      server: "localhost",
      port: 3000
    },
    proxy: {
      use: false,
      server: "localhost",
      port: 8080,
      type: "socks5"
    },
    record: {
      all: true,
      wanted: [],
      unwanted: []
    },
    config_path: "USER_CONFIG",
    save_path: "#{sleeproom_dir}/archive",
    working_path: "#{sleeproom_dir}/working",
    default_save_name: "%ROOMNAME%-%TIME%.ts",
    minyami: {
      threads: 8,
      retries: 999,
    },
    logger: {
      console: true,
      file: {
        use: false,
        path: "#{sleeproom_dir}/log"
      }
    }
  }
  create_config_file(:base, base)
end

.init_configObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sleeproom/utils.rb', line 121

def self.init_config
  mkdir(config_dir) unless Dir.exist?(config_dir)

  mkdir("#{config_dir}/tmp") unless Dir.exist?("#{config_dir}/tmp")
  
  init_base
  
  record = {
    "default" => []
  }

  create_config_file(:record, record)
  create_config_file(:status, [])
  write_config_file(:pid, nil)
end

.load_config(config) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/sleeproom/utils.rb', line 71

def self.load_config(config)
  raise Error if config.empty? || !File.exist?(config_path(config))

  YAML.load_file(config_path(config))
rescue Error => e
  init_config
  retry
end

.load_pidObject



160
161
162
163
164
165
# File 'lib/sleeproom/utils.rb', line 160

def self.load_pid
  SleepRoom.load_config(:pid)
rescue Error
  create_pid(nil)
  retry
end

.load_recordObject



175
176
177
178
179
180
# File 'lib/sleeproom/utils.rb', line 175

def self.load_record
  SleepRoom.load_config(:record)
rescue Error
  create_record
  retry
end

.load_statusObject



153
154
155
156
157
158
# File 'lib/sleeproom/utils.rb', line 153

def self.load_status
  SleepRoom.load_config(:status)
rescue Error
  create_status
  retry
end

.log(type, log) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/sleeproom/utils.rb', line 213

def self.log(type, log)
  if configatron.logger.console == true
    case type
    when :info
      puts("[INFO] #{log}".colorize(:white))
    when :warning
      warn("[WARN] #{log}".colorize(:yellow))
    when :error
      puts("[ERROR] #{log}".colorize(:red))
    end
  end
  file_logger(type, log) if configatron.logger.file.use == true
end

.mkdir(path) ⇒ Object



80
81
82
# File 'lib/sleeproom/utils.rb', line 80

def self.mkdir(path)
  FileUtils.mkdir_p(path) unless Dir.exist?(path)
end

.reload_configBoolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sleeproom/utils.rb', line 138

def self.reload_config
  configs = %i[base]
  configs.each do |config|
    configatron.configure_from_hash(YAML.load_file(config_path(config)))
  end
  true
rescue Errno::ENOENT => e
  info("Creating configuration...")
  init_base
  false
end

.root_pathString

Returns:

  • (String)


12
13
14
# File 'lib/sleeproom/utils.rb', line 12

def self.root_path
  Dir.pwd
end

.running?(pid = nil) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
171
172
173
# File 'lib/sleeproom/utils.rb', line 167

def self.running?(pid=nil)
  pid = SleepRoom.load_config(:pid) if pid.nil?
  Process.kill(0, pid)
  true
rescue
  false
end

.settingsObject



150
151
152
# File 'lib/sleeproom/utils.rb', line 150

def self.settings
  configatron
end

.sleeproom_dirString

Returns:

  • (String)


22
23
24
# File 'lib/sleeproom/utils.rb', line 22

def self.sleeproom_dir
  File.join(user_path, "sleeproom")
end

.user_pathString

Returns:

  • (String)


17
18
19
# File 'lib/sleeproom/utils.rb', line 17

def self.user_path
  Dir.home
end

.warning(string) ⇒ nil

Parameters:

  • string (String)

Returns:

  • (nil)


203
204
205
# File 'lib/sleeproom/utils.rb', line 203

def self.warning(string)
  log(:warning, string)
end

.working_dirObject



26
27
28
# File 'lib/sleeproom/utils.rb', line 26

def self.working_dir
  Dir.pwd
end

.write_config_file(config, settings) ⇒ Boolean

Parameters:

  • filename (Symbol)
  • settings (Hash)

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/sleeproom/utils.rb', line 58

def self.write_config_file(config, settings)
  file = File.new(config_path(config), "w")
  file.puts(YAML.dump(settings))
  file.close
end