-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nbulaj/html_adapters
Html adapters (Nokogiri || Oga)
- Loading branch information
Showing
32 changed files
with
444 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
LineLength: | ||
Max: 120 | ||
AllCops: | ||
TargetRubyVersion: 2.4 | ||
TargetRubyVersion: 2.1 | ||
Exclude: | ||
- 'spec/**/*' | ||
- 'bin/*' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec path: '../' | ||
|
||
gem 'nokogiri', '~> 1.8' | ||
|
||
group :test do | ||
gem 'coveralls', require: false | ||
gem 'evil-proxy', '~> 0.2' | ||
gem 'rspec-rails', '~> 3.6' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec path: '../' | ||
|
||
gem 'oga', '~> 2.0' | ||
|
||
group :test do | ||
gem 'coveralls', require: false | ||
gem 'evil-proxy', '~> 0.2' | ||
gem 'rspec-rails', '~> 3.6' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module ProxyFetcher | ||
class Document | ||
class << self | ||
def parse(data) | ||
new(ProxyFetcher.config.adapter.parse(data)) | ||
end | ||
end | ||
|
||
attr_reader :backend | ||
|
||
def initialize(backend) | ||
@backend = backend | ||
end | ||
|
||
def xpath(*args) | ||
backend.xpath(*args).map { |node| backend.proxy_node.new(node) } | ||
end | ||
|
||
def css(*args) | ||
backend.css(*args).map { |node| backend.proxy_node.new(node) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module ProxyFetcher | ||
class Document | ||
class Adapters | ||
ADAPTER = 'Adapter'.freeze | ||
private_constant :ADAPTER | ||
|
||
class << self | ||
def lookup(name_or_class) | ||
raise Exceptions::BlankAdapter if name_or_class.nil? || name_or_class.to_s.empty? | ||
|
||
case name_or_class | ||
when Symbol, String | ||
adapter_name = name_or_class.to_s.capitalize << ADAPTER | ||
ProxyFetcher::Document.const_get(adapter_name) | ||
else | ||
name_or_class | ||
end | ||
rescue NameError | ||
raise Exceptions::UnknownAdapter, name_or_class | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module ProxyFetcher | ||
class Document | ||
class AbstractAdapter | ||
attr_reader :document | ||
|
||
def initialize(document) | ||
@document = document | ||
end | ||
|
||
# You can override this method in your own adapter class | ||
def xpath(selector) | ||
document.xpath(selector) | ||
end | ||
|
||
# You can override this method in your own adapter class | ||
def css(selector) | ||
document.css(selector) | ||
end | ||
|
||
def proxy_node | ||
self.class.const_get('Node') | ||
end | ||
|
||
def self.setup!(*args) | ||
install_requirements!(*args) | ||
rescue LoadError => error | ||
raise Exceptions::AdapterSetupError.new(name, error.message) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module ProxyFetcher | ||
class Document | ||
class NokogiriAdapter < AbstractAdapter | ||
def self.install_requirements! | ||
require 'nokogiri' | ||
end | ||
|
||
def self.parse(data) | ||
new(::Nokogiri::HTML(data)) | ||
end | ||
|
||
class Node < ProxyFetcher::Document::Node | ||
def at_xpath(*args) | ||
self.class.new(node.at_xpath(*args)) | ||
end | ||
|
||
def at_css(*args) | ||
self.class.new(node.at_css(*args)) | ||
end | ||
|
||
def attr(*args) | ||
clear(node.attr(*args)) | ||
end | ||
|
||
def content | ||
clear(node.content) | ||
end | ||
|
||
def html | ||
node.inner_html | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.