Skip to content

Commit

Permalink
Initial skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkyMarkMcDonald committed Mar 17, 2018
1 parent 9be40e5 commit 46ec168
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 11 deletions.
51 changes: 51 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
PATH
remote: .
specs:
doc_doc (0.1.0)
capybara
poltergeist

GEM
remote: https://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
capybara (2.18.0)
addressable
mini_mime (>= 0.1.3)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (>= 2.0, < 4.0)
cliver (0.3.2)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
poltergeist (1.17.0)
capybara (~> 2.1)
cliver (~> 0.3.1)
websocket-driver (>= 0.2.0)
public_suffix (3.0.2)
rack (2.0.4)
rack-test (0.8.3)
rack (>= 1.0, < 3)
rake (10.5.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
xpath (3.0.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.16.a)
doc_doc!
minitest (~> 5.0)
rake (~> 10.0)

BUNDLED WITH
1.16.0.pre.2
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ $ doc_doc 'https://github.com/SeleniumHQ/selenium/wiki/Logging' --stay-within 'h
{
"links": [
{
"parentText": "Please see the capabilities page for more information about Firefox specific capabilities.",
"linkText": "capabilities page",
"href": "../DesiredCapabilities",
"page": "https://github.com/SeleniumHQ/selenium/wiki/Logging",
"href": "../DesiredCapabilities",
"error": {
"type": "http",
"status": 404
Expand Down
5 changes: 5 additions & 0 deletions bin/doc_doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require 'doc_doc'
puts DocDoc.prescription(ARGV)

7 changes: 5 additions & 2 deletions doc_doc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Gem::Specification.new do |spec|
spec.authors = ["Mark McDonald"]
spec.email = ["[email protected]"]

spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.summary = %q{Helps keep links in documentation up to date}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"

Expand All @@ -28,8 +27,12 @@ Gem::Specification.new do |spec|
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.executables << 'doc_doc'
spec.require_paths = ["lib"]

spec.add_runtime_dependency "capybara"
spec.add_runtime_dependency "poltergeist"

spec.add_development_dependency "bundler", "~> 1.16.a"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
Expand Down
33 changes: 33 additions & 0 deletions example_documentation_site/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html>
<body>
<h2>Happy Paths:</h2>
<ul>
<li>
<a href="page-that-exists.html">Page that exists</a>
</li>
<li>
<a href="page_with_url_fragment.html#what-you-expected">Just one url fragment</a>
</li>
<li>
<a href="https://www.example.com">External site</a>
</li>
</ul>


<h2>Sad Paths:</h2>
<ul>
<li>
<a href="some-page-that-does-not-exist.html">Page that does not exist anymore</a>
</li>
<li>
<a href="page_with_too_many_of_the_same_url_fragment.html#what-you-expected">Too many of the same url fragments</a>
</li>
<li>
<a href="page_without_url_fragment.html#what-you-expected">No url fragments</a>
</li>
<li>
<a href="https://www.nonexistant.example/">External non-existent site</a>
</li>
</ul>
</body>
</html>
5 changes: 5 additions & 0 deletions example_documentation_site/page-that-exists.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>I exist!</h2>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
<h2 id="what-you-expected">I have the url fragment you expected more than once!</h2>
<h2 id="what-you-expected">I have the url fragment you expected more than once!</h2>
<h2 id="what-you-expected">I have the url fragment you expected more than once!</h2>
<h2 id="what-you-expected">I have the url fragment you expected more than once!</h2>
</body>
</html>
6 changes: 6 additions & 0 deletions example_documentation_site/page_with_url_fragment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<div style="height: 4000px"></div>
<h2 id="what-you-expected">I have the url fragment you expected!</h2>
</body>
</html>
6 changes: 6 additions & 0 deletions example_documentation_site/page_without_url_fragment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<div style="height: 4000px"></div>
<h2 id="not-what-you-were-looking for">I don't have the id you were expecting</h2>
</body>
</html>
106 changes: 105 additions & 1 deletion lib/doc_doc.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,109 @@
require "doc_doc/version"
require 'doc_doc/horse_and_buggy'

module DocDoc
# Your code goes here...
def self.prescription(arguments)
danger_zone = arguments[0]
throttle = arguments[1]

horse_and_buggy = HorseAndBuggy.new(throttle)
patients = Quarantine.new(horse_and_buggy, danger_zone).patients

treatments = patients.map do |patient|
visit = HouseVisit.new(horse_and_buggy, patient, danger_zone)
visit.start
illness = visit.illness
Treatment.new(patient, illness, visit) if illness
end.compact

Prescription.new(treatments)
end

Patient = Struct.new(:home)

class Quarantine
def initialize(horse_and_buggy, danger_zone)
@horse_and_buggy = horse_and_buggy
@danger_zone = danger_zone
end

def patients
@horse_and_buggy.visit(@danger_zone)
@horse_and_buggy.all('a').map do |link|
Patient.new(link['href'])
end.uniq(&:home)
end
end

class HouseVisit
def initialize(horse_and_buggy, patient, starting_location)
@horse_and_buggy = horse_and_buggy
@patient = patient
@starting_location = starting_location
end

attr_reader :starting_location

def start
@horse_and_buggy.visit_house(@patient.home)
if @horse_and_buggy.status_code >= 400
@illness = OpenStruct.new(type: 'http', status: @horse_and_buggy.status_code)
end
rescue Capybara::Poltergeist::StatusFailError
@illness = OpenStruct.new(type: 'http', description: 'Could not reach server')
end

def illness
@illness ||= begin
if @patient.home && @patient.home.match(/#/)
hyper_fragment = @patient.home.split('#').last
begin
nil if @horse_and_buggy.find("##{hyper_fragment}")
rescue Capybara::Ambiguous
OpenStruct.new(type: 'fragment', description: 'More than one dom node has this id')
rescue Capybara::ElementNotFound
OpenStruct.new(type: 'fragment', description: 'No dom node with this id found')
end
else
nil
end
end
end
end

class Prescription
def initialize(treatments)
@treatments = treatments
end

def treatments
@treatments
end

def to_s
{
links: treatments
}.to_json.to_s
end
end

class Treatment
def initialize(patient, illness, house_visit)
@patient = patient
@illness = illness
@house_visit = house_visit
end

def as_json
{
page: @house_visit.starting_location,
href: @patient.home,
error: @illness.to_h
}
end

def to_json(_)
as_json.to_json
end
end
end
30 changes: 30 additions & 0 deletions lib/doc_doc/horse_and_buggy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'capybara/dsl'
require 'capybara/poltergeist'

module DocDoc
class HorseAndBuggy
include Capybara::DSL
SECONDS = 1

def initialize(throttle)
@throttle = throttle

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(
app,
timeout: 15 * SECONDS,
phantomjs_options: ['--load-images=no'],
)
end
Capybara.default_driver = :poltergeist
Capybara.match = :one
end

def visit_house(href)
if @throttle
sleep @throttle
end
visit(href)
end
end
end
59 changes: 54 additions & 5 deletions test/doc_doc_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
require "test_helper"
require "webrick/httpserver"

class DocDocTest < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::DocDoc::VERSION
end

def test_it_does_something_useful
assert false
@server = WEBrick::HTTPServer.new(
BindAddress: 'localhost',
Port: 0,
DocumentRoot: __dir__ + '/../example_documentation_site'
)
Thread.new do
@server.start
end
sleep 1

quarantine_entrance = "http://localhost:#{@server.config[:Port]}"
prescription = DocDoc.prescription([quarantine_entrance, nil])

expected_prescription = {
"links" => [
{
"page" => quarantine_entrance,
"href" => "#{quarantine_entrance}/some-page-that-does-not-exist.html",
"error" => {
"type" => 'http',
"status" => 404
}
},
{
"page" => quarantine_entrance,
"href" => "#{quarantine_entrance}/page_with_too_many_of_the_same_url_fragment.html#what-you-expected",
"error" => {
"type" => "fragment",
"description" => "More than one dom node has this id"
}
},
{
"page" => quarantine_entrance,
"href" => "#{quarantine_entrance}/page_without_url_fragment.html#what-you-expected",
"error" => {
"type" => "fragment",
"description" => "No dom node with this id found"
}
},
{
"page" => quarantine_entrance,
"href" => "https://www.nonexistant.example/",
"error" => {
"type" => "http",
"description" => "Could not reach server"
}
}
]
}

assert_equal(expected_prescription, JSON.parse(prescription.to_s))
ensure
@server.shutdown
end
end

0 comments on commit 46ec168

Please sign in to comment.