Refactor vizzjson methods into its own classes

2.0
Lorenzo Planas 12 years ago
parent 74b2087d2f
commit 89c8fb7e18

@ -1,4 +1,5 @@
# coding: UTF-8
require_relative '../../../helpers/vizzjson/layer'
class Api::Json::LayersController < Api::ApplicationController
ssl_required :index, :show, :create, :update, :destroy
@ -21,7 +22,10 @@ class Api::Json::LayersController < Api::ApplicationController
render :text => "#{params[:callback]}( #{@layer.to_tilejson} )"
end
format.json do
render_jsonp(view_context.layer_vizzjson(@layer, full: false))
render_jsonp(CartoDB::VizzJSON::Layer.new(
@layer, { full: false }, Cartodb.config
).to_poro
)
end
end
end

@ -1,4 +1,6 @@
# coding: UTF-8
require_relative '../../../helpers/vizzjson/map'
class Api::Json::TablesController < Api::ApplicationController
ssl_required :index, :show, :create, :update, :destroy
skip_before_filter :api_authorization_required, :only => [ :vizzjson ]
@ -130,7 +132,14 @@ class Api::Json::TablesController < Api::ApplicationController
if @table.present? && (@table.public? || (current_user.present? && @table.owner.id == current_user.id))
response.headers['X-Cache-Channel'] = "#{@table.varnish_key}:vizjson"
response.headers['Cache-Control'] = "no-cache,max-age=86400,must-revalidate, public"
render_jsonp(view_context.map_vizzjson(@table.map, full: false))
render_jsonp(
CartoDB::VizzJSON::Map.new(
@table.map,
{ full: false, url: table_url(@table) },
Cartodb.config,
CartoDB::Logger
).to_poro
)
else
head :forbidden
end

@ -1,19 +0,0 @@
module LayersHelper
def layer_vizzjson(layer, options)
layer.kind == 'carto' ? layer_carto_vizzjson(layer, options) : layer.public_values
end
def layer_carto_vizzjson(layer, options)
Hash[Layer::PUBLIC_ATTRIBUTES.map{ |key|
if key == "options" && !options[:full]
[:options, layer.options.select { |k,v| Cartodb.config[:layer_opts]["public_opts"].include?(k.to_s) }]
elsif key == "infowindow"
[:infowindow, (layer.infowindow.merge(:template => render(:file => layer.template_path)) rescue nil) ]
else
[key, layer.send(key)]
end
}]
end
end

@ -1,41 +0,0 @@
module MapsHelper
def map_vizzjson(map, options = {})
options.reverse_merge! full: true
bounds = JSON.parse("[#{map.view_bounds_sw}, #{map.view_bounds_ne}]") rescue []
CartoDB::Logger.info(map.inspect)
{
version: "0.1.0",
updated_at: map.viz_updated_at,
layers: [
layer_vizzjson(map.base_layers.first, options),
layer_vizzjson(map.data_layers.first, options)
],
overlays: [
{
type: "zoom",
template: '<a class="zoom_in">+</a><a class="zoom_out">-</a>'
},
{
type: "loader",
template: '<div class="loader"></div>'
}
],
description: map.tables.first.description,
title: map.tables.first.name,
url: table_url(map.tables.first),
map_provider: map.provider,
bounds: (bounds.blank? ? nil : bounds),
center: map.center,
zoom: map.zoom
}
end
end

@ -0,0 +1,50 @@
# encoding: utf-8
require 'json'
require 'mustache'
module CartoDB
module VizzJSON
class Layer
PUBLIC_ATTRIBUTES = %W{ options kind infowindow id order }
def initialize(layer, options={}, configuration={})
@layer = layer
@options = options
@configuration = configuration
end #initialize
def to_poro
return layer.public_values unless layer.kind == 'carto'
carto_to_poro
end #to_poro
private
attr_reader :layer, :options, :configuration
def carto_to_poro
Hash[PUBLIC_ATTRIBUTES.map { |key| data_for(key) }]
end #carto_to_poro
def data_for(key)
return [:options, options_data] if key == "options" && !options[:full]
return [:infowindow, infowindow_data] if key == "infowindow"
return [key, layer.send(key)]
end #data_for
def options_data
layer.options.select { |key, value| public_options.include?(key.to_s) }
end #options_data
def infowindow_data
layer.infowindow.merge(template: File.read(layer.template_path))
rescue => exception
end #infowindow_data
def public_options
configuration.fetch(:layer_opts).fetch("public_opts")
end #public_options
end # Layer
end # VizzJSON
end # CartoDB

@ -0,0 +1,60 @@
# encoding: utf-8
require 'json'
require_relative './layer'
module CartoDB
module VizzJSON
class Map
def initialize(map, options={}, configuration={}, logger=nil)
@map = map
@table = map.tables.first
@options = { full: true }.merge(options)
@configuration = configuration
logger.info(map.inspect) if logger
end #initialize
def to_poro
{
version: "0.1.0",
title: table.name,
description: table.description,
url: options.delete(:url),
map_provider: map.provider,
bounds: bounds_from(map),
center: map.center,
zoom: map.zoom,
updated_at: map.viz_updated_at,
layers: [
VizzJSON::Layer.new(map.base_layers.first, options, configuration)
.to_poro,
VizzJSON::Layer.new(map.data_layers.first, options, configuration)
.to_poro
],
overlays: [
{
type: "zoom",
template: '<a class="zoom_in">+</a><a class="zoom_out">-</a>'
},
{
type: "loader",
template: '<div class="loader"></div>'
}
]
}
end #to_poro
private
attr_reader :map, :table, :options, :configuration
def bounds_from(map)
::JSON.parse("[#{map.view_bounds_sw}, #{map.view_bounds_ne}]")
rescue => exception
nil
end #bounds_from
end # Map
end # VizzJSON
end # CartoDB
Loading…
Cancel
Save