Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b03f863
commit 0a58805
Showing
11 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
class URIExtractor | |||
def initialize(file_name) | |||
@file_name = file_name | |||
contents = File.read(@file_name) | |||
@json = JSON.parse(contents) | |||
end | |||
|
|||
def each(&block) | |||
uris.each(&block) | |||
end | |||
|
|||
private | |||
|
|||
def uris | |||
@uris ||= extract_uris(@json) | |||
end | |||
|
|||
def extract_uris(source) | |||
if source.is_a?(Hash) | |||
extract_uris_from_hash(source) | |||
elsif source.is_a?(Array) | |||
extract_uris_from_array(source) | |||
else | |||
[] | |||
end | |||
end | |||
|
|||
def extract_uris_from_hash(hash) | |||
uris = [] | |||
|
|||
hash.each do |key, value| | |||
if key == 'uri' | |||
uris << value | |||
else | |||
uris += extract_uris(value) | |||
end | |||
end | |||
|
|||
uris | |||
end | |||
|
|||
def extract_uris_from_array(array) | |||
array.map { |o| extract_uris(o) }.flatten | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,38 @@ | |||
require 'httparty' | |||
|
|||
class URIValidator | |||
def initialize(file_name) | |||
@file_name = file_name | |||
end | |||
|
|||
def run | |||
uris.each do |uri| | |||
if valid_uri?(uri) | |||
print_progress_marker | |||
else | |||
print_error_message(uri) | |||
end | |||
end | |||
end | |||
|
|||
private | |||
|
|||
def uris | |||
URIExtractor.new(@file_name) | |||
end | |||
|
|||
def valid_uri?(uri) | |||
response = HTTParty.head(uri) | |||
return response.code == 200 | |||
rescue SocketError | |||
return false | |||
end | |||
|
|||
def print_progress_marker | |||
print '.' | |||
end | |||
|
|||
def print_error_message(uri) | |||
puts "\nERROR: #{uri} cannot be found" | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,24 +1,26 @@ | |||
require 'spec_helper' | require 'spec_helper' | ||
|
|
||
describe JSONValidator, '#run' do | describe JSONValidator, '#run' do | ||
it 'returns a dot if the JSON is valid' do | it 'returns true and outputs a dot if the JSON is valid' do | ||
$stdout = io = StringIO.new | $stdout = io = StringIO.new | ||
dir = File.dirname(__FILE__) | dir = File.dirname(__FILE__) | ||
file_name = File.open(dir + '/fixtures/valid.json') | file_name = File.open(dir + '/fixtures/valid.json') | ||
|
|
||
JSONValidator.new(file_name).run | result = JSONValidator.new(file_name).run | ||
|
|
||
result.should be_true | |||
io.string.should eq '.' | io.string.should eq '.' | ||
end | end | ||
|
|
||
|
|
||
it 'returns an error if the JSON is not valid' do | it 'returns false and outputs an error if the JSON is not valid' do | ||
$stdout = io = StringIO.new | $stdout = io = StringIO.new | ||
dir = File.dirname(__FILE__) | dir = File.dirname(__FILE__) | ||
file_name = File.open(dir + '/fixtures/invalid.json') | file_name = File.open(dir + '/fixtures/invalid.json') | ||
|
|
||
JSONValidator.new(file_name).run | result = JSONValidator.new(file_name).run | ||
|
|
||
result.should be_false | |||
io.string.should =~ /ERROR/ | io.string.should =~ /ERROR/ | ||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
require 'spec_helper' | |||
|
|||
describe URIExtractor, '#each' do | |||
it 'yields each URI' do | |||
file_name = File.expand_path('../fixtures/valid.json', __FILE__) | |||
found_uris = [] | |||
|
|||
URIExtractor.new(file_name).each { |uri| found_uris << uri } | |||
|
|||
expect(found_uris).to eq %w( | |||
http://amzn.to/the-linux-programming-interface | |||
http://en.wikipedia.org/wiki/Redirection_%28computing%29 | |||
) | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,34 @@ | |||
require 'spec_helper' | |||
|
|||
describe URIValidator, '#run' do | |||
it 'outputs a dot for each valid URI' do | |||
stub_request(:head, 'http://example.org') | |||
stub_request(:head, 'http://example.com') | |||
file_name = 'good_json_file.json' | |||
URIExtractor.stubs(:new).with(file_name).returns %w( | |||
http://example.org | |||
http://example.com | |||
) | |||
$stdout = io = StringIO.new | |||
|
|||
URIValidator.new(file_name).run | |||
|
|||
io.string.should eq '..' | |||
end | |||
|
|||
it 'outputs an error for each invalid URI' do | |||
stub_request(:head, 'http://bad.example.org').to_raise(SocketError) | |||
stub_request(:head, 'http://thoughtbot.com/404').to_return(status: 404) | |||
file_name = 'bad_json_file.json' | |||
URIExtractor.stubs(:new).with(file_name).returns %w( | |||
http://bad.example.org | |||
http://thoughtbot.com/404 | |||
) | |||
$stdout = io = StringIO.new | |||
|
|||
URIValidator.new(file_name).run | |||
|
|||
io.string.should =~ %r{ERROR: http://bad\.example\.org} | |||
io.string.should =~ %r{ERROR: http://thoughtbot\.com/404} | |||
end | |||
end |