wdd-ruby-ext 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'wdd-ruby-ext/utils/time_gate'
4
4
  require 'wdd-ruby-ext/utils/simpledebug'
5
+ require 'wdd-ruby-ext/utils/hash_object'
5
6
  require 'wdd-ruby-ext/utils/helpers'
6
7
  require 'wdd-ruby-ext/utils/fixed_point'
7
- require 'wdd-ruby-ext/utils/pretty_xml'
8
+ require 'wdd-ruby-ext/utils/pretty_xml'
9
+ require 'wdd-ruby-ext/utils/test_server'
@@ -0,0 +1,130 @@
1
+ # A simple test HTTP Server
2
+ # It will accept a request on any URL
3
+ # If the URL param 'return_data' is passed, it's value you will be returned in the response body.
4
+ # If the URL param 'sleep' is passed, the request will sleep that many seconds before responding.
5
+
6
+ require 'rubygems'
7
+ require 'eventmachine'
8
+ require 'cgi'
9
+ require 'pp'
10
+ require 'logger'
11
+
12
+ module HTTPHandler
13
+ def extract_query_params( query )
14
+ query = CGI::unescape( query )
15
+ pairs = query.split("&")
16
+ pairs.inject({}){|hash, pair| k,v=pair.split('='); v.nil? ? hash : (hash[k.to_sym]=v;hash)}
17
+ end
18
+
19
+ def http_request? data
20
+ http_request = nil
21
+ matches = data.split("\n").first.match(/(\w*) ([^\?]*)\??([^#]*)#?(.*) HTTP\/1\.1/)
22
+ if matches
23
+ http_request = {}
24
+ http_request[:request] = matches[0]
25
+ captures = matches.captures
26
+ http_request[:command] = captures.shift
27
+ http_request[:path] = captures.shift
28
+ http_request[:query_params] = extract_query_params(captures.shift)
29
+ http_request[:anchor] = captures.shift
30
+ end
31
+ # pp http_request
32
+ http_request
33
+ end
34
+
35
+ def process_http_request( http_request )
36
+ if sleep_time = http_request[:query_params][:sleep]
37
+ sleep( sleep_time.to_i )
38
+ end
39
+ unless return_data = http_request[:query_params][:return_data]
40
+ return_data = "Your HTTP Request(#{http_request[:request]}) was processed by the TestServer."
41
+ end
42
+ http_response(return_data)
43
+ end
44
+
45
+ def http_response( return_data, code=200 )
46
+ headers = <<-TXT
47
+ HTTP/1.1 #{ code } OK
48
+ Connection: Keep-Alive
49
+ Content-Type: text/html; charset=utf-8
50
+
51
+ TXT
52
+ headers + return_data
53
+ end
54
+ end
55
+
56
+ module TestServerModule
57
+
58
+ def self.included(base)
59
+ base.send(:include, HTTPHandler) unless base.include?( HTTPHandler )
60
+ end
61
+
62
+ def post_init
63
+ puts 'got connection'
64
+ end
65
+
66
+ def receive_data(data)
67
+ puts "Got data:\n#{data}"
68
+ if http_request = http_request?( data )
69
+ return_data = process_http_request( http_request )
70
+ else
71
+ return_data = "Your request was processed by the TestServer."
72
+ end
73
+ send_data(return_data)
74
+ puts "Sent: #{return_data}"
75
+ close_connection_after_writing
76
+ end
77
+
78
+ def puts string
79
+ TestServer.logger.debug( string )
80
+ end
81
+
82
+ end
83
+
84
+
85
+ class TestServer
86
+ @@logger = Logger.new(STDOUT)
87
+ @@thread = nil
88
+ PORT = 1234
89
+
90
+ def self.puts string
91
+ self.logger.debug( string )
92
+ end
93
+
94
+ def self.logger
95
+ @@logger
96
+ end
97
+
98
+ def self.logger= logger
99
+ @@logger = logger
100
+ end
101
+
102
+ def self.startup( port=PORT )
103
+ return if @@thread
104
+ @@thread = Thread.new do
105
+ begin
106
+ EM.run do
107
+ EM.start_server "0.0.0.0", port, TestServerModule
108
+ end
109
+ rescue Interrupt
110
+ end
111
+ end
112
+ puts "TestServer listening on port #{port}."
113
+ end
114
+
115
+ def self.shutdown
116
+ return unless @@thread
117
+ @@thread.kill
118
+ @@thread.join
119
+ puts "TestServer stopped."
120
+ @@thread = nil
121
+ end
122
+ end
123
+
124
+ if __FILE__ == $0
125
+ require 'typhoeus'
126
+ TestServer.startup
127
+ response = Typhoeus::Request.get("http://localhost:1234/hi?sleep=1&return_data=verified")
128
+ puts "response: #{response.inspect}"
129
+ TestServer.shutdown
130
+ end
data/wdd-ruby-ext.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wdd-ruby-ext}
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["shock"]
12
- s.date = %q{2011-01-19}
12
+ s.date = %q{2011-02-13}
13
13
  s.description = %q{Some of these are borrowed. Some are original. This gem simply provides a single place to source control them all for incorporation into other projects.}
14
14
  s.email = %q{billdoughty@capitalthought.com}
15
15
  s.extra_rdoc_files = [
@@ -42,6 +42,7 @@ Gem::Specification.new do |s|
42
42
  "lib/wdd-ruby-ext/utils/helpers.rb",
43
43
  "lib/wdd-ruby-ext/utils/pretty_xml.rb",
44
44
  "lib/wdd-ruby-ext/utils/simpledebug.rb",
45
+ "lib/wdd-ruby-ext/utils/test_server.rb",
45
46
  "lib/wdd-ruby-ext/utils/time_gate.rb",
46
47
  "spec/lib/utils/fixed_point_spec.rb",
47
48
  "spec/lib/utils/pretty_xml_spec.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wdd-ruby-ext
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 2
10
- version: 0.3.2
9
+ - 3
10
+ version: 0.3.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - shock
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-19 00:00:00 -06:00
18
+ date: 2011-02-13 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -85,6 +85,7 @@ files:
85
85
  - lib/wdd-ruby-ext/utils/helpers.rb
86
86
  - lib/wdd-ruby-ext/utils/pretty_xml.rb
87
87
  - lib/wdd-ruby-ext/utils/simpledebug.rb
88
+ - lib/wdd-ruby-ext/utils/test_server.rb
88
89
  - lib/wdd-ruby-ext/utils/time_gate.rb
89
90
  - spec/lib/utils/fixed_point_spec.rb
90
91
  - spec/lib/utils/pretty_xml_spec.rb