opengraph_parser 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/open_graph.rb +24 -12
- data/lib/redirect_follower.rb +10 -2
- metadata +15 -15
data/lib/open_graph.rb
CHANGED
@@ -3,7 +3,7 @@ require 'redirect_follower'
|
|
3
3
|
require "addressable/uri"
|
4
4
|
|
5
5
|
class OpenGraph
|
6
|
-
attr_accessor :src, :url, :type, :title, :description, :images, :metadata, :response
|
6
|
+
attr_accessor :src, :url, :type, :title, :description, :images, :metadata, :response, :original_images
|
7
7
|
|
8
8
|
def initialize(src, fallback = true)
|
9
9
|
@src = src
|
@@ -29,13 +29,13 @@ class OpenGraph
|
|
29
29
|
doc.css('meta').each do |m|
|
30
30
|
if m.attribute('property') && m.attribute('property').to_s.match(/^og:(.+)$/i)
|
31
31
|
m_content = m.attribute('content').to_s.strip
|
32
|
-
|
32
|
+
metadata_name = m.attribute('property').to_s.gsub("og:", "")
|
33
|
+
@metadata = add_metadata(@metadata, metadata_name, m_content)
|
34
|
+
case metadata_name
|
33
35
|
when *attrs_list
|
34
36
|
self.instance_variable_set("@#{metadata_name}", m_content) unless m_content.empty?
|
35
37
|
when "image"
|
36
38
|
add_image(m_content)
|
37
|
-
else
|
38
|
-
@metadata[m.attribute('property').to_s] = m_content
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -62,12 +62,13 @@ class OpenGraph
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def check_images_path
|
65
|
+
@original_images = @images.dup
|
65
66
|
uri = Addressable::URI.parse(@src)
|
66
67
|
imgs = @images.dup
|
67
68
|
@images = []
|
68
69
|
imgs.each do |img|
|
69
70
|
if Addressable::URI.parse(img).host.nil?
|
70
|
-
full_path =
|
71
|
+
full_path = uri.join(img).to_s
|
71
72
|
add_image(full_path)
|
72
73
|
else
|
73
74
|
add_image(img)
|
@@ -85,14 +86,25 @@ class OpenGraph
|
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
88
|
-
def
|
89
|
-
|
90
|
-
if
|
91
|
-
|
92
|
-
|
93
|
-
|
89
|
+
def add_metadata(metadata_container, path, content)
|
90
|
+
path_elements = path.split(':')
|
91
|
+
if path_elements.size > 1
|
92
|
+
current_element = path_elements.delete_at(0)
|
93
|
+
path = path_elements.join(':')
|
94
|
+
if metadata_container[current_element.to_sym]
|
95
|
+
path_pointer = metadata_container[current_element.to_sym].last
|
96
|
+
index_count = metadata_container[current_element.to_sym].size
|
97
|
+
metadata_container[current_element.to_sym][index_count - 1] = add_metadata(path_pointer, path, content)
|
98
|
+
metadata_container
|
99
|
+
else
|
100
|
+
metadata_container[current_element.to_sym] = []
|
101
|
+
metadata_container[current_element.to_sym] << add_metadata({}, path, content)
|
102
|
+
metadata_container
|
103
|
+
end
|
94
104
|
else
|
95
|
-
|
105
|
+
metadata_container[path.to_sym] ||= []
|
106
|
+
metadata_container[path.to_sym] << {'_value'.to_sym => content}
|
107
|
+
metadata_container
|
96
108
|
end
|
97
109
|
end
|
98
110
|
end
|
data/lib/redirect_follower.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'net/
|
1
|
+
require 'net/https'
|
2
2
|
|
3
3
|
class RedirectFollower
|
4
4
|
class TooManyRedirects < StandardError; end
|
@@ -12,7 +12,15 @@ class RedirectFollower
|
|
12
12
|
def resolve
|
13
13
|
raise TooManyRedirects if redirect_limit < 0
|
14
14
|
|
15
|
-
|
15
|
+
uri = URI.parse(URI.escape(url))
|
16
|
+
if uri.scheme == 'https'
|
17
|
+
https = Net::HTTP.new(uri.host, 443)
|
18
|
+
https.use_ssl = true
|
19
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
20
|
+
self.response = https.request_get(uri.request_uri)
|
21
|
+
else
|
22
|
+
self.response = Net::HTTP.get_response(uri)
|
23
|
+
end
|
16
24
|
|
17
25
|
if response.kind_of?(Net::HTTPRedirection)
|
18
26
|
self.url = redirect_url
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opengraph_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2013-01-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: nokogiri
|
17
|
-
requirement: &
|
17
|
+
requirement: &70256296829180 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70256296829180
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: addressable
|
28
|
-
requirement: &
|
28
|
+
requirement: &70256296828540 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70256296828540
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: rspec
|
39
|
-
requirement: &
|
39
|
+
requirement: &70256296827720 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70256296827720
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rdoc
|
50
|
-
requirement: &
|
50
|
+
requirement: &70256296826860 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *70256296826860
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: bundler
|
61
|
-
requirement: &
|
61
|
+
requirement: &70256296825620 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: '0'
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *70256296825620
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: jeweler
|
72
|
-
requirement: &
|
72
|
+
requirement: &70256296818460 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,7 +77,7 @@ dependencies:
|
|
77
77
|
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *70256296818460
|
81
81
|
description: A simple Ruby library for parsing Open Graph Protocol information from
|
82
82
|
a website. It also includes a fallback solution when the website has no Open Graph
|
83
83
|
information.
|
@@ -108,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
segments:
|
110
110
|
- 0
|
111
|
-
hash:
|
111
|
+
hash: -777082750492777387
|
112
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
113
|
none: false
|
114
114
|
requirements:
|