Class: Roadie::NetHttpProvider

Inherits:
Object
  • Object
show all
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.

Examples:

Allowing all downloads

provider = Roadie::NetHttpProvider.new

Only allowing your own app domains

provider = Roadie::NetHttpProvider.new(
  whitelist: ["myapp.com", "assets.myapp.com", "www.myapp.com"]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NetHttpProvider

Returns a new instance of NetHttpProvider.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :whitelist (Array<String>) — default: []

    A list of host names that downloads are allowed from. Empty set means everything is allowed.



25
26
27
# File 'lib/roadie/net_http_provider.rb', line 25

def initialize(options = {})
  @whitelist = host_set(Array(options.fetch(:whitelist, [])))
end

Instance Attribute Details

#whitelistObject (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

#inspectObject



54
55
56
# File 'lib/roadie/net_http_provider.rb', line 54

def inspect
  "#<#{self.class} whitelist: #{whitelist.inspect}>"
end

#to_sObject



50
51
52
# File 'lib/roadie/net_http_provider.rb', line 50

def to_s
  inspect
end