Class: WebpageArchivist::Webpage
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- WebpageArchivist::Webpage
- Defined in:
- lib/webpage-archivist/models.rb
Constant Summary collapse
- ASSETS_PATH =
File.(ENV['ARCHIVIST_ASSETS_PATH'] || './archivist_assets')
Instance Method Summary collapse
- #after_create ⇒ Object
- #index_path ⇒ Object
- #repository ⇒ Object
-
#repository_dir ⇒ Object
The directory containing the git repository.
- #save_content(content) ⇒ Object
-
#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.
- #validate ⇒ Object
Instance Method Details
#after_create ⇒ Object
66 67 68 69 |
# File 'lib/webpage-archivist/models.rb', line 66 def after_create super repository end |
#index_path ⇒ Object
22 23 24 |
# File 'lib/webpage-archivist/models.rb', line 22 def index_path File.join(repository_dir, 'index.html') end |
#repository ⇒ Object
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_dir ⇒ Object
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.(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, 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 end |
#validate ⇒ Object
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 |