Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature - environments support #50

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<https://github.com/omc/searchyll>
29 changes: 16 additions & 13 deletions lib/searchyll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions lib/searchyll/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion searchyll.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
72 changes: 72 additions & 0 deletions spec/searchyll/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -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