nayutaya-webhook-dispatcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.ja ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+
2
+ require "rake/testtask"
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.libs << "test"
8
+ test.test_files = Dir.glob("test/*_test.rb")
9
+ test.verbose = true
10
+ end
11
+
12
+ task :gemspec do
13
+ require "erb"
14
+ require "lib/webhook-dispatcher/version"
15
+
16
+ src = File.open("webhook-dispatcher.gemspec.erb", "rb") { |file| file.read }
17
+ erb = ERB.new(src, nil, "-")
18
+
19
+ version = WebHookDispatcher::VERSION
20
+ date = Time.now.strftime("%Y-%m-%d")
21
+
22
+ files = Dir.glob("**/*").select { |s| File.file?(s) }
23
+ test_files = Dir.glob("test/**").select { |s| File.file?(s) }
24
+
25
+ File.open("webhook-dispatcher.gemspec", "wb") { |file|
26
+ file.write(erb.result(binding))
27
+ }
28
+ end
data/example.rb ADDED
@@ -0,0 +1,47 @@
1
+
2
+ # このスクリプトはイメージであり、動作しません
3
+
4
+ WebHookPublisher.open_timeout = 5 # sec
5
+ WebHookPublisher.read_timeout = 5 # sec
6
+ WebHookPublisher.user_agent = "hoge"
7
+ WebHookPublisher.acl = x
8
+ WebHookPublisher.acl_with {
9
+ }
10
+
11
+ wp = WebHookPublisher.new
12
+ wp.open_timeout = 5 # sec
13
+ wp.read_timeout = 5 # sec
14
+ wp.user_agent = "hoge"
15
+ wp.acl = WebHookPublisher::Acl.with {
16
+ allow :all
17
+ allow "*", :all
18
+ allow "*", 1024..3000
19
+ allow "*", 0..1024
20
+ allow "*", [1,2,3,4]
21
+ deny IPAdd.new("0.0.0.0/0")
22
+ allow "127.0.0.0/8"
23
+ allow "localhost"
24
+ }
25
+
26
+ acl.clear
27
+ acl.add_deny(...)
28
+ acl.add_allow(...)
29
+ acl.allow?(ipaddr)
30
+ acl.deny?(ipaddr)
31
+ acl.with { ... }
32
+
33
+
34
+ request_obj = WebHookPublisher::Request.new(:get, URI.new(..))
35
+ request_obj.http_method = :get
36
+ request_obj.uri = uri
37
+
38
+ res = wp.request(request_obj)
39
+ res = wp.get(url)
40
+ res = wp.head(url)
41
+ res = wp.post(url, data)
42
+ #=> WebHookPublisher::Response
43
+ res.success? #=> true/false
44
+ res.status #=> :timeout
45
+ res.status_code #=> 200
46
+ res.status_message #=> OK
47
+ res.inner_exception #=> #<RuntimeError>
@@ -0,0 +1,84 @@
1
+
2
+ require "ipaddr"
3
+
4
+ class WebHookDispatcher::Acl
5
+ def initialize
6
+ @records = []
7
+ end
8
+
9
+ def self.with(&block)
10
+ return self.new.with(&block)
11
+ end
12
+
13
+ def size
14
+ return @records.size
15
+ end
16
+
17
+ def add_allow(ipaddr)
18
+ @records << AllowRecord.new(ipaddr)
19
+ return self
20
+ end
21
+
22
+ def add_deny(ipaddr)
23
+ @records << DenyRecord.new(ipaddr)
24
+ return self
25
+ end
26
+
27
+ def with(&block)
28
+ raise(ArgumentError) unless block_given?
29
+
30
+ obj = Object.new
31
+
32
+ this = self
33
+ (class << obj; self; end).class_eval {
34
+ define_method(:allow) { |ipaddr| this.add_allow(ipaddr) }
35
+ define_method(:deny) { |ipaddr| this.add_deny(ipaddr) }
36
+ private :allow, :deny
37
+ }
38
+
39
+ obj.instance_eval(&block)
40
+
41
+ return self
42
+ end
43
+
44
+ def allow?(ipaddr)
45
+ return @records.inject(true) { |result, record|
46
+ result = record.value if record.include?(ipaddr)
47
+ result
48
+ }
49
+ end
50
+
51
+ def deny?(ipaddr)
52
+ return !self.allow?(ipaddr)
53
+ end
54
+
55
+ class RecordBase
56
+ def initialize(ipaddr)
57
+ @ipaddr =
58
+ case ipaddr
59
+ when :all then IPAddr.new("0.0.0.0/0")
60
+ when String then IPAddr.new(ipaddr)
61
+ when IPAddr then ipaddr
62
+ else raise(ArgumentError, "invalid IP address")
63
+ end
64
+ end
65
+
66
+ attr_reader :ipaddr
67
+
68
+ def include?(ipaddr)
69
+ return @ipaddr.include?(ipaddr)
70
+ end
71
+ end
72
+
73
+ class AllowRecord < RecordBase
74
+ def value
75
+ return true
76
+ end
77
+ end
78
+
79
+ class DenyRecord < RecordBase
80
+ def value
81
+ return false
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+
2
+ class WebHookDispatcher
3
+ end
@@ -0,0 +1,15 @@
1
+
2
+ require "uri"
3
+
4
+ class WebHookDispatcher
5
+ module Request
6
+ end
7
+ end
8
+
9
+ class WebHookDispatcher::Request::Base
10
+ def initialize(uri)
11
+ @uri = uri
12
+ end
13
+
14
+ attr_accessor :uri
15
+ end
@@ -0,0 +1,8 @@
1
+
2
+ require "webhook-dispatcher/request/base"
3
+
4
+ class WebHookDispatcher::Request::Get < WebHookDispatcher::Request::Base
5
+ def initialize(uri)
6
+ super(uri)
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+
2
+ class WebHookDispatcher::Response
3
+ def initialize(options = {})
4
+ options = options.dup
5
+ @success = (options.delete(:success) == true)
6
+ @status = options.delete(:status) || :unknown
7
+ @http_code = options.delete(:http_code) || nil
8
+ @message = options.delete(:message) || nil
9
+ @exception = options.delete(:exception) || nil
10
+ raise(ArgumentError, "invalid parameter") unless options.empty?
11
+ end
12
+
13
+ attr_reader :success, :status, :http_code, :message, :exception
14
+
15
+ def success?
16
+ return self.success
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+
2
+ class WebHookDispatcher
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require "webhook-dispatcher/core"
data/test/acl_test.rb ADDED
@@ -0,0 +1,145 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-dispatcher/core"
4
+ require "webhook-dispatcher/acl"
5
+
6
+ class AclTest < Test::Unit::TestCase
7
+ def setup
8
+ @klass = WebHookDispatcher::Acl
9
+ @acl = @klass.new
10
+ end
11
+
12
+ def test_intialize
13
+ acl = @klass.new
14
+ assert_equal(0, acl.size)
15
+ end
16
+
17
+ #
18
+ # クラスメソッド
19
+ #
20
+
21
+ def test_self_with__none
22
+ acl = @klass.with { }
23
+ assert_equal(0, acl.size)
24
+ end
25
+
26
+ def test_self_with__allow_and_deny
27
+ acl = @klass.with {
28
+ allow :all
29
+ deny :all
30
+ }
31
+ assert_equal(2, acl.size)
32
+ end
33
+
34
+ def test_self_with__without_block
35
+ assert_raise(ArgumentError) {
36
+ @klass.with
37
+ }
38
+ end
39
+
40
+ #
41
+ # インスタンスメソッド
42
+ #
43
+
44
+ def test_add_allow
45
+ assert_equal(0, @acl.size)
46
+ @acl.add_allow("0.0.0.0/0")
47
+ assert_equal(1, @acl.size)
48
+ @acl.add_allow("0.0.0.0/0")
49
+ assert_equal(2, @acl.size)
50
+ end
51
+
52
+ def test_add_deny
53
+ assert_equal(0, @acl.size)
54
+ @acl.add_deny("0.0.0.0/0")
55
+ assert_equal(1, @acl.size)
56
+ @acl.add_deny("0.0.0.0/0")
57
+ assert_equal(2, @acl.size)
58
+ end
59
+
60
+ def test_with__none
61
+ @acl.with { }
62
+ assert_equal(0, @acl.size)
63
+ end
64
+
65
+ def test_with__allow
66
+ @acl.with { allow :all }
67
+ assert_equal(1, @acl.size)
68
+ end
69
+
70
+ def test_with__deny
71
+ @acl.with { deny :all }
72
+ assert_equal(1, @acl.size)
73
+ end
74
+
75
+ def test_with__without_block
76
+ assert_raise(ArgumentError) {
77
+ @acl.with
78
+ }
79
+ end
80
+
81
+ def test_allow?
82
+ assert_equal(true, @acl.allow?("127.0.0.1"))
83
+ @acl.add_deny("127.0.0.0/8")
84
+ assert_equal(false, @acl.allow?("127.0.0.1"))
85
+ @acl.add_allow("127.0.0.0/8")
86
+ assert_equal(true, @acl.allow?("127.0.0.1"))
87
+ end
88
+
89
+ def test_deny?
90
+ assert_equal(false, @acl.deny?("127.0.0.1"))
91
+ @acl.add_deny("127.0.0.0/8")
92
+ assert_equal(true, @acl.deny?("127.0.0.1"))
93
+ @acl.add_allow("127.0.0.0/8")
94
+ assert_equal(false, @acl.deny?("127.0.0.1"))
95
+ end
96
+
97
+ def test_complex1
98
+ @acl.with {
99
+ deny :all
100
+ allow "127.0.0.0/8"
101
+ }
102
+
103
+ assert_equal(false, @acl.allow?("126.255.255.255"))
104
+ assert_equal(true, @acl.allow?("127.0.0.0"))
105
+ assert_equal(true, @acl.allow?("127.255.255.255"))
106
+ assert_equal(false, @acl.allow?("128.0.0.0"))
107
+ end
108
+
109
+ def test_complex2
110
+ @acl.with {
111
+ deny :all
112
+ allow "192.168.1.0/24"
113
+ allow "192.168.3.0/24"
114
+ deny "192.168.1.127"
115
+ }
116
+
117
+ assert_equal(false, @acl.allow?("192.168.0.0"))
118
+ assert_equal(true, @acl.allow?("192.168.1.0"))
119
+ assert_equal(false, @acl.allow?("192.168.1.127"))
120
+ assert_equal(true, @acl.allow?("192.168.1.255"))
121
+ assert_equal(false, @acl.allow?("192.168.2.0"))
122
+ assert_equal(true, @acl.allow?("192.168.3.0"))
123
+ assert_equal(true, @acl.allow?("192.168.3.255"))
124
+ end
125
+
126
+ #
127
+ # RecordBaseクラス
128
+ #
129
+
130
+ def test_record_base_initialize
131
+ assert_equal(
132
+ IPAddr.new("127.0.0.0/8"),
133
+ @klass::RecordBase.new(IPAddr.new("127.0.0.0/8")).ipaddr)
134
+ assert_equal(
135
+ IPAddr.new("127.0.0.0/8"),
136
+ @klass::RecordBase.new("127.0.0.0/8").ipaddr)
137
+ assert_equal(
138
+ IPAddr.new("0.0.0.0/0"),
139
+ @klass::RecordBase.new(:all).ipaddr)
140
+
141
+ assert_raise(ArgumentError) {
142
+ @klass::RecordBase.new(:invalid)
143
+ }
144
+ end
145
+ end
@@ -0,0 +1,20 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-dispatcher/request/base"
4
+
5
+ class RequestBaseTest < Test::Unit::TestCase
6
+ def setup
7
+ @klass = WebHookDispatcher::Request::Base
8
+ end
9
+
10
+ def test_initialize
11
+ req = @klass.new(URI.parse("http://example.jp"))
12
+ assert_equal(URI.parse("http://example.jp"), req.uri)
13
+ end
14
+
15
+ def test_uri_attr
16
+ req = @klass.new(URI.parse("http://example.com"))
17
+ req.uri = URI.parse("http://example.org")
18
+ assert_equal(URI.parse("http://example.org"), req.uri)
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-dispatcher/request/get"
4
+
5
+ class RequestGetTest < Test::Unit::TestCase
6
+ def setup
7
+ @klass = WebHookDispatcher::Request::Get
8
+ end
9
+
10
+ def test_initialize
11
+ req = @klass.new(URI.parse("http://example.jp"))
12
+ assert_equal(URI.parse("http://example.jp"), req.uri)
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-dispatcher/core"
4
+ require "webhook-dispatcher/response"
5
+
6
+ class ResponseTest < Test::Unit::TestCase
7
+ def setup
8
+ @klass = WebHookDispatcher::Response
9
+ end
10
+
11
+ def test_initialize
12
+ res = @klass.new(
13
+ :success => true,
14
+ :status => :success,
15
+ :http_code => 200,
16
+ :message => "OK",
17
+ :exception => RuntimeError.new)
18
+ assert_equal(true, res.success)
19
+ assert_equal(true, res.success?)
20
+ assert_equal(:success, res.status)
21
+ assert_equal(200, res.http_code)
22
+ assert_equal("OK", res.message)
23
+ assert_kind_of(RuntimeError, res.exception)
24
+ end
25
+
26
+ def test_initialize__default
27
+ res = @klass.new
28
+ assert_equal(false, res.success)
29
+ assert_equal(false, res.success?)
30
+ assert_equal(:unknown, res.status)
31
+ assert_equal(nil, res.http_code)
32
+ assert_equal(nil, res.message)
33
+ assert_equal(nil, res.exception)
34
+ end
35
+
36
+ def test_initialize__invalid_parameter
37
+ assert_raise(ArgumentError) {
38
+ @klass.new(:invalid => true)
39
+ }
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+
2
+ require "test/unit"
3
+
4
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
@@ -0,0 +1,51 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "webhook-dispatcher"
8
+ s.version = "0.0.1"
9
+ s.date = "2009-06-23"
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "webhook-dispatcher"
15
+ s.description = "webhook-dispatcher"
16
+ s.homepage = "http://github.com/nayutaya/webhook-dispatcher/"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ "example.rb",
24
+ "lib/webhook-dispatcher/acl.rb",
25
+ "lib/webhook-dispatcher/core.rb",
26
+ "lib/webhook-dispatcher/request/base.rb",
27
+ "lib/webhook-dispatcher/request/get.rb",
28
+ "lib/webhook-dispatcher/response.rb",
29
+ "lib/webhook-dispatcher/version.rb",
30
+ "lib/webhook-dispatcher.rb",
31
+ "Rakefile",
32
+ "README.ja",
33
+ "test/acl_test.rb",
34
+ "test/request_base_test.rb",
35
+ "test/request_get_test.rb",
36
+ "test/response_test.rb",
37
+ "test/test_helper.rb",
38
+ "webhook-dispatcher.gemspec",
39
+ "webhook-dispatcher.gemspec.erb",
40
+ ]
41
+ s.test_files = [
42
+ "test/acl_test.rb",
43
+ "test/request_base_test.rb",
44
+ "test/request_get_test.rb",
45
+ "test/response_test.rb",
46
+ "test/test_helper.rb",
47
+ ]
48
+ s.extra_rdoc_files = [
49
+ "README.ja",
50
+ ]
51
+ end
@@ -0,0 +1,35 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "webhook-dispatcher"
8
+ s.version = <%= version.dump %>
9
+ s.date = <%= date.dump %>
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "webhook-dispatcher"
15
+ s.description = "webhook-dispatcher"
16
+ s.homepage = "http://github.com/nayutaya/webhook-dispatcher/"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ <%- files.each { |path| -%>
24
+ <%= path.dump %>,
25
+ <%- } -%>
26
+ ]
27
+ s.test_files = [
28
+ <%- test_files.each { |path| -%>
29
+ <%= path.dump %>,
30
+ <%- } -%>
31
+ ]
32
+ s.extra_rdoc_files = [
33
+ "README.ja",
34
+ ]
35
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nayutaya-webhook-dispatcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuya Kato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: webhook-dispatcher
17
+ email: yuyakato@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.ja
24
+ files:
25
+ - example.rb
26
+ - lib/webhook-dispatcher/acl.rb
27
+ - lib/webhook-dispatcher/core.rb
28
+ - lib/webhook-dispatcher/request/base.rb
29
+ - lib/webhook-dispatcher/request/get.rb
30
+ - lib/webhook-dispatcher/response.rb
31
+ - lib/webhook-dispatcher/version.rb
32
+ - lib/webhook-dispatcher.rb
33
+ - Rakefile
34
+ - README.ja
35
+ - test/acl_test.rb
36
+ - test/request_base_test.rb
37
+ - test/request_get_test.rb
38
+ - test/response_test.rb
39
+ - test/test_helper.rb
40
+ - webhook-dispatcher.gemspec
41
+ - webhook-dispatcher.gemspec.erb
42
+ has_rdoc: false
43
+ homepage: http://github.com/nayutaya/webhook-dispatcher/
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.6
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: webhook-dispatcher
68
+ test_files:
69
+ - test/acl_test.rb
70
+ - test/request_base_test.rb
71
+ - test/request_get_test.rb
72
+ - test/response_test.rb
73
+ - test/test_helper.rb