Class: Fracture

Inherits:
Object
  • Object
show all
Defined in:
lib/fracture/fracture.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, items, is_text, is_path = false) ⇒ Fracture

Returns a new instance of Fracture.



6
7
8
9
10
11
# File 'lib/fracture/fracture.rb', line 6

def initialize label, items, is_text, is_path=false
  self.label = label
  self.items = Array(items)
  self.is_text = is_text
  self.is_path = is_path
end

Instance Attribute Details

#is_pathObject

Returns the value of attribute is_path.



4
5
6
# File 'lib/fracture/fracture.rb', line 4

def is_path
  @is_path
end

#is_textObject

Returns the value of attribute is_text.



4
5
6
# File 'lib/fracture/fracture.rb', line 4

def is_text
  @is_text
end

#itemsObject

Returns the value of attribute items.



4
5
6
# File 'lib/fracture/fracture.rb', line 4

def items
  @items
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/fracture/fracture.rb', line 4

def label
  @label
end

Class Method Details

.allObject



48
49
50
# File 'lib/fracture/fracture.rb', line 48

def self.all
  @all || {}
end

.all_keys_less(fractures) ⇒ Object



68
69
70
# File 'lib/fracture/fracture.rb', line 68

def self.all_keys_less fractures
  all.keys - list_to_s(Array(fractures).flatten)
end

.clearObject



52
53
54
# File 'lib/fracture/fracture.rb', line 52

def self.clear
  @all = {}
end

.define_path(label, *items) ⇒ Object



26
27
28
29
30
31
# File 'lib/fracture/fracture.rb', line 26

def self.define_path label, *items
  @all ||= {}
  raise "#{label} has already been defined" if @all[label.to_s]
  #items = ["##{label}"] if items.empty?
  @all[label.to_s] = self.new(label, items.flatten, false, true)
end

.define_selector(label, *items) ⇒ Object



19
20
21
22
23
24
# File 'lib/fracture/fracture.rb', line 19

def self.define_selector label, *items
  @all ||= {}
  raise "#{label} has already been defined" if @all[label.to_s]
  items = ["##{label}"] if items.empty?
  @all[label.to_s] = self.new(label, items.flatten, false)
end

.define_text(label, *items) ⇒ Object



13
14
15
16
17
# File 'lib/fracture/fracture.rb', line 13

def self.define_text label, *items
  @all ||= {}
  raise "#{label} has already been defined" if @all[label.to_s]
  @all[label.to_s] = self.new(label, items.flatten, true)
end

.find(label) ⇒ Object



41
42
43
44
45
46
# File 'lib/fracture/fracture.rb', line 41

def self.find label
  raise 'No Fractures have been defined' if all.empty?
  ret = all[label.to_s]
  raise "Fracture with Label of '#{label}' was not found" unless ret
  ret
end

.get_body(page) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fracture/fracture.rb', line 132

def self.get_body(page)
  case
    when page.respond_to?(:response) # Controller
      page.response.body
    when page.respond_to?(:body)
      page.body
    when page.kind_of?(String)
      page
    else
      raise 'Page sent is not valid'
  end
end

.have_all_except_test(page, is_not, except_fractures) ⇒ Object



64
65
66
# File 'lib/fracture/fracture.rb', line 64

def self.have_all_except_test page, is_not, except_fractures
  test_fractures page, is_not, all_keys_less(except_fractures), except_fractures
end

.have_only_test(page, is_not, only_fractures) ⇒ Object



60
61
62
# File 'lib/fracture/fracture.rb', line 60

def self.have_only_test page, is_not, only_fractures
  test_fractures page, is_not, only_fractures, all_keys_less(only_fractures)
end

.list_to_s(items) ⇒ Object



56
57
58
# File 'lib/fracture/fracture.rb', line 56

def self.list_to_s items
  items.map { |item| item.to_s }
end


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fracture/fracture.rb', line 82

def self.match_link page_parsed, link
  items = link.split('_')
  items.pop
  case
    when items.last.pluralize == items.last
      items.join('\/d+\/')
      page_parsed.css('a[href')
  end



end

.test_fractures(page, is_not, fracture_labels, reverse_fracture_labels = []) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fracture/fracture.rb', line 95

def self.test_fractures(page, is_not, fracture_labels, reverse_fracture_labels=[])
  page_parsed = Nokogiri::HTML.parse( self.get_body(page) )
  failures = {}
  failures[:should] = []
  failures[:should_not] = []
  Array(fracture_labels).flatten.each do |fracture_label|
    fracture = Fracture.find(fracture_label)
    fracture.items.each do |label|
      if is_not
        if fracture.do_check(page_parsed, label)
          failures[:should_not] << {fracture_label: fracture_label, label: label}
        end
      else
        unless fracture.do_check(page_parsed, label)
          failures[:should] << {fracture_label: fracture_label, label: label}
        end
      end
    end
  end
  Array(reverse_fracture_labels).flatten.each do |fracture_label|
    fracture = Fracture.find(fracture_label)
    fracture.items.each do |label|
      unless is_not
        if fracture.do_check(page_parsed, label)
          failures[:should_not] << {fracture_label: fracture_label, label: label}
        end
      else
        unless fracture.do_check(page_parsed, label)
          failures[:should] << {fracture_label: fracture_label, label: label}
        end
      end
    end
  end
  failures.merge!(passed: (failures[:should].empty? && failures[:should_not].empty?))
  failures
end

Instance Method Details

#do_check(page_parsed, label) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/fracture/fracture.rb', line 72

def do_check page_parsed, label
  # page_parsed = Nokogiri::HTML.parse(page)

  if text?
    page_parsed.text.include?(label)
  else
    page_parsed.at label
  end
end

#path?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/fracture/fracture.rb', line 37

def path?
  !!is_path
end

#text?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/fracture/fracture.rb', line 33

def text?
  !!is_text
end