maveric 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,11 @@
1
1
  class Module
2
2
  ## Build a string akin to the absolute path of the nesting structure.
3
- def nesting_path
4
- ('::'+self.to_s). # '::' prepended for absolute path semantic
5
- gsub(/([a-z])([A-Z])/, '\1_\2').
6
- gsub(/(::)+/, '/').
7
- downcase
3
+ def to_path flatten=false
4
+ path = '/'+self.to_s.gsub(/::+/, '/') # '^/' as absolute path.
5
+ path = path.gsub(/([a-z])([A-Z])/,'\1_\2').sub(/\/Index$/,'/').downcase if flatten
6
+ path
8
7
  end
9
8
 
10
- # ::nodoc::
11
9
  # Still deving this.
12
10
  def self.nesting_path_to_constant path
13
11
  dig = proc{|c,n| c.const_get(n) }
@@ -45,31 +43,36 @@ end
45
43
 
46
44
  ##
47
45
  # Extensions to StandardError to enable their usage in Maveric as responses.
46
+ # The default response is 500 and content-type of text/plain.
48
47
  class StandardError
49
48
  DEFAULT_STATUS = 500
50
49
  DEFAULT_HEADERS = {'Content-Type'=>'text/plain'}
51
- attr_writer :status, :headers, :body
50
+
51
+ ## Returns @status or DEFAULT_STATUS if unset.
52
52
  def status
53
- @status or DEFAULT_STATUS
53
+ defined?(@status)? @status : DEFAULT_STATUS
54
54
  end
55
55
 
56
+ ## Returns @headers or DEFAULT_HEADERS if unset.
56
57
  def headers
57
- @headers or DEFAULT_HEADERS
58
+ defined?(@headers)? @headers : DEFAULT_HEADERS
58
59
  end
59
60
 
60
61
  ## Unless @body is defined, a self inspect and backtrace is output.
61
62
  def body
62
- @body or [inspect, *backtrace]*"\n"
63
+ defined?(@body)? @body : [inspect, *backtrace]*"\n"
63
64
  end
64
65
 
65
- ## See ::Maveric::Controller#to_http
66
- def to_http
67
- response = "Status: #{status}"+::Maveric::EOL
68
- response << headers.map{|(k,v)| "#{k}: #{v}" }*::Maveric::EOL
69
- response << ::Maveric::EOL*2 + body
66
+ ## See Maveric::Controller#to_http
67
+ def to_http(raw=true)
68
+ response = if not raw then 'Status:'
69
+ elsif defined? @env and @env.key? 'HTTP_VERSION' then @env['HTTP_VERSION']
70
+ else 'HTTP/1.1'end
71
+ response += " #{self.status}" + ::Maveric::EOL # Status message? :/
72
+ response << self.headers.map{|k,v| "#{k}: #{v}" }*::Maveric::EOL
73
+ response << ::Maveric::EOL*2 + self.body
70
74
  end
71
75
 
72
- alias_method :old_init, :initialize
73
76
  ##
74
77
  # After a bit of experimenting with Exception and raise I determined a fun
75
78
  # and simple way to generate fast HTTP response error thingies. As long as
@@ -103,6 +106,6 @@ class StandardError
103
106
  @body << "\n\n#{data.compact.map{|e|e.inspect}*"\n"}" unless data.empty?
104
107
  data = msg
105
108
  end
106
- old_init data
109
+ super data
107
110
  end
108
111
  end
@@ -1,14 +1,18 @@
1
1
  %w~fcgi~.each{|m|require m}
2
-
3
- class Maveric::FCGI
4
- def initialize maveric, *opts
5
- @maveric = maveric.new *opts
6
- ::FCGI.each do |req|
7
- req_body = ::StringIO.new( req.in.read || '' )
8
- result = @maveric.dispatch req_body, req.env
9
- req.out << "Status: 200 OK\r\n\r\n"
10
- req.out << result.to_s
11
- req.finish
2
+ ##
3
+ # Opening arguments are a Maveric class and options. Plz, don't pass a Maveric
4
+ # instance. The options of +opts+ are passed to the Maveric on instantiation.
5
+ #
6
+ # Maveric::FCGI(MyMaveric.new)
7
+ def Maveric.FCGI maveric, opts={}
8
+ ::FCGI.each do |req|
9
+ req.out << "Status: 200 OK" + ::Maveric::EOL*2 if $DEBUG
10
+ req.out << response = begin
11
+ maveric.dispatch(req.in, req.env).to_http
12
+ rescue
13
+ "Status: 500 Server Error omgwtf!"+::Maveric::EOL*2+
14
+ "#{$!}\n#{$!.backtrace*"\n"}"
12
15
  end
16
+ req.finish
13
17
  end
14
18
  end
@@ -1,44 +1,24 @@
1
1
  %w~mongrel~.each{|m|require m}
2
2
 
3
- class Maveric::MongrelHandler < Mongrel::HttpHandler
4
- def initialize maveric, opts={}
5
- ::Maveric.type_check :maveric, maveric do |k| k <= ::Maveric end
6
- ::Maveric.type_check :opts, opts, Hash
7
- super()
8
- @request_notify = true
9
- @maveric = maveric.new opts
10
- end
3
+ ##
4
+ # Includes Mongrel::HttpHandlerPlugin into Maveric, allowing the use of Maveric
5
+ # subclasses as HttpHandlers in the context of Mongrel.
6
+ class Maveric
7
+ include Mongrel::HttpHandlerPlugin
11
8
 
12
9
  ##
13
- # NOTE: Update doc.
10
+ # Runs #dispatch on the request and filters the resulting Controller instance
11
+ # to the +response+ object.
14
12
  def process request, response
15
- Maveric.log.info "Mongrel+#{self.class}#process"
16
- reply = @maveric.process request.body, request.params
17
- # output the result
13
+ reply = dispatch request.body, request.params
18
14
  response.start reply.status do |head,out|
19
15
  reply.headers.each {|k,v| head[k] = v }
20
16
  out.write reply.body
21
17
  end
22
18
  end
23
19
 
24
- ## Prepares by loading session and reflection data into the request params.
20
+ ## Runs #prepare_enviroment on the env to get things moving.
25
21
  def request_begins params
26
- begin
27
- Maveric.log.info "Mongrel+#{self.class}#request_begins"
28
- @maveric.prepare_environment params
29
- rescue
30
- Maveric.log.fatal "#{$!.inspect}\n#{$@[0..5]*"\n"}"
31
- end
32
- end
33
-
34
- ## Does nothing yet. Yet.
35
- def request_progess params, clen, total
36
- begin
37
- Maveric.log.info "Mongrel+#{self.class}#request_progess"+
38
- ": #{clen}/#{total}"
39
- Maveric.log.debug params
40
- rescue
41
- Maveric.log.fatal "#{$!.inspect}\n#{$@[0..5]*"\n"}"
42
- end
22
+ prepare_environment params
43
23
  end
44
24
  end
@@ -4,7 +4,10 @@ class Maveric
4
4
  ##
5
5
  # Contains session data and provides conveniance in generating an appropriate
6
6
  # cookie.
7
- class Sessions < Set
7
+ #
8
+ # This is a simple session implementation. Data is kept in a self maintaining
9
+ # Set persisting in memory. Noot good if yoou're dealing with oodles of sets.
10
+ class SimpleSessions < Set
8
11
  COOKIE_NAME = 'SESSIONID'
9
12
 
10
13
  ##
@@ -12,17 +15,18 @@ class Maveric
12
15
  # a hex number. (num.to_s(16) If id is not found a new Session is created
13
16
  # and its Session#id is returned.
14
17
  def session id #wants stringed hex num
15
- ::Maveric.type_check :id, id, String
16
18
 
17
19
  #ensure crusty old sessions don't linger
18
- @__i += 1 rescue @__i = 1
19
- reject!{|s| s.expire < Time.now-3 } if (@__i%=10) == 0 # 3 second grace
20
+ @__i = 0 unless defined? @__i
21
+ @__i += 1
22
+ reject!{|s| s.expires < Time.now-3 } if (@__i%=10) == 0 # 3 second grace
20
23
 
21
- # now get the session
22
- session = find{|s| s.id == id } and return session
23
-
24
- # there isn't one? then make a new one!
25
- add (session=Session.new) and return session
24
+ if id # now get the session if we have something to search for
25
+ session = find{|s| id.casecmp(s.id)==0 }
26
+ else # there isn't one? then make a new one!
27
+ add session=Session.new
28
+ end
29
+ return session
26
30
  end
27
31
 
28
32
  ##
@@ -33,10 +37,8 @@ class Maveric
33
37
  DURATION = 15*60
34
38
  ## The default session length is 15 minutes. Simple to change.
35
39
  def initialize duration=DURATION
36
- ::Maveric.type_check :duration, duration, Integer
37
40
  @duration, @data = duration, {}
38
41
  touch
39
- ::Maveric.log.debug self
40
42
  end
41
43
 
42
44
  attr_reader :expires, :data, :duration
@@ -63,7 +65,7 @@ class Maveric
63
65
  ## Perhaps better named as #to_cookie for clarity?
64
66
  def to_s env=nil
65
67
  touch
66
- c = "#{::Maveric::Sessions::COOKIE_NAME}=#{id}" #required
68
+ c = "#{::Maveric::SimpleSessions::COOKIE_NAME}=#{id}" #required
67
69
  c << "; expires=#{expires.httpdate}"
68
70
  return c unless env
69
71
  c << "; domain=#{env['SERVER_NAME']};"
@@ -75,7 +77,7 @@ class Maveric
75
77
  end
76
78
  end
77
79
 
78
- @sessions = Sessions.new
80
+ @sessions = SimpleSessions.new
79
81
 
80
82
  ## Return the standard session set.
81
83
  def self.sessions
@@ -88,18 +90,19 @@ class Maveric
88
90
  end
89
91
 
90
92
  ##
91
- # Uses env[:cookie] to find the session_id, and retrives the data to set the
93
+ # Uses env[:cookies] to find the session_id, and retrives the data to set the
92
94
  # values or env[:session].
93
95
  def set_session env
94
- session_id = env[:cookies][::Maveric::Sessions::COOKIE_NAME].first
96
+ session_id = env[:cookies][::Maveric::SimpleSessions::COOKIE_NAME].first
95
97
  env[:session] = ::Maveric.session session_id
96
98
  end
97
99
 
98
- adjust_env :set_session
100
+ prepare_env :set_session
99
101
 
100
102
  class Controller
101
103
  ## Touches the session and includes it into the http headers.
102
104
  def session_cleanup controller
105
+ return unless @env[:session]
103
106
  @env[:session].touch
104
107
  @headers['Set-Cookie'] = [@env[:session].to_s(@env)]
105
108
  end
@@ -8,7 +8,6 @@ class Maveric::WEBrickServlet < WEBrick::HTTPServlet::AbstractServlet
8
8
  # initialization. The Maveric (not an instance) is expected as the first
9
9
  # option.
10
10
  def initialize server, maveric, *opts
11
- ::Maveric.type_check :maveric, maveric do |k| k <= ::Maveric end
12
11
  @maveric = maveric.new *@options
13
12
  super server, *opts
14
13
  end
@@ -16,8 +15,6 @@ class Maveric::WEBrickServlet < WEBrick::HTTPServlet::AbstractServlet
16
15
  ## We don't need no stinkin' do_* methods.
17
16
  def service req, res
18
17
  begin
19
- ::Maveric.log.warn req.inspect
20
- req_body = StringIO.new(req.body || '')
21
18
  env = req.meta_vars
22
19
  env['REQUEST_URI'] = req.unparsed_uri
23
20
  result = @maveric.dispatch req_body, env, req
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: maveric
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-01-28 00:00:00 -07:00
6
+ version: 0.4.0
7
+ date: 2007-02-13 00:00:00 -08:00
8
8
  summary: A simple, non-magical, framework.
9
9
  require_paths:
10
10
  - lib/
@@ -30,11 +30,11 @@ authors:
30
30
  - blink
31
31
  files:
32
32
  - lib/maveric.rb
33
- - lib/maveric/mongrel.rb
34
33
  - lib/maveric/fastcgi.rb
34
+ - lib/maveric/extensions.rb
35
35
  - lib/maveric/webrick.rb
36
+ - lib/maveric/mongrel.rb
36
37
  - lib/maveric/sessions.rb
37
- - lib/maveric/extensions.rb
38
38
  test_files: []
39
39
 
40
40
  rdoc_options: []
@@ -47,13 +47,5 @@ extensions: []
47
47
 
48
48
  requirements: []
49
49
 
50
- dependencies:
51
- - !ruby/object:Gem::Dependency
52
- name: log4r
53
- version_requirement:
54
- version_requirements: !ruby/object:Gem::Version::Requirement
55
- requirements:
56
- - - ">"
57
- - !ruby/object:Gem::Version
58
- version: 0.0.0
59
- version:
50
+ dependencies: []
51
+