Class: Ubiquitously::Page

Inherits:
Base show all
Defined in:
lib/ubiquitously/models/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#apply, #debug?

Methods included from SubclassableCallbacks

included, override

Constructor Details

#initialize(attributes = {}) ⇒ Page

Returns a new instance of Page.



5
6
7
# File 'lib/ubiquitously/models/page.rb', line 5

def initialize(attributes = {})
  apply attributes
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/ubiquitously/models/page.rb', line 3

def description
  @description
end

#imageObject

Returns the value of attribute image.



3
4
5
# File 'lib/ubiquitously/models/page.rb', line 3

def image
  @image
end

#tagsObject

Returns the value of attribute tags.



3
4
5
# File 'lib/ubiquitously/models/page.rb', line 3

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/ubiquitously/models/page.rb', line 3

def title
  @title
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/ubiquitously/models/page.rb', line 3

def url
  @url
end

Instance Method Details

#parseObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ubiquitously/models/page.rb', line 9

def parse
  html = Nokogiri::HTML(open(url).read)
  
  self.title = html.xpath("//title").first.text.to_s.strip
  
  self.description = html.xpath("//meta[@name='description']").first["content"] rescue ""
  self.description.strip!
  
  self.tags = html.xpath("//meta[@name='keywords']").first["content"] rescue ""
  self.tags = self.tags.split(/,\s+/).taggify("-", ", ").split(", ")
  
  self.image = html.xpath("//link[@rel='image_src']").first["image_src"] rescue nil
  if self.image.blank?
    self.image = html.xpath("//img").first["src"] rescue nil
  end
  
  self
end