meta_information 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f66071a06238dc6aac14bf94b9c7308c744d87f
4
+ data.tar.gz: 96dc74d10a6542551ed70c7e891d9932b4930ca2
5
+ SHA512:
6
+ metadata.gz: 670b94bc7fdae7a68eed412e51ea0f8648eeab931ff8b6efafde8522694911959d77ae92c779d5e5d5ce77bf5985f3c70c8276fbf16eda1df97fdde6170de4bb
7
+ data.tar.gz: c83c405927fd5b265f1a8cf7fe18cd1b7fffc013f4cdfad091e5aca505a8b758c56980cc476a4c5725b47341c67f12ac335677178efe2b1a5b8b5338beecda7b
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ usage.rb
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.3.3'
3
+
4
+ gem 'nokogiri'
5
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2017 Vladislav Kopylov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # MetaInformation
2
+ Simple gem for parsing meta information from websites. It scan all meta-tags by name or property attributes.
3
+ ## Instalation
4
+ Add this line to your application's Gemfile:
5
+ ```ruby
6
+ gem 'meta_information'
7
+ ```
8
+ Then run `bundle install`
9
+ Or install it yourself as:
10
+ ```sh
11
+ gem install meta_information
12
+ ```
13
+ ## Usage
14
+ ```ruby
15
+ require 'pp'
16
+ meta = MetaInformation.get_meta('https://www.awesome_site.com/awesome_page')
17
+ pp meta
18
+ ###
19
+ #{:succes=>"true",
20
+ # :error=>"",
21
+ # :all_meta=>
22
+ # [{:type=>"name",
23
+ # :name=>"viewport",
24
+ # :content=>"width=device-width, initial-scale=1.0"},
25
+ # {:type=>"name", :name=>"description", :content=>"some description"},
26
+ # {:type=>"name", :name=>"title", :content=>"i am title"},
27
+ # {:type=>"name", :name=>"og:title", :content=>"some content"},
28
+ # {:type=>"name", :name=>"og:description", :content=>"some description"},
29
+ # {:type=>"name",
30
+ # :name=>"og:image",
31
+ # :content=> "https://www.awesome_site.com/assets/awesome_picture.jpg"}]}
32
+ ###
33
+ ```
34
+ ## License
35
+ MIT License.
@@ -0,0 +1,3 @@
1
+ module MetaInformation
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,74 @@
1
+ require './lib/meta_information/version'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
5
+ # MetaInformation - module for scaning meta information
6
+ # form web page
7
+ # for usage
8
+ # MetaInformation.get_meta('https://some_site.com/some_page')
9
+ module MetaInformation
10
+ class << self
11
+ def get_meta(input_url)
12
+ return not_valid_url_error unless valid_url?(input_url)
13
+
14
+ document = create_document(input_url)
15
+ return nokogiri_error if document == false
16
+
17
+ meta_hash = create_meta_array(document)
18
+ success_hash.merge(all_meta: meta_hash)
19
+ end
20
+
21
+ private
22
+
23
+ def create_meta_array(document)
24
+ array = []
25
+ document.css('meta').each do |node|
26
+ if !node['name'].nil?
27
+ array.push(
28
+ type: 'name',
29
+ name: node['name'],
30
+ content: node['content']
31
+ )
32
+ elsif !node['property'].nil?
33
+ array.push(
34
+ type: 'property',
35
+ property: node['property'],
36
+ content: node['content']
37
+ )
38
+ end
39
+ end
40
+ array
41
+ end
42
+
43
+ def valid_url?(uri)
44
+ !!uri.match(/^(https?:\/\/)?([\w\.]+)\.([a-z]{2,6}\.?)(\/[\w\.]*)*\/?$/)
45
+ end
46
+
47
+ def create_document(input_url)
48
+ Nokogiri::HTML(open(input_url))
49
+ rescue
50
+ false
51
+ end
52
+
53
+ def not_valid_url_error
54
+ {
55
+ success: false,
56
+ error: 'url is not valid'
57
+ }
58
+ end
59
+
60
+ def nokogiri_error
61
+ {
62
+ success: false,
63
+ error: 'error with parsing a document'
64
+ }
65
+ end
66
+
67
+ def success_hash
68
+ {
69
+ succes: 'true',
70
+ error: ''
71
+ }
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,15 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "meta_information/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'meta_information'
6
+ s.version = MetaInformation::VERSION
7
+ s.date = '2017-02-26'
8
+ s.summary = 'MetaInformation - Simple gem for parsing meta information'
9
+ s.description = 'Simple gem for parsing meta information from websites. It scan all meta-tags by name or property attributes.'
10
+ s.author = 'Vladislav Kopylov'
11
+ s.email = 'kopylov.vlad@gmail.com'
12
+ s.files = `git ls-files`.split("\n")
13
+ s.homepage = 'https://github.com/kopylovvlad/meta_information'
14
+ s.license = 'MIT'
15
+ end
@@ -0,0 +1,90 @@
1
+ require './lib/meta_information'
2
+
3
+ RSpec.describe 'MetaInformation' do
4
+
5
+ describe 'create_meta_array' do
6
+ describe 'we have meta' do
7
+ it 'must create array' do
8
+ document = Nokogiri::HTML('
9
+ <html>
10
+ <meta name="description" content="" />
11
+ <meta name="title" content="some title" />
12
+ <meta property="author" content="Bob" />
13
+ <meta property="og:title" content="og_title" />
14
+ <meta property="twitter:image" content="http://some_host.com/some_path" />
15
+ <meta property="og:locale" content="ru_RU" />
16
+ <meta property="al:ios:app_store_id" content="12345678900" />
17
+ <body>
18
+ <h1>Mr. Belvedere Fan Club</h1>
19
+ </body>
20
+ </html>
21
+ ')
22
+ expect(MetaInformation.send(:create_meta_array, document)).to eq([
23
+ { type: 'name', name: 'description', content: '' },
24
+ { type: 'name', name: 'title', content: 'some title' },
25
+ { type: 'property', property: 'author', content: 'Bob' },
26
+ { type: 'property', property: 'og:title', content: 'og_title' },
27
+ { type: 'property', property: 'twitter:image', content: 'http://some_host.com/some_path' },
28
+ { type: 'property', property: 'og:locale', content: 'ru_RU' },
29
+ { type: 'property', property: 'al:ios:app_store_id', content: '12345678900' }
30
+ ])
31
+ end
32
+ end
33
+
34
+ describe 'without meta' do
35
+ it 'has empty array' do
36
+ first_document = Nokogiri::HTML('
37
+ <html>
38
+ <body>
39
+ <h1>Mr. Belvedere Fan Club</h1>
40
+ </body>
41
+ </html>
42
+ ')
43
+ second_document = Nokogiri::HTML('')
44
+ expect(MetaInformation.send(:create_meta_array, first_document)).to eq([])
45
+ expect(MetaInformation.send(:create_meta_array, second_document)).to eq([])
46
+ end
47
+ end
48
+ end
49
+
50
+ describe 'valid_url?' do
51
+ it 'must be valid' do
52
+ expect(MetaInformation.send(:valid_url?, 'http://www.somesite.com')).to be_truthy
53
+ expect(MetaInformation.send(:valid_url?, 'https://www.somesite.com')).to be_truthy
54
+ expect(MetaInformation.send(:valid_url?, 'https://somesite.com')).to be_truthy
55
+ expect(MetaInformation.send(:valid_url?, 'wwwsome_site.ru')).to be_truthy
56
+ expect(MetaInformation.send(:valid_url?, 'https://somesite.com/some_page')).to be_truthy
57
+ expect(MetaInformation.send(:valid_url?, 'https://somesite.com/some_page/page')).to be_truthy
58
+ expect(MetaInformation.send(:valid_url?, 'https://somesite.com.uk/some_page')).to be_truthy
59
+ end
60
+
61
+ it 'does not valid' do
62
+ expect(MetaInformation.send(:valid_url?, 'some_site')).to be_falsey
63
+ expect(MetaInformation.send(:valid_url?, 'http\\:wwwsome_site.ru')).to be_falsey
64
+ expect(MetaInformation.send(:valid_url?, 'com.some_site')).to be_falsey
65
+ end
66
+ end
67
+
68
+ describe 'private hash equal' do
69
+ it 'not_valid_url_error hash' do
70
+ expect(MetaInformation.send(:not_valid_url_error)).to eq({
71
+ success: false,
72
+ error: 'url is not valid'
73
+ })
74
+ end
75
+
76
+ it 'nokogiri_error hash' do
77
+ expect(MetaInformation.send(:nokogiri_error)).to eq({
78
+ success: false,
79
+ error: 'error with parsing a document'
80
+ })
81
+ end
82
+
83
+ it 'success_hash hash' do
84
+ expect(MetaInformation.send(:success_hash)).to eq({
85
+ succes: 'true',
86
+ error: ''
87
+ })
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta_information
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladislav Kopylov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple gem for parsing meta information from websites. It scan all meta-tags
14
+ by name or property attributes.
15
+ email: kopylov.vlad@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".ruby-version"
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - lib/meta_information.rb
26
+ - lib/meta_information/version.rb
27
+ - meta_information.gemspec
28
+ - spec/lib/meta_information_spec.rb
29
+ homepage: https://github.com/kopylovvlad/meta_information
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.6.10
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: MetaInformation - Simple gem for parsing meta information
53
+ test_files: []