- Timestamp:
- 09/19/08 15:47:16 (2 months ago)
- Files:
-
- 1 modified
-
lang/ruby/citrus/trunk/plugins/weather_search.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/citrus/trunk/plugins/weather_search.rb
r19598 r19599 7 7 def initialize(*args) 8 8 super 9 @prefix = @config['prefix'] || 'ws '9 @prefix = @config['prefix'] || 'ws' 10 10 @channel = '' 11 11 @area = '' 12 12 end 13 13 14 14 def on_privmsg(prefix, channel, message) 15 15 case message 16 when /^#{@prefix} ([^\s]+)(\s(\++))?$/i16 when /^#{@prefix}\s+(\S+)(?:\s+(\++))?$/i 17 17 @channel = channel 18 18 @area = $1 19 notice(@channel, weather_search(@area, $ 3))19 notice(@channel, weather_search(@area, $2)) 20 20 end 21 21 end 22 22 23 23 def weather_search(area, after_days) 24 24 return help() if area == 'help' 25 25 list = list() 26 26 27 27 if area == 'list' 28 28 notice(@channel, 'Area list from weather search') 29 str = ""29 str = '' 30 30 group = [] 31 31 list['city'].each_with_index do |r,i| … … 38 38 end 39 39 notice(@channel, group.join(' / ')) if group.size > 0 40 return "ended."40 return 'ended.' 41 41 end 42 43 return ws_get(list, @area,after_days)42 43 return ws_get(list, @area, after_days) 44 44 end 45 45 46 46 def help() 47 47 'usage: ws (list | help | AREANAME) [ "+" 1-6 nums ]. Powered by livedoor.' 48 48 end 49 49 50 50 def list() 51 51 data = { 'city' => [], 'source' => [] } … … 59 59 return str 60 60 end 61 61 62 62 xml = REXML::Document.new(res.body) 63 64 REXML::XPath.each(xml, '//city/@title') { |t| data['city'] << t.to_s }65 REXML::XPath.each(xml, '//city/@source') { |t| data['source'] << t.to_s }63 64 REXML::XPath.each(xml, '//city/@title') { |t| data['city'] << t.to_s } 65 REXML::XPath.each(xml, '//city/@source') { |t| data['source'] << t.to_s } 66 66 end 67 67 end 68 68 data 69 69 end 70 71 def ws_get(data, area,after_days)72 return 'Oops! retry typing!!' unless data[ "city"].include? area73 /http:\/\/weather.livedoor.com(.+)/ =~ data["source"][data["city"].index(area)]70 71 def ws_get(data, area, after_days) 72 return 'Oops! retry typing!!' unless data['city'].include? area 73 %r{http://weather\.livedoor\.com(.+)} =~ data['source'][data['city'].index(area)] 74 74 url = $1 75 75 76 76 Net::HTTP.start('weather.livedoor.com', 80) do |http| 77 77 begin … … 79 79 res = http.request(get) 80 80 log res.code.inspect 81 81 82 82 if str = code_message(!res.code.to_i) 83 83 return str 84 84 end 85 85 86 86 xml = REXML::Document.new(res.body) 87 87 title = [] 88 88 REXML::XPath.each(xml, '//item/title') { |t| title << t.text } 89 89 after_num = after_days.nil? ? 0 : after_days.split('').size 90 return title[1+after_num] ? title[1+after_num] : "Nothing data!!"90 return title[1+after_num] ? title[1+after_num] : 'Nothing data!!' 91 91 end 92 92 end 93 return "This plugin is testing now. Just a moment, please..."93 return 'This plugin is testing now. Just a moment, please...' 94 94 end 95 95 96 96 def code_message(str) 97 97 case str 98 98 when 400 99 return "http request error."99 return 'http request error.' 100 100 when 401 101 return "oops,Unauthorized."101 return 'oops, Unauthorized.' 102 102 when 404 103 return "this area is not found."103 return 'this area is not found.' 104 104 end 105 105 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)