Class: Pincers::Nokogiri::Backend

Inherits:
Core::BaseBackend show all
Defined in:
lib/pincers/nokogiri/backend.rb

Direct Known Subclasses

Chenso::Backend

Constant Summary collapse

BOOL_PROPERTIES =

This is a small bool properties subset, I believe its enough for scrapping. For information of where to find the full list: stackoverflow.com/questions/706384/boolean-html-attributes

{
  checked: [:input_checkbox, :input_radio],
  selected: [:option],
  disabled: :all, # no restrictions
  readonly: [:input_text, :input_password, :textarea],
  multiple: [:select]
}

Instance Method Summary collapse

Methods inherited from Core::BaseBackend

#as_http_client, #clear_input, #click_on_element, #document_url, #double_click_on_element, #drag_and_drop, #element_is_actionable?, #elements_equal, #fetch_cookies, #fetch_resource, #hover_over_element, #javascript_enabled?, #merge_http_client, #navigate_back, #navigate_forward, #navigate_to, #refresh_document, #right_click_on_element, #set_element_text, #submit_form, #switch_to_frame, #switch_to_parent_frame, #switch_to_top_frame

Constructor Details

#initialize(_document) ⇒ Backend

Returns a new instance of Backend.



17
18
19
# File 'lib/pincers/nokogiri/backend.rb', line 17

def initialize(_document)
  @document = _document
end

Instance Method Details

#close_documentObject



33
34
35
# File 'lib/pincers/nokogiri/backend.rb', line 33

def close_document
  # no closing needed
end

#documentObject



21
22
23
# File 'lib/pincers/nokogiri/backend.rb', line 21

def document
  @document
end

#document_rootObject



25
26
27
# File 'lib/pincers/nokogiri/backend.rb', line 25

def document_root
  [document]
end

#document_titleObject



29
30
31
# File 'lib/pincers/nokogiri/backend.rb', line 29

def document_title
  document.title
end

#extract_element_attribute(_element, _name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pincers/nokogiri/backend.rb', line 59

def extract_element_attribute(_element, _name)
  _name = _name.to_sym
  if _name == :value
    case classify _element
    when :input_checkbox, :input_radio
      extract_checkable_value _element
    when :select
      extract_select_value _element
    when :option
      extract_option_value _element
    when :textarea
      _element.content
    else
      _element[:value]
    end
  elsif is_boolean? _element, _name
    !_element[_name].nil?
  else
    _element[_name]
  end
end

#extract_element_html(_element) ⇒ Object



55
56
57
# File 'lib/pincers/nokogiri/backend.rb', line 55

def extract_element_html(_element)
  _element.to_html
end

#extract_element_tag(_element) ⇒ Object



47
48
49
# File 'lib/pincers/nokogiri/backend.rb', line 47

def extract_element_tag(_element)
  _element.name
end

#extract_element_text(_element) ⇒ Object



51
52
53
# File 'lib/pincers/nokogiri/backend.rb', line 51

def extract_element_text(_element)
  _element.content
end

#search_by_css(_element, _selector, _limit) ⇒ Object



37
38
39
40
# File 'lib/pincers/nokogiri/backend.rb', line 37

def search_by_css(_element, _selector, _limit)
  # nokogiri does not do any query level optimization when searching just one node
  _element.css _selector
end

#search_by_xpath(_element, _selector, _limit) ⇒ Object



42
43
44
45
# File 'lib/pincers/nokogiri/backend.rb', line 42

def search_by_xpath(_element, _selector, _limit)
  # nokogiri does not do any query level optimization when searching just one node
  _element.xpath _selector
end

#set_element_attribute(_element, _name, _value) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pincers/nokogiri/backend.rb', line 81

def set_element_attribute(_element, _name, _value)
  _name = _name.to_sym

  if _name == :value
    case classify _element
    when :select
      set_select_value _element, _value
    when :textarea
      _element.content = _value
    else
      _element.set_attribute(_name, _value)
    end
  elsif is_boolean? _element, _name
    set_boolean _element, _name, _value
  else
    _element.set_attribute(_name, _value)
  end
end