Class: Vapey::Reindexer

Inherits:
Object
  • Object
show all
Defined in:
lib/vapey/reindexer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReindexer

Returns a new instance of Reindexer.



47
48
49
# File 'lib/vapey/reindexer.rb', line 47

def initialize
  @redis ||= Redis.new(host: ENV.fetch("REDIS_HOST") { "127.0.0.1" }, port: ENV.fetch("REDIS_PORT") { "6379" }.to_i)
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



15
16
17
# File 'lib/vapey/reindexer.rb', line 15

def redis
  @redis
end

Class Method Details

.invokeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vapey/reindexer.rb', line 18

def invoke
  options = { drop: false }
  OptionParser.new do |opts|
    opts.banner = "Usage: reindexer.rb [options]"

    opts.on('-s', '--source SOURCE_DIR', 'Source directory') do |source_dir|
      options[:source_dir] = source_dir
    end

    opts.on('-d', '--drop', 'Drop and create index before reindexing') do 
      options[:drop] = true
    end

  end.parse!

  # Validate the presence of source and destination directories
  unless options[:source_dir]
    puts "Error: source directory (-s or --source) must be specified."
    exit 1
  end

  # Instantiate the ToMkDocs class and perform the conversion
  reindexer = Vapey::Reindexer.new
  reindexer.run(options[:source_dir], drop: options[:drop]) 

  puts "Reindexer completed successfully."
end

Instance Method Details

#run(directory = nil, drop: false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vapey/reindexer.rb', line 51

def run(directory = nil, drop: false)
  vapey = Vapey::Search.new

  if drop
    vapey.recreate_index
  end

  directory ||= ENV.fetch("DOCS_DIRECTORY") { "/Users/david/development/docs/examples" }
  sections = Vapey::MarkdownParser.process(directory)
  sections.each do |section|
    puts "indexing: #{section[:file]} section: #{section[:section_title]}"
    data = section.stringify_keys
    id = data['id']
    vapey.redis.json_set("item:#{id}", Rejson::Path.root_path, data)
  end
end