# -*- coding: utf-8 -*- # Copyright (C) 2013, TADA Tadashi # Original code from tatsu_zine.rb by KADO Masanori # You can redistribute it and/or modify it under GPL. # # display book info in http://p.booklog.jp/ like amazon.rb # USAGE: {{puboo 9999}} require 'open-uri' def puboo_cache_dir cache = "#{@cache_path}/puboo" Dir.mkdir( cache ) unless File.directory?( cache ) cache end def puboo_cache_set( id, result ) File.open( "#{puboo_cache_dir}/#{id}", "w" ) do |f| f.write result end end def puboo_cache_get( id ) File.open( "#{puboo_cache_dir}/#{id}", "r" ) do |f| f.read end rescue nil end def puboo( id, doc = nil ) if !@conf.secure and !(result = puboo_cache_get(id)).nil? return result end link = "http://p.booklog.jp/book/#{id}" doc ||= open( link ).read title = doc.match(%r|価格.*?(.*?).*?
|m).to_a[1] author = doc.match(%r|作者(.*?)|m).to_a[1].gsub(/<.*?>/, '').strip result = <<-EOS #{h title} #{h title}
#{h author}
#{h price}

EOS puboo_cache_set( id, result ) unless @conf.secure result rescue link end if __FILE__ == $0 require 'test/unit' class TestTatsuZine < Test::Unit::TestCase def setup @conf = Struct.new("Conf", :secure).new(true) def h(str); str; end end def test_puboo expect = <<-EOS 入門Puppet - Automate Your Infrastructure 入門Puppet - Automate Your Infrastructure
栗林健太郎
890円(税込)

EOS assert_equal expect, puboo('70667') end end end # Local Variables: # mode: ruby # indent-tabs-mode: t # tab-width: 3 # ruby-indent-level: 3 # End: