Changeset 19599 for lang/ruby

Show
Ignore:
Timestamp:
09/19/08 15:47:16 (2 months ago)
Author:
drry
Message:
  • fixed regexes.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/citrus/trunk/plugins/weather_search.rb

    r19598 r19599  
    77  def initialize(*args) 
    88    super 
    9     @prefix = @config['prefix'] || 'ws ' 
     9    @prefix = @config['prefix'] || 'ws' 
    1010    @channel = '' 
    1111    @area = '' 
    1212  end 
    13    
     13 
    1414  def on_privmsg(prefix, channel, message) 
    1515    case message 
    16     when /^#{@prefix}([^\s]+)(\s(\++))?$/i 
     16    when /^#{@prefix}\s+(\S+)(?:\s+(\++))?$/i 
    1717      @channel = channel 
    1818      @area = $1 
    19       notice(@channel, weather_search(@area, $3)) 
     19      notice(@channel, weather_search(@area, $2)) 
    2020    end 
    2121  end 
    22    
     22 
    2323  def weather_search(area, after_days) 
    2424    return help() if area == 'help' 
    2525    list = list() 
    26      
     26 
    2727    if area == 'list' 
    2828      notice(@channel, 'Area list from weather search') 
    29       str = "" 
     29      str = '' 
    3030      group = [] 
    3131      list['city'].each_with_index do |r,i| 
     
    3838      end 
    3939      notice(@channel, group.join(' / ')) if group.size > 0 
    40       return "ended." 
     40      return 'ended.' 
    4141    end 
    42      
    43     return ws_get(list,@area,after_days) 
     42 
     43    return ws_get(list, @area, after_days) 
    4444  end 
    45    
     45 
    4646  def help() 
    4747    'usage: ws (list | help | AREANAME) [ "+" 1-6 nums ]. Powered by livedoor.' 
    4848  end 
    49    
     49 
    5050  def list() 
    5151    data = { 'city' => [], 'source' => [] } 
     
    5959          return str 
    6060        end 
    61          
     61 
    6262        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 } 
    6666      end 
    6767    end 
    6868    data 
    6969  end 
    70    
    71   def ws_get(data,area,after_days) 
    72     return 'Oops! retry typing!!' unless data["city"].include? area 
    73     /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)] 
    7474    url = $1 
    75      
     75 
    7676    Net::HTTP.start('weather.livedoor.com', 80) do |http| 
    7777      begin 
     
    7979        res = http.request(get) 
    8080        log res.code.inspect 
    81          
     81 
    8282        if str = code_message(!res.code.to_i) 
    8383          return str 
    8484        end 
    85          
     85 
    8686        xml = REXML::Document.new(res.body) 
    8787        title = [] 
    8888        REXML::XPath.each(xml, '//item/title') { |t| title << t.text } 
    8989        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!!' 
    9191      end 
    9292    end 
    93     return "This plugin is testing now. Just a moment, please..." 
     93    return 'This plugin is testing now. Just a moment, please...' 
    9494  end 
    95      
     95 
    9696  def code_message(str) 
    9797    case str 
    9898    when 400 
    99       return "http request error." 
     99      return 'http request error.' 
    100100    when 401 
    101       return "oops,Unauthorized." 
     101      return 'oops, Unauthorized.' 
    102102    when 404 
    103       return "this area is not found." 
     103      return 'this area is not found.' 
    104104    end 
    105105  end