hot_or_not 0.1.0 → 0.1.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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -6,7 +6,7 @@ module HotOrNot
6
6
  def announce_failure(result)
7
7
  end
8
8
 
9
- def announce_error(url, error)
9
+ def announce_error(result)
10
10
  end
11
11
 
12
12
  def starting
@@ -38,8 +38,8 @@ module HotOrNot
38
38
  print "N"
39
39
  end
40
40
 
41
- def announce_error(url, error)
42
- @results << { :status => :error, :url => url, :error => error }
41
+ def announce_error(result)
42
+ @results << { :status => :error, :result => result }
43
43
  print "E"
44
44
  end
45
45
 
@@ -54,7 +54,7 @@ module HotOrNot
54
54
  end
55
55
 
56
56
  def output_error result_hash
57
- to_console "Error:#{$/}Retreiving #{result_hash[:url].url} raised error: #{result_hash[:error].message}#{$/}#{result_hash[:error].backtrace.join($/)}"
57
+ to_console "Error:#{$/}#{result_hash[:result].message}"
58
58
  end
59
59
 
60
60
  def to_console(message)
@@ -1,17 +1,11 @@
1
1
  module HotOrNot
2
2
  class ComparisonResult
3
3
 
4
- DEFAULT_OPTIONS = { :method => :get }
5
4
  class << self
6
5
  def for(compare_url)
7
- options = DEFAULT_OPTIONS.merge compare_url.options
8
- new compare_url, retreive(compare_url.side_a, options), retreive(compare_url.side_b, options)
9
- end
10
-
11
- private
12
- def retreive(url, options)
13
- options[:url] = url
14
- RestClient::Request.execute(options).tap { |r| raise Exception.new("Invalid response code #{r.code} for '#{url}'") unless r.code.to_s == '200' }
6
+ side_a_result = UrlResult.retrieve_for compare_url.side_a, compare_url.options
7
+ side_b_result = UrlResult.retrieve_for compare_url.side_b, compare_url.options
8
+ new compare_url, side_a_result, side_b_result
15
9
  end
16
10
  end
17
11
 
@@ -20,11 +14,17 @@ module HotOrNot
20
14
  def initialize(compare_url, side_a_results, side_b_results)
21
15
  @compare_url, @side_a_results, @side_b_results = compare_url, side_a_results, side_b_results
22
16
  @message, @diff = '', ''
23
- init
17
+ init_message unless success?
24
18
  end
25
19
 
26
20
  def success?
27
- @success
21
+ !error? &&
22
+ @side_a_results.code == @side_b_results.code &&
23
+ side_a_body == side_b_body
24
+ end
25
+
26
+ def error?
27
+ @side_a_results.error? || @side_b_results.error?
28
28
  end
29
29
 
30
30
  def side_a_body
@@ -46,17 +46,24 @@ module HotOrNot
46
46
  end
47
47
 
48
48
  private
49
- def init
50
- return if @success = side_a_body == side_b_body
51
- @message = "#{@compare_url.full_name}: #{@compare_url.url}: Body from #{@compare_url.base_a} did not match body from #{@compare_url.base_b}"
52
- @diff = Diffy::Diff.new(side_a_body, side_b_body)
49
+ def init_message
50
+ @message = if error?
51
+ message = "#{@compare_url.full_name}: #{@compare_url.url}: Error retrieving body"
52
+ message += "#{$/} #{@compare_url.base_a} => #{@side_a_results.error_message}" if @side_a_results.error?
53
+ message += "#{$/} #{@compare_url.base_b} => #{@side_b_results.error_message}" if @side_b_results.error?
54
+ message
55
+ else
56
+ @diff = Diffy::Diff.new(side_a_body, side_b_body)
57
+ "#{@compare_url.full_name}: #{@compare_url.url}: Body from #{@compare_url.base_a} did not match body from #{@compare_url.base_b}"
58
+ end
53
59
  end
54
60
 
55
61
  def body_by_content_type(result)
56
62
  case result.headers[:content_type]
57
63
  when /json/i
58
- h = JSON.parse result.body
59
- JSON.pretty_generate h
64
+ json = JSON.parse result.body
65
+ json.sort!
66
+ JSON.pretty_generate json
60
67
  else
61
68
  result.body
62
69
  end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def <=> other
3
+ self.sort.to_s <=> other.sort.to_s
4
+ end
5
+ end
@@ -11,15 +11,13 @@ module HotOrNot
11
11
  def run!
12
12
  @announcer.starting
13
13
  @urls.each do |url|
14
- begin
15
- result = ComparisonResult.for url
16
- if result.success?
17
- @announcer.announce_success result
18
- else
19
- @announcer.announce_failure result
20
- end
21
- rescue StandardError, Exception => e
22
- @announcer.announce_error url, e
14
+ result = ComparisonResult.for url
15
+ if result.success?
16
+ @announcer.announce_success result
17
+ elsif result.error?
18
+ @announcer.announce_error result
19
+ else
20
+ @announcer.announce_failure result
23
21
  end
24
22
  end
25
23
  @announcer.ending
@@ -0,0 +1,43 @@
1
+ module HotOrNot
2
+ class UrlResult
3
+ extend Forwardable
4
+
5
+ DEFAULT_OPTIONS = { :method => :get }
6
+
7
+ class << self
8
+ def retrieve_for(url, options)
9
+ options = DEFAULT_OPTIONS.merge options
10
+ options[:url] = url
11
+ response, error = nil, nil
12
+ begin
13
+ response = RestClient::Request.execute(options)
14
+ rescue RestClient::Exception => e
15
+ error = e
16
+ end
17
+
18
+ new url, response, error
19
+ end
20
+ end
21
+
22
+ attr_reader :url
23
+ def_delegators :@response, :code, :body, :headers
24
+
25
+ def initialize(url, response, error)
26
+ @url, @error = url, error
27
+ @response = response || error.response
28
+ end
29
+
30
+ def success?
31
+ @error.nil?
32
+ end
33
+
34
+ def error?
35
+ !success?
36
+ end
37
+
38
+ def error_message
39
+ "The url #{@url} received error: #{@error.message}"
40
+ end
41
+
42
+ end
43
+ end
data/lib/hot_or_not.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require 'forwardable'
2
3
 
3
4
  require 'rest-client'
4
5
  require 'diffy'
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
2
 
3
3
  module HotOrNot
4
4
  class CompareUrlTest < Test::Unit::TestCase
@@ -62,7 +62,7 @@ module HotOrNot
62
62
  @compare_url = CompareUrl.new 'Foo Url', '/api/foo', 'http://side_a', 'http://side_b', :headers => { 'authorization' => 'baz321' }
63
63
  end
64
64
 
65
- should "read provid options as passed in" do
65
+ should "read provided options as passed in" do
66
66
  expected_options = { :headers => { 'authorization' => 'baz321' } }
67
67
  assert_equal expected_options, @compare_url.options
68
68
  end
@@ -1,51 +1,99 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
2
 
3
3
  module HotOrNot
4
4
  class ComparisonResultTest < Test::Unit::TestCase
5
- context "comparing results" do
5
+ context "" do
6
6
  setup do
7
7
  @compare_url = CompareUrl.new 'Test People', '/api/people', 'http://side_a', 'http://side_b'
8
8
  end
9
9
 
10
- context "500 error" do
11
- setup do
12
- RestClient::Request.expects(:execute).with(:method => :get, :url => @compare_url.side_a).returns FakeResponse.new('foo', 500)
13
- end
14
-
15
- should "raise error when response code is not 200" do
16
- assert_raises(Exception) { ComparisonResult.for @compare_url }
17
- end
18
- end
19
-
20
- context "when results match" do
10
+ context "building from compare url" do
21
11
  setup do
12
+ @compare_url = CompareUrl.new 'Test People', '/api/people', 'http://side_a', 'http://side_b'
22
13
  RestClient::Request.expects(:execute).with(:method => :get, :url => @compare_url.side_a).returns FakeResponse.new 'foo'
23
14
  RestClient::Request.expects(:execute).with(:method => :get, :url => @compare_url.side_b).returns FakeResponse.new 'foo'
24
- @result = ComparisonResult.for @compare_url
25
15
  end
26
16
 
27
- should "return success" do
28
- assert @result.success?
17
+ should "not throw an exception when building" do
18
+ assert_nothing_raised { ComparisonResult.for @compare_url }
29
19
  end
30
20
  end
31
21
 
32
- context "when results don't match" do
33
- setup do
34
- RestClient::Request.expects(:execute).with(:method => :get, :url => @compare_url.side_a).returns FakeResponse.new 'side_a'
35
- RestClient::Request.expects(:execute).with(:method => :get, :url => @compare_url.side_b).returns FakeResponse.new 'side_b'
36
- @result = ComparisonResult.for @compare_url
37
- end
22
+ context "comparing results" do
23
+
24
+ context "when results match" do
25
+ context "and are strings" do
26
+ setup do
27
+ response = FakeResponse.new 'foo'
28
+ side_a_results = UrlResult.new @compare_url.side_a, response, nil
29
+ side_b_results = UrlResult.new @compare_url.side_b, response, nil
30
+ @result = ComparisonResult.new @compare_url, side_a_results, side_b_results
31
+ end
38
32
 
39
- should "not return success" do
40
- assert_not @result.success?
33
+ should "return success" do
34
+ assert @result.success?
35
+ end
36
+ end
37
+
38
+ context "and are json" do
39
+ setup do
40
+ hash1 = {'a'=>1,'b'=>2}
41
+ hash2 = {'a'=>3,'b'=>4}
42
+ response_a = FakeResponse.new [hash1, hash2].to_json, 200, :content_type => 'application/json'
43
+ response_b = FakeResponse.new [hash2, hash1].to_json, 200, :content_type => 'application/json'
44
+ side_a_results = UrlResult.new @compare_url.side_a, response_a, nil
45
+ side_b_results = UrlResult.new @compare_url.side_b, response_b, nil
46
+ @result = ComparisonResult.new @compare_url, side_a_results, side_b_results
47
+ end
48
+
49
+ should "return success" do
50
+ assert @result.success?
51
+ end
52
+ end
41
53
  end
42
54
 
43
- should "have test unit style failure message" do
44
- assert_match @result.message, /Test People:.*?\/api\/people/
55
+ context "when results don't match" do
56
+ setup do
57
+ response_a = FakeResponse.new 'side_a'
58
+ response_b = FakeResponse.new 'side_b'
59
+ side_a_results = UrlResult.new @compare_url.side_a, response_a, nil
60
+ side_b_results = UrlResult.new @compare_url.side_b, response_b, nil
61
+ @result = ComparisonResult.new @compare_url, side_a_results, side_b_results
62
+ end
63
+
64
+ should "not return success" do
65
+ assert_not @result.success?
66
+ end
67
+
68
+ should "have test unit style failure message" do
69
+ assert_match /Test People:.*?\/api\/people/, @result.message
70
+ end
71
+
72
+ should "provide a diff" do
73
+ assert_present @result.diff
74
+ end
45
75
  end
46
76
 
47
- should "provide a diff" do
48
- assert_present @result.diff
77
+ context "when one result has an error" do
78
+ setup do
79
+ response_a = FakeResponse.new 'side_a'
80
+ error_b = RestClient::InternalServerError.new
81
+ side_a_results = UrlResult.new @compare_url.side_a, response_a, nil
82
+ side_b_results = UrlResult.new @compare_url.side_b, nil, error_b
83
+ @result = ComparisonResult.new @compare_url, side_a_results, side_b_results
84
+ end
85
+
86
+ should "not return success" do
87
+ assert_not @result.success?
88
+ end
89
+
90
+ should "return error" do
91
+ assert @result.error?
92
+ end
93
+
94
+ should "have a message containing the error" do
95
+ assert_match /#{@compare_url.side_b}.*?: Internal Server Error/, @result.message
96
+ end
49
97
  end
50
98
  end
51
99
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
2
 
3
3
  module HotOrNot
4
4
  class ConsoleAnnouncerTest < Test::Unit::TestCase
data/test/runner_test.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
2
 
3
3
  module HotOrNot
4
4
  class RunnerTest < Test::Unit::TestCase
@@ -27,7 +27,7 @@ module HotOrNot
27
27
  @messages[:failure] = true
28
28
  end
29
29
 
30
- def announce_error(url, error)
30
+ def announce_error(result)
31
31
  @messages[:error] = true
32
32
  end
33
33
  end.new
@@ -77,7 +77,7 @@ module HotOrNot
77
77
  end
78
78
  end
79
79
 
80
- context "error during comparison" do
80
+ context "error during retreival" do
81
81
  setup do
82
82
  urls = [mock_compare_url('Foo', '/api/foo', 'foo', 'bar', 404)]
83
83
  Runner.new(urls, @announcer).run!
data/test/test_helper.rb CHANGED
@@ -26,7 +26,7 @@ module HotOrNot
26
26
  end
27
27
 
28
28
  class Test::Unit::TestCase
29
- def assert_false(truth, message=nil)
29
+ def assert_false(truth, message='')
30
30
  assert !truth, message
31
31
  end
32
32
  alias_method :assert_not, :assert_false
@@ -37,10 +37,24 @@ class Test::Unit::TestCase
37
37
  end
38
38
 
39
39
  private
40
- def mock_compare_url(name, url, body_a, body_b, code='200')
40
+ def mock_compare_url(name, url, body_a, body_b, code_a=200, code_b=200)
41
41
  HotOrNot::CompareUrl.new(name, url, 'http://side_a', 'http://side_b').tap do |compare_url|
42
- RestClient::Request.expects(:execute).with(:method => :get, :url => compare_url.side_a).returns HotOrNot::FakeResponse.new(body_a, code)
43
- RestClient::Request.expects(:execute).with(:method => :get, :url => compare_url.side_b).returns HotOrNot::FakeResponse.new(body_b, code) if code.to_s == '200'
42
+ response_a, error_a = response_and_error_for body_a, code_a
43
+ response_b, error_b = response_and_error_for body_b, code_b
44
+ side_a_result = HotOrNot::UrlResult.new compare_url.side_a, response_a, error_a
45
+ side_b_result = HotOrNot::UrlResult.new compare_url.side_b, response_b, error_b
46
+ HotOrNot::ComparisonResult.expects(:for).with(compare_url).returns HotOrNot::ComparisonResult.new(compare_url, side_a_result, side_b_result)
44
47
  end
45
48
  end
49
+
50
+ def response_and_error_for(body, code)
51
+ response, error = nil, nil
52
+ code = code.to_i
53
+ if code == 200
54
+ response = HotOrNot::FakeResponse.new body
55
+ else
56
+ error = RestClient::Exceptions::EXCEPTIONS_MAP[code].new
57
+ end
58
+ [response, error]
59
+ end
46
60
  end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
+
3
+ module HotOrNot
4
+ class UrlResultTest < Test::Unit::TestCase
5
+ context "error" do
6
+ setup do
7
+ @url = 'http://url/request/test'
8
+ exception = RestClient::InternalServerError.new
9
+ RestClient::Request.expects(:execute).with(:method => :get, :url => @url).raises exception
10
+ end
11
+
12
+ should "not raise error on 500" do
13
+ assert_nothing_raised { UrlResult.retrieve_for @url, {} }
14
+ end
15
+
16
+ should "report as error" do
17
+ result = UrlResult.retrieve_for @url, {}
18
+ assert result.error?
19
+ assert_false result.success?
20
+ end
21
+
22
+ should "report the error message" do
23
+ result = UrlResult.retrieve_for @url, {}
24
+ assert_match /error: Internal Server Error/, result.error_message
25
+ end
26
+ end
27
+
28
+ context "successfull response" do
29
+ setup do
30
+ @url = 'http://url/request/test'
31
+ RestClient::Request.expects(:execute).with(:method => :get, :url => @url).returns FakeResponse.new 'hello', 200
32
+ @result = UrlResult.retrieve_for @url, {}
33
+ end
34
+
35
+ should "report success" do
36
+ assert @result.success?
37
+ end
38
+
39
+ should "return the body" do
40
+ assert_equal 'hello', @result.body
41
+ end
42
+
43
+ should "return the http response code" do
44
+ assert_equal 200, @result.code
45
+ end
46
+
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hot_or_not
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joel Friedman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-08 00:00:00 -05:00
18
+ date: 2011-06-16 00:00:00 -05:00
19
19
  default_executable: hot_or_not
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -163,7 +163,9 @@ files:
163
163
  - lib/hot_or_not/announcers/console_announcer.rb
164
164
  - lib/hot_or_not/compare_url.rb
165
165
  - lib/hot_or_not/comparison_result.rb
166
+ - lib/hot_or_not/ext/hash.rb
166
167
  - lib/hot_or_not/runner.rb
168
+ - lib/hot_or_not/url_result.rb
167
169
  - test/compare_url_test.rb
168
170
  - test/comparison_result_test.rb
169
171
  - test/console_announcer_test.rb
@@ -171,6 +173,7 @@ files:
171
173
  - test/data/simple_urls.yml.erb
172
174
  - test/runner_test.rb
173
175
  - test/test_helper.rb
176
+ - test/url_result_test.rb
174
177
  has_rdoc: true
175
178
  homepage: https://github.com/joelash/hot_or_not
176
179
  licenses: