class WikiTipsPost::WikiPostHelper
Public Class Methods
new(title: title, tips: tips, tags: tags)
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 8 def initialize(title: title, tips: tips, tags: tags) @title = title @tips = tips @tags = tags @tmp_dir = Dir.mktmpdir("wiki") @kernel = KernelWrapper.new end
Public Instance Methods
add_new_page()
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 21 def add_new_page File.write(tip_path, @tips) end
create_post()
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 53 def create_post add_new_page @tags.each do |tg| if link_to_tag(tg) link_to_index(tg) end end end
delete_local_repo_clone()
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 67 def delete_local_repo_clone delete_dir="sudo rm -r #{@tmp_dir}" @kernel.system(delete_dir) end
link_to_index(tag)
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 38 def link_to_index(tag) tag_link = "* [[ ##{ tag.gsub(' ', '_') } ]]" index_page_path = File.join(@tmp_dir, INDEX_PAGE) index_page_contents = File.read(index_page_path) unless index_page_contents.include?(tag_link) new_index_page_contents = index_page_contents.strip + "\n" + tag_link content = new_index_page_contents.split("\n").sort content.delete("Category with Tags:") content.insert 0, "Category with Tags:" final_content = content.join("\n") File.open(index_page_path, 'w') { |f| f.write final_content } end end
link_to_tag(tag)
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 25 def link_to_tag(tag) tag_path = File.join( @tmp_dir, '#' + sanitize_wiki_page_title(tag) + '.md' ) file_already_exists = File.exists?(tag_path) new_tag_contents = (file_already_exists ? File.read(tag_path).strip : "") + "\n* [[ #{@title} ]]" File.open(tag_path, 'w' ) { |f| f.write new_tag_contents } !file_already_exists end
prepare_repo()
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 16 def prepare_repo git_clone="git clone #{WIKI_REPO} #{@tmp_dir}" @kernel.system(git_clone) end
push_to_repo()
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 62 def push_to_repo git_push="pushd #{@tmp_dir} && git add . && git commit -m'Added a tip' && git push origin head && popd" @kernel.system(git_push) end
Private Instance Methods
sanitize_wiki_page_title(title)
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 78 def sanitize_wiki_page_title(title) title.strip.gsub(/\W+/, '-') end
tip_path()
click to toggle source
# File lib/wiki_tips_post/wiki_post_helper.rb, line 74 def tip_path File.join(@tmp_dir, sanitize_wiki_page_title(@title) + '.md') end