Module: Premailer::Rails::CSSLoaders::NetworkLoader

Extended by:
NetworkLoader
Included in:
NetworkLoader
Defined in:
lib/premailer/rails/css_loaders/network_loader.rb

Instance Method Summary collapse

Instance Method Details

#asset_host(url) ⇒ Object



32
33
34
35
# File 'lib/premailer/rails/css_loaders/network_loader.rb', line 32

def asset_host(url)
  config = ::Rails.configuration.action_controller.asset_host
  config.respond_to?(:call) ? config.call(url) : config
end

#asset_host_present?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/premailer/rails/css_loaders/network_loader.rb', line 27

def asset_host_present?
  ::Rails.respond_to?(:configuration) &&
    ::Rails.configuration.action_controller.asset_host.present?
end

#load(url) ⇒ Object



7
8
9
10
# File 'lib/premailer/rails/css_loaders/network_loader.rb', line 7

def load(url)
  uri = uri_for_url(url)
  Net::HTTP.get(uri, { 'Accept' => 'text/css' }) if uri
end

#uri_for_url(url) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/premailer/rails/css_loaders/network_loader.rb', line 12

def uri_for_url(url)
  uri = URI(url)

  if uri.host.present?
    return uri if uri.scheme.present?
    URI("http:#{uri}")
  elsif asset_host_present?
    scheme, host = asset_host(url).split(%r{:?//})
    scheme, host = host, scheme if host.nil?
    scheme = 'http' if scheme.blank?
    path = url
    URI(File.join("#{scheme}://#{host}", path))
  end
end