Class: GoogleWeather

Inherits:
Object
  • Object
show all
Defined in:
lib/feature/GoogleWeather.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {:cs => [],:log => Logger.new(File.join(File.dirname(__FILE__),'..','..','log','wish_mailer.log'))}) ⇒ GoogleWeather

Returns a new instance of GoogleWeather.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/feature/GoogleWeather.rb', line 4

def initialize(opts = {:cs => [],:log => Logger.new(File.join(File.dirname(__FILE__),'..','..','log','wish_mailer.log'))})
  # HangZhou
  # http://www.google.com/ig/api?hl=cn&weather=HangZhou
  # BeiJing
  # http://www.google.com/ig/api?hl=cn&weather=BeiJing
  @base_url = "http://www.google.com/ig/api?hl=cn&weather="
  @log = opts[:log] || Logger.new(File.join(File.dirname(__FILE__),'..','..','log','wish_mailer.log'))

  @citys = {}
  @log.info "Citys =>#{opts[:cs].inspect}"

  begin
    case opts[:cs]
    when Array
      opts[:cs].each{|c|@citys[c] = {:uri => URI.parse(@base_url+CGI.escape(c))}}
    when String
      @citys[opts[:cs]] = {:uri => URI.parse{@base_url+CGI.escape(cs)}}
    else
      raise ArgumentError,"Weather.new(citys) #citys couldn't nil,String or Array is ok."
    end
  rescue => e
    @log.error e.to_s
  end

  @log.info @citys.inspect
end

Instance Attribute Details

#citysObject (readonly)

Returns the value of attribute citys.



2
3
4
# File 'lib/feature/GoogleWeather.rb', line 2

def citys
  @citys
end

Instance Method Details

#fdataObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/feature/GoogleWeather.rb', line 49

def fdata
  report = []
  report << "Weather"
  report << "<br/>"*2
  
  report << "<table border='1'>"

  def format_table(* args)
    # args[0].to_s.center(15,'*') + '|' + args[1].to_s.center(20,'*') + '|' + args[2].to_s.center(10,'*') + '|' + args[3].to_s.center(10,'*') + '|' + args[4].to_s.center(15,'*')
    "<tr align='center'>" + args.inject(""){|result,x|result += "<td>#{x}</td>"} + "</tr>"
  end

  def icon_cell(src)
    "<img src=#{src}/>"
  end

  # header = format_table "City","day_of_week","Low","High","Condition"
  header = format_table("City","day_of_week","Low(℃)","High(℃)","Condition","")
  report << header

  try_time = 1

  begin
    @citys.each do |ct,obj|
      obj[:data]["xml_api_reply"]["weather"]["forecast_conditions"].each do |fc|
        report << format_table(ct,fc["day_of_week"]["data"],fc["low"]["data"].to_f.f2c.to_s[0..4],fc["high"]["data"].to_f.f2c.to_s[0..4],fc["condition"]["data"],icon_cell("'#{obj[:uri].scheme + "://" + obj[:uri].host + fc["icon"]["data"]}'"))
      end
    end
    @log.info report.inspect
  rescue NoMethodError => e
    @log.error e.to_s
    @log.info "Try #{try_time}(times)."
    self.get_weather
    (try_time+=1; retry) if(try_time <= 3)
  end

  report << "</table>"
  (report.join $/) + "<br/>"*4
end

#format_table(*args) ⇒ Object



56
57
58
59
# File 'lib/feature/GoogleWeather.rb', line 56

def format_table(* args)
  # args[0].to_s.center(15,'*') + '|' + args[1].to_s.center(20,'*') + '|' + args[2].to_s.center(10,'*') + '|' + args[3].to_s.center(10,'*') + '|' + args[4].to_s.center(15,'*')
  "<tr align='center'>" + args.inject(""){|result,x|result += "<td>#{x}</td>"} + "</tr>"
end

#get_weatherObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/feature/GoogleWeather.rb', line 31

def get_weather
  @citys.each do |city,hobj|
    @log.debug "City=>#{city},URI=>#{hobj[:uri]}"
    begin
    Net::HTTP.start(hobj[:uri].host, hobj[:uri].port) {|http|
      response = http.get(hobj[:uri].request_uri)
      @log.info "Get Weather #{city} from Google...Patient"
      @log.info response.body
      @citys[city][:data] = Hash.from_xml(response.body)
    }
    rescue => e
      @log.error e.to_s
    end
  end

  @citys
end

#icon_cell(src) ⇒ Object



61
62
63
# File 'lib/feature/GoogleWeather.rb', line 61

def icon_cell(src)
  "<img src=#{src}/>"
end