Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user_agent as an option to Parser #146

Merged
merged 4 commits into from Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Unreleased

* Added `user_agent` as an option to Parser [#146](https://github.com/premailer/css_parser/pull/146)

### Version v1.16.0

* Fix parsing space-less media query features like `@media(width:123px)` [#141](https://github.com/premailer/css_parser/pull/141)
Expand Down
6 changes: 3 additions & 3 deletions lib/css_parser/parser.rb
Expand Up @@ -17,7 +17,6 @@ class CircularReferenceError < StandardError; end
# [<tt>io_exceptions</tt>] Throw an exception if a link can not be found. Boolean, default is <tt>true</tt>.
class Parser
USER_AGENT = "Ruby CSS Parser/#{CssParser::VERSION} (https://github.com/premailer/css_parser)"
george-gca marked this conversation as resolved.
Show resolved Hide resolved

STRIP_CSS_COMMENTS_RX = %r{/\*.*?\*/}m.freeze
STRIP_HTML_COMMENTS_RX = /<!--|-->/m.freeze

Expand All @@ -40,7 +39,8 @@ def initialize(options = {})
import: true,
io_exceptions: true,
rule_set_exceptions: true,
capture_offsets: false}.merge(options)
capture_offsets: false,
user_agent: USER_AGENT}.merge(options)

# array of RuleSets
@rules = []
Expand Down Expand Up @@ -597,7 +597,7 @@ def read_remote_file(uri) # :nodoc:
http = Net::HTTP.new(uri.host, uri.port)
end

res = http.get(uri.request_uri, {'User-Agent' => USER_AGENT, 'Accept-Encoding' => 'gzip'})
res = http.get(uri.request_uri, {'User-Agent' => @options[:user_agent], 'Accept-Encoding' => 'gzip'})
src = res.body
charset = res.respond_to?(:charset) ? res.encoding : 'utf-8'

Expand Down