Class: Webdrone::Form
- Inherits:
-
Object
- Object
- Webdrone::Form
- Defined in:
- lib/webdrone/form.rb,
lib/webdrone/logg.rb
Instance Attribute Summary collapse
-
#a0 ⇒ Object
readonly
Returns the value of attribute a0.
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #clic(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
- #find_item(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent) ⇒ Object
- #get(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
-
#initialize(a0) ⇒ Form
constructor
A new instance of Form.
- #mark(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, color: '#af1616', times: nil, delay: nil, shot: nil) ⇒ Object
- #save(filename:, sheet:, item:, name: 'ITEM') ⇒ Object
- #selected?(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Boolean
- #set(key, val, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
- #submit(key = nil, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
- #with_xpath(xpath = nil, &block) ⇒ Object
- #xlsx(sheet: nil, filename: nil) ⇒ Object
Constructor Details
#initialize(a0) ⇒ Form
Returns a new instance of Form.
14 15 16 17 |
# File 'lib/webdrone/form.rb', line 14 def initialize(a0) @a0 = a0 @data = nil end |
Instance Attribute Details
#a0 ⇒ Object (readonly)
Returns the value of attribute a0.
12 13 14 |
# File 'lib/webdrone/form.rb', line 12 def a0 @a0 end |
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/webdrone/form.rb', line 11 def data @data end |
Instance Method Details
#clic(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/webdrone/form.rb', line 125 def clic(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) item = find_item(key, n: n, visible: visible, scroll: scroll, parent: parent) @a0.mark.mark_item item if mark item.click rescue StandardError => error Webdrone.report_error(@a0, error) end |
#find_item(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent) ⇒ Object
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/webdrone/form.rb', line 163 def find_item(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent) @lastitem = \ if @xpath.respond_to? :call @a0.find.xpath @xpath.call(key).to_s, n: n, visible: visible, scroll: scroll, parent: parent elsif @xpath.is_a?(String) && @xpath.include?('%s') @a0.find.xpath sprintf(@xpath, key), n: n, visible: visible, scroll: scroll, parent: parent else @a0.find.xpath Webdrone::XPath.field(key).to_s, n: n, visible: visible, scroll: scroll, parent: parent end end |
#get(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/webdrone/form.rb', line 117 def get(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) item = find_item(key, n: n, visible: visible, scroll: scroll, parent: parent) @a0.mark.mark_item item if mark item[:value] rescue StandardError => error Webdrone.report_error(@a0, error) end |
#mark(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, color: '#af1616', times: nil, delay: nil, shot: nil) ⇒ Object
141 142 143 144 145 |
# File 'lib/webdrone/form.rb', line 141 def mark(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, color: '#af1616', times: nil, delay: nil, shot: nil) @a0.mark.mark_item find_item(key, n: n, visible: visible, scroll: scroll, parent: parent), color: color, times: times, delay: delay, shot: shot rescue StandardError => error Webdrone.report_error(@a0, error) end |
#save(filename:, sheet:, item:, name: 'ITEM') ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/webdrone/form.rb', line 28 def save(filename:, sheet:, item:, name: 'ITEM') prev = @data data = {} @data = data yield ensure File.open(".#{filename}.lock", File::RDWR | File::CREAT, 0o644) do items = {} items[item] = data begin workbook = RubyXL::Parser.parse(filename) worksheet = workbook[sheet] worksheet ||= workbook.add_worksheet sheet rescue StandardError workbook = RubyXL::Workbook.new worksheet = workbook[0] worksheet.sheet_name = sheet end rows = worksheet.sheet_data.rows.collect do |row| row.cells.collect do |cell| cell&.value end end heads = rows.shift || [] rows.each do |row| item = {} key = nil row.each_with_index do |val, i| val = val.to_s if val if i.zero? key = val elsif key items[key] = {} unless items[key] items[key][heads[i]] = val if !heads[i].nil? && items[key][heads[i]].nil? end end end x = heads.shift x ||= name worksheet.add_cell 0, 0, x heads += data.keys.sort heads = heads.uniq heads.each_with_index do |field, coli| worksheet.add_cell 0, coli + 1, field end worksheet.change_row_bold 0, true items.each_with_index do |elem, rowi| key, item = elem worksheet.add_cell rowi + 1, 0, key heads.each_with_index do |field, coli| worksheet.add_cell rowi + 1, coli + 1, item[field] end end workbook.write filename end @data = prev end |
#selected?(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Boolean
133 134 135 136 137 138 139 |
# File 'lib/webdrone/form.rb', line 133 def selected?(key, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) item = find_item(key, n: n, visible: visible, scroll: scroll, parent: parent) @a0.mark.mark_item item if mark item.selected? rescue StandardError => error Webdrone.report_error(@a0, error) end |
#set(key, val, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/webdrone/form.rb', line 93 def set(key, val, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) item = find_item(key, n: n, visible: visible, scroll: scroll, parent: parent) @a0.mark.mark_item item if mark if item.tag_name == 'select' = item.find_elements :xpath, Webdrone::XPath.option(val).to_s raise "option not found for value: #{val} " if .empty? # Take the exact match, or the first one option = .find do |elem| elem.text == val end option ||= .first option.click else item.clear item.send_keys(val) end @data[key] = val if @data nil rescue StandardError => error Webdrone.report_error(@a0, error) end |
#submit(key = nil, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/webdrone/form.rb', line 147 def submit(key = nil, n: 1, visible: true, scroll: false, parent: a0.conf.parent, mark: false) item = find_item(key, n: n, visible: visible, scroll: scroll, parent: parent) if key @a0.mark.mark_item item if mark @lastitem.submit rescue StandardError => error Webdrone.report_error(@a0, error) end |
#with_xpath(xpath = nil, &block) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/webdrone/form.rb', line 19 def with_xpath(xpath = nil, &block) old_xpath, @xpath = @xpath, xpath instance_eval(&block) rescue StandardError => error Webdrone.report_error(@a0, error) ensure @xpath = old_xpath end |
#xlsx(sheet: nil, filename: nil) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/webdrone/form.rb', line 155 def xlsx(sheet: nil, filename: nil) @a0.xlsx.dict(sheet: sheet, filename: filename).each do |k, v| set k, v end rescue StandardError => error Webdrone.report_error(@a0, error) end |