-
-
Save trapped/265d1fb0a926f55125a5 to your computer and use it in GitHub Desktop.
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 "amethyst/all" | |
require "file" | |
require "dir" | |
require "./controllers/home" | |
require "./controllers/request" | |
require "./middleware/header" | |
module CellViewer | |
class App < Base::App | |
settings.configure do |conf| | |
conf.environment = "development" | |
conf.static_dirs = ["/static"] | |
end | |
routes.draw do | |
get "/", "home#home" | |
post "/r", "request#do_request" | |
get "/r/:id", "request#from_id" | |
register CellViewer::HomeController | |
register CellViewer::RequestController | |
end | |
use HeaderMiddleware | |
end | |
end | |
DBPATH = "#{__DIR__}/../requestdb" | |
puts "DBPATH: #{DBPATH}" | |
unless Dir.exists?(DBPATH) | |
puts "DB doesn't exist" | |
if File.exists? DBPATH | |
puts "DB node exists but is not a directory" | |
exit 1 | |
else | |
Dir.mkdir DBPATH | |
puts "Created DB" | |
end | |
end | |
app = CellViewer::App.new.serve 5555 |
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
ERROR: Missing hash value: "HomeController" *MissingKey@Exception#initialize:Array(String) +21 [140689213772528] *MissingKey::new:MissingKey +111 [140689213772528] *Hash(String, Amethyst::Base::Controller+:Class)@Hash(K, V)#fetch:Amethyst::Base::Controller+:Class +155 [140689213772528] *Amethyst::Dispatch::Router#process_named_route:Amethyst::Http::Response +138 [140689213772528] *Amethyst::Dispatch::Router#call:Amethyst::Http::Response +117 [140689213772528] *Amethyst::Middleware::Static#call:Amethyst::Http::Response +243 [140689213772528] *Amethyst::Middleware::TimeLogger#call:Amethyst::Http::Response +514 [140689213772528] *Amethyst::Middleware::HttpLogger#call:Amethyst::Http::Response +574 [140689213772528] *Amethyst::Middleware::ShowExceptions#call:Amethyst::Http::Response +455 [140689213772528] *CellViewer::HeaderMiddleware#call:Amethyst::Http::Response +293 [140689213772528] *Amethyst::Base::Handler#call:HTTP::Response +151 [140689213772528] *HTTP::Server#handle_client:Nil +281 [140689213772528] ~fun_literal_23 +29 [140689213772528] *Fiber#run:(Nil | Void) +256 [140689213772528] ~fun_literal_3 +17 [140689213772528] co_runner +81 [140689213772528] addseverity +1408 [140689213772528] |
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 "amethyst/all" | |
require "../googleapi/*" | |
module CellViewer | |
class HomeController < Controller | |
actions :home | |
view "home", "#{__DIR__}/../../views" | |
def home | |
html render "home" | |
end | |
end | |
end |
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 "amethyst/all" | |
require "json" | |
require "file" | |
require "dir" | |
require "../googleapi/*" | |
module CellViewer | |
class Location | |
getter lat | |
getter lng | |
getter accuracy | |
setter lat | |
setter lng | |
setter accuracy | |
json_mapping({ | |
lat: {type: Float32}, | |
lng: {type: Float32}, | |
accuracy: {type: Float32} | |
}) | |
def initialize | |
@lat = 0 | |
@lng = 0 | |
@accuracy = 0 | |
end | |
end | |
class CellTower | |
getter location | |
getter countrycode | |
getter networkcode | |
getter radiotype | |
getter carrier | |
getter cellid | |
getter locationareacode | |
getter signalstrength | |
getter error | |
setter location | |
setter countrycode | |
setter networkcode | |
setter radiotype | |
setter carrier | |
setter cellid | |
setter locationareacode | |
setter signalstrength | |
setter error | |
json_mapping({ | |
location: {type: Location, nilable: true, omit_null: true}, | |
countrycode: {type: Int32}, | |
networkcode: {type: Int32}, | |
radiotype: {type: String}, | |
carrier: {type: String}, | |
cellid: {type: Int32}, | |
locationareacode: {type: Int32}, | |
signalstrength: {type: Int32}, | |
error: {type: Hash(String, String), nilable: true, omit_null: true} | |
}) | |
def initialize | |
@location = Location.new | |
@countrycode = 0 | |
@networkcode = 0 | |
@radiotype = "" | |
@carrier = "" | |
@cellid = 0 | |
@locationareacode = 0 | |
@signalstrength = 0 | |
@error = nil | |
end | |
end | |
class Request | |
getter base | |
getter cells | |
getter error | |
setter base | |
setter cells | |
setter error | |
json_mapping({ | |
base: {type: String}, | |
cells: {type: Array(CellTower)}, | |
error: {type: String, nilable: true, omit_null: true} | |
}) | |
def initialize | |
@base = "" | |
@cells = [] of CellTower | |
@error = nil | |
end | |
end | |
class RequestController < Controller | |
actions :do_request, :from_id | |
view "request", "#{__DIR__}/../../views" | |
view "error", "#{__DIR__}/../../views" | |
@cells = [] of CellTower | |
@error = "" | |
def from_id | |
if File.exists? "#{DBPATH}/#{request.path_parameters["id"]}" | |
data = Request.from_json File.read("#{DBPATH}/#{request.path_parameters["id"]}") | |
@cells = data.cells | |
@error = data.error | |
html render "request" | |
else | |
@error = { | |
"code": 404, | |
"message": "Not found" | |
} | |
html render "error" | |
end | |
end | |
def next_id() | |
return (Dir.entries(DBPATH).length + 1).to_s(36) | |
end | |
#gets json from request body | |
#request.base.is_empty? -> from home -> new query -> new file -> return filename | |
#else -> from existing query -> copy file content to next id -> query new | |
# cells -> add to file -> return filename | |
def do_request | |
data = Request.from_json request.not_nil!.body.to_s | |
unless data.base.empty? | |
old_data = Request.from_json File.read("#{DBPATH}/#{data.base}") | |
old_data.base = data.base | |
old_data.cells.concat(data.cells).uniq! | |
data = old_data | |
end | |
data.cells.each do |q| | |
#query only new cells | |
next unless q | |
next if q.location | |
req = GoogleAPI::Geolocation::Request.new( | |
q.countrycode, | |
q.networkcode, | |
q.radiotype, | |
q.carrier, | |
[GoogleAPI::Geolocation::CellTower.new( | |
q.cellid, | |
q.locationareacode, | |
q.countrycode, | |
q.networkcode, | |
q.signalstrength | |
)] | |
) | |
res = GoogleAPI::Geolocation.request req | |
if res | |
if location = res.try &.location | |
q.location.not_nil!.lat = location.lat | |
q.location.not_nil!.lng = location.lng | |
q.location.not_nil!.accuracy = res.try &.accuracy | |
else | |
q.error = res.try &.error | |
end | |
else | |
q.error = GoogleAPI::Geolocation::Error.new 0, "No response" | |
end | |
end | |
new_id = next_id() | |
File.open "#{DBPATH}/#{new_id}", "rw" do |entry, data| | |
entry.print data.to_pretty_json | |
end | |
return new_id | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment