Class: Travlrmap::SinatraApp

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/travlrmap/sinatra_app.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SinatraApp

Returns a new instance of SinatraApp.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/travlrmap/sinatra_app.rb', line 3

def initialize(config)
  @config = config
  @map = @config[:map]
  @types = @config[:types]

  raise "The constant APPROOT should be set in config.ru to the directory your webserver is serving from" unless defined?(APPROOT)
  raise "The directory %s set in APPROOT does not exist" % APPROOT unless File.directory?(APPROOT)

  load_map

  super()
end

Instance Method Details

#kml_style_url(type) ⇒ Object



30
31
32
# File 'lib/travlrmap/sinatra_app.rb', line 30

def kml_style_url(type)
  "travlrmap-%s-style" % type
end

#load_mapObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/travlrmap/sinatra_app.rb', line 34

def load_map
  @points = []

  Array(@config[:map][:data]).each do |map|
    point_file = File.join(File.join(APPROOT, "config", map))
    data = YAML.load_file(point_file)

    @points.concat(data[:points])
  end
end

#set_map_vars(view) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/travlrmap/sinatra_app.rb', line 45

def set_map_vars(view)
  @map_view = @config[:views][view]
  @zoom_control = @map[:zoom_control].nil? ? true : @map[:zoom_control]
  @map_type_control = @map[:map_type_control].nil? ? true : @map[:map_type_control]
  @street_view_control = @map[:street_view_control].nil? ? false : @map[:street_view_control]
  @overview_control = @map[:overview_control].nil? ? false : @map[:overview_control]
  @pan_control = @map[:pan_control].nil? ? true : @map[:pan_control]
end

#to_jsonObject



54
55
56
57
58
59
60
# File 'lib/travlrmap/sinatra_app.rb', line 54

def to_json
  @points.sort_by{|p| p[:country]}.map do |point|
    point[:popup_html] = erb(:"_point_comment", :layout => false, :locals => {:point => point})
    point[:icon] = @types[ point[:type] ][:icon]
    point
  end.to_json
end

#to_kmlObject



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
# File 'lib/travlrmap/sinatra_app.rb', line 62

def to_kml
  require 'ruby_kml'

  kml = KMLFile.new
  document = KML::Document.new(:name => "Travlrmap Data")
  folder = KML::Folder.new(:name => "Countries")
  folders = {}

  @types.each do |k, t|
    document.styles << KML::Style.new(
      :id         => kml_style_url(k),
      :icon_style => KML::IconStyle.new(:icon => KML::Icon.new(:href => t[:icon]))
    )
  end

  @points.sort_by{|p| p[:country]}.each do |point|
    unless folders[point[:country]]
      folder.features << folders[point[:country]] = KML::Folder.new(:name => point[:country])
    end

    f = folders[point[:country]]

    f.features << KML::Placemark.new(
      :name        => point[:title],
      :description => erb(:"_point_comment", :layout => false, :locals => {:point => point}),
      :geometry    => KML::Point.new(:coordinates => {:lat => point[:lat], :lng => point[:lon]}),
      :style_url   => "#%s" % kml_style_url(point[:type])
    )
  end

  document.features << folder
  kml.objects << document

  kml.render
end