-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rb
118 lines (90 loc) · 2.75 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require 'rubygems'
require 'sinatra'
require 'pstore'
require "yaml"
require './models/gpx_file.rb'
set :persistence, PStore.new("persistence/persistence.pstore")
set :user_options, YAML::load( File.open( 'config/sharearun.yml' ) )
set :dir_list, Dir.entries("gpx").select{|item|
if item.to_s.match(/^\./)
false
elsif !File.directory?("gpx/#{item}")
false
else
true
end
}
def insert_into_pstore(path_to_gpx)
@pstore = settings.persistence
puts "Crunching on #{path_to_gpx}"
category_folder = path_to_gpx.split("/")[1]
file_name = path_to_gpx.split("/").last
begin
parsed_file = GPX_file.new(path_to_gpx)
@pstore.transaction do
# ensure that an index has been created...
@pstore[category_folder] ||= Array.new
@pstore[path_to_gpx] = parsed_file
#add newly parsed file to the index
@pstore[category_folder].push(path_to_gpx)
end
puts "Analyzed and inserted #{path_to_gpx}"
rescue StandardError => e
puts "crashed while analyzing: #{path_to_gpx}: #{e}"
end
end
get '/' do
@dir_list = settings.dir_list
erb(:index)
end
get '/category/:foldername' do
@dir_list = settings.dir_list
throw :halt, [404, "This category does not exist!"] unless @dir_list.include?(params[:foldername])
@pstore = settings.persistence
@file_list = Dir.glob("gpx/#{params[:foldername]}/*.gpx").sort.reverse
files_to_insert = Array.new
#detect new files
@pstore.transaction(true) do
@file_list.each do |item|
if @pstore[params[:foldername]].nil?
files_to_insert << item
next
elsif !(@pstore[params[:foldername]].include?(item))
files_to_insert << item
end #if
end #do
end #do
#insert new files
files_to_insert.each do |gpxfile|
insert_into_pstore(gpxfile)
end
#update total stats and get file_list for view
@view_list = Array.new
@stats = Hash.new
@stats["total_distance"] = 0.0
@stats["total_duration"] = 0.0
@pstore.transaction do
@pstore[params[:foldername]].each do |gpxfile|
@stats["total_distance"] += @pstore[gpxfile].distance
@stats["total_duration"] += @pstore[gpxfile].duration
@view_list << @pstore[gpxfile]
end unless @pstore[params[:foldername]].nil?
end
@view_list.sort!{|item1, item2| item2.time_start <=> item1.time_start}
erb(:category)
end
get '/details/*' do
@dir_list = settings.dir_list
@pstore = settings.persistence
@google_maps_key = settings.user_options["google_maps_key"]
@file_data = nil
full_path = params[:splat].join("/")
throw :halt, [404, "File #{full_path} does not exist!"] unless File.exist?(full_path)
@pstore.transaction(true) {@file_data = @pstore[full_path]}
if @file_data.nil?
#it's not in there yet --> put it in there
insert_into_pstore(full_path)
@pstore.transaction(true) {@file_data = @pstore[full_path]}
end
erb(:details)
end