auto_response 0.1.8 → 0.2.0

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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/Gemfile +2 -15
  4. data/Gemfile.lock +34 -17
  5. data/Rakefile +23 -0
  6. data/auto_response.gemspec +29 -0
  7. data/bin/{ar → auto_resp} +0 -0
  8. data/lib/ar/proxy_server.rb +23 -10
  9. data/lib/ar/rule_manager.rb +2 -7
  10. data/lib/ar/version.rb +1 -1
  11. data/lib/ar/viewer/components/jquery/.gitignore +1 -0
  12. data/lib/auto_response.rb +42 -14
  13. data/rdoc/AutoResp.html +124 -0
  14. data/rdoc/AutoResp/AutoResponder.html +487 -0
  15. data/rdoc/AutoResp/Parser.html +230 -0
  16. data/rdoc/AutoResp/ProxyServer.html +327 -0
  17. data/rdoc/AutoResp/RuleDSL.html +230 -0
  18. data/rdoc/AutoResp/RuleManager.html +332 -0
  19. data/rdoc/created.rid +7 -0
  20. data/rdoc/images/add.png +0 -0
  21. data/rdoc/images/brick.png +0 -0
  22. data/rdoc/images/brick_link.png +0 -0
  23. data/rdoc/images/bug.png +0 -0
  24. data/rdoc/images/bullet_black.png +0 -0
  25. data/rdoc/images/bullet_toggle_minus.png +0 -0
  26. data/rdoc/images/bullet_toggle_plus.png +0 -0
  27. data/rdoc/images/date.png +0 -0
  28. data/rdoc/images/delete.png +0 -0
  29. data/rdoc/images/find.png +0 -0
  30. data/rdoc/images/loadingAnimation.gif +0 -0
  31. data/rdoc/images/macFFBgHack.png +0 -0
  32. data/rdoc/images/package.png +0 -0
  33. data/rdoc/images/page_green.png +0 -0
  34. data/rdoc/images/page_white_text.png +0 -0
  35. data/rdoc/images/page_white_width.png +0 -0
  36. data/rdoc/images/plugin.png +0 -0
  37. data/rdoc/images/ruby.png +0 -0
  38. data/rdoc/images/tag_blue.png +0 -0
  39. data/rdoc/images/tag_green.png +0 -0
  40. data/rdoc/images/transparent.png +0 -0
  41. data/rdoc/images/wrench.png +0 -0
  42. data/rdoc/images/wrench_orange.png +0 -0
  43. data/rdoc/images/zoom.png +0 -0
  44. data/rdoc/index.html +79 -0
  45. data/rdoc/js/darkfish.js +153 -0
  46. data/rdoc/js/jquery.js +18 -0
  47. data/rdoc/js/navigation.js +142 -0
  48. data/rdoc/js/search.js +94 -0
  49. data/rdoc/js/search_index.js +1 -0
  50. data/rdoc/js/searcher.js +228 -0
  51. data/rdoc/rdoc.css +543 -0
  52. data/rdoc/table_of_contents.html +107 -0
  53. data/readme.md +12 -3
  54. metadata +384 -341
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee5883eec73a27b407746617b71425860d071f19
4
- data.tar.gz: 2cd5a5ca6ec272a54eb829cbd250c6470e0f99b5
3
+ metadata.gz: 55273ec6191009c7efcb1309c6d5eca04cbfec6c
4
+ data.tar.gz: 34944371d7b91159742e123801817853ad1b77bf
5
5
  SHA512:
6
- metadata.gz: ee950fc92e636d6171856b38688ba16c9b0dad2e22cee76354cf818c2897bf3c4879dacaf419f362c25ff3909ccc832deea1d20356c133f84d59ea229dc3ccfa
7
- data.tar.gz: 0674a3690d01b43a0c42e252a12537729328e310239f3a4570142f05a0098ea33ac16f82eaa666fccc21150dc257908725cd1e3784e9df6b79db6cdf82d00be4
6
+ metadata.gz: a7a6d317bbb1f6902d31d7e9563d999789d18b7662f9bd7271a21853731e381828946e0b981ec53eeb7c8618686ce44e7edf51bd3e4416773082b1cfdad59dbb
7
+ data.tar.gz: dcea5a717b81771799b18e0580bf73cd8257ad82991cd2d0a85b8600ea50d1e72622a653b6204852de1b0e2e78d87aa9e13f9191c14a546250aa38b309f503b0
@@ -0,0 +1,2 @@
1
+ bin/start_ar.rb.pid
2
+ pkg
data/Gemfile CHANGED
@@ -1,15 +1,2 @@
1
- source 'http://ruby.taobao.org'
2
- gem 'listen'
3
- gem 'colorize'
4
- gem 'daemons'
5
- gem 'rb-inotify', '~> 0.9'
6
- gem 'string_utf8'
7
- gem 'ptools'
8
-
9
- group :test do
10
- gem 'test-unit'
11
- end
12
-
13
- group :development do
14
- gem 'gem-this'
15
- end
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -1,26 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ auto_response (0.2.0)
5
+ colorize
6
+ daemons (~> 1.1.9)
7
+ listen (~> 2.0)
8
+ ptools
9
+ rb-inotify (~> 0.9)
10
+ string_utf8
11
+
1
12
  GEM
2
- remote: http://ruby.taobao.org/
13
+ remote: https://rubygems.org/
3
14
  specs:
4
- colorize (0.5.8)
15
+ celluloid (0.16.0)
16
+ timers (~> 4.0.0)
17
+ colorize (0.7.3)
5
18
  daemons (1.1.9)
6
- ffi (1.9.0)
7
- gem-this (0.3.7)
8
- listen (0.6.0)
9
- ptools (1.2.2)
10
- rb-inotify (0.9.0)
19
+ ffi (1.9.6)
20
+ hitimes (1.2.2)
21
+ json (1.8.1)
22
+ listen (2.7.11)
23
+ celluloid (>= 0.15.2)
24
+ rb-fsevent (>= 0.9.3)
25
+ rb-inotify (>= 0.9)
26
+ ptools (1.2.6)
27
+ rake (10.3.2)
28
+ rb-fsevent (0.9.4)
29
+ rb-inotify (0.9.5)
11
30
  ffi (>= 0.5.0)
12
- string_utf8 (0.1)
13
- test-unit (2.5.3)
31
+ rdoc (4.1.2)
32
+ json (~> 1.4)
33
+ string_utf8 (0.1.1)
34
+ timers (4.0.1)
35
+ hitimes
14
36
 
15
37
  PLATFORMS
16
38
  ruby
17
39
 
18
40
  DEPENDENCIES
19
- colorize
20
- daemons
21
- gem-this
22
- listen
23
- ptools
24
- rb-inotify (~> 0.9)
25
- string_utf8
26
- test-unit
41
+ auto_response!
42
+ rake
43
+ rdoc
@@ -0,0 +1,23 @@
1
+ require 'rake/clean'
2
+ require 'rubygems'
3
+ require 'rubygems/package_task'
4
+ require 'rdoc/task'
5
+
6
+ Rake::RDocTask.new do |rd|
7
+ rd.main = "README.rdoc"
8
+ rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
9
+ rd.title = 'Your application title'
10
+ end
11
+
12
+ spec = eval(File.read('auto_response.gemspec'))
13
+
14
+ Gem::PackageTask.new(spec) do |pkg|
15
+ end
16
+
17
+ require 'rake/testtask'
18
+ Rake::TestTask.new do |t|
19
+ t.libs << "test"
20
+ t.test_files = FileList['test/tc_*.rb']
21
+ end
22
+
23
+ task :default => :test
@@ -0,0 +1,29 @@
1
+ # Ensure we require the local version and not one we might have installed already
2
+ require File.join( File.dirname(__FILE__),'lib/ar/version.rb')
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'auto_response'
5
+ s.description = <<-EOF
6
+ HTTP debugging made easy.
7
+ EOF
8
+ s.version = AutoResp::VERSION
9
+ s.author = 'qhwa'
10
+ s.email = 'qhwa@163.com'
11
+ s.homepage = 'https://github.com/qhwa/auto_response'
12
+ s.platform = Gem::Platform::RUBY
13
+ s.summary = 'HTTP debugging tool'
14
+ # Add your other files here if you make them
15
+ s.files = `git ls-files`.split("
16
+ ")
17
+ s.require_paths << 'lib'
18
+ s.has_rdoc = false
19
+ s.bindir = 'bin'
20
+ s.executables << 'auto_resp'
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'rdoc'
23
+ s.add_runtime_dependency %q<listen>, "~> 2.0"
24
+ s.add_runtime_dependency %q<colorize>, ">= 0"
25
+ s.add_runtime_dependency %q<daemons>, "~> 1.1.9"
26
+ s.add_runtime_dependency %q<string_utf8>, ">= 0"
27
+ s.add_runtime_dependency %q<rb-inotify>, "~> 0.9"
28
+ s.add_runtime_dependency %q<ptools>, ">= 0"
29
+ end
File without changes
@@ -33,18 +33,30 @@ module AutoResp
33
33
  end
34
34
  end
35
35
 
36
- header, body, status = find_auto_res(req.unparsed_uri)
36
+ begin
37
+ header, body, status = find_auto_res(req.unparsed_uri)
38
+ rescue => e
39
+ header = {}
40
+ body = <<-MSG
41
+ ERROR:
42
+ #{e.message}
43
+ #{e.backtrace.join "\n"}
44
+ MSG
45
+ status = 500
46
+ end
47
+
37
48
  res.status = status if status
38
49
 
39
50
  if @rule
40
51
 
41
52
  logger.debug "match".ljust(8) << ": #{req.unparsed_uri}"
42
53
  logger.debug "header".ljust(8) << ": #{header}"
43
- logger.debug "body".ljust(8) << ": \n#{body}"
54
+ logger.debug "body".ljust(8) << ": \n#{body[0..200]}#{'...' if body.size > 200}"
44
55
  logger.debug "-"*50
45
56
 
46
57
  res['x-auto-response-condition'] = @rule.first.to_s
47
58
  res['x-auto-response-with'] = @rule.last.to_s
59
+
48
60
  if header
49
61
  header.each do |k,v|
50
62
  v = v.join("\n") if v.respond_to?(:join)
@@ -82,6 +94,7 @@ module AutoResp
82
94
  @rule = rule
83
95
  if rule
84
96
  condition, handlers = *rule
97
+ handlers = [handlers] unless handlers.is_a?(Array)
85
98
  handlers.each {|proc| proc.call if proc.respond_to?(:call) }
86
99
  fetch(handlers.last, condition, url)
87
100
  end
@@ -91,9 +104,9 @@ module AutoResp
91
104
  trim_url(rule) === trim_url(url)
92
105
  end
93
106
 
94
- def fetch(txt, declare, uri)
107
+ def fetch(handler, declare, uri)
95
108
 
96
- case txt
109
+ case handler
97
110
  when nil
98
111
  Net::HTTP.get_response(URI(uri)) do |res|
99
112
  return [res.to_hash, res.read_body, res.code]
@@ -101,12 +114,12 @@ module AutoResp
101
114
  when Proc
102
115
  if Regexp === declare
103
116
  mtc = uri.match(declare)
104
- fetch( txt.call(*mtc), declare, uri )
117
+ fetch( handler.call(*mtc), declare, uri )
105
118
  else
106
- fetch( txt.call, declare, uri )
119
+ fetch( handler.call, declare, uri )
107
120
  end
108
121
  when String
109
- if goto = redirect_path(txt)
122
+ if goto = redirect_path(handler)
110
123
  if is_uri?(goto)
111
124
  Net::HTTP.get_response(URI(goto)) do |res|
112
125
  return [res.to_hash, res.read_body, res.code]
@@ -123,12 +136,12 @@ module AutoResp
123
136
  end
124
137
  end
125
138
  else
126
- parse(txt)
139
+ parse(handler)
127
140
  end
128
141
  when Fixnum
129
- [{}, "", txt]
142
+ [{}, "", handler]
130
143
  when Array
131
- [txt[1], txt[2], txt[0]]
144
+ [handler[1], handler[2], handler[0]]
132
145
  end
133
146
  end
134
147
 
@@ -30,15 +30,10 @@ module AutoResp
30
30
  when Hash
31
31
  @last_rule = target.keys.first
32
32
  rules.merge! target
33
- when String
33
+ when String, Regexp
34
34
  rules[target] = args[1]
35
- when Regexp
36
- if block_given?
37
- rules[target] = block
38
- else
39
- rules[target] = args[1]
40
- end
41
35
  end
36
+ rules[target] ||= block if block
42
37
  end
43
38
 
44
39
  end
@@ -1,3 +1,3 @@
1
1
  module AutoResp
2
- VERSION = '0.1.8'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -4,8 +4,7 @@ require 'fileutils'
4
4
  require 'listen'
5
5
  require 'colorize'
6
6
  require 'logger'
7
-
8
- $: << File.expand_path( File.dirname(__FILE__) )
7
+ require 'socket'
9
8
 
10
9
  require 'ar/version'
11
10
  require 'ar/proxy_server'
@@ -19,18 +18,43 @@ module AutoResp
19
18
  class AutoResponder
20
19
 
21
20
  ARHOME = "#{ENV["HOME"]}/.auto_response"
22
- RULES = "#{ARHOME}/rules"
21
+ RULES = "#{ARHOME}/rules"
22
+
23
+ def initialize(config = {})
24
+
25
+ @config = config
26
+ @rule_manager = RuleManager.new
27
+ @logger = init_logger
28
+
29
+ @logger.debug { "Starting AutoResp v#{VERSION}" }
30
+ @logger.debug { "Listening at " << "#{ip_address}:#{proxy_port}".yellow }
23
31
 
24
- def initialize(config={})
25
- @config = config
26
- @rule_manager = RuleManager.new
27
- @logger = Logger.new( $stderr, Logger::WARN )
28
32
  init_autoresponse_home
29
33
  load_rules
30
34
  monitor_rules_change
31
35
  end
32
36
 
33
37
  protected
38
+
39
+ def init_logger
40
+ Logger.new( $stderr, Logger::WARN ).tap do |logger|
41
+ logger.formatter = proc do |serverity, datetime, prog, msg|
42
+ indent = {
43
+ :debug => '--',
44
+ :info => ' ',
45
+ :warn => '[W]',
46
+ :error => '[E]',
47
+ :fatal => '[F]'
48
+ }[serverity.downcase.to_sym]
49
+ "#{indent} #{msg}\n"
50
+ end
51
+ end
52
+ end
53
+
54
+ def ip_address
55
+ Socket.ip_address_list.detect {|intf| intf.ipv4_private? }.ip_address
56
+ end
57
+
34
58
  def init_autoresponse_home
35
59
  unless File.exist?(RULES)
36
60
  pwd = File.expand_path('..', File.dirname(__FILE__))
@@ -44,13 +68,17 @@ module AutoResp
44
68
  @server = ProxyServer.new(
45
69
  self,
46
70
  :BindAddress => @config[:host] || '0.0.0.0',
47
- :Port => @config[:port] || 9000,
71
+ :Port => proxy_port,
48
72
  :logger => @logger
49
73
  )
50
74
  trap('INT') { stop_and_exit }
51
75
  @server
52
76
  end
53
77
 
78
+ def proxy_port
79
+ @config[:port] || 9000
80
+ end
81
+
54
82
  def start_proxy
55
83
  @proxy_thread = Thread.new { init_proxy_server.start }
56
84
  end
@@ -103,8 +131,7 @@ module AutoResp
103
131
 
104
132
  private
105
133
  def load_rules(path=nil)
106
- path ||= @config[:rule_config]
107
- path ||= "#{ARHOME}/rules"
134
+ path ||= @config[:rule_config] || "#{ARHOME}/rules"
108
135
  if File.readable?(path)
109
136
  begin
110
137
  @rule_manager.instance_eval File.read(path)
@@ -116,15 +143,16 @@ module AutoResp
116
143
  end
117
144
 
118
145
  def log_rules
119
- @logger.info "mapping rules:"
146
+ @logger.debug "Mapping rules:"
120
147
  rules.each do |n,v|
121
- @logger.info "\n" << n.to_s.ljust(30).green << "\n=> #{v}"
148
+ @logger.info ""
149
+ @logger.info n.to_s.ljust(30).green
150
+ @logger.info "=> #{v}"
122
151
  end
123
152
  end
124
153
 
125
154
  def monitor_rules_change
126
- listener = Listen.to(ARHOME)
127
- listener.change { reload_rules }
155
+ listener = Listen.to(ARHOME) { reload_rules }
128
156
  Thread.new { listener.start }
129
157
  end
130
158
 
@@ -0,0 +1,124 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
6
+
7
+ <title>module AutoResp - RDoc Documentation</title>
8
+
9
+ <link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">
10
+
11
+ <script type="text/javascript">
12
+ var rdoc_rel_prefix = "./";
13
+ </script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
16
+ <script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
17
+ <script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
18
+ <script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
19
+ <script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
20
+ <script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>
21
+
22
+
23
+ <body id="top" class="module">
24
+ <nav id="metadata">
25
+ <nav id="home-section" class="section">
26
+ <h3 class="section-header">
27
+ <a href="./index.html">Home</a>
28
+ <a href="./table_of_contents.html#classes">Classes</a>
29
+ <a href="./table_of_contents.html#methods">Methods</a>
30
+ </h3>
31
+ </nav>
32
+
33
+
34
+ <nav id="search-section" class="section project-section" class="initially-hidden">
35
+ <form action="#" method="get" accept-charset="utf-8">
36
+ <h3 class="section-header">
37
+ <input type="text" name="search" placeholder="Search" id="search-field"
38
+ title="Type to search, Up and Down to navigate, Enter to load">
39
+ </h3>
40
+ </form>
41
+
42
+ <ul id="search-results" class="initially-hidden"></ul>
43
+ </nav>
44
+
45
+
46
+ <div id="file-metadata">
47
+ <nav id="file-list-section" class="section">
48
+ <h3 class="section-header">Defined In</h3>
49
+ <ul>
50
+ <li>lib/ar/parser.rb
51
+ <li>lib/ar/proxyserver.rb
52
+ <li>lib/ar/rule_dsl.rb
53
+ <li>lib/ar/rule_manager.rb
54
+ <li>lib/autoresponse.rb
55
+ </ul>
56
+ </nav>
57
+
58
+
59
+ </div>
60
+
61
+ <div id="class-metadata">
62
+
63
+
64
+
65
+
66
+ </div>
67
+
68
+ <div id="project-metadata">
69
+
70
+ <nav id="classindex-section" class="section project-section">
71
+ <h3 class="section-header">Class and Module Index</h3>
72
+
73
+ <ul class="link-list">
74
+
75
+ <li><a href="./AutoResp.html">AutoResp</a>
76
+
77
+ <li><a href="./AutoResp/AutoResponder.html">AutoResp::AutoResponder</a>
78
+
79
+ <li><a href="./AutoResp/Parser.html">AutoResp::Parser</a>
80
+
81
+ <li><a href="./AutoResp/ProxyServer.html">AutoResp::ProxyServer</a>
82
+
83
+ <li><a href="./AutoResp/RuleDSL.html">AutoResp::RuleDSL</a>
84
+
85
+ <li><a href="./AutoResp/RuleManager.html">AutoResp::RuleManager</a>
86
+
87
+ </ul>
88
+ </nav>
89
+
90
+ </div>
91
+ </nav>
92
+
93
+ <div id="documentation">
94
+ <h1 class="module">module AutoResp</h1>
95
+
96
+ <div id="description" class="description">
97
+
98
+ </div><!-- description -->
99
+
100
+
101
+
102
+
103
+ <section id="5Buntitled-5D" class="documentation-section">
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+ <!-- Methods -->
113
+
114
+ </section><!-- 5Buntitled-5D -->
115
+
116
+ </div><!-- documentation -->
117
+
118
+
119
+ <footer id="validator-badges">
120
+ <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
121
+ <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.12.
122
+ <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
123
+ </footer>
124
+