# -*- coding: utf-8 -*- # Copyright (C) 2011, KADO Masanori # You can redistribute it and/or modify it under GPL. # # display book info in http://tatsu-zine.com/ like amazon.rb # USAGE: {{tatsu_zine 1}} require 'open-uri' def tatsu_zine_cache_dir cache = "#{@cache_path}/tatsu-zine" Dir.mkdir( cache ) unless File.directory?( cache ) cache end def tatsu_zine_cache_set( id, result ) File.open( "#{tatsu_zine_cache_dir}/#{id}", "w" ) do |f| f.write result end end def tatsu_zine_cache_get( id ) File.open( "#{tatsu_zine_cache_dir}/#{id}", "r" ) do |f| f.read end rescue nil end def tatsu_zine( id, doc = nil ) if !@conf.secure and !(result = tatsu_zine_cache_get(id)).nil? return result end link = "https://tatsu-zine.com/books/#{id}" doc ||= open(link, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE).read title = doc.match(%r||).to_a[1] image = doc.match(%r||).to_a[1] price = doc.match(%r|span itemprop="price">(.*)|).to_a[1] author = doc.match(%r||).to_a[1] result = <<-EOS #{h title} #{h title}
#{h author}
#{h price}

EOS tatsu_zine_cache_set( id, result ) unless @conf.secure result rescue => e @logger.error(e) 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_tatsu_zine expect = <<-EOS Ruby環境構築講座 Windows編 Ruby環境構築講座 Windows編
arton
1036

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