rename services (+ specs)

did committed Mar 08, 2015
commit 24635ef862358a5594f9d3583e9b4cd2823bb5f9
Showing 55 changed files with 1186 additions and 1237 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb +1 -2
@@ @@ -13,8 +13,7 @@ require_relative 'steam/liquid'
require_relative 'steam/models'
require_relative_all 'steam/entities'
- require_relative_all 'steam/repositories'
-
+ require_relative 'steam/repositories'
require_relative 'steam/services'
module Locomotive
locomotive/steam/repositories.rb b/lib/locomotive/steam/repositories.rb +45 -0
@@ @@ -0,0 +1,45 @@
+ require_relative_all 'repositories'
+
+ module Locomotive
+ module Steam
+
+ class Repositories < Struct.new(:site, :locale, :configuration)
+
+ include Morphine
+
+ register :adapter do
+ require_relative 'adapters/filesystem'
+ Steam::FilesystemAdapter.new(configuration.site_path)
+ end
+
+ register :site do
+ SiteRepository.new(adapter, nil, locale)
+ end
+
+ register :page do
+ PageRepository.new(adapter, site, locale)
+ end
+
+ register :snippet do
+ SnippetRepository.new(adapter, site, locale)
+ end
+
+ register :content_type do
+ ContentTypeRepository.new(adapter, site, locale)
+ end
+
+ register :content_entry do
+ ContentEntryRepository.new(adapter, site, locale, content_type)
+ end
+
+ register :theme_asset do
+ ThemeAssetRepository.new(adapter, site, locale)
+ end
+
+ register :translation do
+ TranslationRepository.new(adapter, site, locale)
+ end
+
+ end
+ end
+ end
locomotive/steam/services.rb b/lib/locomotive/steam/services.rb +26 -29
@@ @@ -4,12 +4,13 @@ require_relative_all %w(concerns .), 'services'
module Locomotive
module Steam
+
module Services
def self.build_instance(request = nil)
- Instance.new(request).tap do |service|
+ Instance.new(request).tap do |instance|
if Locomotive::Steam.configuration.services_hook
- Locomotive::Steam.configuration.services_hook.call(service)
+ Locomotive::Steam.configuration.services_hook.call(instance)
end
end
end
@@ @@ -19,97 +20,93 @@ module Locomotive
include Morphine
register :repositories do
- require_relative 'repositories/filesystem.rb'
- Steam::Repositories::Filesystem.build_instance(nil, nil, configuration.site_path)
+ Steam::Repositories.new(nil, nil, configuration)
end
register :site_finder do
- Steam::Services::SiteFinder.new(repositories.site, request)
+ Steam::SiteFinderService.new(repositories.site, request)
end
register :page_finder do
- Steam::Services::PageFinder.new(repositories.page)
+ Steam::PageFinderService.new(repositories.page)
end
register :parent_finder do
- Steam::Services::ParentFinder.new(repositories.page)
+ Steam::ParentFinderService.new(repositories.page)
end
register :snippet_finder do
- Steam::Services::SnippetFinder.new(repositories.snippet)
+ Steam::SnippetFinderService.new(repositories.snippet)
end
register :entry_submission do
- Steam::Services::EntrySubmission.new(repositories.content_type, repositories.content_entry, current_locale)
+ Steam::EntrySubmissionService.new(repositories.content_type, repositories.content_entry, locale)
end
register :liquid_parser do
- Steam::Services::LiquidParser.new(parent_finder, snippet_finder)
+ Steam::LiquidParserService.new(parent_finder, snippet_finder)
end
register :url_builder do
- Steam::Services::UrlBuilder.new(current_site, current_locale)
+ Steam::UrlBuilderService.new(current_site, locale)
end
register :theme_asset_url do
- Steam::Services::ThemeAssetUrl.new(repositories.theme_asset, asset_host, configuration.theme_assets_checksum)
+ Steam::ThemeAssetUrlService.new(repositories.theme_asset, asset_host, configuration.theme_assets_checksum)
end
register :asset_host do
- Steam::Services::AssetHost.new(request, current_site, configuration.asset_host)
+ Steam::AssetHostService.new(request, current_site, configuration.asset_host)
end
register :image_resizer do
- Steam::Services::ImageResizer.new(::Dragonfly.app(:steam), configuration.assets_path)
+ Steam::ImageResizerService.new(::Dragonfly.app(:steam), configuration.assets_path)
end
register :translator do
- Steam::Services::Translator.new(repositories.translation, current_locale)
+ Steam::TranslatorService.new(repositories.translation, locale)
end
register :external_api do
- Steam::Services::ExternalAPI.new
+ Steam::ExternalAPIService.new
end
register :csrf_protection do
- Steam::Services::CsrfProtection.new(configuration.csrf_protection, Rack::Csrf.field, Rack::Csrf.token(request.env))
- end
-
- register :cache do
- Steam::Services::NoCache.new
+ Steam::CsrfProtectionService.new(configuration.csrf_protection, Rack::Csrf.field, Rack::Csrf.token(request.env))
end
register :markdown do
- Steam::Services::Markdown.new
+ Steam::MarkdownService.new
end
register :textile do
- Steam::Services::Textile.new
+ Steam::TextileService.new
end
register :configuration do
Locomotive::Steam.configuration
end
- register :current_locale do
+ register :locale do
I18n.locale
end
- def current_locale
- @current_locale || I18n.locale
+ def locale
+ @locale || I18n.locale
end
- def current_locale=(locale)
+ def locale=(locale)
# Note: "repositories" has already been initialized when called here.
- @current_locale = repositories.current_locale = locale
+ @locale = repositories.locale = locale
end
def current_site
- repositories.current_site
+ repositories.site
end
end
end
+
end
end
locomotive/steam/services/asset_host.rb b/lib/locomotive/steam/services/asset_host.rb +0 -53
@@ @@ -1,53 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class AssetHost
-
- IsHTTP = /^https?:\/\//o
-
- attr_reader :request, :site, :host
-
- def initialize(request, site, host)
- @request, @site = request, site
-
- @host = build_host(host, request, site)
- end
-
- def compute(source, timestamp = nil)
- return source if source.nil?
-
- return add_timestamp_suffix(source, timestamp) if source =~ IsHTTP
-
- url = self.host ? URI.join(host, source).to_s : source
-
- add_timestamp_suffix(url, timestamp)
- end
-
- private
-
- def build_host(host, request, site)
- if host
- if host.respond_to?(:call)
- host.call(request, site)
- else
- host
- end
- else
- nil
- end
- end
-
- def add_timestamp_suffix(source, timestamp)
- if timestamp.nil? || timestamp == 0 || source.include?('?')
- source
- else
- "#{source}?#{timestamp}"
- end
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/asset_host_service.rb b/lib/locomotive/steam/services/asset_host_service.rb +51 -0
@@ @@ -0,0 +1,51 @@
+ module Locomotive
+ module Steam
+
+ class AssetHostService
+
+ IsHTTP = /^https?:\/\//o
+
+ attr_reader :request, :site, :host
+
+ def initialize(request, site, host)
+ @request, @site = request, site
+
+ @host = build_host(host, request, site)
+ end
+
+ def compute(source, timestamp = nil)
+ return source if source.nil?
+
+ return add_timestamp_suffix(source, timestamp) if source =~ IsHTTP
+
+ url = self.host ? URI.join(host, source).to_s : source
+
+ add_timestamp_suffix(url, timestamp)
+ end
+
+ private
+
+ def build_host(host, request, site)
+ if host
+ if host.respond_to?(:call)
+ host.call(request, site)
+ else
+ host
+ end
+ else
+ nil
+ end
+ end
+
+ def add_timestamp_suffix(source, timestamp)
+ if timestamp.nil? || timestamp == 0 || source.include?('?')
+ source
+ else
+ "#{source}?#{timestamp}"
+ end
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/concerns/decorator.rb b/lib/locomotive/steam/services/concerns/decorator.rb +1 -1
@@ @@ -20,7 +20,7 @@ module Locomotive
end
def locale
- repository.current_locale
+ repository.locale
end
def default_locale
locomotive/steam/services/csrf_protection.rb b/lib/locomotive/steam/services/csrf_protection.rb +0 -15
@@ @@ -1,15 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class CsrfProtection < Struct.new(:enabled, :field, :token)
-
- def enabled?
- !!enabled
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/csrf_protection_service.rb b/lib/locomotive/steam/services/csrf_protection_service.rb +13 -0
@@ @@ -0,0 +1,13 @@
+ module Locomotive
+ module Steam
+
+ class CsrfProtectionService < Struct.new(:enabled, :field, :token)
+
+ def enabled?
+ !!enabled
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/entry_submission.rb b/lib/locomotive/steam/services/entry_submission.rb +54 -55
@@ @@ -2,91 +2,90 @@ require 'sanitize'
module Locomotive
module Steam
- module Services
- class EntrySubmission < Struct.new(:content_type_repository, :repository, :current_locale)
+ class EntrySubmissionService < Struct.new(:content_type_repository, :repository, :current_locale)
- include Locomotive::Steam::Services::Concerns::Decorator
+ include Locomotive::Steam::Services::Concerns::Decorator
- def submit(slug, attributes = {})
- type = get_type(slug)
+ def submit(slug, attributes = {})
+ type = get_type(slug)
- return nil if type.nil?
+ return nil if type.nil?
- clean_attributes(attributes)
+ clean_attributes(attributes)
- build_entry(type, attributes) do |entry|
- if validate(entry)
- repository.persist(entry)
- end
+ build_entry(type, attributes) do |entry|
+ if validate(entry)
+ repository.persist(entry)
end
end
+ end
- def find(type_slug, slug)
- type = get_type(type_slug)
+ def find(type_slug, slug)
+ type = get_type(type_slug)
- return nil if type.nil?
+ return nil if type.nil?
- i18n_decorate { repository.by_slug(type, slug) }
- end
+ i18n_decorate { repository.by_slug(type, slug) }
+ end
- def to_json(entry)
- return nil if entry.nil?
+ def to_json(entry)
+ return nil if entry.nil?
- # default values
- hash = { _slug: entry._slug, content_type_slug: entry.content_type_slug }
+ # default values
+ hash = { _slug: entry._slug, content_type_slug: entry.content_type_slug }
- # dynamic attributes
- content_type_repository.fields_for(entry.content_type).each do |field|
- next if %w(belongs_to has_many many_to_many).include?(field.type.to_s)
+ # dynamic attributes
+ content_type_repository.fields_for(entry.content_type).each do |field|
+ next if %w(belongs_to has_many many_to_many).include?(field.type.to_s)
- hash[field.name] = entry.send(field.name)
- end
+ hash[field.name] = entry.send(field.name)
+ end
- # errors
- hash[:errors] = entry.errors.messages unless entry.errors.empty?
+ # errors
+ hash[:errors] = entry.errors.messages unless entry.errors.empty?
- hash.to_json
- end
+ hash.to_json
+ end
- private
+ private
- def get_type(slug)
- return nil if slug.blank?
+ def get_type(slug)
+ return nil if slug.blank?
- content_type_repository.by_slug(slug)
- end
+ content_type_repository.by_slug(slug)
+ end
- def build_entry(type, attributes, &block)
- i18n_decorate { repository.build(type, attributes) }.tap do |entry|
- yield(entry)
- end
+ def build_entry(type, attributes, &block)
+ i18n_decorate { repository.build(type, attributes) }.tap do |entry|
+ yield(entry)
end
+ end
- def validate(entry)
- # simple validations (existence of values) first
- entry.valid?
+ def validate(entry)
+ # simple validations (existence of values) first
+ entry.valid?
- # check if the entry has unique values for its
- # fields marked as unique are really
- content_type_repository.look_for_unique_fields(entry.content_type).each do |name, field|
- if repository.exists?(entry.content_type, name, entry.send(name))
- entry.errors.add(name, :unique)
- end
+ # check if the entry has unique values for its
+ # fields marked as unique are really
+ content_type_repository.look_for_unique_fields(entry.content_type).each do |name, field|
+ if repository.exists?(entry.content_type, name, entry.send(name))
+ entry.errors.add(name, :unique)
end
-
- entry.errors.empty?
end
- def clean_attributes(attributes)
- attributes.each do |key, value|
- next unless value.is_a?(String)
- attributes[key] = Sanitize.clean(value, Sanitize::Config::BASIC)
- end
- end
+ entry.errors.empty?
+ end
+ def clean_attributes(attributes)
+ attributes.each do |key, value|
+ next unless value.is_a?(String)
+ attributes[key] = Sanitize.clean(value, Sanitize::Config::BASIC)
+ end
end
end
+
end
end
+
locomotive/steam/services/external_api.rb b/lib/locomotive/steam/services/external_api.rb +0 -57
@@ @@ -1,57 +0,0 @@
- require 'uri'
- require 'httparty'
-
- module Locomotive
- module Steam
- module Services
- class ExternalAPI
-
- include ::HTTParty
-
- def consume(url, options = {})
- options[:base_uri], path = extract_base_uri_and_path(url)
-
- options.delete(:format) if options[:format] == 'default'
-
- # auth ?
- username, password = options.delete(:username), options.delete(:password)
- options[:basic_auth] = { username: username, password: password } if username
-
- perform_request_to(path, options)
- end
-
- private
-
- def extract_base_uri_and_path(url)
- url = HTTParty.normalize_base_uri(url)
-
- uri = URI.parse(url)
- path = uri.request_uri || '/'
- base_uri = "#{uri.scheme}://#{uri.host}"
- base_uri += ":#{uri.port}" if uri.port != 80
-
- [base_uri, path]
- end
-
- def perform_request_to(path, options)
- # [DEBUG] puts "[WebService] consuming #{path}, #{options.inspect}"
-
- # sanitize the options
- options[:format] = options[:format].gsub(/[\'\"]/, '').to_sym if options.has_key?(:format)
- options[:headers] = { 'User-Agent' => 'LocomotiveCMS' } if options[:with_user_agent]
-
- response = self.class.get(path, options)
- parsed_response = response.parsed_response
-
- if response.code == 200
- HashConverter.to_underscore parsed_response
- else
- Locomotive::Common::Logger.error "[WebService] consumed #{path}, #{options.inspect}, response = #{response.inspect}"
- nil
- end
- end
-
- end
- end
- end
- end
locomotive/steam/services/external_api_service.rb b/lib/locomotive/steam/services/external_api_service.rb +55 -0
@@ @@ -0,0 +1,55 @@
+ require 'uri'
+ require 'httparty'
+
+ module Locomotive
+ module Steam
+ class ExternalAPIService
+
+ include ::HTTParty
+
+ def consume(url, options = {})
+ options[:base_uri], path = extract_base_uri_and_path(url)
+
+ options.delete(:format) if options[:format] == 'default'
+
+ # auth ?
+ username, password = options.delete(:username), options.delete(:password)
+ options[:basic_auth] = { username: username, password: password } if username
+
+ perform_request_to(path, options)
+ end
+
+ private
+
+ def extract_base_uri_and_path(url)
+ url = HTTParty.normalize_base_uri(url)
+
+ uri = URI.parse(url)
+ path = uri.request_uri || '/'
+ base_uri = "#{uri.scheme}://#{uri.host}"
+ base_uri += ":#{uri.port}" if uri.port != 80
+
+ [base_uri, path]
+ end
+
+ def perform_request_to(path, options)
+ # [DEBUG] puts "[WebService] consuming #{path}, #{options.inspect}"
+
+ # sanitize the options
+ options[:format] = options[:format].gsub(/[\'\"]/, '').to_sym if options.has_key?(:format)
+ options[:headers] = { 'User-Agent' => 'LocomotiveCMS' } if options[:with_user_agent]
+
+ response = self.class.get(path, options)
+ parsed_response = response.parsed_response
+
+ if response.code == 200
+ HashConverter.to_underscore parsed_response
+ else
+ Locomotive::Common::Logger.error "[WebService] consumed #{path}, #{options.inspect}, response = #{response.inspect}"
+ nil
+ end
+ end
+
+ end
+ end
+ end
locomotive/steam/services/image_resizer.rb b/lib/locomotive/steam/services/image_resizer.rb +0 -51
@@ @@ -1,51 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class ImageResizer < Struct.new(:resizer, :assets_path)
-
- IsHTTP = /^https?:\/\//o
-
- def resize(source, geometry)
- return nil if disabled? || geometry.blank?
-
- if file = fetch_file(source)
- file.thumb(geometry).url
- else
- Locomotive::Common::Logger.error "Unable to resize on the fly: #{source.inspect}"
- nil
- end
- end
-
- def disabled?
- resizer.nil? || resizer.plugins[:imagemagick].nil?
- end
-
- protected
-
- def fetch_file(source)
- url_or_path = get_url_or_path(source)
-
- if url_or_path =~ IsHTTP
- resizer.fetch_url(url_or_path)
- else
- path = url_or_path.sub(/(\?.*)$/, '')
- resizer.fetch_file(File.join(assets_path || '', 'public', path))
- end
- end
-
- def get_url_or_path(source)
- if source.is_a?(Hash)
- source['url']
- elsif source.respond_to?(:url)
- source.url
- else
- source
- end.strip
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/image_resizer_service.rb b/lib/locomotive/steam/services/image_resizer_service.rb +49 -0
@@ @@ -0,0 +1,49 @@
+ module Locomotive
+ module Steam
+
+ class ImageResizerService < Struct.new(:resizer, :assets_path)
+
+ IsHTTP = /^https?:\/\//o
+
+ def resize(source, geometry)
+ return nil if disabled? || geometry.blank?
+
+ if file = fetch_file(source)
+ file.thumb(geometry).url
+ else
+ Locomotive::Common::Logger.error "Unable to resize on the fly: #{source.inspect}"
+ nil
+ end
+ end
+
+ def disabled?
+ resizer.nil? || resizer.plugins[:imagemagick].nil?
+ end
+
+ protected
+
+ def fetch_file(source)
+ url_or_path = get_url_or_path(source)
+
+ if url_or_path =~ IsHTTP
+ resizer.fetch_url(url_or_path)
+ else
+ path = url_or_path.sub(/(\?.*)$/, '')
+ resizer.fetch_file(File.join(assets_path || '', 'public', path))
+ end
+ end
+
+ def get_url_or_path(source)
+ if source.is_a?(Hash)
+ source['url']
+ elsif source.respond_to?(:url)
+ source.url
+ else
+ source
+ end.strip
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/liquid_parser.rb b/lib/locomotive/steam/services/liquid_parser.rb +0 -25
@@ @@ -1,25 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class LiquidParser < Struct.new(:parent_finder, :snippet_finder)
-
- def parse(page, events_listener = nil)
- _parse(page,
- page: page,
- events_listener: events_listener,
- parent_finder: parent_finder,
- snippet_finder: snippet_finder,
- parser: self)
- end
-
- def _parse(object, options = {})
- # Note: check if the template has already been parsed (caching?)
- object.template ||= ::Liquid::Template.parse(object.liquid_source, options)
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/liquid_parser_service.rb b/lib/locomotive/steam/services/liquid_parser_service.rb +23 -0
@@ @@ -0,0 +1,23 @@
+ module Locomotive
+ module Steam
+
+ class LiquidParserService < Struct.new(:parent_finder, :snippet_finder)
+
+ def parse(page, events_listener = nil)
+ _parse(page,
+ page: page,
+ events_listener: events_listener,
+ parent_finder: parent_finder,
+ snippet_finder: snippet_finder,
+ parser: self)
+ end
+
+ def _parse(object, options = {})
+ # Note: check if the template has already been parsed (caching?)
+ object.template ||= ::Liquid::Template.parse(object.liquid_source, options)
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/markdown.rb b/lib/locomotive/steam/services/markdown.rb +0 -19
@@ @@ -1,19 +0,0 @@
- require 'kramdown'
-
- module Locomotive
- module Steam
- module Services
-
- class Markdown
-
- def to_html(text)
- return '' if text.blank?
-
- Kramdown::Document.new(text, auto_ids: false).to_html
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/markdown_service.rb b/lib/locomotive/steam/services/markdown_service.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ require 'kramdown'
+
+ module Locomotive
+ module Steam
+
+ class MarkdownService
+
+ def to_html(text)
+ return '' if text.blank?
+
+ Kramdown::Document.new(text, auto_ids: false).to_html
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/no_cache.rb b/lib/locomotive/steam/services/no_cache.rb +0 -19
@@ @@ -1,19 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class NoCache
-
- def fetch(key, options = {}, &block)
- @last_response = block.call
- end
-
- def read(key)
- nil
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/page_finder.rb b/lib/locomotive/steam/services/page_finder.rb +0 -46
@@ @@ -1,46 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class PageFinder < Struct.new(:repository)
-
- include Locomotive::Steam::Services::Concerns::Decorator
-
- def find(path)
- decorate do
- repository.by_fullpath(path)
- end
- end
-
- def match(path)
- decorate do
- repository.matching_fullpath(path_combinations(path))
- end
- end
-
- private
-
- def path_combinations(path)
- _path_combinations(path.split('/'))
- end
-
- def _path_combinations(segments, can_include_template = true)
- return nil if segments.empty?
- segment = segments.shift
-
- (can_include_template ? [segment, WILDCARD] : [segment]).map do |_segment|
- if (_combinations = _path_combinations(segments.clone, can_include_template && _segment != WILDCARD))
- [*_combinations].map do |_combination|
- File.join(_segment, _combination)
- end
- else
- [_segment]
- end
- end.flatten
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/page_finder_service.rb b/lib/locomotive/steam/services/page_finder_service.rb +44 -0
@@ @@ -0,0 +1,44 @@
+ module Locomotive
+ module Steam
+
+ class PageFinderService < Struct.new(:repository)
+
+ include Locomotive::Steam::Services::Concerns::Decorator
+
+ def find(path)
+ decorate do
+ repository.by_fullpath(path)
+ end
+ end
+
+ def match(path)
+ decorate do
+ repository.matching_fullpath(path_combinations(path))
+ end
+ end
+
+ private
+
+ def path_combinations(path)
+ _path_combinations(path.split('/'))
+ end
+
+ def _path_combinations(segments, can_include_template = true)
+ return nil if segments.empty?
+ segment = segments.shift
+
+ (can_include_template ? [segment, WILDCARD] : [segment]).map do |_segment|
+ if (_combinations = _path_combinations(segments.clone, can_include_template && _segment != WILDCARD))
+ [*_combinations].map do |_combination|
+ File.join(_segment, _combination)
+ end
+ else
+ [_segment]
+ end
+ end.flatten
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/parent_finder.rb b/lib/locomotive/steam/services/parent_finder.rb +0 -25
@@ @@ -1,25 +0,0 @@
- require_relative 'page_finder'
-
- module Locomotive
- module Steam
- module Services
-
- class ParentFinder < PageFinder
-
- def find(page, fullpath)
- return nil if fullpath.blank?
-
- decorate do
- if fullpath.strip == 'parent'
- repository.parent_of(page)
- else
- repository.by_fullpath(fullpath)
- end
- end
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/parent_finder_service.rb b/lib/locomotive/steam/services/parent_finder_service.rb +23 -0
@@ @@ -0,0 +1,23 @@
+ require_relative 'page_finder_service'
+
+ module Locomotive
+ module Steam
+
+ class ParentFinderService < PageFinderService
+
+ def find(page, fullpath)
+ return nil if fullpath.blank?
+
+ decorate do
+ if fullpath.strip == 'parent'
+ repository.parent_of(page)
+ else
+ repository.by_fullpath(fullpath)
+ end
+ end
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/site_finder.rb b/lib/locomotive/steam/services/site_finder.rb +0 -16
@@ @@ -1,16 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class SiteFinder < Struct.new(:repository, :request)
-
- def find
- # TODO: full uri instead?
- repository.by_host(request.host)
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/site_finder_service.rb b/lib/locomotive/steam/services/site_finder_service.rb +14 -0
@@ @@ -0,0 +1,14 @@
+ module Locomotive
+ module Steam
+
+ class SiteFinder < Struct.new(:repository, :request)
+
+ def find
+ # TODO: full uri instead?
+ repository.by_host(request.host)
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/snippet_finder.rb b/lib/locomotive/steam/services/snippet_finder.rb +0 -19
@@ @@ -1,19 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class SnippetFinder < Struct.new(:repository)
-
- include Locomotive::Steam::Services::Concerns::Decorator
-
- def find(slug)
- decorate do
- repository.by_slug(slug)
- end
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/snippet_finder_service.rb b/lib/locomotive/steam/services/snippet_finder_service.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ module Locomotive
+ module Steam
+
+ class SnippetFinderService < Struct.new(:repository)
+
+ include Locomotive::Steam::Services::Concerns::Decorator
+
+ def find(slug)
+ decorate do
+ repository.by_slug(slug)
+ end
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/textile.rb b/lib/locomotive/steam/services/textile.rb +0 -19
@@ @@ -1,19 +0,0 @@
- require 'RedCloth'
-
- module Locomotive
- module Steam
- module Services
-
- class Textile
-
- def to_html(text)
- return '' if text.blank?
-
- ::RedCloth.new(text).to_html
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/textile_service.rb b/lib/locomotive/steam/services/textile_service.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ require 'RedCloth'
+
+ module Locomotive
+ module Steam
+
+ class TextileService
+
+ def to_html(text)
+ return '' if text.blank?
+
+ ::RedCloth.new(text).to_html
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/theme_asset_url.rb b/lib/locomotive/steam/services/theme_asset_url.rb +0 -45
@@ @@ -1,45 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class ThemeAssetUrl < Struct.new(:repository, :asset_host, :checksum)
-
- def build(path)
- # keep the query string safe
- path.gsub!(/(\?+.+)$/, '')
- query_string = $1
-
- # build the url of the theme asset based on the persistence layer
- _url = repository.url_for(path)
-
- # get a timestamp only the source url does not include a query string
- timestamp = query_string.blank? ? checksums[path] : nil
-
- # prefix by a asset host if given
- url = asset_host ? asset_host.compute(_url, timestamp) : _url
-
- query_string ? "#{url}#{query_string}" : url
- end
-
- def checksums
- if checksum?
- @checksums ||= fetch_checksums
- else
- {}
- end
- end
-
- def checksum?
- !!checksum
- end
-
- private
-
- def fetch_checksums
- repository.checksums
- end
-
- end
- end
- end
- end
locomotive/steam/services/theme_asset_url_service.rb b/lib/locomotive/steam/services/theme_asset_url_service.rb +43 -0
@@ @@ -0,0 +1,43 @@
+ module Locomotive
+ module Steam
+
+ class ThemeAssetUrlService < Struct.new(:repository, :asset_host, :checksum)
+
+ def build(path)
+ # keep the query string safe
+ path.gsub!(/(\?+.+)$/, '')
+ query_string = $1
+
+ # build the url of the theme asset based on the persistence layer
+ _url = repository.url_for(path)
+
+ # get a timestamp only the source url does not include a query string
+ timestamp = query_string.blank? ? checksums[path] : nil
+
+ # prefix by a asset host if given
+ url = asset_host ? asset_host.compute(_url, timestamp) : _url
+
+ query_string ? "#{url}#{query_string}" : url
+ end
+
+ def checksums
+ if checksum?
+ @checksums ||= fetch_checksums
+ else
+ {}
+ end
+ end
+
+ def checksum?
+ !!checksum
+ end
+
+ private
+
+ def fetch_checksums
+ repository.checksums
+ end
+
+ end
+ end
+ end
locomotive/steam/services/translator.rb b/lib/locomotive/steam/services/translator.rb +0 -36
@@ @@ -1,36 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class Translator < Struct.new(:repository, :current_locale)
-
- # Return the translation described by a key.
- #
- # @param [ String ] key The key of the translation.
- # @param [ String ] locale The locale we want the translation in
- # @param [ String ] scope If specified, instead of looking in the translations, it will use I18n instead.
- #
- # @return [ String ] the translated text or nil if not found
- #
- def translate(input, locale, scope = nil)
- locale ||= current_locale
-
- if scope.blank?
- values = repository.find(input).try(:values) || {}
-
- if translation = values[locale.to_s]
- translation
- else
- Locomotive::Common::Logger.warn "Missing translation '#{input}' for the '#{locale}' locale".yellow
- input
- end
- else
- I18n.t(input, scope: scope.split('.'), locale: locale)
- end
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/translator_service.rb b/lib/locomotive/steam/services/translator_service.rb +34 -0
@@ @@ -0,0 +1,34 @@
+ module Locomotive
+ module Steam
+
+ class TranslatorService < Struct.new(:repository, :current_locale)
+
+ # Return the translation described by a key.
+ #
+ # @param [ String ] key The key of the translation.
+ # @param [ String ] locale The locale we want the translation in
+ # @param [ String ] scope If specified, instead of looking in the translations, it will use I18n instead.
+ #
+ # @return [ String ] the translated text or nil if not found
+ #
+ def translate(input, locale, scope = nil)
+ locale ||= current_locale
+
+ if scope.blank?
+ values = repository.find(input).try(:values) || {}
+
+ if translation = values[locale.to_s]
+ translation
+ else
+ Locomotive::Common::Logger.warn "Missing translation '#{input}' for the '#{locale}' locale".yellow
+ input
+ end
+ else
+ I18n.t(input, scope: scope.split('.'), locale: locale)
+ end
+ end
+
+ end
+
+ end
+ end
locomotive/steam/services/url_builder.rb b/lib/locomotive/steam/services/url_builder.rb +0 -42
@@ @@ -1,42 +0,0 @@
- module Locomotive
- module Steam
- module Services
-
- class UrlBuilder < Struct.new(:site, :current_locale)
-
- def url_for(page, locale = nil)
- [''].tap do |segments|
- locale ||= current_locale
- same_locale = locale.to_sym == site.default_locale.to_sym
-
- # locale
- segments << locale unless same_locale
-
- # fullpath
- segments << sanitized_fullpath(page, same_locale)
- end.compact.join('/')
- end
-
- def public_submission_url_for(content_type)
- "/entry_submissions/#{content_type.slug}"
- end
-
- private
-
- def sanitized_fullpath(page, same_locale)
- path = page.fullpath
-
- if page.templatized? && page.content_entry
- path.gsub(Locomotive::Steam::WILDCARD, page.content_entry._slug)
- elsif path == 'index'
- same_locale ? '' : nil
- else
- path
- end
- end
-
- end
-
- end
- end
- end
locomotive/steam/services/url_builder_service.rb b/lib/locomotive/steam/services/url_builder_service.rb +40 -0
@@ @@ -0,0 +1,40 @@
+ module Locomotive
+ module Steam
+
+ class UrlBuilderService < Struct.new(:site, :current_locale)
+
+ def url_for(page, locale = nil)
+ [''].tap do |segments|
+ locale ||= current_locale
+ same_locale = locale.to_sym == site.default_locale.to_sym
+
+ # locale
+ segments << locale unless same_locale
+
+ # fullpath
+ segments << sanitized_fullpath(page, same_locale)
+ end.compact.join('/')
+ end
+
+ def public_submission_url_for(content_type)
+ "/entry_submissions/#{content_type.slug}"
+ end
+
+ private
+
+ def sanitized_fullpath(page, same_locale)
+ path = page.fullpath
+
+ if page.templatized? && page.content_entry
+ path.gsub(Locomotive::Steam::WILDCARD, page.content_entry._slug)
+ elsif path == 'index'
+ same_locale ? '' : nil
+ else
+ path
+ end
+ end
+
+ end
+
+ end
+ end
spec/integration/services/external_api_service_spec.rb +20 -0
@@ @@ -0,0 +1,20 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::ExternalAPIService do
+
+ pending 'API rate limit exceeded'
+
+ let(:service) { described_class.new }
+
+ describe '#consume' do
+
+ let(:url) { 'https://api.github.com/users/did/repos' }
+ let(:options) { { format: "'json'", with_user_agent: true } }
+
+ subject { service.consume(url, options) }
+
+ it { expect(subject.size).to_not eq 0 }
+
+ end
+
+ end
spec/integration/services/external_api_spec.rb +0 -20
@@ @@ -1,20 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::ExternalAPI do
-
- pending 'API rate limit exceeded'
-
- let(:service) { Locomotive::Steam::Services::ExternalAPI.new }
-
- describe '#consume' do
-
- let(:url) { 'https://api.github.com/users/did/repos' }
- let(:options) { { format: "'json'", with_user_agent: true } }
-
- subject { service.consume(url, options) }
-
- it { expect(subject.size).to_not eq 0 }
-
- end
-
- end
spec/unit/repositories/filesystem_spec.rb +0 -44
@@ @@ -1,44 +0,0 @@
- # require 'spec_helper'
-
- # describe Locomotive::Steam::Repositories::Filesystem do
-
- # let(:site) { instance_double('Site', name: 'PCH') }
- # let(:repositories) { Locomotive::Steam::Repositories::Filesystem.build_instance(site) }
-
- # describe '#theme_asset' do
-
- # subject { repositories.theme_asset }
-
- # context 'by default' do
-
- # it 'returns a class of ThemeAssetRepository' do
- # expect(subject.class).to eq Locomotive::Steam::Repositories::Filesystem::ThemeAsset
- # end
-
- # it 'gets access to the site' do
- # expect(subject.site.name).to eq 'PCH'
- # end
-
- # end
-
- # context 'a different repository' do
-
- # before do
- # repositories.theme_asset = MyThemeAssetRepository.new(site)
- # end
-
- # it 'returns a class of ThemeAssetRepository' do
- # expect(subject.class).to eq MyThemeAssetRepository
- # end
-
- # it 'gets access to the site' do
- # expect(subject.site.name).to eq 'PCH'
- # end
-
- # end
-
- # end
-
- # class MyThemeAssetRepository < Struct.new(:site); end
-
- # end
spec/unit/services/asset_host_service_spec.rb +79 -0
@@ @@ -0,0 +1,79 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::AssetHostService do
+
+ let(:request) { nil }
+ let(:site) { nil }
+ let(:host) { nil }
+ let(:timestamp) { nil }
+ let(:asset_host) { described_class.new(request, site, host) }
+ let(:source) { '/sites/42/assets/1/banner.png' }
+
+ subject { asset_host.compute(source, timestamp) }
+
+ describe 'no host provided' do
+
+ it { is_expected.to eq '/sites/42/assets/1/banner.png' }
+
+ end
+
+ describe 'with a timestamp' do
+
+ let(:timestamp) { '42' }
+ it { is_expected.to eq '/sites/42/assets/1/banner.png?42' }
+
+ context 'the source already includes a query string' do
+
+ let(:source) { '/sites/42/assets/1/banner.png?foo' }
+ it { is_expected.to eq '/sites/42/assets/1/banner.png?foo' }
+
+ end
+
+ end
+
+ describe 'the source is already a full url' do
+
+ let(:source) { 'http://somewhere.net/sites/42/assets/1/banner.png' }
+ it { is_expected.to eq 'http://somewhere.net/sites/42/assets/1/banner.png' }
+
+ describe 'also with https' do
+
+ let(:source) { 'https://somewhere.net/sites/42/assets/1/banner.png' }
+ it { is_expected.to eq 'https://somewhere.net/sites/42/assets/1/banner.png' }
+
+ end
+
+ end
+
+ describe 'the host is a string' do
+
+ let(:host) { 'http://assets.locomotivecms.com' }
+ it { is_expected.to eq 'http://assets.locomotivecms.com/sites/42/assets/1/banner.png' }
+
+ end
+
+ describe 'the host is a block' do
+
+ let(:request) { instance_double('Request', ssl: true) }
+ let(:site) { instance_double('Site', cdn: true) }
+ let(:host) { ->(request, site) { site.cdn ? "http#{request.ssl ? 's' : ''}://assets.locomotivecms.com" : nil } }
+
+ it { is_expected.to eq 'https://assets.locomotivecms.com/sites/42/assets/1/banner.png' }
+
+ context 'with a different request var' do
+
+ let(:request) { instance_double('Request', ssl: false) }
+ it { is_expected.to eq 'http://assets.locomotivecms.com/sites/42/assets/1/banner.png' }
+
+ end
+
+ context 'with a different site var' do
+
+ let(:site) { instance_double('Site', cdn: false) }
+ it { is_expected.to eq '/sites/42/assets/1/banner.png' }
+
+ end
+
+ end
+
+ end
spec/unit/services/asset_host_spec.rb +0 -79
@@ @@ -1,79 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::AssetHost do
-
- let(:request) { nil }
- let(:site) { nil }
- let(:host) { nil }
- let(:timestamp) { nil }
- let(:asset_host) { Locomotive::Steam::Services::AssetHost.new(request, site, host) }
- let(:source) { '/sites/42/assets/1/banner.png' }
-
- subject { asset_host.compute(source, timestamp) }
-
- describe 'no host provided' do
-
- it { is_expected.to eq '/sites/42/assets/1/banner.png' }
-
- end
-
- describe 'with a timestamp' do
-
- let(:timestamp) { '42' }
- it { is_expected.to eq '/sites/42/assets/1/banner.png?42' }
-
- context 'the source already includes a query string' do
-
- let(:source) { '/sites/42/assets/1/banner.png?foo' }
- it { is_expected.to eq '/sites/42/assets/1/banner.png?foo' }
-
- end
-
- end
-
- describe 'the source is already a full url' do
-
- let(:source) { 'http://somewhere.net/sites/42/assets/1/banner.png' }
- it { is_expected.to eq 'http://somewhere.net/sites/42/assets/1/banner.png' }
-
- describe 'also with https' do
-
- let(:source) { 'https://somewhere.net/sites/42/assets/1/banner.png' }
- it { is_expected.to eq 'https://somewhere.net/sites/42/assets/1/banner.png' }
-
- end
-
- end
-
- describe 'the host is a string' do
-
- let(:host) { 'http://assets.locomotivecms.com' }
- it { is_expected.to eq 'http://assets.locomotivecms.com/sites/42/assets/1/banner.png' }
-
- end
-
- describe 'the host is a block' do
-
- let(:request) { instance_double('Request', ssl: true) }
- let(:site) { instance_double('Site', cdn: true) }
- let(:host) { ->(request, site) { site.cdn ? "http#{request.ssl ? 's' : ''}://assets.locomotivecms.com" : nil } }
-
- it { is_expected.to eq 'https://assets.locomotivecms.com/sites/42/assets/1/banner.png' }
-
- context 'with a different request var' do
-
- let(:request) { instance_double('Request', ssl: false) }
- it { is_expected.to eq 'http://assets.locomotivecms.com/sites/42/assets/1/banner.png' }
-
- end
-
- context 'with a different site var' do
-
- let(:site) { instance_double('Site', cdn: false) }
- it { is_expected.to eq '/sites/42/assets/1/banner.png' }
-
- end
-
- end
-
- end
spec/unit/services/entry_submission_service_spec.rb +141 -0
@@ @@ -0,0 +1,141 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::EntrySubmissionService do
+
+ let(:site) { instance_double('Site', default_locale: 'en') }
+ let(:locale) { 'en' }
+ let(:type_repository) { instance_double('ContentTypeRepository') }
+ let(:entry_repository) { instance_double('Repository', site: site, locale: locale) }
+ let(:service) { described_class.new(type_repository, entry_repository, locale) }
+
+ describe '#find' do
+
+ let(:type_slug) { 'articles' }
+ let(:slug) { 'hello-world' }
+ subject { service.find(type_slug, slug) }
+
+ context 'unknown content type' do
+
+ before { allow(type_repository).to receive(:by_slug).and_return(nil) }
+ it { is_expected.to eq nil }
+
+ end
+
+ context 'existing content type' do
+
+ let(:type) { instance_double('Articles') }
+ let(:entry) { instance_double('Entry', title: 'Hello world', content_type: type, attributes: { title: 'Hello world' }, localized_attributes: []) }
+
+ before do
+ allow(type_repository).to receive(:by_slug).and_return(type)
+ allow(entry_repository).to receive(:by_slug).with(type, 'hello-world').and_return(entry)
+ end
+
+ it { is_expected.to eq entry }
+
+ end
+
+ end
+
+ describe '#to_json' do
+
+ let(:entry) { nil }
+ subject { service.to_json(entry) }
+
+ it { is_expected.to eq nil }
+
+ context 'existing content entry' do
+
+ let(:errors) { {} }
+ let(:fields) { [instance_double('TitleField', name: :title, type: :string)] }
+ let(:type) { instance_double('Articles') }
+ let(:entry) { instance_double('DecoratedEntry', _slug: 'hello-world', title: 'Hello world', content_type: type, content_type_slug: :articles, errors: errors) }
+
+ before { allow(type_repository).to receive(:fields_for).with(type).and_return(fields) }
+
+ it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world"}' }
+
+ context 'with errors' do
+
+ let(:errors) { instance_double('Errors', empty?: false, messages: { title: ["can't be blank"] }) }
+ it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world","errors":{"title":["can\'t be blank"]}}' }
+
+ end
+
+ end
+
+ end
+
+ describe '#submit' do
+
+ let(:slug) { nil }
+ let(:attributes) { { title: 'Hello world' } }
+ subject { service.submit(slug, attributes) }
+
+ it { is_expected.to eq nil }
+
+ context 'unknown content type' do
+
+ let(:slug) { 'articles' }
+
+ before { allow(type_repository).to receive(:by_slug).with('articles').and_return nil }
+
+ it { is_expected.to eq nil }
+
+ end
+
+ context 'existing content type' do
+
+ let(:unique_fields) { {} }
+ let(:first_validation) { false }
+ let(:errors) { [:title] }
+ let(:type) { instance_double('Comments') }
+ let(:entry) { instance_double('Entry', title: 'Hello world', content_type: type, valid?: first_validation, errors: errors, attributes: { title: 'Hello world' }, localized_attributes: []) }
+ let(:slug) { 'comments' }
+
+ before do
+ allow(type_repository).to receive(:by_slug).and_return(type)
+ allow(type_repository).to receive(:look_for_unique_fields).and_return(unique_fields)
+ allow(entry_repository).to receive(:build).with(type, attributes).and_return(entry)
+ end
+
+ context 'valid' do
+
+ before { expect(entry_repository).to receive(:persist) }
+
+ let(:first_validation) { true }
+ let(:errors) { {} }
+
+ it { is_expected.to eq entry }
+ it { expect(subject.errors.empty?).to eq true }
+
+ end
+
+ context 'not valid' do
+
+ before { expect(entry_repository).not_to receive(:persist) }
+
+ it { is_expected.to eq entry }
+ it { expect(subject.errors).to eq([:title]) }
+
+ context 'with unique fields' do
+
+ let(:unique_fields) { { title: instance_double('Field', name: 'title') } }
+
+ before do
+ allow(entry_repository).to receive(:exists?).with(type, :title, 'Hello world').and_return(true)
+ expect(entry.errors).to receive(:add).with(:title, :unique).and_return(true)
+ end
+
+ it { is_expected.to eq entry }
+ it { expect(subject.errors).to eq([:title]) }
+
+ end
+
+ end
+
+ end
+
+ end
+
+ end
spec/unit/services/entry_submission_spec.rb +0 -141
@@ @@ -1,141 +0,0 @@
- # require 'spec_helper'
-
- # describe Locomotive::Steam::Services::EntrySubmission do
-
- # let(:site) { instance_double('Site', default_locale: 'en') }
- # let(:locale) { 'en' }
- # let(:type_repository) { Locomotive::Steam::Repositories::Filesystem::ContentType.new(nil, site) }
- # let(:entry_repository) { Locomotive::Steam::Repositories::Filesystem::ContentEntry.new(nil, site) }
- # let(:service) { Locomotive::Steam::Services::EntrySubmission.new(type_repository, entry_repository, locale) }
-
- # describe '#find' do
-
- # let(:type_slug) { 'articles' }
- # let(:slug) { 'hello-world' }
- # subject { service.find(type_slug, slug) }
-
- # context 'unknown content type' do
-
- # before { allow(type_repository).to receive(:by_slug).and_return(nil) }
- # it { is_expected.to eq nil }
-
- # end
-
- # context 'existing content type' do
-
- # let(:type) { instance_double('Articles') }
- # let(:entry) { instance_double('Entry', title: 'Hello world', content_type: type, attributes: { title: 'Hello world' }, localized_attributes: []) }
-
- # before do
- # allow(type_repository).to receive(:by_slug).and_return(type)
- # allow(entry_repository).to receive(:by_slug).with(type, 'hello-world').and_return(entry)
- # end
-
- # it { is_expected.to eq entry }
-
- # end
-
- # end
-
- # describe '#to_json' do
-
- # let(:entry) { nil }
- # subject { service.to_json(entry) }
-
- # it { is_expected.to eq nil }
-
- # context 'existing content entry' do
-
- # let(:errors) { {} }
- # let(:fields) { [instance_double('TitleField', name: :title, type: :string)] }
- # let(:type) { instance_double('Articles') }
- # let(:entry) { instance_double('DecoratedEntry', _slug: 'hello-world', title: 'Hello world', content_type: type, content_type_slug: :articles, errors: errors) }
-
- # before { allow(type_repository).to receive(:fields_for).with(type).and_return(fields) }
-
- # it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world"}' }
-
- # context 'with errors' do
-
- # let(:errors) { instance_double('Errors', empty?: false, messages: { title: ["can't be blank"] }) }
- # it { is_expected.to eq '{"_slug":"hello-world","content_type_slug":"articles","title":"Hello world","errors":{"title":["can\'t be blank"]}}' }
-
- # end
-
- # end
-
- # end
-
- # describe '#submit' do
-
- # let(:slug) { nil }
- # let(:attributes) { { title: 'Hello world' } }
- # subject { service.submit(slug, attributes) }
-
- # it { is_expected.to eq nil }
-
- # context 'unknown content type' do
-
- # let(:slug) { 'articles' }
-
- # before { allow(type_repository).to receive(:by_slug).with('articles').and_return nil }
-
- # it { is_expected.to eq nil }
-
- # end
-
- # context 'existing content type' do
-
- # let(:unique_fields) { {} }
- # let(:first_validation) { false }
- # let(:errors) { [:title] }
- # let(:type) { instance_double('Comments') }
- # let(:entry) { instance_double('Entry', title: 'Hello world', content_type: type, valid?: first_validation, errors: errors, attributes: { title: 'Hello world' }, localized_attributes: []) }
- # let(:slug) { 'comments' }
-
- # before do
- # allow(type_repository).to receive(:by_slug).and_return(type)
- # allow(type_repository).to receive(:look_for_unique_fields).and_return(unique_fields)
- # allow(entry_repository).to receive(:build).with(type, attributes).and_return(entry)
- # end
-
- # context 'valid' do
-
- # before { expect(entry_repository).to receive(:persist) }
-
- # let(:first_validation) { true }
- # let(:errors) { {} }
-
- # it { is_expected.to eq entry }
- # it { expect(subject.errors.empty?).to eq true }
-
- # end
-
- # context 'not valid' do
-
- # before { expect(entry_repository).not_to receive(:persist) }
-
- # it { is_expected.to eq entry }
- # it { expect(subject.errors).to eq([:title]) }
-
- # context 'with unique fields' do
-
- # let(:unique_fields) { { title: instance_double('Field', name: 'title') } }
-
- # before do
- # allow(entry_repository).to receive(:exists?).with(type, :title, 'Hello world').and_return(true)
- # expect(entry.errors).to receive(:add).with(:title, :unique).and_return(true)
- # end
-
- # it { is_expected.to eq entry }
- # it { expect(subject.errors).to eq([:title]) }
-
- # end
-
- # end
-
- # end
-
- # end
-
- # end
spec/unit/services/external_api_service_spec.rb +80 -0
@@ @@ -0,0 +1,80 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::ExternalAPIService do
+
+ let(:service) { described_class.new }
+
+ describe '#consume' do
+
+ let(:url) { '' }
+ let(:options) { {} }
+ let(:code) { 200 }
+ let(:parsed_response) { Hash.new }
+ let(:response) { instance_double('Response', code: code, parsed_response: parsed_response) }
+
+ subject { service.consume(url, options) }
+
+ describe 'sets the base uri from a simple url' do
+
+ let(:url) { 'http://blog.locomotiveapp.org' }
+ it do
+ expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
+ subject
+ end
+
+ describe 'wrong response (<> 200)' do
+
+ let(:code) { 500 }
+ it do
+ expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
+ expect(subject).to eq nil
+ end
+
+ end
+
+ describe 'returns a collection instead of a hash' do
+
+ let(:parsed_response) { [{ 'averagePrice' => 1 }] }
+ it do
+ expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
+ expect(subject.first['average_price']).to eq 1
+ end
+
+ end
+
+ end
+
+ describe 'sets the base uri from a much more complex url' do
+
+ let(:url) { 'http://free.worldweatheronline.com/feed/weather.ashx?key=secretapikey&format=json' }
+ it do
+ expect(service.class).to receive(:get).with('/feed/weather.ashx?key=secretapikey&format=json', { base_uri: 'http://free.worldweatheronline.com' }).and_return(response)
+ subject
+ end
+
+ end
+
+
+ describe 'sets both the base uri and the path from an url with parameters' do
+
+ let(:url) { 'http://blog.locomotiveapp.org/api/read/json?num=3' }
+ it do
+ expect(service.class).to receive(:get).with('/api/read/json?num=3', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
+ subject
+ end
+
+ end
+
+ describe 'sets auth credentials' do
+
+ let(:url) { 'http://blog.locomotiveapp.org' }
+ let(:options) { { username: 'john', password: 'foo' } }
+ it do
+ expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org', basic_auth: { username: 'john', password: 'foo' } }).and_return(response)
+ subject
+ end
+ end
+
+ end
+
+ end
spec/unit/services/external_api_spec.rb +0 -80
@@ @@ -1,80 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::ExternalAPI do
-
- let(:service) { Locomotive::Steam::Services::ExternalAPI.new }
-
- describe '#consume' do
-
- let(:url) { '' }
- let(:options) { {} }
- let(:code) { 200 }
- let(:parsed_response) { Hash.new }
- let(:response) { instance_double('Response', code: code, parsed_response: parsed_response) }
-
- subject { service.consume(url, options) }
-
- describe 'sets the base uri from a simple url' do
-
- let(:url) { 'http://blog.locomotiveapp.org' }
- it do
- expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
- subject
- end
-
- describe 'wrong response (<> 200)' do
-
- let(:code) { 500 }
- it do
- expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
- expect(subject).to eq nil
- end
-
- end
-
- describe 'returns a collection instead of a hash' do
-
- let(:parsed_response) { [{ 'averagePrice' => 1 }] }
- it do
- expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
- expect(subject.first['average_price']).to eq 1
- end
-
- end
-
- end
-
- describe 'sets the base uri from a much more complex url' do
-
- let(:url) { 'http://free.worldweatheronline.com/feed/weather.ashx?key=secretapikey&format=json' }
- it do
- expect(service.class).to receive(:get).with('/feed/weather.ashx?key=secretapikey&format=json', { base_uri: 'http://free.worldweatheronline.com' }).and_return(response)
- subject
- end
-
- end
-
-
- describe 'sets both the base uri and the path from an url with parameters' do
-
- let(:url) { 'http://blog.locomotiveapp.org/api/read/json?num=3' }
- it do
- expect(service.class).to receive(:get).with('/api/read/json?num=3', { base_uri: 'http://blog.locomotiveapp.org' }).and_return(response)
- subject
- end
-
- end
-
- describe 'sets auth credentials' do
-
- let(:url) { 'http://blog.locomotiveapp.org' }
- let(:options) { { username: 'john', password: 'foo' } }
- it do
- expect(service.class).to receive(:get).with('/', { base_uri: 'http://blog.locomotiveapp.org', basic_auth: { username: 'john', password: 'foo' } }).and_return(response)
- subject
- end
- end
-
- end
-
- end
spec/unit/services/image_resizer_service_spec.rb +75 -0
@@ @@ -0,0 +1,75 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::ImageResizerService do
+
+ let(:resizer) { nil }
+ let(:path) { nil }
+ let(:service) { described_class.new(resizer, path) }
+
+ describe '#resize' do
+
+ let(:geometry) { '400x30#' }
+ let(:input) { 'http://upload.wikimedia.org/wikipedia/en/thumb/b/b5/Metropolitan_railway_steam_locomotive_2781022036.png/240px-Metropolitan_railway_steam_locomotive_2781022036.png' }
+
+ subject { service.resize(input, geometry) }
+
+ describe 'no resizer' do
+ it { is_expected.to eq nil }
+ end
+
+ describe 'DragonFly' do
+
+ let(:resizer) { Dragonfly.app(:steam) }
+
+ describe 'no imagemagick' do
+ before { expect(resizer.plugins).to receive(:[]).with(:imagemagick).and_return(nil) }
+ it { is_expected.to eq nil }
+ end
+
+ describe 'no geometry' do
+ let(:geometry) { '' }
+ it { is_expected.to eq nil }
+ end
+
+ it { is_expected.to match /images\/dynamic\/.*\/240px-Metropolitan_railway_steam_locomotive_2781022036.png/ }
+
+ describe 'a local asset' do
+
+ let(:input) { '/sites/42/theme/images/banner.png' }
+ it { is_expected.to match /images\/dynamic\/.*\/banner.png/ }
+
+ end
+
+ describe 'a hash' do
+
+ let(:input) { { 'url' => '/sites/42/theme/images/banner.png' } }
+ it { is_expected.to match /images\/dynamic\/.*\/banner.png/ }
+
+ end
+
+ describe 'an object responding to the url method (Carrierwave uploaded file)' do
+
+ let(:input) { instance_double('UploadedFile', url: '/sites/42/theme/acme.png') }
+ it { is_expected.to match /images\/dynamic\/.*\/acme.png/ }
+
+ end
+
+ describe 'an url with a timestamp' do
+
+ let(:input) { '/sites/42/theme/images/banner.png?24e29997bcb00e97d8252cdd29d14e2d' }
+ it { is_expected.to match /images\/dynamic\/.*\/banner.png\?sha=[a-z0-9]+/ }
+
+ end
+
+ describe 'unable to fetch a file' do
+
+ before { allow(service).to receive(:fetch_file).and_return(nil) }
+ it { is_expected.to eq nil }
+
+ end
+
+ end
+
+ end
+
+ end
spec/unit/services/image_resizer_spec.rb +0 -75
@@ @@ -1,75 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::ImageResizer do
-
- let(:resizer) { nil }
- let(:path) { nil }
- let(:service) { Locomotive::Steam::Services::ImageResizer.new(resizer, path) }
-
- describe '#resize' do
-
- let(:geometry) { '400x30#' }
- let(:input) { 'http://upload.wikimedia.org/wikipedia/en/thumb/b/b5/Metropolitan_railway_steam_locomotive_2781022036.png/240px-Metropolitan_railway_steam_locomotive_2781022036.png' }
-
- subject { service.resize(input, geometry) }
-
- describe 'no resizer' do
- it { is_expected.to eq nil }
- end
-
- describe 'DragonFly' do
-
- let(:resizer) { Dragonfly.app(:steam) }
-
- describe 'no imagemagick' do
- before { expect(resizer.plugins).to receive(:[]).with(:imagemagick).and_return(nil) }
- it { is_expected.to eq nil }
- end
-
- describe 'no geometry' do
- let(:geometry) { '' }
- it { is_expected.to eq nil }
- end
-
- it { is_expected.to match /images\/dynamic\/.*\/240px-Metropolitan_railway_steam_locomotive_2781022036.png/ }
-
- describe 'a local asset' do
-
- let(:input) { '/sites/42/theme/images/banner.png' }
- it { is_expected.to match /images\/dynamic\/.*\/banner.png/ }
-
- end
-
- describe 'a hash' do
-
- let(:input) { { 'url' => '/sites/42/theme/images/banner.png' } }
- it { is_expected.to match /images\/dynamic\/.*\/banner.png/ }
-
- end
-
- describe 'an object responding to the url method (Carrierwave uploaded file)' do
-
- let(:input) { instance_double('UploadedFile', url: '/sites/42/theme/acme.png') }
- it { is_expected.to match /images\/dynamic\/.*\/acme.png/ }
-
- end
-
- describe 'an url with a timestamp' do
-
- let(:input) { '/sites/42/theme/images/banner.png?24e29997bcb00e97d8252cdd29d14e2d' }
- it { is_expected.to match /images\/dynamic\/.*\/banner.png\?sha=[a-z0-9]+/ }
-
- end
-
- describe 'unable to fetch a file' do
-
- before { allow(service).to receive(:fetch_file).and_return(nil) }
- it { is_expected.to eq nil }
-
- end
-
- end
-
- end
-
- end
spec/unit/services/markdown_service_spec.rb +36 -0
@@ @@ -0,0 +1,36 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::MarkdownService do
+
+ let(:service) { described_class.new }
+
+ describe '#to_html' do
+
+ let(:text) { <<-EOF
+ First level header
+ ==================
+
+ Second level header
+ -------------------
+ EOF
+ }
+
+ subject { service.to_html(text) }
+
+ it do
+ is_expected.to eq <<-EOF
+ <h1>First level header</h1>
+
+ <h2>Second level header</h2>
+ EOF
+ end
+
+ describe 'no text' do
+
+ let(:text) { nil }
+ it { is_expected.to eq '' }
+ end
+
+ end
+
+ end
spec/unit/services/markdown_spec.rb +0 -36
@@ @@ -1,36 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::Markdown do
-
- let(:service) { Locomotive::Steam::Services::Markdown.new }
-
- describe '#to_html' do
-
- let(:text) { <<-EOF
- First level header
- ==================
-
- Second level header
- -------------------
- EOF
- }
-
- subject { service.to_html(text) }
-
- it do
- is_expected.to eq <<-EOF
- <h1>First level header</h1>
-
- <h2>Second level header</h2>
- EOF
- end
-
- describe 'no text' do
-
- let(:text) { nil }
- it { is_expected.to eq '' }
- end
-
- end
-
- end
spec/unit/services/parent_finder_service_spec.rb +41 -0
@@ @@ -0,0 +1,41 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::ParentFinderService do
+
+ let(:site) { instance_double('Site', default_locale: :en) }
+ let(:repository) { instance_double('PageRepository', site: site, locale: :en)}
+ let(:service) { described_class.new(repository) }
+
+ describe '#find' do
+
+ let(:name) { '' }
+ let(:another_page) { instance_double('Index', title: 'Index', attributes: {}) }
+ let(:page) { instance_double('AboutUs', title: 'About us') }
+
+ subject { service.find(page, name).try(:title) }
+
+ it { is_expected.to eq nil }
+
+ describe 'using the parent keyword' do
+
+ let(:name) { 'parent' }
+
+ before { expect(repository).to receive(:parent_of).and_return(another_page) }
+
+ it { is_expected.to eq 'Index' }
+
+ end
+
+ describe 'using the fullpath' do
+
+ let(:name) { 'index' }
+
+ before { expect(repository).to receive(:by_fullpath).with('index').and_return(another_page) }
+
+ it { is_expected.to eq 'Index' }
+
+ end
+
+ end
+
+ end
spec/unit/services/parent_finder_spec.rb +0 -41
@@ @@ -1,41 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::ParentFinder do
-
- let(:site) { instance_double('Site', default_locale: :en) }
- let(:repository) { instance_double('PageRepository', site: site, current_locale: :en)}
- let(:service) { Locomotive::Steam::Services::ParentFinder.new(repository) }
-
- describe '#find' do
-
- let(:name) { '' }
- let(:another_page) { instance_double('Index', title: 'Index', attributes: {}) }
- let(:page) { instance_double('AboutUs', title: 'About us') }
-
- subject { service.find(page, name).try(:title) }
-
- it { is_expected.to eq nil }
-
- describe 'using the parent keyword' do
-
- let(:name) { 'parent' }
-
- before { expect(repository).to receive(:parent_of).and_return(another_page) }
-
- it { is_expected.to eq 'Index' }
-
- end
-
- describe 'using the fullpath' do
-
- let(:name) { 'index' }
-
- before { expect(repository).to receive(:by_fullpath).with('index').and_return(another_page) }
-
- it { is_expected.to eq 'Index' }
-
- end
-
- end
-
- end
spec/unit/services/textile_service_spec.rb +34 -0
@@ @@ -0,0 +1,34 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::TextileService do
+
+ let(:service) { described_class.new }
+
+ describe '#to_html' do
+
+ let(:text) { <<-EOF
+ h1. Give RedCloth a try!
+
+ A *simple* paragraph
+ EOF
+ }
+
+ subject { service.to_html(text) }
+
+ it do
+ is_expected.to eq <<-EOF
+ <h1>Give RedCloth a try!</h1>
+ <p>A <strong>simple</strong> paragraph</p>
+ EOF
+ .strip
+ end
+
+ describe 'no text' do
+
+ let(:text) { nil }
+ it { is_expected.to eq '' }
+ end
+
+ end
+
+ end
spec/unit/services/textile_spec.rb +0 -34
@@ @@ -1,34 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::Textile do
-
- let(:service) { Locomotive::Steam::Services::Textile.new }
-
- describe '#to_html' do
-
- let(:text) { <<-EOF
- h1. Give RedCloth a try!
-
- A *simple* paragraph
- EOF
- }
-
- subject { service.to_html(text) }
-
- it do
- is_expected.to eq <<-EOF
- <h1>Give RedCloth a try!</h1>
- <p>A <strong>simple</strong> paragraph</p>
- EOF
- .strip
- end
-
- describe 'no text' do
-
- let(:text) { nil }
- it { is_expected.to eq '' }
- end
-
- end
-
- end
spec/unit/services/translator_service_spec.rb +65 -0
@@ @@ -0,0 +1,65 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::TranslatorService do
+
+ let(:default_locale) { 'en' }
+ let(:repository) { instance_double('Repository') }
+ let(:service) { described_class.new(repository, default_locale) }
+
+ describe '#translate' do
+
+ let(:input) { 'example_test' }
+ let(:locale) { nil }
+ let(:scope) { nil }
+
+ subject { service.translate(input, locale, scope) }
+
+ describe 'existing translation' do
+
+ let(:translation) { instance_double('Translation', values: { 'en' => 'Example text', 'es' => 'Texto de ejemplo' }) }
+
+ before do
+ allow(repository).to receive(:find).with('example_test').and_return(translation)
+ end
+
+ it { is_expected.to eq 'Example text' }
+
+ context 'no translation found' do
+
+ let(:translation) { nil }
+ it { is_expected.to eq 'example_test' }
+
+ end
+
+ context 'specifying a locale' do
+
+ let(:locale) { 'es' }
+ it { is_expected.to eq 'Texto de ejemplo' }
+
+ end
+
+ context "specifying a locale that doesn't exist" do
+
+ let(:locale) { 'nl' }
+
+ it 'reverts to default locale' do
+ is_expected.to eq "example_test"
+ end
+
+ end
+
+ context "specifying a scope" do
+
+ let(:input) { 'fr' }
+ let(:locale) { 'en' }
+ let(:scope) { 'locomotive.locales' }
+
+ it { is_expected.to eq 'French' }
+
+ end
+
+ end
+
+ end
+
+ end
spec/unit/services/translator_spec.rb +0 -65
@@ @@ -1,65 +0,0 @@
- # require 'spec_helper'
-
- # describe Locomotive::Steam::Services::Translator do
-
- # let(:default_locale) { 'en' }
- # let(:repository) { Locomotive::Steam::Repositories::Filesystem::Translation.new(nil) }
- # let(:service) { Locomotive::Steam::Services::Translator.new(repository, default_locale) }
-
- # describe '#translate' do
-
- # let(:input) { 'example_test' }
- # let(:locale) { nil }
- # let(:scope) { nil }
-
- # subject { service.translate(input, locale, scope) }
-
- # describe 'existing translation' do
-
- # let(:translation) { instance_double('Translation', values: { 'en' => 'Example text', 'es' => 'Texto de ejemplo' }) }
-
- # before do
- # allow(repository).to receive(:find).with('example_test').and_return(translation)
- # end
-
- # it { is_expected.to eq 'Example text' }
-
- # context 'no translation found' do
-
- # let(:translation) { nil }
- # it { is_expected.to eq 'example_test' }
-
- # end
-
- # context 'specifying a locale' do
-
- # let(:locale) { 'es' }
- # it { is_expected.to eq 'Texto de ejemplo' }
-
- # end
-
- # context "specifying a locale that doesn't exist" do
-
- # let(:locale) { 'nl' }
-
- # it 'reverts to default locale' do
- # is_expected.to eq "example_test"
- # end
-
- # end
-
- # context "specifying a scope" do
-
- # let(:input) { 'fr' }
- # let(:locale) { 'en' }
- # let(:scope) { 'locomotive.locales' }
-
- # it { is_expected.to eq 'French' }
-
- # end
-
- # end
-
- # end
-
- # end
spec/unit/services/url_builder_service_spec.rb +48 -0
@@ @@ -0,0 +1,48 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::UrlBuilderService do
+
+ let(:site) { instance_double('Site', default_locale: 'en') }
+ let(:locale) { 'en' }
+ let(:service) { described_class.new(site, locale) }
+
+ describe '#url_for' do
+
+ let(:page) { instance_double('AboutUs', fullpath: 'about-us', templatized?: false) }
+
+ subject { service.url_for(page) }
+
+ it { is_expected.to eq '/about-us' }
+
+ describe 'a locale different from the default one' do
+
+ let(:locale) { 'fr' }
+ it { is_expected.to eq '/fr/about-us' }
+
+ end
+
+ describe 'no need to put the index slug' do
+
+ let(:page) { instance_double('Index', fullpath: 'index', templatized?: false) }
+ it { is_expected.to eq '/' }
+
+ context 'different locale' do
+
+ let(:locale) { 'fr' }
+ it { is_expected.to eq '/fr' }
+
+ end
+
+ end
+
+ describe 'templatized page' do
+
+ let(:article) { instance_double('Article', _slug: 'hello-world') }
+ let(:page) { instance_double('Template', fullpath: 'articles/content_type_template', templatized?: true, content_entry: article) }
+ it { is_expected.to eq '/articles/hello-world' }
+
+ end
+
+ end
+
+ end
spec/unit/services/url_builder_spec.rb +0 -48
@@ @@ -1,48 +0,0 @@
- require 'spec_helper'
-
- describe Locomotive::Steam::Services::UrlBuilder do
-
- let(:site) { instance_double('Site', default_locale: 'en') }
- let(:locale) { 'en' }
- let(:service) { Locomotive::Steam::Services::UrlBuilder.new(site, locale) }
-
- describe '#url_for' do
-
- let(:page) { instance_double('AboutUs', fullpath: 'about-us', templatized?: false) }
-
- subject { service.url_for(page) }
-
- it { is_expected.to eq '/about-us' }
-
- describe 'a locale different from the default one' do
-
- let(:locale) { 'fr' }
- it { is_expected.to eq '/fr/about-us' }
-
- end
-
- describe 'no need to put the index slug' do
-
- let(:page) { instance_double('Index', fullpath: 'index', templatized?: false) }
- it { is_expected.to eq '/' }
-
- context 'different locale' do
-
- let(:locale) { 'fr' }
- it { is_expected.to eq '/fr' }
-
- end
-
- end
-
- describe 'templatized page' do
-
- let(:article) { instance_double('Article', _slug: 'hello-world') }
- let(:page) { instance_double('Template', fullpath: 'articles/content_type_template', templatized?: true, content_entry: article) }
- it { is_expected.to eq '/articles/hello-world' }
-
- end
-
- end
-
- end