Class: Horseman::Dom::Document
- Inherits:
-
Object
- Object
- Horseman::Dom::Document
- Defined in:
- lib/horseman/dom/document.rb
Instance Attribute Summary collapse
-
#dom ⇒ Object
readonly
Returns the value of attribute dom.
-
#forms ⇒ Object
readonly
Returns the value of attribute forms.
-
#frames ⇒ Object
readonly
Returns the value of attribute frames.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
Instance Method Summary collapse
-
#initialize(body) ⇒ Document
constructor
A new instance of Document.
- #method_missing(method, *arguments, &block) ⇒ Object
- #parse!(body) ⇒ Object
- #respond_to?(method_sym, include_private = false) ⇒ Boolean
Constructor Details
#initialize(body) ⇒ Document
Returns a new instance of Document.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/horseman/dom/document.rb', line 13 def initialize(body) @forms = {} @scripts = [] @frames = {} @field_types = { 'text' => :text, 'checkbox' => :checkbox, 'hidden' => :hidden, 'submit' => :submit } @encoding_types = { 'application/x-www-form-urlencoded' => :url, 'multipart/form-data' => :multipart } parse! body end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/horseman/dom/document.rb', line 89 def method_missing(method, *arguments, &block) case method when :[] indexer = arguments[0] form = @forms.select {|id, form| id.to_s == indexer}.map {|k,v| v}.first return form unless form.nil? end puts "Not implemented in Document: #{method} #{arguments.join(',')}" end |
Instance Attribute Details
#dom ⇒ Object (readonly)
Returns the value of attribute dom.
11 12 13 |
# File 'lib/horseman/dom/document.rb', line 11 def dom @dom end |
#forms ⇒ Object (readonly)
Returns the value of attribute forms.
11 12 13 |
# File 'lib/horseman/dom/document.rb', line 11 def forms @forms end |
#frames ⇒ Object (readonly)
Returns the value of attribute frames.
11 12 13 |
# File 'lib/horseman/dom/document.rb', line 11 def frames @frames end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
11 12 13 |
# File 'lib/horseman/dom/document.rb', line 11 def scripts @scripts end |
Instance Method Details
#parse!(body) ⇒ Object
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 |
# File 'lib/horseman/dom/document.rb', line 33 def parse!(body) doc = Nokogiri::HTML(body) doc.css('form').select{|f| f.attr('id')}.each do |f| form = Form.new(f.attr('id'), f.attr('name')) form.action = f.attr('action') || '/' form.encoding = @encoding_types[f.attr('enctype')] || :url form.fields = {} f.css('input').select{|i| i.attr('name')}.each do |i| field = FormField.new(i.attr('id'), i.attr('name')) field.type = @field_types[i.attr('type')] || :text field.value = i.attr('value') form.fields[field.name.to_sym] = field form.submit = field if (field.type == :submit) end @forms[form.id.to_sym] = form end valid_script_types = ['javascript', 'text/javascript'] doc.css('script').select{|s| (s.attr('type').nil?) || (valid_script_types.include? s.attr('type')) }.each do |s| script = Script.new if s.attr('src') # TODO -- account for HTTP failures js_src = s.attr('src') begin script.body = open(s.attr('src')) {|f| f.read.strip} rescue puts "Could not load javascript at #{js_src}" end else script.body = s.inner_html.strip end @scripts << script end doc.css('frame').select{|f| f.attr('src') && f.attr('name')}.each do |f| frame_src = f.attr('src') begin frame_body = open(frame_src) {|f| f.read.strip} @frames[f.attr('name').to_sym] = Document.new(frame_body) rescue puts "Could not load frame at #{frame_src}" end end @dom = doc end |
#respond_to?(method_sym, include_private = false) ⇒ Boolean
85 86 87 |
# File 'lib/horseman/dom/document.rb', line 85 def respond_to?(method_sym, include_private = false) true end |