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

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

  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



56
57
58
# File 'lib/travlrmap/sinatra_app.rb', line 56

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

#load_mapObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/travlrmap/sinatra_app.rb', line 60

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



71
72
73
74
75
76
77
78
79
# File 'lib/travlrmap/sinatra_app.rb', line 71

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]
  @js_map_update = true
end

#to_jsonObject



81
82
83
84
85
86
87
# File 'lib/travlrmap/sinatra_app.rb', line 81

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



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
117
118
119
120
121
122
123
# File 'lib/travlrmap/sinatra_app.rb', line 89

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