Skip to content

Commit

Permalink
Move classes into their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkyMarkMcDonald committed Mar 17, 2018
1 parent 46ec168 commit 5f06584
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 88 deletions.
93 changes: 5 additions & 88 deletions lib/doc_doc.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require "doc_doc/version"
require 'doc_doc/horse_and_buggy'
require 'doc_doc/quarantine'
require 'doc_doc/house_visit'
require 'doc_doc/patient'
require 'doc_doc/prescription'
require 'doc_doc/treatment'

module DocDoc
def self.prescription(arguments)
Expand All @@ -18,92 +23,4 @@ def self.prescription(arguments)

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
37 changes: 37 additions & 0 deletions lib/doc_doc/house_visit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module DocDoc
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
end
3 changes: 3 additions & 0 deletions lib/doc_doc/patient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module DocDoc
Patient = Struct.new(:home)
end
17 changes: 17 additions & 0 deletions lib/doc_doc/prescription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module DocDoc
class Prescription
def initialize(treatments)
@treatments = treatments
end

def treatments
@treatments
end

def to_s
{
links: treatments
}.to_json.to_s
end
end
end
15 changes: 15 additions & 0 deletions lib/doc_doc/quarantine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module DocDoc
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
end
21 changes: 21 additions & 0 deletions lib/doc_doc/treatment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module DocDoc
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

0 comments on commit 5f06584

Please sign in to comment.