Class: Yapra::Plugin::MechanizeBase

Inherits:
Base
  • Object
show all
Defined in:
lib/yapra/plugin/mechanize_base.rb

Constant Summary

Constants included from FeedItemOperator

FeedItemOperator::LOCAL_VAL_RE

Instance Attribute Summary

Attributes included from ContextAware

#pipeline, #plugin_config, #yapra

Instance Method Summary collapse

Methods inherited from Base

#logger

Methods included from ErbApplier

#apply_template

Methods included from FeedItemOperator

#set_attribute_to

Methods included from ContextAware

#config

Instance Method Details

#agentObject



6
7
8
9
# File 'lib/yapra/plugin/mechanize_base.rb', line 6

def agent
  pipeline.context['mechanize_agent'] ||= (defined?(Mechanize) ? Mechanize.new : WWW::Mechanize.new)
  pipeline.context['mechanize_agent']
end

#extract_attribute_from(element, item, binding = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yapra/plugin/mechanize_base.rb', line 11

def extract_attribute_from element, item, binding=nil
  if plugin_config['extract_xpath']
    plugin_config['extract_xpath'].each do |k, v|
      value = nil
      case v.class.to_s
      when 'String'
        value = element.search(v).to_html.toutf8
      when 'Hash'
        ele = element.at( v['first_node'] )
        value = ( ele.nil? ) ? nil : ele.get_attribute( v['attr'] )
      end
      set_attribute_to item, k, value
    end
  end

  if plugin_config['apply_template_after_extracted']
    plugin_config['apply_template_after_extracted'].each do |k, template|
      value = apply_template template, binding
      set_attribute_to item, k, value
    end
  end
end