bats 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/bats.rb CHANGED
@@ -27,8 +27,13 @@ class Bats
27
27
  statusCode( i ).headers( :Location => l )
28
28
  end
29
29
 
30
- def self.statusCode i; ::HTTPResponse.const_get( "Status#{i}" ); end
31
- def statusCode i; ::HTTPResponse.const_get( "Status#{i}" ); end
30
+ def self.statusCode i
31
+ Class.new( ::HTTPResponse.const_get( "Status#{i}" ) )
32
+ end
33
+
34
+ def statusCode i
35
+ Class.new( ::HTTPResponse.const_get( "Status#{i}" ) )
36
+ end
32
37
 
33
38
  def self.call env; new.call( env ); end
34
39
  def call env
data/lib/bats.rb.save ADDED
@@ -0,0 +1,73 @@
1
+ exit
2
+ clear
3
+ exit
4
+ %w( httpresponse wizarding ).each do |f|
5
+ require 'bats/modules/httpresponse'
6
+ end
7
+
8
+ class Bats
9
+ extend Wizarding
10
+
11
+ traits :routes
12
+
13
+ def self.inherited c
14
+ c.traits *traits.keys
15
+ end
16
+
17
+ def self.addRoute m, p, o = nil, &b
18
+ @traits[ :routes ] ||= {}
19
+ @traits[ :routes ][ m ] ||= {}
20
+ @traits[ :routes ][ m ][ p ] = ( block_given? ) ? b : o
21
+ end
22
+
23
+ def self.get p, o = nil, &b; addRoute( :get, p, o, &b ); end
24
+ def self.post p, o = nil, &b; addRoute( :post, p, o, &b ); end
25
+ def self.put p, o = nil, &b; addRoute( :put, p, o, &b ); end
26
+ def self.delete p, o = nil, &b; addRoute( :delete, p, o, &b ); end
27
+
28
+ def self.redirect l, isTemporary = true
29
+ i = ( isTemporary ) ? '307' : '301'
30
+ statusCode( i ).headers( :Location => l )
31
+ end
32
+
33
+ def self.statusCode i; ::HTTPResponse.const_get( "Status#{i}" ); end
34
+ def statusCode i; ::HTTPResponse.const_get( "Status#{i}" ); end
35
+
36
+ def self.call env; new.call( env ); end
37
+ def call env
38
+ method = env[ 'REQUEST_METHOD' ].downcase.to_sym
39
+ path = env[ 'PATH_INFO' ]
40
+ matches ||= nil
41
+ if @routes && @routes[method] then
42
+ if @routes[method].include?( path ) then
43
+ route = @routes[method][ path ]
44
+ else
45
+ @routes[method].each do | p, b |
46
+ if p.kind_of?( Regexp ) then
47
+ if matches = path.match( p ) then
48
+ matches = matches[ 1, matches.length - 1 ]
49
+ matches.map! do | i |
50
+ i = ( i ) ? i : ''
51
+ i = i.to_i if i =~ /^\d+$/
52
+ i
53
+ end
54
+ route = b
55
+ break # Eh! give me a break here!
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ route ||= statusCode( 404 )
62
+ args = ( matches ) ? [ env, *matches ] : [ env ]
63
+ begin
64
+ route = route.call( args ) if route.kind_of?( Proc )
65
+ route.call( env )
66
+ rescue
67
+ b = '<h1>Ooops... The code broke.</h1>'
68
+ b += "<h3>#{$!.to_s}</h3>#{$!.backtrace.join( '<br/>' )}"
69
+ route = statusCode(500).body b
70
+ route.call( env )
71
+ end
72
+ end
73
+ end
@@ -12,6 +12,11 @@ module HTTPResponse
12
12
  @traits[ :headers ] ||= {}
13
13
  @traits[ :headers ].merge!( h )
14
14
  end
15
+
16
+ def self.body b
17
+ headers { 'Content-Length', b.length }
18
+ @traits[ :body ] = b
19
+ end
15
20
 
16
21
  def self.call env; new.call( env ); end
17
22
 
@@ -0,0 +1,37 @@
1
+ require 'bats/modules/classbaker'
2
+
3
+ module HTTPResponse
4
+ extend ClassBaker
5
+
6
+ class Response
7
+ extend ::Wizarding
8
+
9
+ traits :status, :headers, :body
10
+
11
+ def self.headers h
12
+ @traits[ :headers ] ||= {}
13
+ @traits[ :headers ].merge!( h )
14
+ end
15
+
16
+ def self.call env; new.call( env ); end
17
+
18
+ def call env
19
+ [ @status, @headers, @body ]
20
+ end
21
+ end
22
+
23
+ require 'yaml'
24
+ statuses = YAML.load_file( "#{File.expand_path( '../yaml', File.dirname( __FILE__ ) )}/statuses.yaml" ) # Somewhere in the nether regions beyond column 80!
25
+ statuses.each do | k, v |
26
+ bakeClass(
27
+ "Status#{k}" => {
28
+ :inherit => 'Response',
29
+ :status => k,
30
+ :headers => v[0],
31
+ :body => v[1]
32
+ }
33
+ )
34
+ end
35
+
36
+ end
37
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hans Oksendahl
@@ -33,10 +33,12 @@ extra_rdoc_files:
33
33
  files:
34
34
  - lib/bats.rb
35
35
  - lib/bats/yaml/statuses.yaml
36
+ - lib/bats/modules/httpresponse.rb.save
36
37
  - lib/bats/modules/httpresponse.rb
37
38
  - lib/bats/modules/classbaker.rb
38
39
  - lib/bats/modules/wizarding.rb
39
40
  - lib/bats/modules/metaid.rb
41
+ - lib/bats.rb.save
40
42
  - README
41
43
  has_rdoc: true
42
44
  homepage: http://hansoksendahl.com/bats