Class: Webdrone::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/webdrone/form.rb,
lib/webdrone/logg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a0) ⇒ Form

Returns a new instance of Form.



11
12
13
14
# File 'lib/webdrone/form.rb', line 11

def initialize(a0)
  @a0 = a0
  @data = nil
end

Instance Attribute Details

#a0Object

Returns the value of attribute a0.



9
10
11
# File 'lib/webdrone/form.rb', line 9

def a0
  @a0
end

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/webdrone/form.rb', line 9

def data
  @data
end

Instance Method Details

#clic(key, n: 1, visible: true) ⇒ Object



111
112
113
114
115
# File 'lib/webdrone/form.rb', line 111

def clic(key, n: 1, visible: true)
  self.find_item(key, n: n, visible: visible).click
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#get(key, n: 1, visible: true) ⇒ Object



105
106
107
108
109
# File 'lib/webdrone/form.rb', line 105

def get(key, n: 1, visible: true)
  self.find_item(key, n: n, visible: visible)[:value]
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#mark(key, n: 1, visible: true, color: '#af1616', times: nil, sleep: nil, shot: nil) ⇒ Object



117
118
119
120
121
# File 'lib/webdrone/form.rb', line 117

def mark(key, n: 1, visible: true, color: '#af1616', times: nil, sleep: nil, shot: nil)
  @a0.mark.mark_item self.find_item(key, n: n, visible: visible), color: color, times: times, sleep: sleep, shot: shot
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#save(filename:, sheet:, item:, name: 'ITEM') ⇒ Object



25
26
27
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
# File 'lib/webdrone/form.rb', line 25

def save(filename:, sheet:, item:, name: 'ITEM')
  prev = @data
  data = {}
  @data = data

  yield
ensure
  File.open(".#{filename}.lock", File::RDWR | File::CREAT, 0644) do |file|
    items = {}
    items[item] = data

    begin
      workbook = RubyXL::Parser.parse(filename)
      worksheet = workbook[sheet]
      worksheet = workbook.add_worksheet sheet unless worksheet
    rescue
      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 if cell != nil
      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 == 0
          key = val
        elsif key
          items[key] = {} unless items[key]
          items[key][heads[i]] = val if heads[i] != nil and 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

#set(key, val, n: 1, visible: true) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/webdrone/form.rb', line 90

def set(key, val, n: 1, visible: true)
  item = self.find_item(key, n: n, visible: visible)
  if item.tag_name == 'select'
    option = item.find_element :xpath, XPath::HTML.option(val).to_s
    option.click
  else
    item.clear
    item.send_keys(val)
  end
  @data[key] = val if @data
  nil
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#submit(key = nil, n: 1, visible: true) ⇒ Object



123
124
125
126
127
128
# File 'lib/webdrone/form.rb', line 123

def submit(key = nil, n: 1, visible: true)
  self.find_item(key, n: n, visible: visible) if key
  @lastitem.submit
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#with_xpath(xpath = nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/webdrone/form.rb', line 16

def with_xpath(xpath = nil, &block)
  old_xpath, @xpath = @xpath, xpath
  instance_eval &block
rescue => exception
  Webdrone.report_error(@a0, exception)
ensure
  @xpath = old_xpath
end

#xlsx(sheet: nil, filename: nil) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/webdrone/form.rb', line 130

def xlsx(sheet: nil, filename: nil)
  @a0.xlsx.dict(sheet: sheet, filename: filename).each do |k, v|
    self.set k, v
  end
rescue => exception
  Webdrone.report_error(@a0, exception)
end