-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulate_server.rb
executable file
·70 lines (58 loc) · 1.84 KB
/
simulate_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
#!/usr/bin/env ruby
#
# encoding: UTF-8
# File: simulate_server.rb
# This file use to simulate http api(json) server, use for apps developer,
# don't need to wait for backends implement all the code complement.
#
# Writen by Lytsing Huang 2013-11-19
#
require 'rubygems'
require 'json'
require 'timeout'
require 'webrick'
include WEBrick
$dir = Dir::pwd
$port = 8080
def start_webrick(config = {})
config.update(:Port => $port, :DocumentRoot => $dir)
server = HTTPServer.new(config)
yield server if block_given?
# Trap signals so as to shutdown cleanly.
['INT', 'TERM'].each {|signal|
trap(signal) {server.shutdown}
}
server.start
end
class BaseServlet < HTTPServlet::AbstractServlet
def do_GET(req, resp)
puts "Header: " + req.raw_header.to_s
puts "Body: " + req.body
resp['Content-type'] = "application/json; charset=utf-8"
resp.body = {:status => 1}.to_json
end
# Respond with an HTTP POST just as we do for the HTTP GET.
alias do_POST do_GET
end
class LoginServlet < BaseServlet
def do_GET(req, resp)
resp.status = 200 # Success
resp.body = {:status => 1, :name => "lytsing Huang", :token => "2343423432",
:rank => 2, :memberId => 10086, :logoUrl => "images/logo.png",
:city => "Shenzhen", :level => 4 }.to_json
end
alias do_POST do_GET
end
class ClientUpdateServlet < BaseServlet
def do_GET(req, resp)
resp.body = {:status => 0, :fileSize => 2048, :md5 => "28a76acc891d0fd85966a27f34d897e0",
:lastestVersion => "2.2.1", :fileURL => "http:\/\/wenku.baidu.com\/view\/91c69a0d7cd184254b3535d0.apk",
:content => "amazing..."}.to_json
end
alias do_POST do_GET
end
start_webrick {|server|
server.mount("/api/v2/client_update.json", ClientUpdateServlet);
server.mount("/api/v2/login.json", LoginServlet);
# TODO: add your code here.
}