Class: FormReader

Inherits:
Object show all
Defined in:
lib/web/formreader.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: FormElements, Option, Select

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ FormReader

Returns a new instance of FormReader.



6
7
8
9
10
# File 'lib/web/formreader.rb', line 6

def initialize( html )
  @html = html
  @parsed = nil
  @url = ""
end

Instance Attribute Details

#formsObject

Returns the value of attribute forms.



5
6
7
# File 'lib/web/formreader.rb', line 5

def forms
  @forms
end

#htmlObject Also known as: body

Returns the value of attribute html.



2
3
4
# File 'lib/web/formreader.rb', line 2

def html
  @html
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
23
24
# File 'lib/web/formreader.rb', line 20

def [] (key)
  # ugly ugly ugly
  form_elements( key )
  forms[key]
end

#form_elements(name) ⇒ Object



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
# File 'lib/web/formreader.rb', line 26

def form_elements( name )
  unless( forms[name] )
    html_tree.get_elements("form").each{ |form|
      if (name == form.attribute("name"))
        forms[name] ||= FormElements.new

        forms[name].action = form.attribute("action")

        form.get_elements("input").each{ |e|
          if ["text","hidden","submit","password","file"].include? e.attribute("type") or e.attribute("type").nil?
            forms[name].push_text( e.attribute("name"),
                                   Web.unescape(e.attribute("value")) || "" )
          elsif e.attribute("type") == "checkbox"
            # boolean checkboxes
            #   - assumed if there is no value
            if (e.attribute("value") == "" || e.attribute("value").nil?)
              forms[name].push( e.attribute("name"),
                                if (e.attribute("checked"))
                                  true
                                else
                                  false
                                end )
            else
              # multi select checkboxes
              select = forms[name].find_select( e.attribute("name") )
              select.multiple = true
              select.push_option(e)
            end
          elsif e.attribute("type") == "radio"
            select = forms[name].find_select( e.attribute("name") )
            select.multiple = false
            select.push_option(e)
          elsif e.attribute("type") == "file"
          end
        }
        form.get_elements("textarea").each{ |e|
          forms[name].push_text e.attribute("name"), e.content.to_s.strip
        }
        form.get_elements("select").each { |e|
          select = Select.new
          select.multiple = e.attribute("multiple") != nil
          e.get_elements("option").each{ |option|
            select.shift_option( option )
          }
          forms[name].push_select e.attribute("name"), select
        }
      end
    }
  end
  forms[name]
end

#get_contentObject



12
13
14
# File 'lib/web/formreader.rb', line 12

def get_content
  html
end

#get_fields(name) ⇒ Object



78
79
80
# File 'lib/web/formreader.rb', line 78

def get_fields( name )
  form_elements(name).get_fields
end

#get_options(name) ⇒ Object



82
83
84
# File 'lib/web/formreader.rb', line 82

def get_options( name )
  form_elements(name).get_options
end

#html_treeObject



86
87
88
89
90
91
92
93
# File 'lib/web/formreader.rb', line 86

def html_tree
  unless (@html_tree)
    p = HTMLTree::Parser.new(true, false)
    p.feed get_content
    @html_tree = p.tree
  end
  @html_tree
end

#merge_fields(name, edited_fields) ⇒ Object



96
97
98
# File 'lib/web/formreader.rb', line 96

def merge_fields( name, edited_fields )
  form_elements(name).merge_fields(edited_fields)
end