+++++++++++++++++++++++++++++++++++++++++++++++
2012-11-10 続・メイドちゃん、さらに続きます。
+++++++++++++++++++++++++++++++++++++++++++++++
ちょこちょこと機能を追加したメイドちゃんの挙動が、
いい感じに安定してきたので、ここで変更後のソースとかさらしてみる。
メイドちゃんが前回と比べてどの辺が成長したかというと。
・雨が降りそうなときに教えてくれる。
・セリフ増量
・無駄にオブジェクト化
つまり私が得をする感じの追加です。
ソースコードは続きから
*********************
そいうわけでソースコードです。
・maid-tyan.rb(メールを検索したり送信したりする)
・MaidMail.rb (メールの中身を作る)
の2つのファイルで動かしてあります。
maid-tyan.rb
#coding: utf-8 require 'gmail' require 'kconv' day = Time.now require "./MaidMail.rb" #gmailにログイン USERNAME='mail@gmail.com' #gmailのアドレス PASSWORD='pass' #gmailのパスワード gmail = Gmail.new(USERNAME,PASSWORD) #メールを送信するかしないかのフラグ mailFlag = 0 fullSchedule = "" #Workフォルダ内の未読を調べる mail = gmail.mailbox('Work').emails(:unread).map do |mail| #件名があるときだけ、 if mail.subject != nil schedule = Kconv.toutf8(mail.subject) today = day.strftime("%m%d") #今日のタスクがある if /^#{today}/ =~ schedule mailFlag += 1 schedule = schedule.sub(/^#{today}\s/,'') #複数のタスクを分ける (空白を"・・"に置き換えてから”\n・”にする) while /\s/ =~ schedule schedule = schedule.sub(/\s/,"・・") end while /・・/ =~ schedule schedule = schedule.sub(/・・/,"\n・") end fullSchedule += "・" + schedule + "\n" #本文処理⇒備考に if !mail.text_part && !mail.html_part if mail.body.decoded.encode("UTF-8", mail.charset) != "" fullSchedule += " (" + mail.body.decoded.encode("UTF-8", mail.charset) + ")\n" end elsif mail.text_part fullSchedule += " (" + mail.text_part.decoded + ")\n" elsif mail.html_part fullSchedule += " (" + mail.html_part.decoded + ")\n" end #mail.mark(:unread) #読み込んだメールを未読に(テスト用) next end mail.mark(:unread) #使わなかったメールを未読に end end #予定のあるときのみ送信 if mailFlag > 0 maidSerif = MaidMail.new #メール送信 gmail.deliver do to maidSerif.sendTo subject maidSerif.sub body maidSerif.text(fullSchedule) end #出力結果テスト用 #maidSerif.showMail (fullSchedule) end
MaidMail.rb
# coding: utf-8 require "weather_hacker" class MaidMail def initialize(address = "keitai@docomo.ne.jp") @sendTo = address @sendSub = "メイドちゃんより" @sendText = "" end def sendTo return @sendTo end def sub return @sendSub end def status array = [ "ちょっぴりおねむなメイドちゃんです.ヾ(^▽^*ζ", "ζ>∇<)ノ できる女 メイドちゃんです.", "あなたのかわいいメイドちゃんです.ヾ(´v`*ζ", "メイドちゃんですよ♪ v(・ω´・+*ζ", "ζノ≧▽≦)ノ メイドちゃんが可憐に登場です♪" ] return array[rand(array.length)] end def weather zipcode = "000-0000" #郵便番号 forecast = WeatherHacker.new(zipcode) if "雨" == forecast.today['weather'] #”雨”の予報のとき weatherMessage = "雨ですね。風邪などひかれませんようお気を付け下さい。\n" elsif /雨/ =~ forecast.today['weather'] # ”雨のち曇り”とか”雨時々晴”とかの予報のとき weatherMessage = "今日は折り畳み傘がいるかもです。\n" end return weatherMessage end def text(fullSchedule) day = Time.now maid_tyan_messageFirst = <<-EOS ご主人様 おはようございます! #{self.status} #{self.weather} 今日(#{day.strftime("%m月%d日")})のご予定をお知らせしますね。 EOS maid_tyan_messageLast = <<-EOS それでは 今日も元気に頑張ってください♪ EOS @sendText += maid_tyan_messageFirst + fullSchedule + maid_tyan_messageLast return @sendText end def showMail(fullSchedule) text(fullSchedule) print @sendTo + "\n" print @sendSub + "\n" print @sendText + "\n" end end
またなにか面白そうなの思いついたら、機能を付け加えていきます。