mason-client 0 → 0.0.1
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.
- data/Gemfile +3 -0
- data/Gemfile.lock +24 -0
- data/Rakefile +8 -0
- data/lib/mason_client.rb +57 -0
- data/lib/mason_client/app.rb +19 -0
- data/lib/mason_client/http.rb +25 -0
- data/lib/mason_client/mock.rb +27 -0
- data/lib/mason_client/request.rb +19 -0
- data/lib/mason_client/response.rb +13 -0
- data/lib/mason_client/result.rb +19 -0
- data/mason-client.gemspec +13 -0
- data/test/mason_client_test.rb +66 -0
- metadata +66 -6
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
mason-client (0)
|
|
5
|
+
json
|
|
6
|
+
rack
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: http://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
contest (0.1.2)
|
|
12
|
+
json (1.4.6)
|
|
13
|
+
rack (1.2.1)
|
|
14
|
+
rake (0.8.7)
|
|
15
|
+
|
|
16
|
+
PLATFORMS
|
|
17
|
+
ruby
|
|
18
|
+
|
|
19
|
+
DEPENDENCIES
|
|
20
|
+
contest
|
|
21
|
+
json
|
|
22
|
+
mason-client!
|
|
23
|
+
rack
|
|
24
|
+
rake
|
data/Rakefile
ADDED
data/lib/mason_client.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "rack"
|
|
3
|
+
|
|
4
|
+
require "mason_client/request"
|
|
5
|
+
require "mason_client/result"
|
|
6
|
+
require "mason_client/response"
|
|
7
|
+
require "mason_client/app"
|
|
8
|
+
require "mason_client/mock"
|
|
9
|
+
require "mason_client/http"
|
|
10
|
+
|
|
11
|
+
module MasonClient
|
|
12
|
+
class Error < StandardError; end
|
|
13
|
+
|
|
14
|
+
def self.setup(url, &block)
|
|
15
|
+
@url = url
|
|
16
|
+
@handler = block
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.build(repo, sha1, command, callback)
|
|
20
|
+
request = Request.new(repo, sha1, command, callback)
|
|
21
|
+
url = backend.run(request)
|
|
22
|
+
|
|
23
|
+
Response.new(url)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.app
|
|
27
|
+
@app ||= App.new(@handler)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.backend
|
|
31
|
+
@backend ||= HTTP.new(@url)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.handle(result)
|
|
35
|
+
@handler.call(result)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.enable_mock!
|
|
39
|
+
@backend = Mock.new(@url)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.disable_mock!
|
|
43
|
+
@backend = nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.mocked?
|
|
47
|
+
@backend.is_a?(Mock)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.success!
|
|
51
|
+
@backend.success!
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.fail!
|
|
55
|
+
@backend.fail!
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MasonClient
|
|
2
|
+
class App
|
|
3
|
+
def initialize(handler)
|
|
4
|
+
@handler = handler
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def call(env)
|
|
8
|
+
request = Rack::Request.new(env)
|
|
9
|
+
payload = JSON.parse(request.params["payload"])
|
|
10
|
+
result = Result.from(payload)
|
|
11
|
+
|
|
12
|
+
if @handler.call(result)
|
|
13
|
+
Rack::Response.new("OK", 200, {"Content-Type" => "text/plain"}).finish
|
|
14
|
+
else
|
|
15
|
+
raise Error, "failed to handle build result for #{result.callback}"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module MasonClient
|
|
2
|
+
class HTTP
|
|
3
|
+
def initialize(url)
|
|
4
|
+
@url = url
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def run(request)
|
|
8
|
+
response = Net::HTTP.post_form(URI(@url), "payload" => request.to_json)
|
|
9
|
+
|
|
10
|
+
unless response.code == "301"
|
|
11
|
+
return
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
response["Location"]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def success!
|
|
18
|
+
raise Error, "success! can only be used when mocked"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def fail!
|
|
22
|
+
raise Error, "fail! can only be used when mocked"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module MasonClient
|
|
2
|
+
class Mock
|
|
3
|
+
def initialize(url)
|
|
4
|
+
@url = url
|
|
5
|
+
|
|
6
|
+
success!
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def success!
|
|
10
|
+
@status = 0
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def fail!
|
|
14
|
+
@status = 1
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def run(request)
|
|
18
|
+
id = Time.now.to_f
|
|
19
|
+
url = "#{@url}/#{id}"
|
|
20
|
+
result = Result.new(url, request.callback, @status)
|
|
21
|
+
|
|
22
|
+
MasonClient.handle(result)
|
|
23
|
+
|
|
24
|
+
url
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MasonClient
|
|
2
|
+
class Request
|
|
3
|
+
def initialize(repo, ref, command, callback)
|
|
4
|
+
@repo = repo
|
|
5
|
+
@ref = ref
|
|
6
|
+
@command = command
|
|
7
|
+
@callback = callback
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
attr_reader :repo, :ref, :command, :callback
|
|
11
|
+
|
|
12
|
+
def to_json
|
|
13
|
+
{ :repo => repo,
|
|
14
|
+
:ref => ref,
|
|
15
|
+
:command => command,
|
|
16
|
+
:callback => callback }.to_json
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MasonClient
|
|
2
|
+
class Result
|
|
3
|
+
def self.from(payload)
|
|
4
|
+
new(payload["url"], payload["callback"], payload["status"])
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def initialize(url, callback, status)
|
|
8
|
+
@url = url
|
|
9
|
+
@callback = callback
|
|
10
|
+
@status = status
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
attr_reader :url, :callback
|
|
14
|
+
|
|
15
|
+
def success?
|
|
16
|
+
@status.zero?
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = "mason-client"
|
|
3
|
+
s.version = "0.0.1"
|
|
4
|
+
s.summary = "builds things"
|
|
5
|
+
|
|
6
|
+
s.add_dependency "json"
|
|
7
|
+
s.add_dependency "rack"
|
|
8
|
+
|
|
9
|
+
s.add_development_dependency "contest"
|
|
10
|
+
s.add_development_dependency "rake"
|
|
11
|
+
|
|
12
|
+
s.files = `git ls-files`.split("\n")
|
|
13
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require "test/unit"
|
|
2
|
+
require "contest"
|
|
3
|
+
|
|
4
|
+
require "mason_client"
|
|
5
|
+
|
|
6
|
+
class MasonClientTest < Test::Unit::TestCase
|
|
7
|
+
setup do
|
|
8
|
+
@results = {}
|
|
9
|
+
|
|
10
|
+
MasonClient.enable_mock!
|
|
11
|
+
MasonClient.setup("http://mason.example.com") do |result|
|
|
12
|
+
@results[result.url] = result
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def request_build
|
|
17
|
+
MasonClient.build(
|
|
18
|
+
"git@github.com:sinatra/sinatra.git",
|
|
19
|
+
"HEAD",
|
|
20
|
+
"rake",
|
|
21
|
+
"http://example.com/ci"
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe "build api" do
|
|
26
|
+
test "success" do
|
|
27
|
+
MasonClient.success!
|
|
28
|
+
|
|
29
|
+
response = request_build
|
|
30
|
+
|
|
31
|
+
assert response.success?
|
|
32
|
+
assert @results[response.url].success?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test "fail" do
|
|
36
|
+
MasonClient.fail!
|
|
37
|
+
|
|
38
|
+
response = request_build
|
|
39
|
+
|
|
40
|
+
assert response.success?
|
|
41
|
+
assert ! @results[response.url].success?
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
describe "mocking api" do
|
|
46
|
+
setup do
|
|
47
|
+
MasonClient.disable_mock!
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test "mocked?" do
|
|
51
|
+
MasonClient.enable_mock!
|
|
52
|
+
assert MasonClient.mocked?
|
|
53
|
+
|
|
54
|
+
MasonClient.disable_mock!
|
|
55
|
+
assert ! MasonClient.mocked?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
test "success!" do
|
|
59
|
+
assert_raise(NoMethodError) { MasonClient.success! }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
test "fail!" do
|
|
63
|
+
assert_raise(NoMethodError) { MasonClient.fail! }
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
metadata
CHANGED
|
@@ -4,7 +4,9 @@ version: !ruby/object:Gem::Version
|
|
|
4
4
|
prerelease: false
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
|
-
|
|
7
|
+
- 0
|
|
8
|
+
- 1
|
|
9
|
+
version: 0.0.1
|
|
8
10
|
platform: ruby
|
|
9
11
|
authors: []
|
|
10
12
|
|
|
@@ -12,10 +14,57 @@ autorequire:
|
|
|
12
14
|
bindir: bin
|
|
13
15
|
cert_chain: []
|
|
14
16
|
|
|
15
|
-
date: 2010-09-
|
|
17
|
+
date: 2010-09-19 00:00:00 +02:00
|
|
16
18
|
default_executable:
|
|
17
|
-
dependencies:
|
|
18
|
-
|
|
19
|
+
dependencies:
|
|
20
|
+
- !ruby/object:Gem::Dependency
|
|
21
|
+
name: json
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
segments:
|
|
28
|
+
- 0
|
|
29
|
+
version: "0"
|
|
30
|
+
type: :runtime
|
|
31
|
+
version_requirements: *id001
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: rack
|
|
34
|
+
prerelease: false
|
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
segments:
|
|
40
|
+
- 0
|
|
41
|
+
version: "0"
|
|
42
|
+
type: :runtime
|
|
43
|
+
version_requirements: *id002
|
|
44
|
+
- !ruby/object:Gem::Dependency
|
|
45
|
+
name: contest
|
|
46
|
+
prerelease: false
|
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
segments:
|
|
52
|
+
- 0
|
|
53
|
+
version: "0"
|
|
54
|
+
type: :development
|
|
55
|
+
version_requirements: *id003
|
|
56
|
+
- !ruby/object:Gem::Dependency
|
|
57
|
+
name: rake
|
|
58
|
+
prerelease: false
|
|
59
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
segments:
|
|
64
|
+
- 0
|
|
65
|
+
version: "0"
|
|
66
|
+
type: :development
|
|
67
|
+
version_requirements: *id004
|
|
19
68
|
description:
|
|
20
69
|
email:
|
|
21
70
|
executables: []
|
|
@@ -24,8 +73,19 @@ extensions: []
|
|
|
24
73
|
|
|
25
74
|
extra_rdoc_files: []
|
|
26
75
|
|
|
27
|
-
files:
|
|
28
|
-
|
|
76
|
+
files:
|
|
77
|
+
- Gemfile
|
|
78
|
+
- Gemfile.lock
|
|
79
|
+
- Rakefile
|
|
80
|
+
- lib/mason_client.rb
|
|
81
|
+
- lib/mason_client/app.rb
|
|
82
|
+
- lib/mason_client/http.rb
|
|
83
|
+
- lib/mason_client/mock.rb
|
|
84
|
+
- lib/mason_client/request.rb
|
|
85
|
+
- lib/mason_client/response.rb
|
|
86
|
+
- lib/mason_client/result.rb
|
|
87
|
+
- mason-client.gemspec
|
|
88
|
+
- test/mason_client_test.rb
|
|
29
89
|
has_rdoc: true
|
|
30
90
|
homepage:
|
|
31
91
|
licenses: []
|