Module: Ym4r::YahooMaps::BuildingBlock::MapImage

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.get(param) ⇒ Object

Send a request to the Map image API. Gets back a url to an image. This image can be downloaded later.



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
# File 'lib/ym4r/yahoo_maps/building_block/map_image.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 Map Image service")
  end
  
  url = "http://api.local.yahoo.com/MapsService/V1/mapImage?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 << "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 Map Image service")
  end
  
  doc = REXML::Document.new(xml) 
  
  if doc.root.name == "Error"
    raise RateLimitExceededException.new("Rate limit exceeded for Yahoo! Maps Map Image service")
  else
    result = doc.root
    MapImage::Result.new(result.attributes['warning'],
                         result.text)
  end
end