Class: WebpageArchivist::Webpage

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/webpage-archivist/models.rb

Constant Summary collapse

ASSETS_PATH =
File.expand_path(ENV['ARCHIVIST_ASSETS_PATH'] || './archivist_assets')

Instance Method Summary collapse

Instance Method Details

#after_createObject



66
67
68
69
# File 'lib/webpage-archivist/models.rb', line 66

def after_create
  super
  repository
end

#index_pathObject



22
23
24
# File 'lib/webpage-archivist/models.rb', line 22

def index_path
  File.join(repository_dir, 'index.html')
end

#repositoryObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/webpage-archivist/models.rb', line 39

def repository
  unless @repository
    if File.exist? "#{repository_dir}.git"
      @repository = Grit::Repo.new(repository_dir)
    else
      Dir.mkdir_if_not_exist repository_dir
      @repository = Grit::Repo.init(repository_dir)
    end
  end
  @repository
end

#repository_dirObject

The directory containing the git repository



31
32
33
34
35
36
37
# File 'lib/webpage-archivist/models.rb', line 31

def repository_dir
  unless @repository_dir
    @repository_dir = File.expand_path(File.join(ASSETS_PATH, self.id.to_s))
    Dir.mkdir_if_not_exist @repository_dir
  end
  @repository_dir
end

#save_content(content) ⇒ Object



26
27
28
# File 'lib/webpage-archivist/models.rb', line 26

def save_content content
  File.open(index_path, 'w') { |f| f.write(content) }
end

#update_repo_commit_changes(files, message) ⇒ Object

Update the repo and commit the changes

files

the files that should be in the repository

message

the commit message



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/webpage-archivist/models.rb', line 74

def update_repo_commit_changes files, message
  Dir.foreach(repository_dir) do |file|
    unless file.start_with?('.') || ('index.html' == file) || files.include?(file)
      File.delete File.join(repository_dir, file)
    end
  end
  status = repository.status
  repository.add status.untracked.keys
  repository.add status.changed.keys
  repository.remove status.deleted.keys
  repository.commit_index message
end

#validateObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/webpage-archivist/models.rb', line 51

def validate
  super
  validates_presence [:uri, :name]
  validates_max_length 5000, :uri
  validates_max_length 250, :name
  validates_unique :name, :message => "[#{self.name}] is already taken"
  if self.uri
    begin
      URI.parse self.uri
    rescue URI::InvalidURIError
      errors.add('uri', "[#{self.uri}] is not a valid uri")
    end
  end
end