Class: Robostripper::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/robostripper/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
# File 'lib/robostripper/resource.rb', line 6

def initialize(source)
  @html = (source.is_a? String) ? HTTP.get(source) : source
  @cached = {}
  @attrs = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/robostripper/resource.rb', line 30

def method_missing(method, *args, &block)
  return super if method == :to_ary or (args.length == 0 and block.nil?)
  @attrs << method

  if block.nil? and args.length == 0
    block = lambda { nil }
  elsif block.nil?
    options = (args.length == 1) ? {} : args[1]
    block = lambda { scan(args.first, options) }
  end

  define_singleton_method(method, &block)
end

Class Method Details

.add_item(name, &block) ⇒ Object



24
25
26
27
28
# File 'lib/robostripper/resource.rb', line 24

def self.add_item(name, &block)
  define_method(name) {
    @cached[name] ||= Resource.new(@html).tap { |r| r.instance_eval &block }
  }
end

.add_list(name, path, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/robostripper/resource.rb', line 16

def self.add_list(name, path, &block)
  define_method(name) {
    @cached[name] ||= @html.css(path).map { |result| 
      Resource.new(result).tap { |r| r.instance_eval &block }
    }
  }
end

Instance Method Details

#encode(text) ⇒ Object



12
13
14
# File 'lib/robostripper/resource.rb', line 12

def encode(text)
  URI.encode(text)
end

#scan(paths, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/robostripper/resource.rb', line 44

def scan(paths, options = {})
  result = @html
  paths = [ paths ] unless paths.is_a? Array
  paths.each { |path|
    result = result.search(path)
  }      

  if result.nil? or result.length == 0
    nil
  elsif options.fetch(:all, false)
    result
  elsif options.has_key? :nontext
    result.children.select(&:text?).map(&:text).join(options[:nontext])
  elsif options.has_key? :attribute
    result.first[options[:attribute]]
  else
    result.text
  end
end

#to_sObject



64
65
66
67
# File 'lib/robostripper/resource.rb', line 64

def to_s
  attrs = @attrs.to_a.inject({}) { |h, attr| h[attr] = send(attr); h }
  "<#{self.class.name} #{attrs}>"
end