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

def initialize(config)
  @config = config
  @config[:sets] ||= {}
  @map = @config[:map]
  @types = @config[:types]
  @sets = @config[:sets] || {}
  @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)

  @map[:map_types] = ["hybrid", "roadmap", "satellite", "terrain", "osm"] unless @map[:map_types]
  @map[:default_map_type] = "roadmap" unless @map[:default_map_type]

  super()
end

Instance Method Details

#kml_style_url(type) ⇒ Object



64
65
66
# File 'lib/travlrmap/sinatra_app.rb', line 64

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

#load_map(set = nil) ⇒ Object



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

def load_map(set=nil)
  @points = []

  if set
    files = Array(@config[:sets][set][:data])
  else
    files = Array(@config[:map][:data])
  end

  files.each do |map|
    if map.is_a?(Symbol)
      @points.concat(load_map(map))
    else
      point_file = File.join(File.join(APPROOT, "config", map))

      if File.directory?(point_file)
        Dir.entries(point_file).grep(/\.yaml$/).each do |points|
          file = File.join(point_file, points)
          data = YAML.load_file(file)
          @points.concat(data[:points])
        end
      else
        data = YAML.load_file(point_file)
        @points.concat(data[:points])
      end
    end
  end
end

#point_from_json(json) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/travlrmap/sinatra_app.rb', line 152

def point_from_json(json)
  data = JSON.parse(json)

  point = {}

  ["title", "comment", "country", "date", "href", "linktext", "linkimg", "lon", "lat"].each do |i|
    if data[i].is_a?(String)
      point[i.intern] = data[i] unless data[i].empty?
    else
      point[i.intern] = data[i]
    end
  end

  point[:type] = data["type"].intern

  point
end

#set_map_vars(view, set = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/travlrmap/sinatra_app.rb', line 97

def set_map_vars(view, set=nil)
  @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
  @active_set = set
end

#to_jsonObject



108
109
110
111
112
113
114
# File 'lib/travlrmap/sinatra_app.rb', line 108

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/travlrmap/sinatra_app.rb', line 116

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