nayutaya-webhook-publisher 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -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-publisher/version"
15
+
16
+ src = File.open("webhook-publisher.gemspec.erb", "rb") { |file| file.read }
17
+ erb = ERB.new(src, nil, "-")
18
+
19
+ version = WebHookPublisher::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-publisher.gemspec", "wb") { |file|
26
+ file.write(erb.result(binding))
27
+ }
28
+ end
@@ -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,2 @@
1
+
2
+ require "webhook-publisher/core"
@@ -0,0 +1,84 @@
1
+
2
+ require "ipaddr"
3
+
4
+ class WebHookPublisher::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 WebHookPublisher
3
+ end
@@ -0,0 +1,13 @@
1
+
2
+ require "uri"
3
+
4
+ class WebHookPublisher::Request
5
+ end
6
+
7
+ class WebHookPublisher::Request::Base
8
+ def initialize(uri)
9
+ @uri = uri
10
+ end
11
+
12
+ attr_accessor :uri
13
+ end
@@ -0,0 +1,18 @@
1
+
2
+ class WebHookPublisher::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 WebHookPublisher
3
+ VERSION = "0.0.0"
4
+ end
@@ -0,0 +1,145 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-publisher/core"
4
+ require "webhook-publisher/acl"
5
+
6
+ class AclTest < Test::Unit::TestCase
7
+ def setup
8
+ @klass = WebHookPublisher::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,21 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-publisher/core"
4
+ require "webhook-publisher/request/base"
5
+
6
+ class RequestBaseTest < Test::Unit::TestCase
7
+ def setup
8
+ @klass = WebHookPublisher::Request::Base
9
+ end
10
+
11
+ def test_initialize
12
+ req = @klass.new(URI.parse("http://example.jp"))
13
+ assert_equal(URI.parse("http://example.jp"), req.uri)
14
+ end
15
+
16
+ def test_uri_attr
17
+ req = @klass.new(URI.parse("http://example.com"))
18
+ req.uri = URI.parse("http://example.org")
19
+ assert_equal(URI.parse("http://example.org"), req.uri)
20
+ end
21
+ end
@@ -0,0 +1,41 @@
1
+
2
+ require File.dirname(__FILE__) + "/test_helper"
3
+ require "webhook-publisher/core"
4
+ require "webhook-publisher/response"
5
+
6
+ class ResponseTest < Test::Unit::TestCase
7
+ def setup
8
+ @klass = WebHookPublisher::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,48 @@
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-publisher"
8
+ s.version = "0.0.0"
9
+ s.date = "2009-06-23"
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "webhook-publisher"
15
+ s.description = "webhook-publisher"
16
+ s.homepage = "http://github.com/nayutaya/webhook-publisher/"
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-publisher/acl.rb",
25
+ "lib/webhook-publisher/core.rb",
26
+ "lib/webhook-publisher/request/base.rb",
27
+ "lib/webhook-publisher/response.rb",
28
+ "lib/webhook-publisher/version.rb",
29
+ "lib/webhook-publisher.rb",
30
+ "Rakefile",
31
+ "README.ja",
32
+ "test/acl_test.rb",
33
+ "test/request_base.rb",
34
+ "test/response_test.rb",
35
+ "test/test_helper.rb",
36
+ "webhook-publisher.gemspec",
37
+ "webhook-publisher.gemspec.erb",
38
+ ]
39
+ s.test_files = [
40
+ "test/acl_test.rb",
41
+ "test/request_base.rb",
42
+ "test/response_test.rb",
43
+ "test/test_helper.rb",
44
+ ]
45
+ s.extra_rdoc_files = [
46
+ "README.ja",
47
+ ]
48
+ 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-publisher"
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-publisher"
15
+ s.description = "webhook-publisher"
16
+ s.homepage = "http://github.com/nayutaya/webhook-publisher/"
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,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nayutaya-webhook-publisher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
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-publisher
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-publisher/acl.rb
27
+ - lib/webhook-publisher/core.rb
28
+ - lib/webhook-publisher/request/base.rb
29
+ - lib/webhook-publisher/response.rb
30
+ - lib/webhook-publisher/version.rb
31
+ - lib/webhook-publisher.rb
32
+ - Rakefile
33
+ - README.ja
34
+ - test/acl_test.rb
35
+ - test/request_base.rb
36
+ - test/response_test.rb
37
+ - test/test_helper.rb
38
+ - webhook-publisher.gemspec
39
+ - webhook-publisher.gemspec.erb
40
+ has_rdoc: false
41
+ homepage: http://github.com/nayutaya/webhook-publisher/
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 1.8.6
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: webhook-publisher
66
+ test_files:
67
+ - test/acl_test.rb
68
+ - test/request_base.rb
69
+ - test/response_test.rb
70
+ - test/test_helper.rb