Skip to content

Instantly share code, notes, and snippets.

@jonahoffline
Last active December 24, 2015 13:29
Show Gist options
  • Save jonahoffline/6804942 to your computer and use it in GitHub Desktop.
Save jonahoffline/6804942 to your computer and use it in GitHub Desktop.
Refactoring of John's code without any external dependency.
require 'open-uri'
require 'json'
require 'ostruct'
# Usage example:
# require './usgs_data'
#
# quakes = Earthquake::Data.get_summary
# quakes.metadata
#=> {"generated"=>1380773976000,
# "url"=>
# "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson",
# "title"=>"USGS Magnitude 2.5+ Earthquakes, Past Day",
# "status"=>200,
# "api"=>"1.0.11",
# "count"=>22}
#
# Note: You can call any key from the original json like a method :)
module Earthquake
module Utils
def self.included(base)
base.extend self
end
module_function
def fetch_data
open('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson').read
end
def parse
JSON.parse(fetch_data)
end
end
class Data
include Earthquake::Utils
def self.get_summary
OpenStruct.new(parse)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment