oaf 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWQyNWVmNjg3MTg0NjYwMzgyYzI1OTVhZGQ3ZDFiMWM3NzhhNzBjZQ==
4
+ MDU1ZjY5NGMxZTlkYzViNjQ5ZmFiYTUxZDdjZjdkYzU0ODkxZTVkMg==
5
5
  data.tar.gz: !binary |-
6
- NDdiZWMxMmYzMDI3NmZhYmNkY2VhYzVhZmEzZGQ5N2ZmNjVkNzRmNA==
6
+ MTU4MGZlZjE5NjY1YmEwMmZiNDUzZWY3OGRkYmNiZjQ1ZTk4NmM4MA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NjY1ODU1NmM0N2NjYWZhMDZmMTg3MmIxNmY5MTFiNTUxZjgzOWU4ZDk3OWQ2
10
- YzliNDgxMDM2ZGRlNWY5YjI2ZWM5ZmQ3NmI3OWNhZDAyNmJmOTZiYWYwMTE0
11
- NzIwNmQ2ZDgxNjVkOTdjNWI5NDNiOTI0OWUxMDEzNjkwMWI2MWE=
9
+ YjI5ZWVlNDM4YmEzNWZlMGM1Mzc5NjIzN2RiZTczOWQyZDk3YmIxYTA3ZDMz
10
+ MTMzMjlmODlmZWVhODI4YjNhMGNhMzBmZDU5OWMwODY3OWQ3NjVkMzQ2MmE3
11
+ OGVjN2RlNWI3NTQ1M2UzMjE0YjIzNmE0MDUwNmIwOGQwMTQ3NGI=
12
12
  data.tar.gz: !binary |-
13
- OThmYWJkMTUwNDliZDYzMGFjMWM1NmI3ODUxMzIxNTIxMmQ1MTYxNWJjY2Rj
14
- MjI0OTBlNDE2ZWQ4M2I3ZmVmMzE5MzU4MWZlNWUyNjllMjM3YzM4MTM4ZDA0
15
- NmRhZjM0ZGFkMTIwZDZmMTI5MjA2YzliZDk1MDJhOTA3M2NiOWQ=
13
+ YTY4MTA0YmE3YmUxODNhNTRhMTRmMDc1ZTcwZTQ5MWU5NzkzNDU0NTViYWJm
14
+ YzkzZTBiY2NjNzc2OTA3NzVkZDk0NmZkODBhZjIzZGFjYzVlOWY4YTA1MzZh
15
+ ZGRhMDUxNWQ1NTcyZWU2MmI5YTE1ZjlhOWRhYzA3Y2U2ZTU3NmM=
data/README.md CHANGED
@@ -56,6 +56,7 @@ Hello, world!
56
56
 
57
57
  ### HTTP Methods
58
58
  Files must carry the extension of the HTTP method used to invoke them.
59
+ Oaf should support any HTTP method, including custom methods.
59
60
 
60
61
  ### Headers and Status
61
62
  You can indicate HTTP headers and status using stdout from your script.
data/bin/oaf CHANGED
@@ -61,4 +61,4 @@ else
61
61
  exit 1
62
62
  end
63
63
 
64
- Oaf::HTTP.serve options[:path], options[:port]
64
+ Oaf::HTTP::Server.serve options[:path], options[:port]
data/lib/oaf.rb CHANGED
@@ -22,4 +22,5 @@
22
22
 
23
23
  require 'oaf/version'
24
24
  require 'oaf/util'
25
- require 'oaf/http'
25
+ require 'oaf/http/handler'
26
+ require 'oaf/http/server'
@@ -0,0 +1,97 @@
1
+ # oaf - Care-free web app prototyping using files and scripts
2
+ # Copyright 2013 Ryan Uber <ru@ryanuber.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require 'oaf/util'
24
+ require 'oaf/http/server'
25
+ require 'webrick'
26
+
27
+ module Oaf::HTTP
28
+
29
+ # Provides all required handlers to WEBrick for serving all basic HTTP
30
+ # methods. WEBrick handles GET, POST, HEAD, and OPTIONS out of the box,
31
+ # but to mock most RESTful applications we are going to want PUT and
32
+ # DELETE undoubtedly.
33
+ class Handler < WEBrick::HTTPServlet::AbstractServlet
34
+
35
+ # Creates a new abstract server object and allows passing in the root
36
+ # path of the server via an argument.
37
+ #
38
+ # == Parameters:
39
+ # server::
40
+ # A WEBrick::HTTPServer object
41
+ # path::
42
+ # A string containing the root path
43
+ #
44
+ def initialize server, path
45
+ super server
46
+ @path = path
47
+ end
48
+
49
+ # Main server method. Oaf does not really differentiate between different
50
+ # HTTP methods, but needs to at least support passing them all.
51
+ #
52
+ # == Parameters:
53
+ # req::
54
+ # A WEBrick::HTTPRequest object
55
+ # res::
56
+ # A WEBrick::HTTPResponse object
57
+ #
58
+ def process_request req, res
59
+ req_headers = req.header
60
+ req_query = req.query
61
+ req_body = Oaf::HTTP::Server.get_request_body req
62
+ file = Oaf::Util.get_request_file @path, req.path, req.request_method
63
+ out = Oaf::Util.get_output file, req_headers, req_body, req_query
64
+ res_headers, res_status, res_body = Oaf::HTTP::Server.parse_response out
65
+ Oaf::HTTP::Server.set_response! res, res_headers, res_body, res_status
66
+ end
67
+
68
+ # A magic respond_to? implementation to trick WEBrick into thinking that any
69
+ # do_* methods are already defined. This allows method_missing to do its job
70
+ # once WEBrick makes its call to the method.
71
+ #
72
+ # == Parameters:
73
+ # method::
74
+ # The name of the class method being checked
75
+ #
76
+ # == Returns:
77
+ # Boolean, true if the method name matches do_[A-Z]+, else super.
78
+ #
79
+ def respond_to? method
80
+ method.to_s =~ /^do_[A-Z]+$/ ? true : super
81
+ end
82
+
83
+ # A magic method to handle any and all do_* methods. This allows Oaf to
84
+ # claim some degree of support for any HTTP method, be it a known and
85
+ # commonly used method such as PUT or DELETE, or custom methods.
86
+ #
87
+ # == Parameters:
88
+ # method::
89
+ # The name of the method being called
90
+ # *opt::
91
+ # A list of arguments to pass along to the processing method
92
+ #
93
+ def method_missing method, *opt
94
+ method.to_s =~ /^do_[A-Z]+$/ ? process_request(*opt) : super
95
+ end
96
+ end
97
+ end
@@ -20,11 +20,13 @@
20
20
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
+ require 'oaf/util'
24
+ require 'oaf/http/handler'
23
25
  require 'webrick'
24
26
 
25
- module Oaf
27
+ module Oaf::HTTP
26
28
 
27
- module HTTP
29
+ module Server
28
30
  extend Oaf
29
31
  extend self
30
32
 
@@ -100,15 +102,7 @@ module Oaf
100
102
  #
101
103
  def serve path, port
102
104
  server = WEBrick::HTTPServer.new :Port => port
103
- server.mount_proc '/' do |req, res|
104
- req_headers = req.header
105
- req_query = req.query
106
- req_body = Oaf::HTTP::get_request_body req
107
- file = Oaf::Util.get_request_file path, req.path, req.request_method
108
- out = Oaf::Util.get_output file, req_headers, req_body, req_query
109
- res_headers, res_status, res_body = Oaf::HTTP.parse_response out
110
- Oaf::HTTP.set_response! res, res_headers, res_body, res_status
111
- end
105
+ server.mount '/', Oaf::HTTP::Handler, path
112
106
  trap 'INT' do server.shutdown end
113
107
  server.start
114
108
  end
data/lib/oaf/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
23
  module Oaf
24
- VERSION = '0.2.3'
24
+ VERSION = '0.2.4'
25
25
  end
@@ -25,7 +25,7 @@ require 'spec_helper'
25
25
  module Oaf
26
26
  describe "Returning HTTP Responses" do
27
27
  it "should return safe defaults if output is empty" do
28
- headers, status, body = Oaf::HTTP.parse_response ''
28
+ headers, status, body = Oaf::HTTP::Server.parse_response ''
29
29
  headers.should eq({})
30
30
  status.should eq(200)
31
31
  body.should eq("\n")
@@ -33,7 +33,7 @@ module Oaf
33
33
 
34
34
  it "should return safe defaults when only body is present" do
35
35
  text = "This is a test\n"
36
- headers, status, body = Oaf::HTTP.parse_response text
36
+ headers, status, body = Oaf::HTTP::Server.parse_response text
37
37
  headers.should eq({})
38
38
  status.should eq(200)
39
39
  body.should eq("This is a test\n")
@@ -41,25 +41,25 @@ module Oaf
41
41
 
42
42
  it "should return headers correctly" do
43
43
  text = "---\nx-powered-by: oaf"
44
- headers, status, body = Oaf::HTTP.parse_response text
44
+ headers, status, body = Oaf::HTTP::Server.parse_response text
45
45
  headers.should eq({'x-powered-by' => 'oaf'})
46
46
  end
47
47
 
48
48
  it "should return status correctly" do
49
49
  text = "---\n201"
50
- headers, status, body = Oaf::HTTP.parse_response text
50
+ headers, status, body = Oaf::HTTP::Server.parse_response text
51
51
  status.should eq(201)
52
52
  end
53
53
 
54
54
  it "should return body correctly" do
55
55
  text = "This is a test\n---\n200"
56
- headers, status, body = Oaf::HTTP.parse_response text
56
+ headers, status, body = Oaf::HTTP::Server.parse_response text
57
57
  body.should eq("This is a test\n")
58
58
  end
59
59
 
60
60
  it "should return body correctly when no metadata is present" do
61
61
  text = "This is a test"
62
- headers, status, body = Oaf::HTTP.parse_response text
62
+ headers, status, body = Oaf::HTTP::Server.parse_response text
63
63
  body.should eq("This is a test\n")
64
64
  end
65
65
  end
@@ -85,24 +85,21 @@ module Oaf
85
85
  Dir.delete @tempdir1
86
86
  end
87
87
 
88
- before(:each) do
88
+ it "should start an HTTP server" do
89
89
  @webrick = double()
90
90
  @webrick.should_receive(:start).once.and_return(true)
91
91
  WEBrick::HTTPServer.stub(:new).and_return(@webrick)
92
- end
93
-
94
- it "should start an HTTP server" do
95
- @webrick.should_receive(:mount_proc).with('/').once \
96
- .and_yield(Oaf::FakeReq.new, Oaf::FakeRes.new)
97
- Oaf::HTTP.serve '/tmp', 9000
92
+ @webrick.should_receive(:mount) \
93
+ .with('/', Oaf::HTTP::Handler, '/tmp').once \
94
+ .and_return(true)
95
+ Oaf::HTTP::Server.serve '/tmp', 9000
98
96
  end
99
97
 
100
98
  it "should parse the request properly" do
101
99
  req = Oaf::FakeReq.new :path => @f1request
102
100
  res = Oaf::FakeRes.new
103
- @webrick.should_receive(:mount_proc).with('/').once \
104
- .and_yield(req, res)
105
- Oaf::HTTP.serve @tempdir1, 9000
101
+ handler = Oaf::HTTP::Handler.new Oaf::FakeServlet.new, @tempdir1
102
+ handler.process_request req, res
106
103
  res.body.should eq("This is a test.\n")
107
104
  res.status.should eq(201)
108
105
  res.header.should eq('x-powered-by' => 'oaf')
@@ -111,12 +108,23 @@ module Oaf
111
108
  it "should accept containable methods properly" do
112
109
  req = Oaf::FakeReq.new({:path => @f2request, :method => 'PUT'})
113
110
  res = Oaf::FakeRes.new
114
- @webrick.should_receive(:mount_proc).with('/').once \
115
- .and_yield(req, res)
116
- Oaf::HTTP.serve(@tempdir1, 9000)
111
+ handler = Oaf::HTTP::Handler.new Oaf::FakeServlet.new, @tempdir1
112
+ handler.process_request req, res
117
113
  res.body.should eq("Containable Test\n")
118
114
  res.status.should eq(202)
119
115
  res.header.should eq('x-powered-by' => 'oaf')
120
116
  end
117
+
118
+ it "should respond to any HTTP method" do
119
+ req = Oaf::FakeReq.new :path => @f1request
120
+ res = Oaf::FakeRes.new
121
+ Oaf::HTTP::Handler.any_instance.stub(:process_request).and_return(true)
122
+ handler = Oaf::HTTP::Handler.new Oaf::FakeServlet.new, @tempdir1
123
+ handler.should_receive(:process_request).with(req, res).once
124
+ handler.respond_to?(:do_GET).should be_true
125
+ handler.respond_to?(:do_get).should be_false
126
+ handler.respond_to?(:nonexistent).should be_false
127
+ handler.do_PUT(req, res)
128
+ end
121
129
  end
122
130
  end
@@ -9,7 +9,7 @@ module Oaf
9
9
  @body = @path = @request_method = @query = nil
10
10
  @header = Hash.new
11
11
 
12
- def initialize opts={}
12
+ def initialize opts={}, *args
13
13
  @body = opts[:body] ? opts[:body] : nil
14
14
  @path, @query = opts[:path] ? opts[:path].split('?') : ['/']
15
15
  @request_method = opts[:method] ? opts[:method] : 'GET'
@@ -43,4 +43,13 @@ module Oaf
43
43
  @header[field] = value
44
44
  end
45
45
  end
46
+
47
+ class FakeServlet
48
+
49
+ def [](field)
50
+ end
51
+
52
+ def []=(field, value)
53
+ end
54
+ end
46
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Uber
@@ -95,7 +95,8 @@ files:
95
95
  - Rakefile
96
96
  - bin/oaf
97
97
  - lib/oaf.rb
98
- - lib/oaf/http.rb
98
+ - lib/oaf/http/handler.rb
99
+ - lib/oaf/http/server.rb
99
100
  - lib/oaf/util.rb
100
101
  - lib/oaf/version.rb
101
102
  - oaf.gemspec