Skip to content

Instantly share code, notes, and snippets.

@common-nighthawk
Created August 17, 2016 03:59
Show Gist options
  • Select an option

  • Save common-nighthawk/dd1ead0ff48c70c0f5fc2d5cd190aa4c to your computer and use it in GitHub Desktop.

Select an option

Save common-nighthawk/dd1ead0ff48c70c0f5fc2d5cd190aa4c to your computer and use it in GitHub Desktop.

Revisions

  1. common-nighthawk created this gist Aug 17, 2016.
    61 changes: 61 additions & 0 deletions post_location.sh
    Original 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!