Module: XblGamercard::MicroScraper::ClassMethods

Defined in:
lib/xbl_gamercard/micro_scraper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.field_name(selector) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/xbl_gamercard/micro_scraper.rb', line 69

def self.field_name(selector)
  word = selector.split(/[\W]/).last
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

Instance Method Details

#extract(selector, opts = nil, &block) ⇒ Object

Specifies a field that is simply extracted from the document



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xbl_gamercard/micro_scraper.rb', line 28

def extract(selector, opts=nil, &block)
  opts = opts.nil? ? {} : opts.dup
  opts[:as] ||= ClassMethods.field_name(selector)
  opts[:from] ||= :element

  define_method(opts[:as]) do
    elem = send(opts[:from]).css(selector)[0]

    next block.call(elem) if block
    next elem
  end
end

#extract_all(selector, opts = nil, &block) ⇒ Object

Specifies a field that returns an array of matched elements



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xbl_gamercard/micro_scraper.rb', line 56

def extract_all(selector, opts=nil, &block)
  opts = opts.nil? ? {} : opts.dup
  opts[:as] ||= ClassMethods.field_name(selector)
  opts[:from] ||= :element

  define_method(opts[:as]) do
    elems = send(opts[:from]).css(selector).to_a

    next elems.map { |e| block.call(e) } if block
    next elems
  end
end

#extract_int(selector, opts = nil, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/xbl_gamercard/micro_scraper.rb', line 48

def extract_int(selector, opts=nil, &block)
  extract_text(selector, opts) do |elem|
    next block.call(elem.to_i) if block
    next elem.to_i
  end
end

#extract_text(selector, opts = nil, &block) ⇒ Object



41
42
43
44
45
46
# File 'lib/xbl_gamercard/micro_scraper.rb', line 41

def extract_text(selector, opts=nil, &block)
  extract(selector, opts) do |elem|
    next block.call(elem.text) if block
    next elem.text
  end
end