meta_info 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +7 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +13 -0
- data/lib/meta_info.rb +21 -0
- data/lib/meta_info/document.rb +66 -0
- data/lib/meta_info/exception.rb +11 -0
- data/lib/meta_info/version.rb +3 -0
- data/meta_info.gemspec +24 -0
- data/spec/examples/meta_tags.html +6 -0
- data/spec/examples/opengraph.html +10 -0
- data/spec/examples/twitter_card.html +10 -0
- data/spec/meta_info_spec.rb +41 -0
- data/spec/spec_helper.rb +10 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a2992d920fbd7bfff6fbe9a16c1cf0c99eb1865
|
4
|
+
data.tar.gz: 26d972edb89c225f01a3145bd18d7d44eee73f0f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f44606cf69cbb1adc9cc91f8576a3a2e3860e7b62f27abed6da3f2d706e44bb10afae9782a5243355f991133466e906b7df90ce410408ccc6e66f449b629510
|
7
|
+
data.tar.gz: b107bad6e77171eb60dcf6b156c4935600d81016e085b72937251879eab82bb237e553d7f768edeac9591163466c674fba7c35571240175b7e9a27d31861b327
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Arkadiusz Buras(macbury)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# MetaInfo
|
2
|
+
|
3
|
+
Simple gem for extracting meta information from web page using opengraph tags, twitter cards tags or normal meta tags
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'meta_info'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install meta_info
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
document = MetaInfo.parse(open("http://www.rottentomatoes.com/m/1217700-kick_ass/"))
|
22
|
+
|
23
|
+
document.title => "Kick-Ass"
|
24
|
+
|
25
|
+
document.description => "Not for the faint of heart, Kick-Ass takes the comic adaptation genre to new levels of visual style, bloody violence, and gleeful profanity."
|
26
|
+
|
27
|
+
document.image => "http://content9.flixster.com/movie/11/17/37/11173707_800.jpg"
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
5
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
6
|
+
end
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
9
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
10
|
+
spec.rcov = true
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
data/lib/meta_info.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
require "meta_info/version"
|
3
|
+
require "meta_info/exception"
|
4
|
+
require "meta_info/document"
|
5
|
+
|
6
|
+
module MetaInfo
|
7
|
+
|
8
|
+
def self.parse(html)
|
9
|
+
begin
|
10
|
+
document = MetaInfo::Document.new(html)
|
11
|
+
if document.valid?
|
12
|
+
return document
|
13
|
+
else
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
rescue MetaInfo::Exception::NoHTML => e
|
17
|
+
return false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module MetaInfo
|
2
|
+
|
3
|
+
class Document
|
4
|
+
|
5
|
+
def initialize(html)
|
6
|
+
raise MetaInfo::Exception::NoHTML if html.nil? || html.empty?
|
7
|
+
@doc = Nokogiri::HTML.parse(html)
|
8
|
+
end
|
9
|
+
|
10
|
+
def doc
|
11
|
+
@doc
|
12
|
+
end
|
13
|
+
|
14
|
+
def title
|
15
|
+
get_attr("title") || meta_title
|
16
|
+
end
|
17
|
+
|
18
|
+
def description
|
19
|
+
get_attr("description") || meta_description
|
20
|
+
end
|
21
|
+
|
22
|
+
def image
|
23
|
+
get_attr("image")
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid?
|
27
|
+
title && description != false
|
28
|
+
end
|
29
|
+
|
30
|
+
def meta_description
|
31
|
+
search_meta("name", /^description$/i, "content")
|
32
|
+
end
|
33
|
+
|
34
|
+
def twitter_attr(name)
|
35
|
+
search_meta('name', /^twitter:#{name}$/i, 'content')
|
36
|
+
end
|
37
|
+
|
38
|
+
def og_attr(name)
|
39
|
+
search_meta('property', /^og:#{name}$/i, 'content')
|
40
|
+
end
|
41
|
+
|
42
|
+
def meta_title
|
43
|
+
if doc.css("title").empty?
|
44
|
+
return false
|
45
|
+
else
|
46
|
+
return doc.css("title")[0].text
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_attr(name)
|
51
|
+
og_attr(name) || twitter_attr(name)
|
52
|
+
end
|
53
|
+
|
54
|
+
def search_meta(attribute, regexp, value_key)
|
55
|
+
doc.css('meta').each do |m|
|
56
|
+
if m.attribute(attribute) && m.attribute(attribute).to_s.match(regexp)
|
57
|
+
return m.attribute(value_key).to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/meta_info.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'meta_info/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "meta_info"
|
8
|
+
spec.version = MetaInfo::VERSION
|
9
|
+
spec.authors = ["macbury"]
|
10
|
+
spec.email = ["macbury@gmail.com"]
|
11
|
+
spec.description = "Simple gem for extracting meta information from web page using opengraph tags, twitter cards tags or normal meta tags"
|
12
|
+
spec.summary = "Simple gem for extracting meta information from web page using opengraph tags, twitter cards tags or normal meta tags"
|
13
|
+
spec.homepage = "https://github.com/macbury/MetaInfo"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_dependency "nokogiri", "~> 1.4.7"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<html xmlns:og="http://opengraphprotocol.org/schema/">
|
2
|
+
<head>
|
3
|
+
<meta property="og:type" content="przepisypl:recipe" />
|
4
|
+
<meta property="og:image" content="image_url" />
|
5
|
+
<meta property="og:title" content="testing title" />
|
6
|
+
<meta property="og:description" content="testing description" />
|
7
|
+
|
8
|
+
</head>
|
9
|
+
|
10
|
+
</html>
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<meta name="twitter:card" content="summary">
|
4
|
+
<meta name="twitter:url" content="http://www.nytimes.com/2012/02/19/arts/music/amid-police-presence-fans-congregate-for-whitney-houstons-funeral-in-newark.html">
|
5
|
+
<meta name="twitter:title" content="testing title">
|
6
|
+
<meta name="twitter:description" content="testing description">
|
7
|
+
<meta name="twitter:image" content="image_url">
|
8
|
+
</head>
|
9
|
+
|
10
|
+
</html>
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe MetaInfo do
|
4
|
+
|
5
|
+
let(:opengraph){ File.open(File.dirname(__FILE__) + '/examples/opengraph.html').read }
|
6
|
+
let(:twitter_card){ File.open(File.dirname(__FILE__) + '/examples/twitter_card.html').read }
|
7
|
+
let(:meta_tags){ File.open(File.dirname(__FILE__) + '/examples/meta_tags.html').read }
|
8
|
+
|
9
|
+
describe ".parse" do
|
10
|
+
it "should return false for nil or empty html" do
|
11
|
+
MetaInfo.parse("").should be_false
|
12
|
+
MetaInfo.parse(nil).should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should return MetaInfo::Document for valid html" do
|
16
|
+
MetaInfo.parse(meta_tags).should be_kind_of(MetaInfo::Document)
|
17
|
+
MetaInfo.parse(opengraph).should be_kind_of(MetaInfo::Document)
|
18
|
+
MetaInfo.parse(twitter_card).should be_kind_of(MetaInfo::Document)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe MetaInfo::Document do
|
23
|
+
it "should return proper title" do
|
24
|
+
MetaInfo.parse(meta_tags).title.should eq("testing title")
|
25
|
+
MetaInfo.parse(opengraph).title.should eq("testing title")
|
26
|
+
MetaInfo.parse(twitter_card).title.should eq("testing title")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return proper description" do
|
30
|
+
MetaInfo.parse(meta_tags).description.should eq("testing description")
|
31
|
+
MetaInfo.parse(opengraph).description.should eq("testing description")
|
32
|
+
MetaInfo.parse(twitter_card).description.should eq("testing description")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return proper image" do
|
36
|
+
MetaInfo.parse(meta_tags).image.should be_false
|
37
|
+
MetaInfo.parse(opengraph).image.should eq("image_url")
|
38
|
+
MetaInfo.parse(twitter_card).image.should eq("image_url")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meta_info
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- macbury
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.7
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.4.7
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Simple gem for extracting meta information from web page using opengraph
|
56
|
+
tags, twitter cards tags or normal meta tags
|
57
|
+
email:
|
58
|
+
- macbury@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- Guardfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/meta_info.rb
|
70
|
+
- lib/meta_info/document.rb
|
71
|
+
- lib/meta_info/exception.rb
|
72
|
+
- lib/meta_info/version.rb
|
73
|
+
- meta_info.gemspec
|
74
|
+
- spec/examples/meta_tags.html
|
75
|
+
- spec/examples/opengraph.html
|
76
|
+
- spec/examples/twitter_card.html
|
77
|
+
- spec/meta_info_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
homepage: https://github.com/macbury/MetaInfo
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.0.5
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Simple gem for extracting meta information from web page using opengraph
|
103
|
+
tags, twitter cards tags or normal meta tags
|
104
|
+
test_files:
|
105
|
+
- spec/examples/meta_tags.html
|
106
|
+
- spec/examples/opengraph.html
|
107
|
+
- spec/examples/twitter_card.html
|
108
|
+
- spec/meta_info_spec.rb
|
109
|
+
- spec/spec_helper.rb
|