Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment