gnarly 0.0.1 → 0.0.2

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.
@@ -1,10 +1,10 @@
1
+ require 'gnarly/provides.rb'
1
2
  require 'gnarly/url.rb'
2
- require 'gnarly/request.rb'
3
- require 'gnarly/mimeparse.rb'
4
3
 
5
4
  module Gnarly
6
5
 
7
6
  class Application
7
+ include Provides
8
8
 
9
9
  def initialize()
10
10
  @urls = []
@@ -16,69 +16,11 @@ module Gnarly
16
16
  @urls << url
17
17
  end
18
18
 
19
- def environment()
20
- @environment
21
- end
22
-
23
- def request()
24
- @request
25
- end
26
-
27
- def ok(body, mime=nil)
28
- headers = mime ? {"Content-type" => mime} : {}
29
- [200, headers, [body]]
30
- end
31
-
32
- def not_found(body="Not Found", mime="text/plain")
33
- [404, {"Content-type" => mime}, [body]]
34
- end
35
-
36
- def method_not_allowed(allow, body="Method Not Allowed", mime="text/plain")
37
- [405,
38
- {"Content-type" => mime, "Allow" => allow},
39
- [body]]
40
- end
41
-
42
- def not_acceptable(acceptable=nil, body=nil, mime="text/plain")
43
- body = "" unless body
44
- [406,
45
- {"Content-type" => mime},
46
- [body]]
47
- end
48
-
49
- def internal_server_error(body="Internal Server Error", mime="text/plain")
50
- [500, {"Content-type" => mime}, [body]]
51
- end
52
-
53
- def provides(*mimes, &block)
54
- mime = MIMEParse.best_match mimes, request.accept
55
- if mime
56
- response = block.call mime if block
57
- if response.is_a? Array and response.size == 3
58
- status, headers, body = response
59
- unless headers.key? "Content-type"
60
- charset = body_charset(body)
61
- headers["Content-type"] =
62
- charset ? "#{mime}; charset=#{charset}" : mime
63
- end
64
- response
65
- else
66
- name, line = @responder.source_location
67
- msg = "Bad return type for block in #{filename} line #{line}"
68
- internal_server_error msg
69
- end
70
- else
71
- not_acceptable
72
- end
73
- end
74
-
75
19
  def call(env)
76
- @request = Request.new env
77
- @environment = env
78
- path = env["PATH_INFO"]
20
+ environment env
21
+ path = request.path
79
22
  if url = @urls.detect { |u| u.match? path }
80
- method = env["REQUEST_METHOD"]
81
- @responder = url.responder method
23
+ @responder = url.responder request.method
82
24
  if @responder
83
25
  instance_exec *url.parameters, &@responder
84
26
  else
@@ -89,17 +31,12 @@ module Gnarly
89
31
  end
90
32
  end
91
33
 
92
- private
93
-
94
- def body_charset(body)
95
- charset = nil
96
- body.each do |s|
97
- charset = s.encoding.to_s
98
- break
99
- end
100
- charset
101
- end
34
+ end
102
35
 
36
+ def self.application(&block)
37
+ app = Application.new
38
+ app.instance_eval &block if block
39
+ app
103
40
  end
104
41
 
105
42
  end
@@ -0,0 +1,30 @@
1
+ require 'gnarly/request.rb'
2
+
3
+ module Gnarly
4
+
5
+ module Base
6
+
7
+ def environment(env=nil)
8
+ if env
9
+ @environment = env
10
+ @request = nil
11
+ end
12
+ @environment
13
+ end
14
+
15
+ def request()
16
+ @request ||= Request.new @environment
17
+ end
18
+
19
+ def body_charset(body)
20
+ charset = nil
21
+ body.each do |s|
22
+ charset = s.encoding.to_s
23
+ break
24
+ end
25
+ charset
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,35 @@
1
+ require 'gnarly/base.rb'
2
+ require 'gnarly/status.rb'
3
+ require 'gnarly/mimeparse.rb'
4
+
5
+ module Gnarly
6
+
7
+ module Provides
8
+ include Base
9
+ include Status
10
+
11
+ def provides(*mimes, &block)
12
+ mime = MIMEParse.best_match mimes, request.accept
13
+ if mime
14
+ response = block.call mime if block
15
+ if response.is_a? Array and response.size == 3
16
+ status, headers, body = response
17
+ unless headers.key? "Content-type"
18
+ charset = body_charset(body)
19
+ headers["Content-type"] =
20
+ charset ? "#{mime}; charset=#{charset}" : mime
21
+ end
22
+ response
23
+ else
24
+ name, line = @responder.source_location
25
+ msg = "Bad return type for block in #{filename} line #{line}"
26
+ internal_server_error msg
27
+ end
28
+ else
29
+ not_acceptable
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -1,3 +1,4 @@
1
+ require 'rack/utils'
1
2
 
2
3
  module Gnarly
3
4
 
@@ -8,6 +9,14 @@ module Gnarly
8
9
  parse_content_type
9
10
  end
10
11
 
12
+ def path()
13
+ @path ||= @environment["PATH_INFO"]
14
+ end
15
+
16
+ def method()
17
+ @method ||= @environment["REQUEST_METHOD"]
18
+ end
19
+
11
20
  def accept()
12
21
  @accept ||= @environment["HTTP_ACCEPT"]
13
22
  end
@@ -30,6 +39,10 @@ module Gnarly
30
39
  @body
31
40
  end
32
41
 
42
+ def params()
43
+ @params ||= ::Rack::Utils.parse_query(@environment["QUERY_STRING"])
44
+ end
45
+
33
46
  private
34
47
 
35
48
  def parse_content_type()
@@ -0,0 +1,34 @@
1
+
2
+ module Gnarly
3
+
4
+ module Status
5
+
6
+ def ok(body, mime=nil)
7
+ headers = mime ? {"Content-type" => mime} : {}
8
+ [200, headers, [body]]
9
+ end
10
+
11
+ def not_found(body="Not Found", mime="text/plain")
12
+ [404, {"Content-type" => mime}, [body]]
13
+ end
14
+
15
+ def method_not_allowed(allow, body="Method Not Allowed", mime="text/plain")
16
+ [405,
17
+ {"Content-type" => mime, "Allow" => allow},
18
+ [body]]
19
+ end
20
+
21
+ def not_acceptable(acceptable=nil, body=nil, mime="text/plain")
22
+ body = "" unless body
23
+ [406,
24
+ {"Content-type" => mime},
25
+ [body]]
26
+ end
27
+
28
+ def internal_server_error(body="Internal Server Error", mime="text/plain")
29
+ [500, {"Content-type" => mime}, [body]]
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'uuid'
2
+ require 'json'
3
+ require 'gnarly/provides.rb'
4
+
5
+ module Gnarly
6
+
7
+ class UUID
8
+ include Provides
9
+
10
+ def initialize(app, opts={})
11
+ @app = app
12
+ @path = opts[:path] ||= "/uuid"
13
+ @default_number = opts[:default_number] ||= 1
14
+ @generator = ::UUID.new
15
+ end
16
+
17
+ def call(env)
18
+ environment env
19
+ if request.path == "/uuid"
20
+ provides "text/plain", "application/json" do |mime|
21
+ n = request.params["n"]
22
+ num = n ? n.to_i : @default_number
23
+ uuids = num.times.collect do
24
+ @generator.generate
25
+ end
26
+ case mime
27
+ when "text/plain"
28
+ ok uuids.join("\n")
29
+ when "application/json"
30
+ ok uuids.to_json
31
+ end
32
+ end
33
+ else
34
+ @app.call env
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
data/lib/gnarly.rb CHANGED
@@ -2,10 +2,4 @@ require 'gnarly/application.rb'
2
2
 
3
3
  module Gnarly
4
4
 
5
- def self.application(&block)
6
- app = Application.new
7
- app.instance_eval &block
8
- app
9
- end
10
-
11
5
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kim Dalsgaard
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-02-24 00:00:00 +01:00
17
+ date: 2010-02-25 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -28,9 +28,13 @@ extra_rdoc_files: []
28
28
 
29
29
  files:
30
30
  - lib/gnarly/application.rb
31
+ - lib/gnarly/base.rb
31
32
  - lib/gnarly/mimeparse.rb
33
+ - lib/gnarly/provides.rb
32
34
  - lib/gnarly/request.rb
35
+ - lib/gnarly/status.rb
33
36
  - lib/gnarly/url.rb
37
+ - lib/gnarly/uuid.rb
34
38
  - lib/gnarly.rb
35
39
  has_rdoc: true
36
40
  homepage: