webspeak 0.0.1

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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2007-10-21
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Darren Boyd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,42 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ bin/webspeak
7
+ bin/webspeak_listener
8
+ config/hoe.rb
9
+ config/requirements.rb
10
+ examples/google_cookies.rb
11
+ examples/web_search.rb
12
+ lib/webspeak.rb
13
+ lib/webspeak/console_app.rb
14
+ lib/webspeak/http_dialect.rb
15
+ lib/webspeak/http_listener.rb
16
+ lib/webspeak/request.rb
17
+ lib/webspeak/response_accessor.rb
18
+ lib/webspeak/session.rb
19
+ lib/webspeak/version.rb
20
+ log/debug.log
21
+ script/destroy
22
+ script/generate
23
+ script/txt2html
24
+ setup.rb
25
+ spec/http_dialect_spec.rb
26
+ spec/http_listener_spec.rb
27
+ spec/parse_request_servlet_spec.rb
28
+ spec/request_spec.rb
29
+ spec/response_accessor_spec.rb
30
+ spec/session_spec.rb
31
+ spec/spec.opts
32
+ spec/spec_helper.rb
33
+ spec/webspeak_spec.rb
34
+ tasks/deployment.rake
35
+ tasks/environment.rake
36
+ tasks/rspec.rake
37
+ tasks/website.rake
38
+ website/index.html
39
+ website/index.txt
40
+ website/javascripts/rounded_corners_lite.inc.js
41
+ website/stylesheets/screen.css
42
+ website/template.rhtml
data/README.txt ADDED
@@ -0,0 +1 @@
1
+ README
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/bin/webspeak ADDED
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Darren Boyd on 2007-9-24.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ begin
7
+ require 'rubygems'
8
+ rescue LoadError
9
+ # no rubygems to load, so we fail silently
10
+ end
11
+
12
+ require 'optparse'
13
+
14
+ # NOTE: the option -p/--path= is given as an example, and should probably be replaced in your application.
15
+
16
+ OPTIONS = {
17
+ }
18
+ MANDATORY_OPTIONS = %w( )
19
+
20
+ parser = OptionParser.new do |opts|
21
+ begin
22
+ opts.banner = <<BANNER_END
23
+ Usage: #{File.basename($0)} [options] <file> [<file> ...]
24
+
25
+ Options are:
26
+ BANNER_END
27
+ opts.on("-c", "--console", "Run in console mode.") {OPTIONS[:console] = true}
28
+
29
+ opts.on("-h", "--help",
30
+ "Show this help message.") { puts opts; exit }
31
+
32
+ opts.parse!(ARGV)
33
+
34
+ if ARGV.empty? and !OPTIONS[:console]
35
+ $stderr.puts "Please provide a file."
36
+ $stderr.puts opts
37
+ exit 1
38
+ end
39
+
40
+ if MANDATORY_OPTIONS && MANDATORY_OPTIONS.find { |option| OPTIONS[option.to_sym].nil? }
41
+ $stderr.puts opts
42
+ exit 1
43
+ end
44
+ rescue SystemExit => e
45
+ raise e
46
+ rescue Exception => e
47
+ $stderr.puts e.message
48
+ $stderr.puts opts
49
+ exit 1
50
+ end
51
+ end
52
+
53
+ require 'webspeak'
54
+
55
+ if OPTIONS[:console]
56
+ libs = " -r webspeak/console_app"
57
+ libs << (" -r " + ARGV.join(" -r ")) unless ARGV.empty?
58
+ exec "irb --noinspect #{libs}"
59
+ else
60
+ @interpreter = WebSpeak::HTTP::Base.new
61
+ ARGV.each do |file|
62
+ @interpreter.instance_eval(File.read(file), file)
63
+ end
64
+ end
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Darren Boyd on 2007-9-24.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ begin
7
+ require 'rubygems'
8
+ rescue LoadError
9
+ # no rubygems to load, so we fail silently
10
+ end
11
+
12
+ require 'optparse'
13
+
14
+ # NOTE: the option -p/--path= is given as an example, and should probably be replaced in your application.
15
+
16
+ OPTIONS = {
17
+ :path => '~'
18
+ }
19
+ MANDATORY_OPTIONS = %w( )
20
+
21
+ parser = OptionParser.new do |opts|
22
+ opts.banner = <<BANNER
23
+ This application is wonderful because...
24
+
25
+ Usage: #{File.basename($0)} [options]
26
+
27
+ Options are:
28
+ BANNER
29
+ opts.separator ""
30
+ opts.on("-p", "--path=PATH", String,
31
+ "The root path for selecting files",
32
+ "Default: ~") { |OPTIONS[:path]| }
33
+ opts.on("-h", "--help",
34
+ "Show this help message.") { puts opts; exit }
35
+ opts.parse!(ARGV)
36
+
37
+ if MANDATORY_OPTIONS && MANDATORY_OPTIONS.find { |option| OPTIONS[option.to_sym].nil? }
38
+ puts opts; exit
39
+ end
40
+ end
41
+
42
+ path = OPTIONS[:path]
43
+
44
+ # do stuff
45
+ require 'webspeak'
46
+ WebSpeak::HTTPListener.new().listen
data/config/hoe.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'webspeak/version'
2
+
3
+ AUTHOR = 'Darren Boyd' # can also be an array of Authors
4
+ EMAIL = "darren.boyd@gmail.com"
5
+ DESCRIPTION = "description of gem"
6
+ GEM_NAME = 'webspeak' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'webspeak' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = Webspeak::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'webspeak documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\\n\\n")
62
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+
64
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
+
66
+ end
67
+
68
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'webspeak'
@@ -0,0 +1,3 @@
1
+
2
+ host 'www.google.com'
3
+ get
@@ -0,0 +1,9 @@
1
+
2
+ # setup our request information
3
+ host 'www.google.com'
4
+ path '/search'
5
+ param 'q' => 'webspeak'
6
+ get
7
+
8
+ # display the body
9
+ puts response.body
data/lib/webspeak.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'webspeak/session'
3
+ require 'webspeak/request'
4
+ require 'webspeak/http_listener'
5
+ require 'webspeak/http_dialect'
6
+ require 'webspeak/response_accessor'
@@ -0,0 +1,3 @@
1
+ require "webspeak"
2
+ self.include WebSpeak::HTTP::Dialect
3
+
@@ -0,0 +1,71 @@
1
+ module WebSpeak
2
+ module HTTP
3
+
4
+ module Dialect
5
+ def host(host)
6
+ request.host = host
7
+ end
8
+
9
+ def port(port)
10
+ request.port = port
11
+ end
12
+
13
+ def path(path)
14
+ request.path = path
15
+ end
16
+
17
+ def header(*args)
18
+ name, value = get_name_value_pair('header', args)
19
+ request.headers[name] = value
20
+ end
21
+
22
+ def query_param(*args)
23
+ name, value = get_name_value_pair('query_param', args)
24
+ request.query_params[name] = value
25
+ end
26
+
27
+ def param(*args)
28
+ name, value = get_name_value_pair('param', args)
29
+ request.params[name] = value
30
+ end
31
+
32
+ def cookie(*args)
33
+ name, value = get_name_value_pair('cookie', args)
34
+ request.cookies[name] = value
35
+ end
36
+
37
+ def post
38
+ request.post()
39
+ end
40
+
41
+ def get
42
+ request.get()
43
+ end
44
+
45
+ def request
46
+ @request ||= WebSpeak::Request.new
47
+ end
48
+
49
+ def response
50
+ @response_accessor = ResponseAccessor.new(request.response)
51
+ end
52
+
53
+ protected
54
+ def get_name_value_pair(method_name, args)
55
+ raise "wrong number of arguments for '#{method_name}'. " +
56
+ "Use either 'name' => 'value' or 'name', 'value'" if
57
+ args.size < 1 || args.size > 2 || (args.size == 1 && (!args.class == Hash))
58
+ if args.length == 2
59
+ name, value = args[0,2]
60
+ else
61
+ name, value = args[0].keys.first, args[0].values.first
62
+ end
63
+ [name, value]
64
+ end
65
+ end
66
+
67
+ class Base
68
+ include Dialect
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,100 @@
1
+
2
+ require 'webrick'
3
+ require 'logger'
4
+
5
+ module WebSpeak
6
+ class HTTPListener
7
+ include WEBrick
8
+
9
+ def initialize(port=8080)
10
+ @port = port
11
+ @logger = Log::new
12
+ @logger.level = Logger::ERROR
13
+ end
14
+
15
+ def listen
16
+ $s = HTTPServer.new(:Port => @port, :Logger => @logger, :AccessLog => [])
17
+ $s.mount("/", ParseRequestServlet)
18
+ puts "Listening on port #{@port} and path '/'"
19
+ trap("INT") { $s.shutdown }
20
+ $s.start
21
+ end
22
+
23
+ end
24
+
25
+ class ParseRequestServlet < WEBrick::HTTPServlet::AbstractServlet
26
+
27
+ # def self.query_parsers
28
+ # @query_parsers ||= []
29
+ # end
30
+ #
31
+ # def self.parse_query(&parse_function)
32
+ # raise "parse_query requires a block." unless parse_function
33
+ # query_parsers << parse_function
34
+ # end
35
+ #
36
+ def do_POST(req, res)
37
+ return if req.path == '/favicon.ico'
38
+
39
+ result = StringIO.new
40
+ def result.newline(n=1)
41
+ n.times { puts "" }
42
+ self
43
+ end
44
+
45
+ # Parse out the 'query' params
46
+ query_params = {}
47
+ if req.request_uri.query
48
+ req.request_uri.query.split("&").each do |qp|
49
+ name, value = qp.split("=", 2)
50
+ query_params[name] = value
51
+ end
52
+ end
53
+
54
+ result.newline.puts(
55
+ "# use <something>",
56
+ "",
57
+ "host '<your server>'",
58
+ "port 8080",
59
+ "path '#{req.request_uri.path}'",
60
+ "")
61
+
62
+ query_params.each do |name, value|
63
+ result.puts("query_param '#{name}' => '#{value}'")
64
+ end
65
+
66
+ headers_to_ignore = ["cookie", "content-length", "host"]
67
+
68
+ result.newline(2)
69
+ result.puts("# Some of these are probably not needed")
70
+ result.puts("# and can be safely removed for brevity.")
71
+ req.header.each do |header,values|
72
+ break if headers_to_ignore.include? header.downcase
73
+ values.each {|val| result.puts "header '#{header}' => '#{val}'"}
74
+ end
75
+ result.newline(2)
76
+ #
77
+ # ParseRequestServlet.query_parsers.each do |parser|
78
+ # result.puts(parser.call(req.query))
79
+ # end
80
+ # result.newline(2)
81
+ #
82
+ req.query.each do |name, value|
83
+ result.puts("param '#{name}' => '#{value}'")
84
+ end
85
+ result.newline(2)
86
+
87
+ req.cookies.each do |cookie|
88
+ result.puts("cookie '#{cookie.name}' => '#{cookie.value}'")
89
+ end
90
+ result.newline(2)
91
+
92
+ result.puts("post",
93
+ "")
94
+
95
+ res.body = result.string
96
+
97
+ @server.shutdown # we only listen to one request
98
+ end
99
+ end
100
+ end