Class: Redsync::Project

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Project

Returns a new instance of Project.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/redsync/project.rb', line 10

def initialize(options)
  @url = options[:url]
  @api_key = options[:api_key]
  @data_dir = File.expand_path(options[:data_dir])
  @extension = options[:extension] || "txt"

  initialize_system_files

  @syncstat = {}
  @syncdone = {}
  @syncstat_file = File.join(@data_dir, "__redsync_syncstat.yml")
  load_syncstat

  @remote_wiki = RemoteWiki.new(url: @url, api_key: @api_key)
  @local_wiki = LocalWiki.new(data_dir: @data_dir, extension: @extension)
end

Instance Method Details

#cleanupObject



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

def cleanup
  @syncstat.each do |name, timestamp|
    next if @syncdone[name]
    if @local_wiki.get(name).nil? && @remote_wiki.get(name)
      delete_remote(name)
    end

    if @local_wiki.get(name) && @remote_wiki.get(name).nil?
      delete_local(name)
    end

    if @local_wiki.get(name).nil? && @remote_wiki.get(name).nil?
      @syncstat.delete(name)
      save_syncstat
    end
  end
end

#delete_local(name) ⇒ Object



119
120
121
# File 'lib/redsync/project.rb', line 119

def delete_local(name)
  puts "Deleted on redmine:\t#{name}"
end

#delete_remote(name) ⇒ Object



124
125
126
# File 'lib/redsync/project.rb', line 124

def delete_remote(name)
  puts "Deleted locally:\t#{name}"
end

#download(name) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/redsync/project.rb', line 101

def download(name)
  puts "Downloading:\t#{name}"
  @local_wiki.write(name, @remote_wiki.get(name).content)
  @syncstat[name] = Time.new
  @syncdone[name] = true
  save_syncstat
end

#downsyncObject



62
63
64
65
66
67
68
69
# File 'lib/redsync/project.rb', line 62

def downsync
  @remote_wiki.list.each do |page|
    next if @syncdone[page.name]
    if @syncstat[page.name].nil? || page.mtime > @syncstat[page.name] && page.mtime > @local_wiki.get(page.name).mtime
      download(page.name)
    end
  end
end

#initialize_system_filesObject



28
29
30
31
32
33
# File 'lib/redsync/project.rb', line 28

def initialize_system_files
  unless File.exist? @data_dir
    puts "Creating #{@data_dir}"
    FileUtils.mkdir_p(@data_dir) 
  end
end

#load_syncstatObject



36
37
38
39
40
41
# File 'lib/redsync/project.rb', line 36

def load_syncstat
  @syncstat = {}
  @syncdone = {}
  return unless File.exist? @syncstat_file
  @syncstat = YAML.load_file(@syncstat_file)
end

#save_syncstatObject



44
45
46
47
48
# File 'lib/redsync/project.rb', line 44

def save_syncstat
  File.open(@syncstat_file, "w+:UTF-8") do |f|
    f.write(@syncstat.to_yaml)
  end
end

#status_checkObject



51
52
# File 'lib/redsync/project.rb', line 51

def status_check
end

#syncObject



55
56
57
58
59
# File 'lib/redsync/project.rb', line 55

def sync
  downsync
  upsync
  cleanup
end

#to_sObject



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

def to_s
  str = "#<Redsync::Project"
  str << " url = \"#{@url}\"\n"
  str << " data_dir = \"#{@data_dir}\"\n"
  str << " extension = \"#{@extension}\"\n"
  str << " pages = #{@syncstat.count}\n"
  str << ">"
end

#upload(name) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/redsync/project.rb', line 110

def upload(name)
  puts "Uploading:\t#{name}"
  @remote_wiki.write(name, @local_wiki.get(name).content)
  @syncstat[name] = Time.new
  @syncdone[name] = true
  save_syncstat
end

#upsyncObject



72
73
74
75
76
77
78
79
# File 'lib/redsync/project.rb', line 72

def upsync
  @local_wiki.list.each do |page|
    next if @syncdone[page.name]
    if @syncstat[page.name].nil? || page.mtime > @syncstat[page.name] && page.mtime > @remote_wiki.get(page.name).mtime
      upload(page.name)
    end
  end
end