Class: Redsync::SyncStat

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redsync/sync_stat.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, agent) ⇒ SyncStat

Returns a new instance of SyncStat.



8
9
10
11
12
13
14
15
# File 'lib/redsync/sync_stat.rb', line 8

def initialize(config, agent)
  @config = config
  @agent = agent

  @file = File.join(@config[:data_dir], "__redsync_stat.yml")
  FileUtils.touch @file unless File.exist? @file
  @stat = YAML.load_file(@file) || {}
end

Instance Method Details

#eachObject



98
99
100
# File 'lib/redsync/sync_stat.rb', line 98

def each
  @stat.each {|k,v| yield v}
end

#for(name) ⇒ Object



103
104
105
# File 'lib/redsync/sync_stat.rb', line 103

def for(name)
  @stat[name]
end

#history(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/redsync/sync_stat.rb', line 81

def history(name)
  puts "--Getting page history for #{name}" if @config[:verbose]
  now = DateTime.now
  history = []
  page = @agent.get(@config[:wiki_base_url] + "/" + URI.encode(name) + "/history")
  page.search("table.wiki-page-versions tbody tr").each do |tr|
    timestamp = DateTime.parse(tr.search("td")[3].text + now.zone) 
    author_name = tr.search("td")[4].text.strip
    history << {
      :timestamp => timestamp,
      :author_name => author_name
    }
  end
  history
end

#local_updated_page_namesObject



139
140
141
142
143
144
# File 'lib/redsync/sync_stat.rb', line 139

def local_updated_page_names
  self.inject([]) do |sum, page|
    sum << page[:name] if page[:downloaded_at] && (page[:local_updated_at] > page[:downloaded_at])
    sum
  end
end

#new_page_namesObject



123
124
125
126
127
128
# File 'lib/redsync/sync_stat.rb', line 123

def new_page_names
  self.inject([]) do |sum, page|
    sum << page[:name] if !page[:remote_updated_at]
    sum
  end
end

#refreshObject



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/redsync/sync_stat.rb', line 18

def refresh
  puts "Refreshing pages list"
  page = @agent.get(@config[:wiki_base_url] + "/date_index")
  now = DateTime.now

  # Get remote and local update times using remote list
  page.search("#content h3").each do |h3|
    links = h3.next_element.search("a")
    links.each do |link|
      url = @config[:url] + link.attr("href")
      name = URI.decode(url.match(/^#{@config[:wiki_base_url]}\/(.*)$/)[1]).force_encoding("UTF-8")
      local_file = File.join(@config[:data_dir], "#{name}.#{@config[:extension]}")

      remote_updated_at = DateTime.parse(h3.text + "T00:00:00" + now.zone)

      if File.exist? local_file
        local_updated_at = File.stat(local_file).mtime.to_datetime
        if remote_updated_at.year == now.year && remote_updated_at.month == now.month && remote_updated_at.day == now.day
          remote_updated_at = history(name)[0][:timestamp]
        end
      else
        local_updated_at = DateTime.civil
      end

      update(name, {
        :name => name,
        :url => url,
        :local_file => local_file,
        :remote_updated_at => remote_updated_at,
        :local_updated_at => local_updated_at,
      }, true)

      update(name, {
        :downloaded_at => local_updated_at
      }, true) unless File.exist? local_file
    end
  end

  # Look for new page files at local
  Dir.entries(@config[:data_dir]).each do |file|
    fullpath = File.join(@config[:data_dir], file)
    next if File.directory?(fullpath)
    next if file =~ /^__redsync_/
    name = Iconv.iconv("UTF-8", "UTF-8-MAC", file) if RUBY_PLATFORM =~ /darwin/
    name = name.first.match(/(.*)\.#{extension}$/)[1]
    next if @stat[name]

    local_file = File.join(@config[:data_dir], "#{name}.#{extension}")
    local_updated_at = File.stat(local_file).mtime.to_datetime
    update(name, {
      :name => name,
      :url => @config[:wiki_base_url] + "/#{name}",
      :local_file => local_file,
      :remote_updated_at => nil,
      :local_updated_at => local_updated_at,
      :downloaded_at => local_updated_at
    }, true)
  end

  write
end

#remote_updated_page_namesObject



131
132
133
134
135
136
# File 'lib/redsync/sync_stat.rb', line 131

def remote_updated_page_names
  self.inject([]) do |sum, page|
    sum << page[:name] if page[:remote_updated_at] && (page[:remote_updated_at] > page[:downloaded_at])
    sum
  end
end

#update(name, hash, suspend_write = false) ⇒ Object



108
109
110
111
112
# File 'lib/redsync/sync_stat.rb', line 108

def update(name, hash, suspend_write = false)
  @stat[name] ||= {}
  @stat[name].merge! hash
  write unless suspend_write
end

#writeObject



115
116
117
118
119
120
# File 'lib/redsync/sync_stat.rb', line 115

def write
  File.open(@file, "w+:UTF-8") do |f|
    f.write("# DO NOT EDIT THIS FILE!\n")
    f.write(@stat.to_yaml)
  end
end