Class: Arrogance::TagHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/arrogance/tools.rb

Overview

– <link www.link.com /> <– if you do this, please stop. It makes life less delightful

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ TagHandler

Returns a new instance of TagHandler.



30
31
32
33
# File 'lib/arrogance/tools.rb', line 30

def initialize(url)
  @url = url
  discover_tags
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



18
19
20
# File 'lib/arrogance/tools.rb', line 18

def author
  @author
end

#dateObject

Returns the value of attribute date.



18
19
20
# File 'lib/arrogance/tools.rb', line 18

def date
  @date
end

#descriptionObject

Returns the value of attribute description.



18
19
20
# File 'lib/arrogance/tools.rb', line 18

def description
  @description
end

Returns the value of attribute link.



18
19
20
# File 'lib/arrogance/tools.rb', line 18

def link
  @link
end

#titleObject

Returns the value of attribute title.



18
19
20
# File 'lib/arrogance/tools.rb', line 18

def title
  @title
end

#urlObject

Returns the value of attribute url.



18
19
20
# File 'lib/arrogance/tools.rb', line 18

def url
  @url
end

Instance Method Details

#discover_author_tag(set, doc) ⇒ Object

– get author_tag



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/arrogance/tools.rb', line 46

def discover_author_tag(set, doc)
  ary = []
  set.each do |node|
    node.children.each do |child|
      str, class_ary = child.to_s.gsub(/<.*?>/, ''), []
      str.split(//).each {|s| class_ary << s.class }
      unless class_ary.empty?
        if /<.*?>/.match(child.to_s) && class_ary.mode != Fixnum && str.split(//).length < 20
          not_tags = ['title', 'total', 'category', 'comments', 'updated', 'id']
          tag = /<.*?>/.match(child.to_s).to_s.gsub(/(<|>)/, '').strip.split(' ')[0] #string brutality...
          unless not_tags.include?(tag)
            ary << tag
          end
        end
      end
    end
    unless ary.empty?
      self.author = ary.mode.to_s.gsub(/(<|>)/, '')
    else
      if (auth = doc.xpath('//author//name')[0].to_s) && ! auth.empty?
        self.author = '//author//name'
      else
        self.author = 'none'
      end
    end
  end
end

#discover_date_tag(set) ⇒ Object

– get date tag



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/arrogance/tools.rb', line 133

def discover_date_tag(set)
  ary = []
  set.each do |node|
    node.children.each do |child|
      str = /<.*?>/.match(child.to_s)
      if /[^(u(P|p)|U(P|p)](D|d)ate/.match(str.to_s) && str.to_s.length < 20 
        ary << str
      end
    end
  end
  if ary.empty?
    self.date = 'updated'
  else
    self.date = ary.mode.to_s.gsub(/(<|>)/, '')
  end
  self.date = 'updated' unless self.date.length > 1
  return self.date
end

#discover_description_tag(set) ⇒ Object

– get description tag



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/arrogance/tools.rb', line 104

def discover_description_tag(set)
  ary, hash = [], {}
  set.each do |node|
    node.children.each do |child|
      hash[/<.*?>/.match(child.to_s).to_s] ||= 0
      hash[/<.*?>/.match(child.to_s).to_s] += child.to_s.length
    end
    largest = ''
    hash.each_with_index do |kv, idx|
      if idx == 0
        largest = kv[0]
      else
        if kv[1] > hash[largest]
          largest = kv[0]
        end
      end
    end
    ary << largest
  end
  self.description = ary.mode.to_s.gsub(/(<|>)/, '').strip.split(' ')[0]
end

– get link tag



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/arrogance/tools.rb', line 75

def discover_link_tag(set)
  ary = []
  not_links = ['id', 'author']
  set.each do |node|
    node.children.each do |child|
      str = /<.*?>/.match(child.to_s).to_s.gsub(/<.*?>/, '')
      if str.length < 100 && /<.*?>/.match(child.to_s).to_s.gsub(/(<|>)/, '').strip.split(' ')[0] != 'content'
        if /http:/.match(child.to_s) || /www./.match(child.to_s)
          unless not_links.include?(/<.*?>/.match(child.to_s).to_s.gsub(/(<|>)/, ''))
            ary << (/<.*?>/.match(child.to_s).to_s)
          end
        end
      end
    end
  end
  unless ary.empty?
    if ary.include?('<origLink>')
      self.link = 'origLink'
    elsif !ary.include?('<origLink>') && ary.include?('<link>')
      self.link = 'link'
    else        
      self.link = (ary.mode.to_s.split(' ').length > 1 ? 'manual' : ary.mode.to_s.gsub(/(<|>)/, ''))
    end
  else
    self.link = ''
  end
end

#discover_tagsObject



35
36
37
38
39
40
41
42
43
# File 'lib/arrogance/tools.rb', line 35

def discover_tags
  doc = Nokogiri::XML(open(self.url)).remove_namespaces!
  set = (doc.xpath('//item').empty? ? doc.xpath('//entry') : doc.xpath('//item'))
  discover_author_tag(set, doc)
  discover_link_tag(set)
  discover_description_tag(set)
  discover_title_tag
  discover_date_tag(set)
end

#discover_title_tagObject

– get title tag, its always title.



127
128
129
# File 'lib/arrogance/tools.rb', line 127

def discover_title_tag 
  return self.title = 'title'
end