Class: Raev::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/raev/url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Url

Returns a new instance of Url.



12
13
14
15
16
# File 'lib/raev/url.rb', line 12

def initialize(url)
  @url = url
  @doc = nil
  @linked_data = nil
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



9
10
11
# File 'lib/raev/url.rb', line 9

def doc
  @doc
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/raev/url.rb', line 8

def url
  @url
end

Instance Method Details

#authorObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/raev/url.rb', line 153

def author
			node = document.search('meta[name="author"]').first

			if node && node.attribute("content")
return node.attribute("content").value
			end
			
  cssSelectors = [
    '.author-info .name',
    '.author-top a',
'.yt-user-info a',
    'a[rel~="author"]',
    'a[itemprop~="author"]',
    '.author h3 a',
    '.author',
    '.posted-by a',
    '.entryAuthor a',
    'a.names',
    'a.byline-author',
    '.byline a',
    '.author.vcard a',
    'p.info a',
    '.author-name',
    '.upcased',
    'a[rel~="nofollow"]'
  ]

  node = document.search(cssSelectors.join(", ")).first
  
  if node
    words = node.content.split.size
  
    if words <= 4
      return Sanitize.clean(node.content).strip[0..255]
    end
  end
  
  ""
end

#baseObject



18
19
20
21
22
# File 'lib/raev/url.rb', line 18

def base      
  base_url = @url.split('/')[2]  
  base_url = base_url.gsub('www.', '') unless base_url.nil?
  base_url
end

#bestRatingObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/raev/url.rb', line 211

def bestRating
	node = document.search('*[itemprop="bestRating"]').first
	
	if node
		if node.attribute("content")
			value = node.attribute("content").value
		
			if value
				return value.to_f
			end
		end
	end
	
	nil			
end

#cleanObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/raev/url.rb', line 24

def clean
  unless @url.nil?
    utm_index = @url.index(/(\?|&)utm_/)
    unless(utm_index.nil?)
      return url.slice(0, utm_index)
    end
  end
  
  @url
end

#feedObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/raev/url.rb', line 67

def feed
  feed_url = nil
  
  node = document.css('link[type="application/rss+xml"][rel="alternate"]')
  
  if node.first
    feed_url = node.first["href"]
  else
    node = document.css('a:match_href("http://feeds.")', Raev::Parser.new)
            
    if node.first
      feed_url = node.first["href"]
    end
  end
  
  if feed_url && feed_url[0,1] == "/"
    feed_url = @url + feed_url
  end
  
  feed_url
end

#headlineObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/raev/url.rb', line 89

def headline
  if linked_data && linked_data["headline"]
    return Sanitize.clean(linked_data["headline"])
  end
  
  page_title = nil
  
  node = document.css(".twitter-share-button")
  
  if node.first
    if node.first['data-text']
      page_title = node.first['data-text']
    end
  end

  if page_title.nil?
    document.css("head meta").each do |meta|
      if meta['property'] == 'og:title' || meta['property'] == 'twitter:title'
        page_title = meta['content']
      end
    end
  end
        
  if page_title.nil?
    node = document.css("#article h1, a[rel=\"bookmark\"], h2[itemprop=\"name\"]")
            
    if node.first
      page_title = node.first.content
    end
  end
  
  unless page_title.nil?
    page_title.gsub!(/ +/, ' ')
  end
  
  page_title
end

#pubdateObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/raev/url.rb', line 127

def pubdate      
  if linked_data && linked_data["datePublished"]
    return Date.parse(linked_data["datePublished"])
  end
  
  date_elements = @url.match(/[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}/).to_s.split("/")
  
  if date_elements.size == 3
    return Date.new(date_elements[0].to_i, date_elements[1].to_i, date_elements[2].to_i)      
  else
    node = document.search("meta[itemprop='datePublished'], meta[name='pub_date']").first
    
    if node
      return Date.parse(node.attribute("content"))
    else
      node = document.search(".entryDate, .entrydate").first

      if node
        return Chronic.parse(node.content.gsub(/[^a-zA-Z0-9\s]/,"").strip)
      end
    end
  end
  
  nil
end

#ratingValueObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/raev/url.rb', line 193

def ratingValue
	node = document.search('*[itemprop="ratingValue"]').first
	
	if node
		if node.attribute("content")
			value = node.attribute("content").value
		else
			value = node.content
		end
	end

	if value
		value.to_f
	else
		nil
	end
end

#resolvedObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/raev/url.rb', line 35

def resolved
  unless @url.nil?
    begin
      return RedirectFollower(@url, 5)
    rescue => ex
      puts "Could not resolve #{@url}. #{ex.class}: #{ex.message}"
    end
  end

  @url
end

#resolved_and_cleanObject



47
48
49
50
# File 'lib/raev/url.rb', line 47

def resolved_and_clean
  resolved_url = Url.new(self.resolved)
  resolved_url.clean      
end

#twitterObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/raev/url.rb', line 56

def twitter
  node = document.css('a:match_href("twitter.com")', Raev::Parser.new)
        
  if node.first
    twitter_url = node.first["href"]
    twitter_url.split('/').last
  else
    nil
  end
end

#without_httpObject



52
53
54
# File 'lib/raev/url.rb', line 52

def without_http
  @url.sub("http://", "")
end