Skip to content

Commit

Permalink
Add encoding @config option
Browse files Browse the repository at this point in the history
  • Loading branch information
vifreefly committed Jan 18, 2019
1 parent 1f682b5 commit 96fe695
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/kimurai/browser_builder/mechanize_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def build
logger.debug "BrowserBuilder (mechanize): enabled before_request.delay"
end

# encoding
if encoding = @config[:encoding]
@browser.config.encoding = encoding
logger.debug "BrowserBuilder (mechanize): enabled encoding: #{encoding}"
end

# return Capybara session instance
@browser
end
Expand Down
6 changes: 6 additions & 0 deletions lib/kimurai/browser_builder/poltergeist_phantomjs_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def build
logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled before_request.delay"
end

# encoding
if encoding = @config[:encoding]
@browser.config.encoding = encoding
logger.debug "BrowserBuilder (poltergeist_phantomjs): enabled encoding: #{encoding}"
end

# return Capybara session instance
@browser
end
Expand Down
6 changes: 6 additions & 0 deletions lib/kimurai/browser_builder/selenium_chrome_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ def build
logger.debug "BrowserBuilder (selenium_chrome): enabled before_request.delay"
end

# encoding
if encoding = @config[:encoding]
@browser.config.encoding = encoding
logger.debug "BrowserBuilder (selenium_chrome): enabled encoding: #{encoding}"
end

# return Capybara session instance
@browser
end
Expand Down
6 changes: 6 additions & 0 deletions lib/kimurai/browser_builder/selenium_firefox_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ def build
logger.debug "BrowserBuilder (selenium_firefox): enabled before_request.delay"
end

# encoding
if encoding = @config[:encoding]
@browser.config.encoding = encoding
logger.debug "BrowserBuilder (selenium_firefox): enabled encoding: #{encoding}"
end

# return Capybara session instance
@browser
end
Expand Down
11 changes: 10 additions & 1 deletion lib/kimurai/capybara_ext/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ def restart!
def current_response(response_type = :html)
case response_type
when :html
Nokogiri::HTML(body)
if config.encoding
if config.encoding == :auto
charset = body.force_encoding("ISO-8859-1").encode("UTF-8")[/<meta.*?charset=["]?([\w+\d+\-]*)/i, 1]
Nokogiri::HTML(body, nil, charset)
else
Nokogiri::HTML(body, nil, config.encoding)
end
else
Nokogiri::HTML(body)
end
when :json
JSON.parse(body)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kimurai/capybara_ext/session/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Capybara
class SessionConfig
attr_accessor :cookies, :proxy, :user_agent
attr_accessor :cookies, :proxy, :user_agent, :encoding
attr_writer :retry_request_errors, :skip_request_errors

def retry_request_errors
Expand Down

0 comments on commit 96fe695

Please sign in to comment.