Class: BilibiliSunday::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/bilibili_sunday/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(downloader) ⇒ RequestHandler

Returns a new instance of RequestHandler.



11
12
13
# File 'lib/bilibili_sunday/server.rb', line 11

def initialize(downloader)
	@downloader = downloader
end

Instance Method Details

#handle_active_videosObject



65
66
67
# File 'lib/bilibili_sunday/server.rb', line 65

def handle_active_videos
	return 200, {result: @downloader.active_videos}
end

#handle_all_videosObject



61
62
63
# File 'lib/bilibili_sunday/server.rb', line 61

def handle_all_videos
	return 200, {result: @downloader.all_videos}
end

#handle_cid_for_video_url(url) ⇒ Object



45
46
47
# File 'lib/bilibili_sunday/server.rb', line 45

def handle_cid_for_video_url(url)
	return 200, {result: @downloader.cid_for_video_url(url)}
end

#handle_error(error_code, error_message) ⇒ Object



73
74
75
# File 'lib/bilibili_sunday/server.rb', line 73

def handle_error(error_code, error_message)
	return 500, {error: {code: error_code, message: error_message}}
end

#handle_query_status(cid) ⇒ Object



57
58
59
# File 'lib/bilibili_sunday/server.rb', line 57

def handle_query_status(cid)
	return 200, {result: @downloader.query_status(cid)}
end

#handle_remove_cache(cid) ⇒ Object



69
70
71
# File 'lib/bilibili_sunday/server.rb', line 69

def handle_remove_cache(cid)
	return 200, {result: @downloader.remove_cache(cid)}
end

#handle_request(method, params) ⇒ Object



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
43
# File 'lib/bilibili_sunday/server.rb', line 15

def handle_request(method, params)
	begin
		result = /^bilibili_sunday.(.*?)$/.match(method)

		return handle_error(1, 'No matching method. ') unless result

		method = result[1]

		if method == 'cid_for_video_url'
			handle_cid_for_video_url(params[0])
		elsif method == 'title_for_video_url'
			handle_title_for_video_url(params[0])
		elsif method == 'request_cache'
			handle_request_cache(params[0].to_i)
		elsif method == 'query_status'
			handle_query_status(params[0].to_i)
		elsif method == 'all_videos'
			handle_all_videos
		elsif method == 'active_videos'
			handle_active_videos
		elsif method == 'remove_cache'
			handle_remove_cache(params[0].to_i)
		else
			handle_error(1, 'No matching method. ')
		end
	rescue
		return handle_error(2, 'Internal server error. ')
	end
end

#handle_request_cache(cid) ⇒ Object



53
54
55
# File 'lib/bilibili_sunday/server.rb', line 53

def handle_request_cache(cid)
	return 200, {result: @downloader.request_cache(cid)}
end

#handle_title_for_video_url(url) ⇒ Object



49
50
51
# File 'lib/bilibili_sunday/server.rb', line 49

def handle_title_for_video_url(url)
	return 200, {result: @downloader.title_for_video_url(url)}
end