ruby-cheerio 0.0.5
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/lib/ruby-cheerio.rb +69 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1e4c90f3d811b25fe724424e2bac7e6798618dd0
|
4
|
+
data.tar.gz: 432515cd9a9c5d4c4cb00484906faa436f144338
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49543af0319f56e37472d273885c48a23bc2d61fb7efb9bde074cc687a75bb076d44fc1ae40577747f0782ea24317795ae44f812c48ab7f469e32276bd625f99
|
7
|
+
data.tar.gz: 04859a0953a97ac0e10562eb90f135a0ddd2230e32c21372a7c418da70f077f1c2ea39703cb74f81a24e2189fc4ecbc16315a04d90dc16ee5caf103af4145db8
|
data/lib/ruby-cheerio.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Dependent GEM
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
|
5
|
+
class RubyCheerio
|
6
|
+
|
7
|
+
# This creates an attribute readers and writers needed for the instance
|
8
|
+
# variables.
|
9
|
+
|
10
|
+
attr_accessor :text, :html
|
11
|
+
|
12
|
+
# This method defines the instance of a RubyCheerio object with attributes
|
13
|
+
# like text, html and if an argument is not passed, it throws an ArgumentError
|
14
|
+
|
15
|
+
def initialize html=nil
|
16
|
+
if valid_args? html
|
17
|
+
if html.instance_of? Nokogiri::HTML::Document
|
18
|
+
@html_nokogiri = html
|
19
|
+
elsif html.instance_of? String and !html.empty?
|
20
|
+
html = html.strip
|
21
|
+
@html_nokogiri = Nokogiri::HTML(html)
|
22
|
+
end
|
23
|
+
@text = @html_nokogiri.text
|
24
|
+
@html = Nokogiri::HTML(@html_nokogiri.to_html).css('body').inner_html
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# This method can find an element using element-name, class-name, id-name or
|
29
|
+
# all together. This works like finding elements using jQuery styled selectors.
|
30
|
+
|
31
|
+
def find selector=nil
|
32
|
+
if valid_args? selector
|
33
|
+
elements_list = @html_nokogiri.css(selector).to_a.map{|e| self.class.new(e.to_html)}
|
34
|
+
if !elements_list.nil?
|
35
|
+
return elements_list
|
36
|
+
else
|
37
|
+
return nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# This method can return the property of an element, which is selected using the
|
43
|
+
# selector being passed.
|
44
|
+
|
45
|
+
def prop(selector, attribute)
|
46
|
+
if valid_args?(selector, attribute)
|
47
|
+
element = @html_nokogiri.css(selector)
|
48
|
+
if element.instance_of? Nokogiri::XML::NodeSet
|
49
|
+
return element[0][attribute]
|
50
|
+
elsif element.instance_of? Nokogiri::XML::Element
|
51
|
+
return element[attribute]
|
52
|
+
else
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# This is a private method used to validate the argument being passed.
|
61
|
+
|
62
|
+
def valid_args? *args
|
63
|
+
args.each do |arg|
|
64
|
+
raise ArgumentError.new('Invalid Argument') if arg.nil? or (arg.instance_of? String and arg.strip.empty?)
|
65
|
+
end
|
66
|
+
return true
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-cheerio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dineshprabu S
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Ruby Cheerio is a jQuery style HTML parser, which take selectors as input.
|
28
|
+
This is a Ruby version NodeJS package named 'Cheerio', which is extensively used
|
29
|
+
by crawlers. Please visit the home page for usage details.
|
30
|
+
email: dineshsprabu@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/ruby-cheerio.rb
|
36
|
+
homepage: https://github.com/dineshsprabu/ruby-cheerio
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.6.4
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: JQuery styled HTML parser
|
60
|
+
test_files: []
|