intervention 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/History.md +1 -0
- data/License.md +20 -0
- data/README.md +32 -0
- data/lib/intervention.rb +69 -0
- data/lib/intervention/client.rb +33 -0
- data/lib/intervention/server.rb +36 -0
- data/lib/intervention/version.rb +4 -0
- data/spec/helper.rb +13 -0
- data/spec/intervention_spec.rb +11 -0
- metadata +168 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d83fa10542e7ef1ea5c39a549562f08909d634a0
|
4
|
+
data.tar.gz: 435fa92b37c325a8fd40cf15481ff412564101f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86982dcff951986b9c32c57c8cb6016f90cc1f4f90931f30956f25bf3963aee546a4369380c5a010eb45964b9f325313f037035534e43ac4b74910969bfefe7d
|
7
|
+
data.tar.gz: 40e9ad7204db7dc0c53238f5157a945b3c3acebdef510e0a7d875b6c60fe2ef791cee5cabd62fe37d02088f20a7bde99d18bb583e47e056413e5bb3f28d68a2b
|
data/History.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
History.md
|
data/License.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Ben Slaughter
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
Intervention
|
2
|
+
============
|
3
|
+
[![Code Climate](https://codeclimate.com/github/benSlaughter/intervention.png)](https://codeclimate.com/github/benSlaughter/intervention)
|
4
|
+
[![Build Status](https://travis-ci.org/benSlaughter/intervention.png?branch=master)](https://travis-ci.org/benSlaughter/intervention)
|
5
|
+
[![Dependency Status](https://gemnasium.com/benSlaughter/intervention.png)](https://gemnasium.com/benSlaughter/intervention)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/benSlaughter/intervention/badge.png)](https://coveralls.io/r/benSlaughter/intervention)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/intervention.png)](http://badge.fury.io/rb/intervention)
|
8
|
+
|
9
|
+
A simple proxy management structure that can contain several proxies that can handle multiple transactions.
|
10
|
+
Each proxy can be configured to run a block of code upon a request or response being made.
|
11
|
+
|
12
|
+
Intervention has the ability to add 'interventions', Calls to external modules, classes and objects.
|
13
|
+
|
14
|
+
# Getting started
|
15
|
+
|
16
|
+
## setup
|
17
|
+
Intervention is a gem, to add it to your system run the following:
|
18
|
+
```
|
19
|
+
gem install intervention
|
20
|
+
```
|
21
|
+
## Using intervention
|
22
|
+
|
23
|
+
To use intervention in your code, require intervention at the start of your file.
|
24
|
+
```
|
25
|
+
require 'intervention'
|
26
|
+
```
|
27
|
+
|
28
|
+
### Starting Intervention
|
29
|
+
|
30
|
+
Please see the example
|
31
|
+
|
32
|
+
TODO: Update readme
|
data/lib/intervention.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'hashie'
|
3
|
+
require 'json'
|
4
|
+
require 'uri'
|
5
|
+
require 'observer'
|
6
|
+
require 'pry'
|
7
|
+
require 'zlib'
|
8
|
+
require 'eventmachine'
|
9
|
+
require 'segregate'
|
10
|
+
|
11
|
+
require_relative 'intervention/version'
|
12
|
+
require_relative 'intervention/server'
|
13
|
+
require_relative 'intervention/client'
|
14
|
+
|
15
|
+
Thread.abort_on_exception = true
|
16
|
+
|
17
|
+
module Intervention
|
18
|
+
class << self
|
19
|
+
|
20
|
+
def method_missing meth, *args, **kwargs, &block
|
21
|
+
if config.respond_to? meth
|
22
|
+
config.send meth, *args, **kwargs, &block
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def start *args, **kwargs, &block
|
29
|
+
EventMachine.start_server 'localhost', config.listen_port, Intervention::Client
|
30
|
+
end
|
31
|
+
|
32
|
+
def configure
|
33
|
+
yield config
|
34
|
+
end
|
35
|
+
|
36
|
+
def on event, &block
|
37
|
+
@config.event_handlers ||= Hashie::Mash.new
|
38
|
+
@config.event_handlers[event] = block
|
39
|
+
end
|
40
|
+
|
41
|
+
def callback object
|
42
|
+
@config.callbacks ||= []
|
43
|
+
@config.callbacks << object
|
44
|
+
end
|
45
|
+
|
46
|
+
def config
|
47
|
+
@config ||= Hashie::Mash.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def event_handlers
|
51
|
+
@config.event_handlers ||= Hashie::Mash.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def callbacks
|
55
|
+
@config.callbacks ||= []
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
thread = Thread.current
|
61
|
+
Thread.new{ EventMachine.run{ thread.wakeup } }
|
62
|
+
# pause until reactor starts
|
63
|
+
Thread.stop
|
64
|
+
|
65
|
+
Intervention.configure do |config|
|
66
|
+
config.listen_port = 2222
|
67
|
+
config.host_address = 'localhost'
|
68
|
+
config.host_port = 80
|
69
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Intervention
|
2
|
+
class Client < EventMachine::Connection
|
3
|
+
attr_reader :server, :parser
|
4
|
+
|
5
|
+
def inspect
|
6
|
+
"#<Client:%s listen:%s>" % [(object_id << 1).to_s(16), Intervention.config.listen_port]
|
7
|
+
end
|
8
|
+
|
9
|
+
def post_init
|
10
|
+
Intervention.config.client = self
|
11
|
+
@parser = Segregate.new(self)
|
12
|
+
@server = EventMachine.connect Intervention.config.host_address, Intervention.config.host_port, Intervention::Server, client: self
|
13
|
+
end
|
14
|
+
|
15
|
+
def receive_data data
|
16
|
+
@parser.parse_data data
|
17
|
+
end
|
18
|
+
|
19
|
+
def on_message_complete parser
|
20
|
+
callback :request
|
21
|
+
@parser.headers['host'] = Intervention.config.host_address
|
22
|
+
@parser.headers['accept-encoding'] = "deflate"
|
23
|
+
@server.send_data @parser.raw_data
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def callback event
|
29
|
+
Intervention.event_handlers[event].call(self) if Intervention.event_handlers.key? event
|
30
|
+
Intervention.callbacks.each { |c| c.send(event, self) if c.respond_to? event }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Intervention
|
2
|
+
class Server < EventMachine::Connection
|
3
|
+
attr_reader :client, :parser
|
4
|
+
|
5
|
+
def inspect
|
6
|
+
"#<Server:%s host:%s port:%s>" % [(object_id << 1).to_s(16), Intervention.config.host_address, Intervention.config.host_port]
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize *args, **kwargs, &block
|
10
|
+
super
|
11
|
+
@client = kwargs[:client]
|
12
|
+
end
|
13
|
+
|
14
|
+
def post_init
|
15
|
+
Intervention.config.server = self
|
16
|
+
@parser = Segregate.new(self)
|
17
|
+
end
|
18
|
+
|
19
|
+
def receive_data data
|
20
|
+
@parser.parse_data data
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_message_complete parser
|
24
|
+
callback :response
|
25
|
+
@client.send_data @parser.raw_data
|
26
|
+
@client.close_connection_after_writing
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def callback event
|
32
|
+
Intervention.event_handlers[event].call(self) if Intervention.event_handlers.key? event
|
33
|
+
Intervention.callbacks.each { |c| c.send(event, self) if c.respond_to? event }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/spec/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: intervention
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Slaughter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: yard
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coveralls
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: hashie
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: eventmachine
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: segregate
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: A simple proxy that can be configured to perform actions upon a request
|
126
|
+
or response
|
127
|
+
email: b.p.slaughter@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- README.md
|
133
|
+
- License.md
|
134
|
+
- History.md
|
135
|
+
- lib/intervention/client.rb
|
136
|
+
- lib/intervention/server.rb
|
137
|
+
- lib/intervention/version.rb
|
138
|
+
- lib/intervention.rb
|
139
|
+
- spec/helper.rb
|
140
|
+
- spec/intervention_spec.rb
|
141
|
+
homepage: http://benslaughter.github.io/intervention/
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - '>='
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.0.3
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: 'Intervention: Super simple proxy'
|
165
|
+
test_files:
|
166
|
+
- spec/helper.rb
|
167
|
+
- spec/intervention_spec.rb
|
168
|
+
has_rdoc:
|