Module: LoyalSpider::EntityAble::InstanceMethods

Defined in:
lib/loyal_spider/ables/entity_able.rb

Instance Method Summary collapse

Instance Method Details

#authorsObject



54
55
56
# File 'lib/loyal_spider/ables/entity_able.rb', line 54

def authors
  @authors ||= []
end

#authors_arrayObject



58
59
60
# File 'lib/loyal_spider/ables/entity_able.rb', line 58

def authors_array
  @authors_array ||= self.authors.map{|_author| _author[:text] }
end

#imagesObject

图片



40
41
42
43
44
45
46
47
48
# File 'lib/loyal_spider/ables/entity_able.rb', line 40

def images
  @images ||= Nokogiri::HTML.parse(self.content).css('img').map do |img_doc|
    {
      :src => img_doc.attr('src'),
      :title => img_doc.attr('title'),
      :alt   => img_doc.attr('alt')
    }
  end
end

#images?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/loyal_spider/ables/entity_able.rb', line 50

def images?
  self.images.any?
end

#initialize(attrs = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/loyal_spider/ables/entity_able.rb', line 23

def initialize attrs={}
  attrs.each do |key, value|
    self.send(:"#{key}=", value)
  end

  self.errors = {}
end

#tagsObject



31
32
33
# File 'lib/loyal_spider/ables/entity_able.rb', line 31

def tags
  @tags ||= []
end

#tags_arrayObject



35
36
37
# File 'lib/loyal_spider/ables/entity_able.rb', line 35

def tags_array
  @tags_array ||= self.tags.map{|_tag| _tag[:text] }
end

#valid!Object



67
68
69
70
71
72
73
74
75
# File 'lib/loyal_spider/ables/entity_able.rb', line 67

def valid!
  unless self.content.to_s.strip.size > 0
    add_error :content, '不能为空'
  end

  unless self.url.to_s.strip.size > 0
    add_error :url, '不能为空'
  end
end

#valid?Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/loyal_spider/ables/entity_able.rb', line 62

def valid?
  self.valid!
  self.errors.empty?
end