Class: Doko
- Inherits:
-
Object
- Object
- Doko
- Defined in:
- lib/doko.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(str) ⇒ Doko
constructor
A new instance of Doko.
- #parse ⇒ Object
Constructor Details
#initialize(str) ⇒ Doko
Returns a new instance of Doko.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/doko.rb', line 39 def initialize(str) if str.kind_of? URI str = open(str.to_s).read elsif str.match( /^#{URI.regexp}$/ ) str = open(str).read end if str.match(/<html/i) @text = (Nokogiri::HTML(str)/"body").text else @text = str end end |
Class Method Details
.deep(str, base_uri = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/doko.rb', line 12 def self.deep(str,base_uri=nil) addrs = parse(str) if addrs.empty? addrs = links(str,base_uri).map{ |u| parse(u) }.flatten end addrs end |
.links(str, base_uri = nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/doko.rb', line 22 def self.links(str,base_uri=nil) out = [] if str.match( /^#{URI.regexp}$/ ) uri = URI.parse(str) doc = Nokogiri::HTML(open(uri).read) elsif str.kind_of? String uri = URI.parse(base_uri) doc = Nokogiri::HTML(str) end doc.search("a").each do |a| if a[:href] && a[:href].match(/access/) && !a[:href].match(/http/) out << uri + a[:href] end end out.uniq end |
.parse(str) ⇒ Object
8 9 10 |
# File 'lib/doko.rb', line 8 def self.parse(str) self.new(str).parse end |
Instance Method Details
#parse ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/doko.rb', line 52 def parse body = @text body.tr!("0-9","0-9") body.tr!("()","()") body.tr!("、",",") body.tr!(" "," ") body.tr!(".",".") body.tr!(":",":") blackchars = ",()\n" addrs = body.scan(/\b([^\s,()]{2,3}(都|道|府|県)[^\s,()]{1,8}(市|区|町|村)[^#{blackchars}]+)/).map{ |m| clean(m[0]) } if addrs.empty? addrs = body.scan(/([^\s]{1,6}(市|区).{1,8}(区|町|村)[^\s,()]{2,10}\d)/).map{ |m| clean(m[0]) } end addrs.select{ |a| !a.match(/を/) } end |