Created
August 17, 2016 03:59
-
-
Save common-nighthawk/dd1ead0ff48c70c0f5fc2d5cd190aa4c to your computer and use it in GitHub Desktop.
Revisions
-
common-nighthawk created this gist
Aug 17, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ #!/usr/bin/env ruby require 'JSON' class LocationGetter attr_reader :endpoint def initialize(endpoint) @endpoint = endpoint end def fetch_location response = `curl #{endpoint}` JSON.parse(response) end end class Location attr_reader :datetime attr_accessor :ip, :city, :state def initialize(location_blob) @datetime = Time.now.utc.strftime("%Y%m%d%H%M") @ip = location_blob["ip"] @city = location_blob["city"].downcase @state = location_blob["region_name"].downcase end def details { ip: ip, city: city, state: state } end end class APIRequest attr_reader :key, :value def initialize(location) @key = location.datetime @value = location.details.to_json end def post! cmd = "curl -X PUT -d '#{value}' '#{endpoint}'" system(cmd) end private def endpoint "https://secret-secret-secret.firebaseio.com/datetimes/#{key}.json?auth=#{auth_token}" end def auth_token "HA!YOUWISH" end end my_location_blob = LocationGetter.new("http://freegeoip.net/json/").fetch_location my_location = Location.new(my_location_blob) APIRequest.new(my_location).post!