Class: Roadie::NetHttpProvider
- Inherits:
-
Object
- Object
- Roadie::NetHttpProvider
- Defined in:
- lib/roadie/net_http_provider.rb
Overview
External asset provider that downloads stylesheets from some other server using Ruby’s built-in Net::HTTP library.
You can pass a whitelist of hosts that downloads are allowed on.
Instance Attribute Summary collapse
- #whitelist ⇒ Object readonly
Instance Method Summary collapse
- #find_stylesheet(url) ⇒ Object
- #find_stylesheet!(url) ⇒ Object
-
#initialize(options = {}) ⇒ NetHttpProvider
constructor
A new instance of NetHttpProvider.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ NetHttpProvider
Returns a new instance of NetHttpProvider.
25 26 27 |
# File 'lib/roadie/net_http_provider.rb', line 25 def initialize( = {}) @whitelist = host_set(Array(.fetch(:whitelist, []))) end |
Instance Attribute Details
#whitelist ⇒ Object (readonly)
22 23 24 |
# File 'lib/roadie/net_http_provider.rb', line 22 def whitelist @whitelist end |
Instance Method Details
#find_stylesheet(url) ⇒ Object
29 30 31 32 33 |
# File 'lib/roadie/net_http_provider.rb', line 29 def find_stylesheet(url) find_stylesheet!(url) rescue CssNotFound nil end |
#find_stylesheet!(url) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/roadie/net_http_provider.rb', line 35 def find_stylesheet!(url) response = download(url) if response.is_a? Net::HTTPSuccess Stylesheet.new(url, response_body(response)) else raise CssNotFound.new( css_name: url, message: "Server returned #{response.code}: #{truncate response.body}", provider: self ) end rescue Timeout::Error raise CssNotFound.new(css_name: url, message: "Timeout", provider: self) end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/roadie/net_http_provider.rb', line 54 def inspect "#<#{self.class} whitelist: #{whitelist.inspect}>" end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/roadie/net_http_provider.rb', line 50 def to_s inspect end |