Last active
December 24, 2015 13:29
-
-
Save jonahoffline/6804942 to your computer and use it in GitHub Desktop.
Refactoring of John's code without any external dependency.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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