Skip to content

Instantly share code, notes, and snippets.

@kaochenlong
Created May 26, 2016 07:59
Show Gist options
  • Save kaochenlong/301a9613c28b4db93b647270e894d4ab to your computer and use it in GitHub Desktop.
Save kaochenlong/301a9613c28b4db93b647270e894d4ab to your computer and use it in GitHub Desktop.
require 'net/http'
require 'json'
class Webpage
def call(env)
content = File.read("map.html").gsub("{{locations}}", get_ubike)
[200, {'Content-Type' => 'text/html'}, [content]]
end
private
def get_ubike
url = URI("http://data.ntpc.gov.tw/api/v1/rest/datastore/382000000A-000352-001")
raw_data = Net::HTTP.get(url)
JSON.parse(raw_data)["result"]["records"].map do |raw|
[ raw["sna"], raw["lat"].to_f, raw["lng"].to_f ]
end.to_s
end
end
run Webpage.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment