Class: GithubWiki::Scrapper

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

Instance Method Summary collapse

Constructor Details

#initialize(github_user, github_repo, output_directory = nil) ⇒ Scrapper

Returns a new instance of Scrapper.



8
9
10
11
12
13
# File 'lib/github_wiki.rb', line 8

def initialize(github_user, github_repo, output_directory = nil)
  @output_directory = output_directory || "#{github_user}-#{github_repo}-wiki"

  @github_user, @github_repo = github_user, github_repo

end

Instance Method Details

#build_edit_url(page_slug) ⇒ Object



19
20
21
22
23
24
# File 'lib/github_wiki.rb', line 19

def build_edit_url(page_slug)
  my_user    = Git.global_config('github.user')
  my_token   = Git.global_config('github.token')

  "http://github.com/#{@github_user}/#{@github_repo}/wikis/#{page_slug}/edit?login=#{my_user}&token=#{my_token}"
end

#index_urlObject



15
16
17
# File 'lib/github_wiki.rb', line 15

def index_url
 "http://wiki.github.com/#{@github_user}/#{@github_repo}" 
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/github_wiki.rb', line 26

def run
  doc = Nokogiri::HTML(open(index_url))

  FileUtils.mkdir_p @output_directory

  doc.search('.sidebar ul li a').each do |page_link|
    page_title = page_link.content
    page_slug = page_title.to_url

    edit_url = build_edit_url(page_slug)
    edit_html = open(edit_url)
    edit_doc = Nokogiri::HTML(edit_html)

    textile = nil
    edit_doc.search('textarea').each do |wiki_body|
      textile = wiki_body.content
    end

    File.open("#{@output_directory}/#{page_slug}.textile", "w") do |file|
      file.write textile
    end
  end
  
end