diff --git a/README.md b/README.md index a80a420..4938386 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ elasticsearch: default_type: "post" # Optional. Default type is "post". custom_settings: _es_settings.yml # Optional. No default. Relative to your src folder custom_mappings: _es_mappings.yml # Optional. No default. Relative to your src folder + environments: # Optional. Set environments where Searchyll should run + - 'production' # Default runs on all environment if empty + - 'development' # If set will only run in speccified environments ``` ### Custom Settings File Example @@ -69,4 +72,4 @@ prompt that will allow you to experiment. ## Contributing Bug reports and pull requests are welcome on GitHub at -https://github.com/omc/searchyll + diff --git a/lib/searchyll.rb b/lib/searchyll.rb index db89e0b..a633d76 100644 --- a/lib/searchyll.rb +++ b/lib/searchyll.rb @@ -12,9 +12,14 @@ Jekyll::Hooks.register(:site, :pre_render) do |site| config = Searchyll::Configuration.new(site) if config.valid? - puts "setting up indexer hook" - indexers[site] = Searchyll::Indexer.new(config) - indexers[site].start + # return if we should only run in production + if config.should_execute_in_current_environment? + puts "setting up indexer hook" + indexers[site] = Searchyll::Indexer.new(config) + indexers[site].start + else + puts "Only running in #{site.config['elasticsearch']['environments']}" + end else puts 'Invalid Elasticsearch configuration provided, skipping indexing...' config.reasons.each do |r| @@ -31,12 +36,11 @@ # gets random pages like your home page Jekyll::Hooks.register :pages, :post_render do |page| - # strip html - nokogiri_doc = Nokogiri::HTML(page.output) - - # puts %( indexing page #{page.url}) - if (indexer = indexers[page.site]) + # strip html + nokogiri_doc = Nokogiri::HTML(page.output) + + # puts %( indexing page #{page.url}) indexer << ({ "id" => page.name, "url" => page.url, @@ -47,12 +51,11 @@ # gets both posts and collections Jekyll::Hooks.register :documents, :post_render do |document| - # strip html - nokogiri_doc = Nokogiri::HTML(document.output) - - # puts %( indexing document #{document.url}) - if (indexer = indexers[document.site]) + # strip html + nokogiri_doc = Nokogiri::HTML(document.output) + # puts %( indexing document #{document.url}) + indexer << ({ "id" => document.id, "url" => document.url, diff --git a/lib/searchyll/configuration.rb b/lib/searchyll/configuration.rb index 5e7326a..0b1286d 100644 --- a/lib/searchyll/configuration.rb +++ b/lib/searchyll/configuration.rb @@ -79,6 +79,15 @@ def elasticsearch_settings_path site.config['elasticsearch']['custom_settings'] end + def should_execute_in_current_environment? + settings = site.config['elasticsearch'] + + return true if settings['environments'].nil? + return true unless settings['environments'].is_a?(Array) + + settings['environments'].include? site.config['environment'] + end + def elasticsearch_mapping read_yaml(elasticsearch_mapping_path, nil) end diff --git a/searchyll.gemspec b/searchyll.gemspec index 2ff62ab..992392c 100644 --- a/searchyll.gemspec +++ b/searchyll.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_development_dependency "bundler", "~> 1.10" + spec.add_development_dependency "bundler", ">= 1.10" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec" spec.add_development_dependency "guard-rspec" diff --git a/spec/searchyll/configuration_spec.rb b/spec/searchyll/configuration_spec.rb new file mode 100644 index 0000000..0174177 --- /dev/null +++ b/spec/searchyll/configuration_spec.rb @@ -0,0 +1,72 @@ +require File.expand_path('../spec_helper', File.dirname(__FILE__)) +require "jekyll" + +class TestSite + attr_accessor :config + def initialize(config) + @config = config + end +end + +describe Searchyll::Configuration do + + it 'is expected to return true when production no environment spessified' do + site_config = { + 'environment' => 'production', + 'elasticsearch' => { + } + } + site = TestSite.new site_config + conf = Searchyll::Configuration.new site + expect(conf.should_execute_in_current_environment?).to eq(true) + end + + it 'is expected to return true when production and environment is nil' do + site_config = { + 'environment' => 'production', + 'elasticsearch' => { + 'environments' => nil + } + } + site = TestSite.new site_config + conf = Searchyll::Configuration.new site + expect(conf.should_execute_in_current_environment?).to eq(true) + end + + it 'is expected to return true when production and environment is not a array' do + site_config = { + 'environment' => 'production', + 'elasticsearch' => { + 'environments' => true + } + } + site = TestSite.new site_config + conf = Searchyll::Configuration.new site + expect(conf.should_execute_in_current_environment?).to eq(true) + end + + it 'is expected to return false when production and different environment spessified' do + site_config = { + 'environment' => 'production', + 'elasticsearch' => { + 'environments' => ['dev'] + } + } + site = TestSite.new site_config + conf = Searchyll::Configuration.new site + expect(conf.should_execute_in_current_environment?).to eq(false) + end + + it 'is expected to return true when production and several environment spessified' do + site_config = { + 'environment' => 'production', + 'elasticsearch' => { + 'environments' => ['dev', 'test', 'stage', 'production'] + } + } + site = TestSite.new site_config + conf = Searchyll::Configuration.new site + expect(conf.should_execute_in_current_environment?).to eq(true) + end + +end