Class: WikiTipsPost::WikiPostHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki_tips_post/wiki_post_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(title: title, tips: tips, tags: tags) ⇒ WikiPostHelper

Returns a new instance of WikiPostHelper.



9
10
11
12
13
14
15
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 9

def initialize(title: title, tips: tips, tags: tags)
  @title = title
  @tips = tips
  @tags = tags
  @tmp_dir = Dir.mktmpdir("wiki")
  @kernel = KernelWrapper.new
end

Instance Method Details

#add_new_pageObject



22
23
24
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 22

def add_new_page
  File.write(tip_path, @tips) 
end

#create_postObject



55
56
57
58
59
60
61
62
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 55

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_cloneObject



69
70
71
72
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 69

def delete_local_repo_clone
  delete_dir="sudo rm -r #{@tmp_dir}"
  @kernel.system(delete_dir)
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 39

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.index(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


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 26

def link_to_tag(tag)
  tag_path = File.join(
    @tmp_dir, 
    '#' + tag.chomp(' ').reverse.chomp(' ').reverse.gsub(' ','_') + '.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_repoObject



17
18
19
20
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 17

def prepare_repo
  git_clone="git clone #{WIKI_REPO} #{@tmp_dir}"
  @kernel.system(git_clone)
end

#push_to_repoObject



64
65
66
67
# File 'lib/wiki_tips_post/wiki_post_helper.rb', line 64

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