Class: TDiary::Dispatcher

Inherits:
Object
  • 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.



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

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

Class Method Details

.extract_status_for_legacy_tdiary(head) ⇒ Object

FIXME temporary method during (scratch) refactoring



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

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



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

def index
	new( :index )
end

.send_headers(status, headers) ⇒ Object

stolen from Rack::Handler::CGI.send_headers



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

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



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

def update
	new( :update )
end

Instance Method Details

#call(env) ⇒ Object



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

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.



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

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