Class: LinkPreviewGenerator

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

Class Method Summary collapse

Class Method Details

.get_html_elements(html) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/link_preview_generator.rb', line 64

def self.get_html_elements(html)
  elements = {}
  #parse the html 
  resp = Nokogiri::HTML(html)
  #fetch title
  elements[:title] = !(resp.css("meta [property='og:title']").empty?) ? resp.css("meta [property='og:title']")[0]["content"] : (!(resp.css("title").empty?) ? resp.css("title")[0].children[0].content : "" )

  #fetch description
  elements[:description] = !(resp.css("meta [property='og:description']").empty?) ? resp.css("meta [property='og:description']")[0]["content"] : ""

  #fetch image
  elements[:image_url] = !(resp.css("meta [property='og:image']").empty?) ? resp.css("meta [property='og:image']")[0]["content"] : ""

  #fetch type
  elements[:link_type] = !(resp.css("meta [property='og:type']").empty?) ? resp.css("meta [property='og:type']")[0]["content"] : ""

  #if type is video feth the video link
  elements[:video_url] = (elements[:link_type] == "video") ? resp.css("meta [property='og:url']")[0]["content"] : ""

  return elements
end

.get_preview_elements(url) ⇒ Object



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

def self.get_preview_elements(url)
  link_elements = nil
  
  # generate a html get request to the link
  begin
    html_body = self.html_get_req(url) 
  rescue Exception => e
    puts "ERROR getting link preview : "+e.message
    return nil
  end 
  
  #parse the html body and retrieve the preiview elements
  if html_body != nil
    link_elements = self.get_html_elements(html_body) 
    return link_elements
  else
    return nil
  end 

end

.html_get_req(url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/link_preview_generator.rb', line 28

def self.html_get_req(url)

  ishttps = false
  host = 80

  # if the link does not containt http or https then add
  if (/^https:\/\//.match(url)) 
    host = 443
    ishttps = true
  elsif (/^http:\/\//.match(url))
    # do nothing
  else
    url = "http://"+url
  end
  
  uri = URI.parse(url);
  http = Net::HTTP.new(uri.host, host)

  #if https link enable ssl
  if ishttps
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  
  #Http request
  request = Net::HTTP::Get.new(uri.request_uri)   
  response = http.request(request)
  
  if response.code == "200"
    return response.body
  else
    return nil
  end 

end