rubai 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --colour
3
+ --format documentation
@@ -1,5 +1,6 @@
1
1
  require 'rubai/mime_type'
2
2
  require 'rubai/response'
3
+ require 'rubai/rack_response'
3
4
  require 'rubai/rack_app'
4
5
 
5
6
  module Rubai
@@ -1,3 +1,11 @@
1
1
  require "rack"
2
2
  require "rubai"
3
- Rack::Server.start app: Rubai::RackApp.new
3
+ require "optparse"
4
+
5
+ options = Hash[:app, Rubai::RackApp.new, :Port, 9292]
6
+ parser = OptionParser.new do |opts|
7
+ opts.on("-p", "--port PORT") { |p| options[:Port] = p }
8
+ end
9
+ parser.parse!
10
+
11
+ Rack::Server.start options
@@ -1,5 +1,22 @@
1
1
  module Rubai
2
2
  class RackApp
3
+
4
+ def call(env)
5
+ @env = env
6
+
7
+ rack_response = RackResponse.new
8
+
9
+ if File.file?(path)
10
+ response = success_response
11
+ else
12
+ response = not_found_response
13
+ end
14
+
15
+ rack_response.respond_with(response)
16
+ end
17
+
18
+ private
19
+
3
20
  def path
4
21
  if env["PATH_INFO"] == "/"
5
22
  path = "index.html"
@@ -13,26 +30,24 @@ module Rubai
13
30
  @env
14
31
  end
15
32
 
16
- def call(env)
17
- @env = env
18
-
19
- unless File.file?(path)
20
- response = Response.new
21
- response.status = 404
22
- response.content_type = MimeType.new(:html).content_type
23
- # TODO: this response should be configurable
24
- response << "<html>"
25
- response << " <body>"
26
- response << " NO!"
27
- response << " </body>"
28
- response << "</html>"
29
- return response.for_rack
30
- end
33
+ def not_found_response
34
+ response = Response.new
35
+ response.status = 404
36
+ response.content_type = MimeType.new(:html).content_type
37
+ # TODO: this response should be configurable
38
+ response << "<html>"
39
+ response << " <body>"
40
+ response << " NO!"
41
+ response << " </body>"
42
+ response << "</html>"
43
+ response
44
+ end
31
45
 
46
+ def success_response
32
47
  extension = File.extname(path)
33
48
  mime_type = MimeType.new(extension)
34
- response = Response.new 200, mime_type.content_type, [File.read(path)]
35
- response.for_rack
49
+ Response.new 200, mime_type.content_type, [File.read(path)]
36
50
  end
37
51
  end
52
+
38
53
  end
@@ -0,0 +1,25 @@
1
+ module Rubai
2
+ class RackResponse
3
+ def initialize(response=nil)
4
+ @response = response
5
+ end
6
+
7
+ def respond_with(response)
8
+ @response = response
9
+ call
10
+ end
11
+
12
+ def call
13
+ [response.status, response.headers, response.body]
14
+ end
15
+
16
+ def to_s
17
+ "#{response.status}, #{response.headers}, #{response.body.join}"
18
+ end
19
+
20
+ private
21
+ def response
22
+ @response
23
+ end
24
+ end
25
+ end
@@ -1,13 +1,15 @@
1
1
  module Rubai
2
2
  class Response
3
+ attr_reader :status
4
+
3
5
  def initialize(status = 200, content_type = "text/html", body=[])
4
6
  @status = status
5
7
  self.content_type= content_type
6
8
  @body = body
7
9
  end
8
10
 
9
- def headers
10
- @headers ||= {}
11
+ def self.new_with_body body
12
+ self.new 200, "text/html", [body]
11
13
  end
12
14
 
13
15
  def body
@@ -18,20 +20,30 @@ module Rubai
18
20
  body << body_text
19
21
  end
20
22
 
21
- def []=(header, value)
22
- headers[header] = value
23
- end
24
-
25
23
  def content_type=(content_type)
26
24
  self["Content-Type"] = content_type
27
25
  end
28
26
 
27
+ def content_type
28
+ self["Content-Type"]
29
+ end
30
+
29
31
  def status=(status)
30
32
  @status = status
31
33
  end
32
34
 
33
- def for_rack
34
- [@status, headers, @body]
35
+ def headers
36
+ @headers ||= {}
37
+ end
38
+
39
+ private
40
+
41
+ def [](header)
42
+ headers[header]
43
+ end
44
+
45
+ def []=(header, value)
46
+ headers[header] = value
35
47
  end
36
48
  end
37
49
  end
@@ -1,20 +1,28 @@
1
- # -*- encoding: utf-8 -*-
1
+ #encoding: utf-8
2
+ require File.join(File.expand_path("../", __FILE__), "version.rb")
3
+
2
4
  Gem::Specification.new do |s|
3
5
  s.name = "rubai"
4
- s.version = "0.1.0"
6
+ s.version = Rubai::VERSION
5
7
  s.platform = Gem::Platform::RUBY
6
8
  s.authors = ["Ricardo Valeriano"]
7
9
  s.email = ["ricardo.valeriano@gmail.com"]
8
10
  s.homepage = "http://github.com/ricardovaleriano/rubai"
9
11
  s.summary = "Simple local http server for your local dirs"
10
- s.description = <<SUM
11
- Simple local http server for your local dirs
12
- SUM
12
+ s.description = "Simple local http server for your local dirs"
13
13
  s.required_rubygems_version = ">= 0"
14
14
 
15
15
  s.require_path = 'lib'
16
- s.files = `git ls-files | sed 's/examples.*//'`.split("\n").reject{|f| f == ""}
16
+
17
+ s.files = `git ls-files | sed 's/examples.*//'`.
18
+ split("\n").
19
+ reject{|f| f == ""}
20
+
21
+ s.test_files = `git ls-files -- spec/*`.
22
+ split("\n")
23
+
24
+ s.executable = 'rubai'
17
25
 
18
26
  s.add_dependency 'rack'
19
- s.executables = ['rubai']
27
+ s.add_development_dependency 'rspec'
20
28
  end
@@ -0,0 +1,19 @@
1
+ module Rubai
2
+ describe RackResponse do
3
+ let(:status) { 200 }
4
+ let(:header) { Hash["Content-Type", "text/html"] }
5
+ let(:body) { "test" }
6
+ let(:response) { Response.new_with_body(body) }
7
+ let(:rack_response) { [status, header, [body]] }
8
+
9
+ context "::new(Response.new)" do
10
+ subject { RackResponse.new(response) }
11
+ its(:call) { should be_eql rack_response }
12
+ end
13
+
14
+ context "#respond_with(Response.new)" do
15
+ subject { RackResponse.new.respond_with(response) }
16
+ it { should be_eql rack_response }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Rubai
2
+ describe Response, "Generates a generic response" do
3
+ context "#new with default parameters" do
4
+ its(:body) { should eq [] }
5
+ its(:status) { should eq 200 }
6
+ its(:content_type) { should eq "text/html" }
7
+ end
8
+
9
+ context "#<<" do
10
+ let(:some_text) { "omg! response..." }
11
+ subject(:response) { Response.new }
12
+ it "appends string to response #body" do
13
+ response << some_text
14
+ expect(response.body).to include some_text
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ lib = File.expand_path('../../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+ require 'rubai'
@@ -0,0 +1,3 @@
1
+ module Rubai
2
+ VERSION = "0.1.1"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -27,9 +27,23 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- description: ! ' Simple local http server for your local dirs
31
-
32
- '
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple local http server for your local dirs
33
47
  email:
34
48
  - ricardo.valeriano@gmail.com
35
49
  executables:
@@ -38,16 +52,20 @@ extensions: []
38
52
  extra_rdoc_files: []
39
53
  files:
40
54
  - .gitignore
41
- - Gemfile
42
- - Gemfile.lock
55
+ - .rspec
43
56
  - Readme.md
44
57
  - bin/rubai
45
58
  - lib/rubai.rb
46
59
  - lib/rubai/cli.rb
47
60
  - lib/rubai/mime_type.rb
48
61
  - lib/rubai/rack_app.rb
62
+ - lib/rubai/rack_response.rb
49
63
  - lib/rubai/response.rb
50
64
  - rubai.gemspec
65
+ - spec/rubai/rack_response_spec.rb
66
+ - spec/rubai/response_spec.rb
67
+ - spec/spec_helper.rb
68
+ - version.rb
51
69
  homepage: http://github.com/ricardovaleriano/rubai
52
70
  licenses: []
53
71
  post_install_message:
@@ -72,4 +90,7 @@ rubygems_version: 1.8.23
72
90
  signing_key:
73
91
  specification_version: 3
74
92
  summary: Simple local http server for your local dirs
75
- test_files: []
93
+ test_files:
94
+ - spec/rubai/rack_response_spec.rb
95
+ - spec/rubai/response_spec.rb
96
+ - spec/spec_helper.rb
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source :rubygems
2
-
3
- gem "rack"
@@ -1,10 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- rack (1.4.1)
5
-
6
- PLATFORMS
7
- ruby
8
-
9
- DEPENDENCIES
10
- rack