class Periplus::Request

Constants

ADDRESS_STRUCTURE
BING_MAPS_URL
LOCATION_PATH
QUERY_IMAGE_PATH
ROUTE_IMAGE_PATH
ROUTE_PATH
WAYPOINT_FORMAT

Public Class Methods

new(api_key) click to toggle source
# File lib/periplus/request.rb, line 6
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

address_map_url(address, pushpins = [], options = {}) click to toggle source

Generate a URL for a location map

  • address is a hash or object with properties or keys like street, address, city, state, province, etc.

  • pushpins is an optional list of hashes with :latitude, :longitude, :type (optional – 1, 2, 3, etc. per the bing spec) or :label (optional – no longer than 2 characters per bing spec)

  • options is a hash that gets turned directly into url params for bing

# File lib/periplus/request.rb, line 48
def address_map_url(address, pushpins = [], options = {})
  options = default_options(options)
  base = "#{BING_MAPS_URL}#{QUERY_IMAGE_PATH}#{URI.escape(format_waypoint(address))}?#{options.to_params}"
  if pushpins and pushpins.length > 0
    formatted_pins = pushpins.map {|p| "pp=#{format_pushpin(p)}" }.join '&'
    base = "#{base}&#{formatted_pins}" if pushpins
  end
  base
end
location_details_url(address, options = {}) click to toggle source
# File lib/periplus/request.rb, line 58
def location_details_url(address, options = {})
  options = default_options(options).merge(:o => "json")
                                    .merge(structure_address(address))
  "#{BING_MAPS_URL}#{LOCATION_PATH}?#{options.to_params}"
end
route_details_url(waypoints, options = {}) click to toggle source
# File lib/periplus/request.rb, line 16
def route_details_url(waypoints, options = {})
  options = default_options(options).merge(hashify_waypoints(waypoints))
                                    .merge(:o => "json")

  "#{BING_MAPS_URL}#{ROUTE_PATH}?#{options.to_params}"
end
route_map_url(waypoints, pushpins = [], options = {}) click to toggle source

Generate a URL for a routes map.

  • waypoints is a list of hashes or objects with properties or keys like street, address, city, state, province, etc.

  • pushpins is an optional list of hashes with :latitude, :longitude, :type (optional – 1, 2, 3, etc. per the bing spec) or :label (optional – no longer than 2 characters per bing spec)

  • options is a hash that gets turned directly into url params for bing

# File lib/periplus/request.rb, line 30
def route_map_url(waypoints, pushpins = [], options = {})
  options = options.merge(hashify_waypoints(waypoints))
                   .merge(:key => @api_key)
  base = "#{BING_MAPS_URL}#{ROUTE_IMAGE_PATH}?#{options.to_params}"
  if pushpins and pushpins.length > 0
    formatted_pins = pushpins.map {|p| "pp=#{format_pushpin(p)}" }.join '&'
    base = "#{base}&#{formatted_pins}" if pushpins
  end
  base
end

Private Instance Methods

default_options(given_options) click to toggle source
# File lib/periplus/request.rb, line 65
def default_options(given_options)
  given_options.merge(:key => @api_key)
end
format_pushpin(pushpin) click to toggle source
# File lib/periplus/request.rb, line 94
def format_pushpin(pushpin)
  "#{pushpin[:latitude]},#{pushpin[:longitude]};#{pushpin[:type] || ""};#{pushpin[:label] || ""}"
end
format_waypoint(waypoint) click to toggle source
# File lib/periplus/request.rb, line 141
def format_waypoint(waypoint)
  return waypoint if waypoint.instance_of? String
  
  # use lat/long if provided
  if has_key_or_attribute?(waypoint, :latitude) and
      has_key_or_attribute?(waypoint, :longitude)
    latitude = get_by_key_or_attribute waypoint, :latitude
    longitude = get_by_key_or_attribute waypoint, :longitude
    return "#{latitude},#{longitude}" if latitude and longitude
  end
  
  if WAYPOINT_FORMAT
      .find_all { |el| el.instance_of? Symbol }
      .any? { |key| has_key_or_attribute?(waypoint, key) }
    
    # find all matching elements
    q = WAYPOINT_FORMAT.map do |attr|
      attr.instance_of?(String) ? attr : get_by_key_or_attribute(waypoint, attr)
    end.find_all { |el| el }
    
    q.inject('') do |query, el|
      # if it's punctuation or the first character, don't put a space before it
      if el =~ /^[.,!?]$/ or query.length == 0
        "#{query}#{el}"
      else
        "#{query} #{el}"
      end
    end
  else
    # we didn't have any elements matching
    waypoint.to_s
  end
end
get_by_key_or_attribute(object, key_or_attribute) click to toggle source
# File lib/periplus/request.rb, line 84
def get_by_key_or_attribute(object, key_or_attribute)
  if object.respond_to? :has_key?
    if object.has_key? key_or_attribute
      object[key_or_attribute]
    end
  elsif object.respond_to? key_or_attribute
    object.send key_or_attribute
  end
end
has_key_or_attribute?(object, key_or_attribute) click to toggle source
# File lib/periplus/request.rb, line 79
def has_key_or_attribute?(object, key_or_attribute)
  object.respond_to? key_or_attribute or 
    (object.respond_to? :has_key? and object.has_key? key_or_attribute)
end
hashify_waypoints(waypoints) click to toggle source

turns a list of waypoints into a bing-api-friendly “wp.1”, “wp.2”, etc…

# File lib/periplus/request.rb, line 70
def hashify_waypoints(waypoints)
  counter = 1
  waypoints.inject({}) do |hash, waypoint|
    hash["wp.#{counter}"] = format_waypoint(waypoint)
    counter = counter + 1
    hash
  end
end
structure_address(address) click to toggle source
# File lib/periplus/request.rb, line 113
def structure_address(address)
  return {:query => address} if address.kind_of? String

  ADDRESS_STRUCTURE.inject({}) do |structured, key_val|
    unconverted, converted = key_val
    
    if has_key_or_attribute? address, converted
      structured[converted] ||= get_by_key_or_attribute address, converted
    elsif has_key_or_attribute? address, unconverted
      structured[converted] ||= get_by_key_or_attribute address, unconverted
    end
    structured
  end
end