Class: CouchdbReindexer::Indexer

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

Constant Summary collapse

@@default_options =
{
  database: "database",
  host: "127.0.0.1",
  protocol: "http",
  port: "5984",
  retries: 10
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Indexer

Returns a new instance of Indexer.



18
19
20
21
22
23
24
25
# File 'lib/couchdb_reindexer.rb', line 18

def initialize(options)
  @options = @@default_options.merge!(options)
  puts "***************************************************************"
  puts "********************** RUNNING REINDEXER **********************"
  puts "ON: #{default_url}"
  puts "***************************************************************"
  run
end

Instance Attribute Details

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



8
9
10
# File 'lib/couchdb_reindexer.rb', line 8

def options=(value)
  @options = value
end

Instance Method Details

#default_urlObject



48
49
50
# File 'lib/couchdb_reindexer.rb', line 48

def default_url
  "#{@options[:protocol]}://#{@options[:host]}:#{@options[:port]}/#{@options[:database]}"
end

#docs_uriObject



52
53
54
# File 'lib/couchdb_reindexer.rb', line 52

def docs_uri
  URI.parse("#{default_url}/_all_docs?startkey=%22_design/%22&endkey=%22_design0%22&include_docs=true")
end

#get_docsObject



60
61
62
63
64
65
# File 'lib/couchdb_reindexer.rb', line 60

def get_docs
  docs_request = make_request(docs_uri)
  docs_json = '[{}]'
  docs_json = docs_request.body if docs_request.code == '200'
  JSON.parse(docs_json)
end

#make_request(url, retries = 1) ⇒ Object

Raises:

  • (Net::ReadTimeout)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/couchdb_reindexer.rb', line 67

def make_request(url, retries = 1)
  raise Net::ReadTimeout if retries.negative?
  request = Net::HTTP::Get.new(url)

  if @options[:username] && @options[:password]
    request.basic_auth @options[:username], @options[:password]
  end

  begin
    Net::HTTP.start(url.hostname, url.port) { |http| http.request(request) }
  rescue Net::ReadTimeout
    make_request(url, retries - 1)
  end
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/couchdb_reindexer.rb', line 27

def run
  docs = get_docs
  if docs.to_json != '[{}]'
    docs = docs['rows']
    docs.each do |doc|
      puts "Document: #{doc['doc']['_id']}"
      doc['doc']['views'].each do |view|
        puts "\tRequesting view: #{doc['doc']['_id']}/_view/#{view[0]}"
        response = make_request(view_uri(doc['doc']['_id'], view[0]))
        if response.code == '200'
          puts "\tResponse status: OK!"
        else
          puts "\tResponse status: Error!"
        end
      end
    end
  else
    puts 'The database is empty or not reachable.'
  end
end

#view_uri(document_name, view_name) ⇒ Object



56
57
58
# File 'lib/couchdb_reindexer.rb', line 56

def view_uri(document_name, view_name)
  URI.parse("#{default_url}/#{document_name}/_view/#{view_name}")
end