Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:CapnKernul/webbed into develop
Browse files Browse the repository at this point in the history
Conflicts:
	Gemfile.lock
	test/test_helper.rb
  • Loading branch information
Alexander Kern committed May 1, 2011
2 parents 3a40bd5 + 9520b73 commit ecaad54
Show file tree
Hide file tree
Showing 27 changed files with 651 additions and 181 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ InstalledFiles
# YARD artifacts
.yardoc
_yardoc
doc/
doc/

# Gem-specific
Gemfile.lock
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
script: 'rake test'
rvm:
- 1.8.7
- 1.9.2
- ree
- rbx
- jruby
5 changes: 4 additions & 1 deletion .yardopts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--readme README.md
--markup markdown
--markup-provider maruku
--markup-provider maruku
--default-return ""
--title "Webbed Documentation"
--hide-void-return
32 changes: 0 additions & 32 deletions Gemfile.lock

This file was deleted.

40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
Webbed
======
# webbed - take control of HTTP [![StillMaintained Status](http://stillmaintained.com/CapnKernul/webbed.png)](http://stillmaintained.com/CapnKernul/webbed) [![Build Status](http://travis-ci.org/CapnKernul/webbed.png)](http://travis-ci.org/CapnKernul/webbed) #

Take control of HTTP.
Webbed provides useful abstractions on top of the basic parts of the HTTP
specification, most notably HTTP Requests and Responses. It is far more
full-featured than `Rack::Request` and `Rack::Response`, allowing you to easily
access the complex features of HTTP.

Requirements
------------
## Usage ##

* [Addressable](http://github.com/sporkmonger/addressable)
Coming soon. Check out the tests and documentation in the meantime.

Install
-------
## Installation ##

* `gem install webbed`
Without bundler:

License
-------
gem install webbed

With bundler:

gem 'webbed'

## Note on Patches/Pull Requests ##

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit, but do not mess with the `Rakefile`. If you want to have your own version, that is fine but bump the version in a commit by itself in another branch so I can ignore it when I pull.
* Send me a pull request. Bonus points for git flow feature branches.

## Resources ##

* [GitHub Repository](https://github.com/CapnKernul/webbed)
* [Documentation](http://rubydoc.info/github/CapnKernul/webbed)

## License ##

Webbed is licensed under the MIT License. See `LICENSE` for details.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ task :default => :test

require 'rake/testtask'
Rake::TestTask.new do |t|
t.ruby_opts += ['-rubygems']
t.libs << 'test'
t.pattern = 'test/**/test_*.rb'
end
10 changes: 10 additions & 0 deletions lib/webbed.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Webbed provides two important abstraction on the HTTP specification (RFC
# 2616), {Request} and {Response}. Although the core classes have the
# bare-minimum of functionality for interacting with the two classes, they have
# been extended with a variety of helper modules, adding a significant amount of
# semantic meaning to the instances of each class.
#
# Webbed can be used with or without Rack, but it includes Rack helpers for
# convenience.
#
# @todo Add exmaples of webbed in action.
module Webbed
autoload :Headers, 'webbed/headers'
autoload :HTTPVersion, 'webbed/http_version'
Expand Down
43 changes: 25 additions & 18 deletions lib/webbed/generic_message.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
module Webbed
# Generic methods used for both {Request}'s and {Response}'s.
# @abstract
# Generic methods used for both {Request}'s and {Response}'s
#
# @abstract Include and implement `#status_line`.
module GenericMessage
# Returns the HTTP Version of the message.
# The HTTP-Version of the message
#
# Automatically converts the HTTP Version to an instance of {HTTPVersion} if
# it is not already one.
# The method automatically converts the new value to an instance of
# {HTTPVersion} if it is not already one.
#
# @return [HTTPVersion]
attr_reader :http_version

# Returns the headers of the message.
def http_version=(new_http_version)
@http_version = Webbed::HTTPVersion.new(new_http_version)
end

# The Headers of the message
#
# Automatically converts the headers to an instance of {Headers} if it
# is not already one.
# The method automatically converts the new value to an instance of
# {Headers} if it is not already one.
#
# @return [Headers]
attr_reader :headers

# Returns the entity body of the message.
attr_accessor :entity_body

def headers=(headers)
@headers = Webbed::Headers.new(headers)
def headers=(new_headers)
@headers = Webbed::Headers.new(new_headers)
end

def http_version=(http_version)
@http_version = Webbed::HTTPVersion.new(http_version)
end
# The Entity Body of the message
#
# @return [#to_s]
attr_accessor :entity_body

# Convert the message into an HTTP message as per RFC 2616.
# Converts the message into an HTTP Message as per RFC 2616.
#
# @return [String] the HTTP message
# @return [String]
def to_s
"#{start_line}#{headers.to_s}\r\n#{entity_body}"
end
Expand Down
41 changes: 33 additions & 8 deletions lib/webbed/helpers/entity_headers_helper.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
module Webbed
module Helpers
# Request and Response helper for Entity Headers
module EntityHeadersHelper
# The Content-Length of the Entity (as defined in the Content-Length Header).
#
# @return [Fixnum, nil]
def content_length
headers['Content-Length'] ? headers['Content-Length'].to_i : nil
end

def content_length=(content_length)
headers['Content-Length'] = content_length.to_s
# Sets the Content-Length of the Entity (as defined in the Content-Length Header).
#
# @param new_content_length [#to_s]
def content_length=(new_content_length)
headers['Content-Length'] = new_content_length.to_s
end

# The Content-Location of the Entity (as defined in the Content-Location Header).
#
# @return [Addressable::URI, nil]
def content_location
headers['Content-Location'] ? Addressable::URI.parse(headers['Content-Location']) : nil
end

def content_location=(content_location)
headers['Content-Location'] = content_location.to_s
# Sets the Content-Location of the Entity (as defined in the Content-Location Header).
#
# @param new_content_location [#to_s]
def content_location=(new_content_location)
headers['Content-Location'] = new_content_location.to_s
end

# The Content-MD5 of the Entity (as defined in the Content-MD5 Header).
#
# @return [String, nil]
def content_md5
headers['Content-MD5']
end

def content_md5=(content_md5)
headers['Content-MD5'] = content_md5
# Sets the Content-MD5 of the Entity (as defined in the Content-MD5 Header).
#
# @param new_content_md5 [String]
def content_md5=(new_content_md5)
headers['Content-MD5'] = new_content_md5
end

# The Content-Type of the Entity (as defined in the Content-Type Header).
#
# @return [MediaType, nil]
def content_type
headers['Content-Type'] ? Webbed::MediaType.new(headers['Content-Type']) : nil
end

def content_type=(content_type)
headers['Content-Type'] = content_type.to_s
# Sets the Content-Type of the Entity (as defined in the Content-Type Header).
#
# @param new_content_type [#to_s]
def content_type=(new_content_type)
headers['Content-Type'] = new_content_type.to_s
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/webbed/helpers/method_helper.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
module Webbed
module Helpers
# Request helper for the Method
module MethodHelper
# Whether or not the Request is safe based on the Method
#
# @return [Boolean]
def safe?
method.safe?
end

# Whether or not the Request is idempotent based on the Method
#
# @return [Boolean]
def idempotent?
method.idempotent?
end

# Whether or not the Request uses the OPTIONS Method
#
# @return [Boolean]
def options?
method == 'OPTIONS'
end

# Whether or not the Request uses the GET Method
#
# @return [Boolean]
def get?
method == 'GET'
end

# Whether or not the Request uses the HEAD Method
#
# @return [Boolean]
def head?
method == 'HEAD'
end

# Whether or not the Request uses the POST Method
#
# @return [Boolean]
def post?
method == 'POST'
end

# Whether or not the Request uses the PUT Method
#
# @return [Boolean]
def put?
method == 'PUT'
end

# Whether or not the Request uses the DELETE Method
#
# @return [Boolean]
def delete?
method == 'DELETE'
end

# Whether or not the Request uses the TRACE Method
#
# @return [Boolean]
def trace?
method == 'TRACE'
end

# Whether or not the Request uses the CONNECT Method
#
# @return [Boolean]
def connect?
method == 'CONNECT'
end

# Whether or not the Request uses the PATCH Method
#
# @return [Boolean]
def patch?
method == 'PATCH'
end
Expand Down
16 changes: 16 additions & 0 deletions lib/webbed/helpers/rack_request_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
module Webbed
module Helpers
# Request helper for converting Rack environments into Requests
module RackRequestHelper
module ClassMethods
# Create a new Request from a Rack environment
#
# This method will take the HTTP-Version, Method, Request-URI, Headers,
# Entity Body, and scheme from the Rack environment and create a new
# Request using that data. The Rack environment can be accessed through
# `#rack_env`.
#
# @param rack_env [Hash{String => String}]
# @return [Request]
# @note The Rack environment will never be modified.
def from_rack(rack_env)
env = rack_env.dup
method = env.delete('REQUEST_METHOD')
Expand Down Expand Up @@ -30,9 +41,14 @@ def from_rack(rack_env)
end

module InstanceMethods
# The Rack environment of the Request
#
# @return [Hash{String => String}]
attr_accessor :rack_env
end

# @see ClassMethods
# @see InstanceMethods
def self.included(base)
base.send :include, InstanceMethods
base.send :extend, ClassMethods
Expand Down
Loading

0 comments on commit ecaad54

Please sign in to comment.