Class: TDiary::Dispatcher

Inherits:
Object show all
Defined in:
lib/tdiary/dispatcher.rb,
lib/tdiary/dispatcher/index_main.rb,
lib/tdiary/dispatcher/update_main.rb

Defined Under Namespace

Classes: IndexMain, UpdateMain

Constant Summary collapse

TARGET =
{
	:index => IndexMain,
	:update => UpdateMain
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Dispatcher

Returns a new instance of Dispatcher.



15
16
17
# File 'lib/tdiary/dispatcher.rb', line 15

def initialize( target )
	@target = TARGET[target]
end

Class Method Details

.extract_status_for_legacy_tdiary(head) ⇒ Object

FIXME temporary method during (scratch) refactoring



47
48
49
50
51
52
53
54
55
56
# File 'lib/tdiary/dispatcher.rb', line 47

def extract_status_for_legacy_tdiary( head )
	status_str = head.delete('status')
	return 200 if !status_str || status_str.empty?

	if m = status_str.match(/(\d+)\s(.+)\Z/)
		m[1].to_i
	else
		200
	end
end

.indexObject



58
59
60
# File 'lib/tdiary/dispatcher.rb', line 58

def index
	new( :index )
end

.send_headers(status, headers) ⇒ Object

stolen from Rack::Handler::CGI.send_headers



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tdiary/dispatcher.rb', line 33

def send_headers(status, headers)
	begin
		headers['type'] = headers.delete('Content-Type')
		$stdout.print CGI.new.header({'Status'=>status}.merge(headers))
	rescue EOFError
		charset = headers.delete('charset')
		headers['Content-Type'] ||= headers.delete( 'type' )
		headers['Content-Type'] += "; charset=#{charset}" if charset
		$stdout.print headers.map{|k,v| "#{k}: #{v}\r\n"}.join << "\r\n"
	end
	$stdout.flush
end

.updateObject



62
63
64
# File 'lib/tdiary/dispatcher.rb', line 62

def update
	new( :update )
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
# File 'lib/tdiary/dispatcher.rb', line 19

def call( env )
	req = adopt_rack_request_to_plain_old_tdiary_style( env )
	dispatch_cgi(req, RackCGI.new)
end

#dispatch_cgi(request, cgi) ⇒ Object

FIXME rename method name to more suitable one.



25
26
27
28
29
# File 'lib/tdiary/dispatcher.rb', line 25

def dispatch_cgi(request, cgi)
	result = @target.run( request, cgi )
	result.headers.reject!{|k,v| k.to_s.downcase == "status" }
	result.to_a
end