maveric 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,51 +1,66 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
4
2
  name: maveric
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.4.0
7
- date: 2007-02-13 00:00:00 -08:00
8
- summary: A simple, non-magical, framework.
9
- require_paths:
10
- - lib/
11
- email: blinketje@gmail.com
12
- homepage: http://maveric.rubyforge.org/
13
- rubyforge_project:
14
- description:
15
- autorequire: maveric
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 1.0.0
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
- - blink
31
- files:
32
- - lib/maveric.rb
33
- - lib/maveric/fastcgi.rb
34
- - lib/maveric/extensions.rb
35
- - lib/maveric/webrick.rb
36
- - lib/maveric/mongrel.rb
37
- - lib/maveric/sessions.rb
38
- test_files: []
39
-
40
- rdoc_options: []
41
-
42
- extra_rdoc_files: []
7
+ - Scytrin dai Kinthra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
43
11
 
12
+ date: 2009-12-14 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: scytrin@gmail.com
44
27
  executables: []
45
28
 
46
29
  extensions: []
47
30
 
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - README
35
+ - lib/maveric.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/scytrin/maveric
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --main
43
+ - README
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
48
58
  requirements: []
49
59
 
50
- dependencies: []
60
+ rubyforge_project: maveric
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A simple, non-magical, MVC-ish framework for Rack.
65
+ test_files: []
51
66
 
@@ -1,111 +0,0 @@
1
- class Module
2
- ## Build a string akin to the absolute path of the nesting structure.
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
7
- end
8
-
9
- # Still deving this.
10
- def self.nesting_path_to_constant path
11
- dig = proc{|c,n| c.const_get(n) }
12
- path.gsub(/_([a-z])/){$1.upcase}.split('/')
13
- end
14
- end
15
-
16
- class Symbol
17
- # #to_s makes sense, but #to_i is just as effective.
18
- def <=> obj
19
- obj.instance_of?(Symbol) ?
20
- self.to_i <=> obj.to_i :
21
- super
22
- end
23
- end
24
-
25
- module Enumerable
26
- # blink> wtf do I call this? #find{|e| r = do_stuff(e) and break r }
27
- # manveru> map.find
28
- # LoganCapaldo> first_mapped
29
- # LoganCapaldo> find_such_that
30
- # tpope> Enumerable#eject
31
- # blink> o.O
32
- # tpope> returns on the first non-nil yield
33
- # tpope> it rhymes with select and detect and reject and collect and inject
34
- # tpope> it's a little confusing, but it's a confusing concept
35
-
36
- ##
37
- # Used to return the first value for which the given block evalutes as true.
38
- # Think .map(&b).compact.first
39
- def eject &block
40
- find{|e| result = block[e] and break result }
41
- end
42
- end
43
-
44
- ##
45
- # Extensions to StandardError to enable their usage in Maveric as responses.
46
- # The default response is 500 and content-type of text/plain.
47
- class StandardError
48
- DEFAULT_STATUS = 500
49
- DEFAULT_HEADERS = {'Content-Type'=>'text/plain'}
50
-
51
- ## Returns @status or DEFAULT_STATUS if unset.
52
- def status
53
- defined?(@status)? @status : DEFAULT_STATUS
54
- end
55
-
56
- ## Returns @headers or DEFAULT_HEADERS if unset.
57
- def headers
58
- defined?(@headers)? @headers : DEFAULT_HEADERS
59
- end
60
-
61
- ## Unless @body is defined, a self inspect and backtrace is output.
62
- def body
63
- defined?(@body)? @body : [inspect, *backtrace]*"\n"
64
- end
65
-
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
74
- end
75
-
76
- ##
77
- # After a bit of experimenting with Exception and raise I determined a fun
78
- # and simple way to generate fast HTTP response error thingies. As long as
79
- # you're within a call to Maveric#dispatch (with no further rescue clauses)
80
- # the StandardError will propogate up and be returned. As a StandardError
81
- # has similar accessors as a Controller, they should be compatible with any
82
- # outputting implimentation for Maveric.
83
- #
84
- # raise StandardErrpr; # 500 error
85
- # raise StandardError, 'Crap!'; # 500 error with message set to 'Crap!'
86
- # raise StandardError, [status, headers, body, *other_data] # Magic!
87
- #
88
- # In the final example line an Array is passed to raise as a second argument
89
- # rather than a String. This is taken as the HTTP status code. The next
90
- # element is tested to be a Hash, if so then it's values are merged into
91
- # the HTTP headers. The next item is tested to be a String, if so it is
92
- # appended to the response body. All remaining elements are appended to
93
- # the response body in inspect format.
94
- #
95
- # A simple way of only including data that might be interpreted to being the
96
- # status, headers, or body is to place a nil previous to the data you want
97
- # appended to the response body.
98
- def initialize data=nil
99
- # consider autosetting @status, like 503 for NoMethodError
100
- if data.is_a? Array and data[0].is_a? Integer
101
- @body = ''
102
- @status = data.shift if Integer === data.first
103
- @headers = DEF_HEADERS.merge data.shift if Hash === data.first
104
- @body << data.shift if String === data.first
105
- msg = @body.dup
106
- @body << "\n\n#{data.compact.map{|e|e.inspect}*"\n"}" unless data.empty?
107
- data = msg
108
- end
109
- super data
110
- end
111
- end
@@ -1,18 +0,0 @@
1
- %w~fcgi~.each{|m|require m}
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"}"
15
- end
16
- req.finish
17
- end
18
- end
@@ -1,24 +0,0 @@
1
- %w~mongrel~.each{|m|require m}
2
-
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
8
-
9
- ##
10
- # Runs #dispatch on the request and filters the resulting Controller instance
11
- # to the +response+ object.
12
- def process request, response
13
- reply = dispatch request.body, request.params
14
- response.start reply.status do |head,out|
15
- reply.headers.each {|k,v| head[k] = v }
16
- out.write reply.body
17
- end
18
- end
19
-
20
- ## Runs #prepare_enviroment on the env to get things moving.
21
- def request_begins params
22
- prepare_environment params
23
- end
24
- end
@@ -1,112 +0,0 @@
1
- require 'set'
2
-
3
- class Maveric
4
- ##
5
- # Contains session data and provides conveniance in generating an appropriate
6
- # cookie.
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
11
- COOKIE_NAME = 'SESSIONID'
12
-
13
- ##
14
- # Looks up a sessions by id given. The id argument should be a string of
15
- # a hex number. (num.to_s(16) If id is not found a new Session is created
16
- # and its Session#id is returned.
17
- def session id #wants stringed hex num
18
-
19
- #ensure crusty old sessions don't linger
20
- @__i = 0 unless defined? @__i
21
- @__i += 1
22
- reject!{|s| s.expires < Time.now-3 } if (@__i%=10) == 0 # 3 second grace
23
-
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
30
- end
31
-
32
- ##
33
- # Because apparently sessions are handy or helpful or something like that.
34
- # Implemented as that the hex of their #object_id (the one you'd see in
35
- # an Object#inspect) is treated as a session id.
36
- class Session
37
- DURATION = 15*60
38
- ## The default session length is 15 minutes. Simple to change.
39
- def initialize duration=DURATION
40
- @duration, @data = duration, {}
41
- touch
42
- end
43
-
44
- attr_reader :expires, :data, :duration
45
-
46
- ## Returns an upcased hexed object_id, not #object_id.to_s(16) however.
47
- def id
48
- @id||=[object_id<<1].pack('i').unpack('I')[0].to_s(16).upcase
49
- end
50
-
51
- ##
52
- # One session is less than another if it expires sooner. Useful if we
53
- # are managing in a manner akin to a priority stack.
54
- def <=> obj
55
- Session === obj ?
56
- self.expires <=> obj.expires :
57
- super
58
- end
59
-
60
- ## Set this session's expiry to @duration+Time.now
61
- def touch
62
- @expires = Time.now.gmtime + Integer(@duration)
63
- end
64
-
65
- ## Perhaps better named as #to_cookie for clarity?
66
- def to_s env=nil
67
- touch
68
- c = "#{::Maveric::SimpleSessions::COOKIE_NAME}=#{id}" #required
69
- c << "; expires=#{expires.httpdate}"
70
- return c unless env
71
- c << "; domain=#{env['SERVER_NAME']};"
72
- # need to determine a good way to discern proper path.
73
- # pertinant Maveric?
74
- c << "; secure" if false
75
- c
76
- end
77
- end
78
- end
79
-
80
- @sessions = SimpleSessions.new
81
-
82
- ## Return the standard session set.
83
- def self.sessions
84
- @sessions
85
- end
86
-
87
- ## Return a session via Session#session
88
- def self.session id
89
- @sessions.session id
90
- end
91
-
92
- ##
93
- # Uses env[:cookies] to find the session_id, and retrives the data to set the
94
- # values or env[:session].
95
- def set_session env
96
- session_id = env[:cookies][::Maveric::SimpleSessions::COOKIE_NAME].first
97
- env[:session] = ::Maveric.session session_id
98
- end
99
-
100
- prepare_env :set_session
101
-
102
- class Controller
103
- ## Touches the session and includes it into the http headers.
104
- def session_cleanup controller
105
- return unless @env[:session]
106
- @env[:session].touch
107
- @headers['Set-Cookie'] = [@env[:session].to_s(@env)]
108
- end
109
-
110
- after :session_cleanup
111
- end
112
- end
@@ -1,30 +0,0 @@
1
- %w~webrick webrick/httpservlet/abstract~.each{|m|require m}
2
-
3
- ## Adaptation of CampingHandler, quick works.
4
- class Maveric::WEBrickServlet < WEBrick::HTTPServlet::AbstractServlet
5
-
6
- ##
7
- # As a WEBrick servlet, but pass all but the first option to the Maveric on
8
- # initialization. The Maveric (not an instance) is expected as the first
9
- # option.
10
- def initialize server, maveric, *opts
11
- @maveric = maveric.new *@options
12
- super server, *opts
13
- end
14
-
15
- ## We don't need no stinkin' do_* methods.
16
- def service req, res
17
- begin
18
- env = req.meta_vars
19
- env['REQUEST_URI'] = req.unparsed_uri
20
- result = @maveric.dispatch req_body, env, req
21
- result.headers.each do |k, v|
22
- if k =~ /^X-SENDFILE$/i then @local_path = v
23
- else [*v].each {|x| res[k] = x } end
24
- end
25
- res.status, h, res.body = *result
26
- rescue ::Maveric::ServerError => error
27
- Maveric.log.fatal "WEBrick done got f'd up: #{error.inspect}"
28
- end
29
- end
30
- end