complete PR #96 (don't access the site object when initializing a service)

did committed Jun 27, 2017
commit 5b6b6100c8513bede4f9902378476f54afa3712b
Showing 2 changed files with 22 additions and 20 deletions
locomotive/steam/services/asset_host_service.rb b/lib/locomotive/steam/services/asset_host_service.rb +21 -19
@@ @@ -3,12 +3,10 @@ module Locomotive
class AssetHostService
- attr_reader :request, :site, :host
+ attr_reader :request, :site
- def initialize(request, site, host)
- @request, @site = request, site
-
- @host = build_host(host, request, site)
+ def initialize(request, site, default_host)
+ @request, @site, @default_host = request, site, default_host
end
def compute(source, timestamp = nil)
@@ @@ -23,6 +21,20 @@ module Locomotive
add_timestamp_suffix(url, timestamp)
end
+ def host
+ return @host if @host
+
+ @host = if site.try(:asset_host).present?
+ build_host_with_protocol(site.asset_host)
+ elsif @default_host.respond_to?(:call)
+ @default_host.call(request, site)
+ elsif @default_host.present?
+ build_host_with_protocol(@default_host)
+ else
+ nil
+ end
+ end
+
private
def build_url(host, source)
@@ @@ -30,20 +42,6 @@ module Locomotive
URI.join(host, clean_source).to_s
end
- def build_host(host, request, site)
- if site && site.try(:asset_host) && !site.asset_host.empty?
- site.asset_host =~ Steam::IsHTTP ? site.asset_host : "https://#{site.asset_host}"
- elsif host
- if host.respond_to?(:call)
- host.call(request, site)
- else
- host =~ Steam::IsHTTP ? host : "https://#{host}"
- end
- else
- nil
- end
- end
-
def add_timestamp_suffix(source, timestamp)
if timestamp.nil? || timestamp == 0 || source.include?('?')
source
@@ @@ -52,6 +50,10 @@ module Locomotive
end
end
+ def build_host_with_protocol(host)
+ host =~ Steam::IsHTTP ? host : "https://#{host}"
+ end
+
end
end
locomotive/steam/services/site_finder_service.rb b/lib/locomotive/steam/services/site_finder_service.rb +1 -1
@@ @@ -6,7 +6,7 @@ module Locomotive
attr_accessor_initialize :repository, :request
def find
- repository.by_domain(request.host) if request
+ repository.by_domain(request.host)
end
end