Module: Ym4r::YahooMaps::BuildingBlock::Traffic

Defined in:
lib/ym4r/yahoo_maps/building_block/traffic.rb

Defined Under Namespace

Classes: Result, ResultSet

Class Method Summary collapse

Class Method Details

.get(param) ⇒ Object

Send a request to the traffic REST API.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ym4r/yahoo_maps/building_block/traffic.rb', line 11

def self.get(param)
  unless param.has_key?(:street) or
      param.has_key?(:city) or
      param.has_key?(:state) or
      param.has_key?(:zip) or
      param.has_key?(:location) or
      (param.has_key?(:longitude) and param.has_key?(:latitude))
    raise MissingParameterException.new("Missing location data for the Yahoo! Maps Traffic service")
  end
  
  url = "http://api.local.yahoo.com/MapsService/V1/trafficData?appid=#{Ym4r::YahooMaps::APP_ID}&"
  url << "street=#{param[:street]}&" if param.has_key?(:street)
  url << "city=#{param[:city]}&" if param.has_key?(:city)
  url << "state=#{param[:state]}&" if param.has_key?(:state)
  url << "zip=#{param[:zip]}&" if param.has_key?(:zip)
  url << "location=#{param[:location]}&" if param.has_key?(:location)
  url << "latitude=#{param[:latitude]}&" if param.has_key?(:latitude)
  url << "longitude=#{param[:longitude]}&" if param.has_key?(:longitude)
  url << "image_type=#{param[:image_type]}&" if param.has_key?(:image_type) #defaults to PNG
  url << "image_height=#{param[:image_height]}&" if param.has_key?(:image_height) #defaults to 500
  url << "image_width=#{param[:image_width]}&" if param.has_key?(:image_width) #defaults to 620
  url << "zoom=#{param[:zoom]}&" if param.has_key?(:zoom) #defaults to 6
  url << "radius=#{param[:radius]}&" if param.has_key?(:radius)
  url << "include_map=#{param[:include_map]?1:0}&" if param.has_key?(:include_map)
  url << "severity=#{param[:severity]}&" if param.has_key?(:severity)
  url << "output=xml"
  
  begin
    xml = open(URI.encode(url)).read
  rescue OpenURI::HTTPError => error
    raise BadRequestException.new(error.to_s)
  rescue
    raise ConnectionException.new("Unable to connect to Yahoo! Maps Traffic REST service")
  end
  
  doc = REXML::Document.new(xml) 
  
  if doc.root.name == "Error"
    raise RateLimitExceededException.new("Rate limit exceeded for Yahoo! Maps Traffic REST service")
  else
    results = Traffic::ResultSet.new(Time.at(doc.root.elements['LastUpdateDate'].text.to_i),doc.root.elements['Warning'].nil? ? nil : doc.root.elements['Warning'].text)
    
    doc.root.elements.each('//Result') do |result|
      data = result.elements
      results << Traffic::Result.new(result.attributes['type'],
                                     data['Title'].text,
                                     data['Description'].text,
                                     data['Latitude'].text.to_f,
                                     data['Longitude'].text.to_f,
                                     data['Direction'].text,
                                     data['Severity'].text.to_i,
                                     Time.at(data['ReportDate'].text.to_i),
                                     Time.at(data['UpdateDate'].text.to_i),
                                     Time.at(data['EndDate'].text.to_i),
                                     data['ImageUrl'].nil? ? nil : data['ImageUrl'].text)
    end
    results
    
  end
end