go_figure 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = GoFigure::VERSION
8
8
  s.authors = ["Ketan Padegaonkar"]
9
9
  s.email = ["KetanPadegaonkar@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/ThoughtWorksInc/go-figure"
11
11
  s.summary = %q{A Ruby DSL to write a configuration file for the a go server.}
12
12
  s.description = %q{This provides a ruby DSL to create a configuration file for the go server (thoughtworks-studios.com/go)}
13
13
 
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
-
21
20
 
22
21
  # specify any dependencies here; for example:
23
22
  # s.add_development_dependency "rspec"
@@ -8,9 +8,19 @@ module GoFigure
8
8
  }
9
9
 
10
10
  attr_accessor *DEFAULT_OPTIONS.keys
11
- attr_accessor :http_fetcher
11
+ @@http_fetcher = nil
12
12
 
13
- def initialize(options)
13
+ class << self
14
+ def http_fetcher=(fetcher)
15
+ @@http_fetcher = fetcher
16
+ end
17
+
18
+ def http_fetcher
19
+ @@http_fetcher ||= GoFigure::HttpFetcher.new
20
+ end
21
+ end
22
+
23
+ def initialize(options={})
14
24
  DEFAULT_OPTIONS.each do |k, v|
15
25
  self.send("#{k}=", options[k] || v)
16
26
  end
@@ -18,7 +28,7 @@ module GoFigure
18
28
 
19
29
  def get_config
20
30
  response = http_fetcher.get(config_xml_url)
21
- if response.status == 200
31
+ if response.code == '200'
22
32
  return GoConfig.new(:md5 => response['X-CRUISE-CONFIG-MD5'], :xml => response.body)
23
33
  else
24
34
  raise "Could not fetch the go config file"
@@ -30,7 +40,7 @@ module GoFigure
30
40
  end
31
41
 
32
42
  def http_fetcher
33
- @http_fetcher || HttpFetcher.new
43
+ self.class.http_fetcher
34
44
  end
35
45
 
36
46
  private
@@ -1,10 +1,10 @@
1
1
  require 'net/http'
2
2
  require 'net/https'
3
3
 
4
- module UserImport
4
+ module GoFigure
5
5
  class HttpFetcher
6
6
 
7
- def fetch(url, user, pass)
7
+ def get(url, params = {})
8
8
  url = URI.parse(url)
9
9
 
10
10
  http = Net::HTTP.new(url.host, url.port)
@@ -12,16 +12,34 @@ module UserImport
12
12
 
13
13
  res = http.start do |http|
14
14
  req = Net::HTTP::Get.new(url.path)
15
- req.basic_auth user, pass
16
15
  http.request(req)
17
16
  end
18
17
 
19
18
  case res
20
19
  when Net::HTTPSuccess
21
- return res.body
20
+ return res
22
21
  end
23
22
  res.error!
24
23
  end
24
+
25
+ def post(url, params = {})
26
+ url = URI.parse(url)
27
+
28
+ http = Net::HTTP.new(url.host, url.port)
29
+ http.use_ssl = url.scheme == 'https'
30
+
31
+ res = http.start do |http|
32
+ req = Net::HTTP::Post.new(url.path)
33
+ req.set_form_data(params) if params.any?
34
+ http.request(req)
35
+ end
36
+
37
+ case res
38
+ when Net::HTTPSuccess
39
+ return res
40
+ end
41
+ res.error!
42
+ end
43
+
25
44
  end
26
45
  end
27
-
@@ -0,0 +1 @@
1
+ require 'go_figure/test/stub_http_fetcher.rb'
@@ -0,0 +1,91 @@
1
+ require 'ostruct'
2
+ module GoFigure
3
+ class StubHttpFetcher
4
+ # Careful : this will respond to any call with nil
5
+ class HashStruct < OpenStruct
6
+ def [](key)
7
+ self.headers[key]
8
+ end
9
+ end
10
+
11
+ module Response
12
+ def invoked!
13
+ @invoked = true
14
+ end
15
+
16
+ def invoked?
17
+ !!@invoked
18
+ end
19
+ end
20
+
21
+ class StringResponse
22
+ include Response
23
+ def initialize(content, code = 200, headers = {})
24
+ @content = content
25
+ @code = code.to_s
26
+ @headers = headers
27
+ end
28
+
29
+ def execute
30
+ HashStruct.new(:body => @content, :code => @code, :headers => @headers)
31
+ end
32
+
33
+ end
34
+
35
+ class ErrorResponse
36
+ include Response
37
+ def initialize(error)
38
+ @error = error
39
+ end
40
+
41
+ def execute
42
+ raise @error
43
+ end
44
+ end
45
+
46
+ def get(url)
47
+ if body = interweb[:get][url]
48
+ body.invoked!
49
+ return body.execute
50
+ else
51
+ raise "404 - Could not find #{url}. Available URLs are #{@interweb[:get].keys.inspect}"
52
+ end
53
+ end
54
+
55
+ def post(url, params = {})
56
+ if body = interweb[:post][url]
57
+ response = body.execute
58
+ if response.headers == params
59
+ body.invoked!
60
+ return response
61
+ end
62
+ end
63
+
64
+ raise "404 - Could not find a url listening to #{url.inspect} that responds to post params #{params.inspect}. Available URLs are #{@interweb[:post].keys.inspect}"
65
+ end
66
+
67
+ def invoked?(url, type = :get, params = {})
68
+ if body = interweb[type][url]
69
+ response = body.execute
70
+ if response.headers == params
71
+
72
+ body.invoked?
73
+ end
74
+ end
75
+ end
76
+
77
+ def register_content(content, url, type = :get, headers = {})
78
+ interweb[type][url] = StringResponse.new(content, 200, headers)
79
+ end
80
+
81
+ def register_error(url, type = :get, error)
82
+ interweb[type][url] = ErrorResponse.new(error)
83
+ end
84
+
85
+ def interweb
86
+ @interweb ||= {:get => {}, :post => {}}
87
+ end
88
+
89
+ end
90
+ end
91
+
@@ -1,3 +1,3 @@
1
1
  module GoFigure
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -7,7 +7,7 @@ module GoFigure
7
7
  def test_should_fetch_xml_config_on_localhost
8
8
  md5 = '7455edff001e2f262beb7c13f10ff7cb'
9
9
  endpoint = GoConfigEndpoint.new(:host => 'example.com', :port => 1234)
10
- endpoint.http_fetcher = fetcher_with_config_file('foo.xml', md5, config_endpoint)
10
+ GoConfigEndpoint.http_fetcher = fetcher_with_config_file('foo.xml', md5, config_endpoint)
11
11
 
12
12
  config = endpoint.get_config
13
13
  assert_equal config_file('foo.xml'), config.original_xml
@@ -17,7 +17,7 @@ module GoFigure
17
17
  def test_should_post_back_new_config_xml_content_with_original_md5
18
18
  md5 = '7455edff001e2f262beb7c13f10ff7cb'
19
19
  endpoint = GoConfigEndpoint.new(:host => 'example.com', :port => 1234)
20
- endpoint.http_fetcher = fetcher_with_config_file('foo.xml', md5, config_endpoint)
20
+ GoConfigEndpoint.http_fetcher = fetcher_with_config_file('foo.xml', md5, config_endpoint)
21
21
 
22
22
  config = endpoint.get_config
23
23
  config.set_pipeline("http://git.example.com/foo/bar.git", "my_rails_app")
@@ -1,5 +1,5 @@
1
1
  require 'test/unit'
2
- require 'stub_http_fetcher'
2
+ require 'go_figure/test'
3
3
 
4
4
  require 'go_figure'
5
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go_figure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70114218851080 !ruby/object:Gem::Requirement
16
+ requirement: &70222388707400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 1.5.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70114218851080
24
+ version_requirements: *70222388707400
25
25
  description: This provides a ruby DSL to create a configuration file for the go server
26
26
  (thoughtworks-studios.com/go)
27
27
  email:
@@ -40,13 +40,14 @@ files:
40
40
  - lib/go_figure/go_config.rb
41
41
  - lib/go_figure/go_config_endpoint.rb
42
42
  - lib/go_figure/http_fetcher.rb
43
+ - lib/go_figure/test.rb
44
+ - lib/go_figure/test/stub_http_fetcher.rb
43
45
  - lib/go_figure/version.rb
44
46
  - test/fixtures/foo.xml
45
47
  - test/go_figure/go_config_endpoint_test.rb
46
48
  - test/go_figure/go_config_test.rb
47
- - test/stub_http_fetcher.rb
48
49
  - test/test_helper.rb
49
- homepage: ''
50
+ homepage: https://github.com/ThoughtWorksInc/go-figure
50
51
  licenses: []
51
52
  post_install_message:
52
53
  rdoc_options: []
@@ -74,5 +75,4 @@ test_files:
74
75
  - test/fixtures/foo.xml
75
76
  - test/go_figure/go_config_endpoint_test.rb
76
77
  - test/go_figure/go_config_test.rb
77
- - test/stub_http_fetcher.rb
78
78
  - test/test_helper.rb
@@ -1,88 +0,0 @@
1
- require 'ostruct'
2
- class StubHttpFetcher
3
- # Careful : this will respond to any call with nil
4
- class HashStruct < OpenStruct
5
- def [](key)
6
- self.headers[key]
7
- end
8
- end
9
-
10
- module Response
11
- def invoked!
12
- @invoked = true
13
- end
14
-
15
- def invoked?
16
- !!@invoked
17
- end
18
- end
19
-
20
- class StringResponse
21
- include Response
22
- def initialize(content, status = 200, headers = {})
23
- @content = content
24
- @status = 200
25
- @headers = headers
26
- end
27
-
28
- def execute
29
- HashStruct.new(:body => @content, :status => @status, :headers => @headers)
30
- end
31
-
32
- end
33
-
34
- class ErrorResponse
35
- include Response
36
- def initialize(error)
37
- @error = error
38
- end
39
-
40
- def execute
41
- raise @error
42
- end
43
- end
44
-
45
- def get(url)
46
- if body = interweb[:get][url]
47
- body.invoked!
48
- return body.execute
49
- else
50
- raise "404 - Could not find #{url}. Available URLs are #{@interweb[:get].keys.inspect}"
51
- end
52
- end
53
-
54
- def post(url, params = {})
55
- if body = interweb[:post][url]
56
- response = body.execute
57
- if response.headers == params
58
- body.invoked!
59
- return response
60
- end
61
- end
62
-
63
- raise "404 - Could not find a url listening to #{url.inspect} that responds to post params #{params.inspect}. Available URLs are #{@interweb[:post].keys.inspect}"
64
- end
65
-
66
- def invoked?(url, type = :get, params = {})
67
- if body = interweb[type][url]
68
- response = body.execute
69
- if response.headers == params
70
- body.invoked?
71
- end
72
- end
73
- end
74
-
75
- def register_content(content, url, type = :get, headers = {})
76
- interweb[type][url] = StringResponse.new(content, 200, headers)
77
- end
78
-
79
- def register_error(url, type = :get, error)
80
- interweb[type][url] = ErrorResponse.new(error)
81
- end
82
-
83
- def interweb
84
- @interweb ||= {:get => {}, :post => {}}
85
- end
86
-
87
- end
88
-