Skip to content

Commit

Permalink
start testing! with some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nz committed Apr 25, 2016
1 parent 9f4c3be commit 8e63e9f
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 36 deletions.
15 changes: 15 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

end
27 changes: 3 additions & 24 deletions lib/searchyou.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
require "searchyou/version"
require "net/http"
require "json"

module Jekyll
require "jekyll/plugin"
require "jekyll/generator"

class SearchyouIndexer < Jekyll::Generator
safe true
priority :lowest

def generate(site)

indexer = Searchyou::Indexer.new(site)
indexer.run!

site.posts.docs.each do |doc|
indexer << doc.data.merge({
id: doc.basename_without_ext,
content: doc.content
})
end

indexer.done!
end
end

end
require "searchyou/generator"
31 changes: 31 additions & 0 deletions lib/searchyou/generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'searchyou/indexer'

module Searchyou

class Generator < Jekyll::Generator
safe true
priority :lowest

def self.elasticsearch_url(site)
ENV['ELASTICSEARCH_URL'] ||
ENV['BONSAI_URL'] ||
site.config['elasticsearch']['url']
end

def generate(site)
url = self.class.elasticsearch_url(site)
indexer = Searchyou::Indexer.new(site)
indexer.start

site.posts.docs.each do |doc|
indexer << doc.data.merge({
id: doc.basename_without_ext,
content: doc.content
})
end

indexer.finish
end
end

end
19 changes: 10 additions & 9 deletions lib/searchyou/indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ class Indexer

BATCH_SIZE = 50

attr_accessor :queue, :working, :site, :timestamp, :es
attr_accessor :queue
attr_accessor :working
attr_accessor :elasticsearch_url
attr_accessor :timestamp

def initialize(site)
self.site = site
def initialize(elasticsearch_url)
self.elasticsearch_url = elasticsearch_url
self.queue = Queue.new
self.working = true
self.timestamp = Time.now
self.es = Elasticsearch::Client.new(
url: site.config['elasticsearch']['url']
)
end

def <<(doc)
Expand All @@ -28,7 +28,7 @@ def es_index_name
end

# Prepare our indexing run by creating a new index.
def prepare!
def prepare_index
es.indices.create(
index: es_index_name
)
Expand All @@ -37,8 +37,9 @@ def prepare!
# set replication to 0?
end

def run!
prepare!
def start
prepare_index

self.indexer_thread = Thread.new do
loop do
break unless working?
Expand Down
1 change: 1 addition & 0 deletions searchyou.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
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"
spec.add_development_dependency "jekyll"

spec.add_dependency "elasticsearch-ruby"
Expand Down
22 changes: 22 additions & 0 deletions spec/searchyou/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))

describe Searchyou::Generator do

it 'is a Jekyll Generator' do
expect(Searchyou::Generator.new).to be_a(Jekyll::Generator)
end

let(:jekyll_site) do
double(
'jekyll site',
config: { 'elasticsearch' => { 'url' => 'http://localhost:9200' }}
)
end

# TODO: integrated site generation
it 'can generate an index' do
g = Searchyou::Generator.new
g.generate(jekyll_site)
end

end
9 changes: 9 additions & 0 deletions spec/searchyou/indexer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))

describe Searchyou::Indexer do

it 'is instantiated with an Elasticsearch URL' do
expect { Searchyou::Indexer.new }.to raise_error(ArgumentError)
end

end
4 changes: 1 addition & 3 deletions spec/searchyou_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'spec_helper'

describe Searchyou do

it 'has a version number' do
expect(Searchyou::VERSION).not_to be nil
end

it 'does something useful' do
expect(false).to eq(true)
end
end

0 comments on commit 8e63e9f

Please sign in to comment.