Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script for "Rubyソースコード完全解説 (Ruby Hacking Guide)"
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
%w(uri json nokogiri curb cgi).each {|g| require g } | ||
|
||
root = 'http://i.loveruby.net/ja/rhg/book/' | ||
book = { | ||
:title => 'Rubyソースコード完全解説', | ||
:authors => [ 'Minero Aoki' ], | ||
:cover_image => 'http://direct.ips.co.jp/directsys/Images/Goods/1/1721B.gif', | ||
:content_xpath => '//body', | ||
:chapters => [] | ||
} | ||
|
||
def curl(url) | ||
c = Curl::Easy.new(url.to_s) | ||
c.follow_location = true | ||
c.perform | ||
c.body_str | ||
end | ||
|
||
doc = Nokogiri::HTML(curl(root)) | ||
doc.xpath('//ul/li/a').each do |a| | ||
chapter_url = URI(root) + a[:href] | ||
chapter = { | ||
:uri => chapter_url, | ||
:title => a.text, | ||
} | ||
book[:chapters] << chapter | ||
end | ||
|
||
puts JSON.pretty_generate(book) |