mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
AirTrail (https://github.com/johanohly/AirTrail): - File export (Flights → Export for AirTrail) in AirTrail's import format, validated against its schema; same-day repeat legs get leg numbers so AirTrail's de-duplication doesn't merge them. - Automatic sync through AirTrail's REST API (Profile → AirTrail sync): per-user connection with an encrypted API key and scope check; flights are created/updated/deleted in background jobs; a full sync first links flights already in AirTrail (e.g. from a file import) instead of duplicating them; airports are resolved and cached, with manual mapping for unknown codes. - GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's track format for both sync and export; the flight page shows a summary. - Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type. Airport database (OurAirports, the source AirTrail uses): - Managed like AirTrail: downloaded in the background on first start, updated in place from Admin → Airports (adds, changes, removes; ids stable), with admin-managed custom airports that updates never touch. - Each airport carries AirTrail's code, so logged codes translate to AirTrail's exactly (1B9 → K1B9, 4MB → K5M3). - From/To autocomplete on new flights (airports this aircraft has used rank first); From is pre-filled with the last flight's destination; codes are normalized to uppercase. - Flights → Standardize airport codes: rewrite logged codes to the standard code, and fix codes not in the database (typos such as 0R8 → 08R are suggested). Also: admin pages grouped under an Admin dropdown menu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
3.4 KiB
Ruby
80 lines
3.4 KiB
Ruby
# In-memory AirTrail API for tests (WebMock). Mirrors /api/v1 shapes from
|
|
# AirTrail's openapi.yaml: { data: ... } envelopes, cursor pagination,
|
|
# Bearer auth and { error: { code, message } } errors.
|
|
class FakeAirTrail
|
|
BASE = "https://airtrail.test".freeze
|
|
AIRPORTS = {
|
|
"KOQU" => 101, "KBID" => 102, "KPVD" => 103, "KMFD" => 104, "K1B9" => 105, "NH53" => 106
|
|
}.freeze
|
|
|
|
attr_reader :flights, :requests
|
|
attr_accessor :scopes, :valid_key
|
|
|
|
def initialize(test)
|
|
@test = test
|
|
@flights = {}
|
|
@next_id = 1000
|
|
@requests = []
|
|
@scopes = AirTrailConnection::REQUIRED_SCOPES.dup
|
|
@valid_key = "at_secret"
|
|
stub!
|
|
end
|
|
|
|
def add_existing(date:, from:, to:, reg: "N12345", flight_number: nil, duration: nil)
|
|
id = (@next_id += 1)
|
|
@flights[id] = { "id" => id, "date" => date, "from" => airport(from), "to" => airport(to),
|
|
"flightNumber" => flight_number, "aircraftReg" => reg, "duration" => duration }
|
|
id
|
|
end
|
|
|
|
private
|
|
|
|
def airport(code) = { "id" => AIRPORTS.fetch(code), "icao" => code, "iata" => nil, "name" => "#{code} Airport" }
|
|
|
|
def stub!
|
|
@test.stub_request(:any, /airtrail\.test/).to_return do |req|
|
|
@requests << [ req.method, req.uri.path, req.body.to_s.empty? ? nil : JSON.parse(req.body) ]
|
|
next json(401, error: { code: "unauthorized", message: "Invalid API key" }) unless req.headers["Authorization"] == "Bearer #{@valid_key}"
|
|
route(req)
|
|
end
|
|
end
|
|
|
|
def route(req)
|
|
path = req.uri.path
|
|
query = URI.decode_www_form(req.uri.query.to_s).to_h
|
|
case [ req.method, path ]
|
|
in [ :get, "/api/v1/me" ]
|
|
json(200, data: { id: "u-ricky", username: "ricky", displayName: "Ricky", role: nil, isOwner: true,
|
|
permissions: [], credential: { kind: "apiKey", scopes: @scopes } })
|
|
in [ :get, "/api/v1/airports" ]
|
|
code = query["query"].to_s.upcase
|
|
json(200, data: AIRPORTS.key?(code) ? [ airport(code) ] : [], page: { nextCursor: nil })
|
|
in [ :get, "/api/v1/aircraft" ]
|
|
json(200, data: query["query"] == "C150" ? [ { id: 7, icao: "C150", name: "Cessna 150" } ] : [], page: { nextCursor: nil })
|
|
in [ :get, "/api/v1/flights" ]
|
|
all = @flights.values.sort_by { -_1["id"] }
|
|
offset = query["cursor"].to_i
|
|
page = all[offset, 2] || [] # tiny pages to exercise pagination
|
|
json(200, data: page, page: { nextCursor: offset + 2 < all.size ? (offset + 2).to_s : nil })
|
|
in [ :post, "/api/v1/flights" ]
|
|
body = JSON.parse(req.body)
|
|
return json(422, error: { code: "validation_failed", message: "Validation failed", details: [ { path: [ "fromId" ], message: "Required" } ] }) unless body["fromId"]
|
|
id = (@next_id += 1)
|
|
@flights[id] = body.merge("id" => id)
|
|
json(201, data: { id: id })
|
|
in [ :put, %r{\A/api/v1/flights/(\d+)\z} ]
|
|
id = path[/\d+\z/].to_i
|
|
return json(404, error: { code: "not_found", message: "Flight not found" }) unless @flights.key?(id)
|
|
@flights[id] = JSON.parse(req.body).merge("id" => id)
|
|
json(200, data: @flights[id])
|
|
in [ :delete, %r{\A/api/v1/flights/(\d+)\z} ]
|
|
id = path[/\d+\z/].to_i
|
|
@flights.delete(id) ? { status: 204, body: "" } : json(404, error: { code: "not_found", message: "Flight not found" })
|
|
else
|
|
json(404, error: { code: "not_found", message: "No route" })
|
|
end
|
|
end
|
|
|
|
def json(status, payload) = { status: status, body: JSON.generate(payload), headers: { "Content-Type" => "application/json" } }
|
|
end
|