Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created June 9, 2012 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cupakromer/2902415 to your computer and use it in GitHub Desktop.
Save cupakromer/2902415 to your computer and use it in GitHub Desktop.
module Fetcher
class BasicTraffic < Base
add_fetcher_options after: :count_severe_incidents
def self.inherited( subclass )
subclass.add_fetcher_options after: :count_severe_incidents
super
end
private
def bounding_box( order )
zip_geocode_data.bounding_box(order).join(',')
end
def zip_geocode_data
results = Geocoder.search(@cue).first
results.is_zip? ? results : Geocoder.search(results.extract_zip).first
end
def count_severe_incidents
extract_incidents(@data).count{ |incident| incident[:severity] == 4 }
end
def extract_incidents( incident_response_body )
fail "Must be implemented by child class."
end
end
end
module Fetcher
class BingMapsTraffic < BasicTraffic
include APIKey
api_key_param_name :key
TRAFFIC_INCIDENTS_URI = "/Traffic/Incidents"
base_uri "http://dev.virtualearth.net/REST/v1"
format :json
def uri
order = [:south_lat, :west_lng, :north_lat, :east_lng]
TRAFFIC_INCIDENTS_URI + "/#{bounding_box order}"
end
private
def options
wrap_query_options api_key_option.merge severity: 4, o: "json"
end
def extract_incidents( incident_response_body )
incident_response_body[:resourceSets][0][:resources]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment