Sha256: 2f5118773803f77b32a841d3b6eea2438c649e168132bf495b0ec6ba4124b3c2

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

require 'uri'

module Voom
  module Presenters
    module Plugins
      module GoogleMaps
        class GoogleMap < DSL::Components::EventBase

          attr_reader :url, :google_api_key, :height, :width

          def initialize(**attribs_, &block)
            super(type: :google_map, **attribs_, &block)
            @address = attribs.delete(:address)
            @latitude = attribs.delete(:latitude)
            @longitude = attribs.delete(:longitude)
            @width = attribs.delete(:width) { 640 }
            @height = attribs.delete(:height) { 640 }
            @zoom = attribs.delete(:zoom) { 14 }
            @scale = attribs.delete(:scale) { 1 }
            @google_api_key = attribs.delete(:google_api_key) { ENV['GOOGLE_API_KEY'] }
            @url = build_static_map_image_url
            expand!
          end

          private

          def build_static_map_image_url
            return @img_url if locked?
            @img_url = "https://maps.googleapis.com/maps/api/staticmap?center=#{query_string}&zoom=#{@zoom}&scale=#{@scale}&size=#{@width}x#{@height}&markers=|#{query_string}&key=#{@google_api_key}"
          end

          def query_string
            return "#{@latitude},#{@longitude}" if @latitude && @longitude
            URI.escape(@address)
          end

        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
voom-presenters-2.1.2 lib/voom/presenters/plugins/google_maps/google_map.rb
voom-presenters-2.1.0 lib/voom/presenters/plugins/google_maps/google_map.rb
voom-presenters-2.0.3 lib/voom/presenters/plugins/google_maps/google_map.rb
voom-presenters-2.0.2 lib/voom/presenters/plugins/google_maps/google_map.rb
voom-presenters-2.0.1 lib/voom/presenters/plugins/google_maps/google_map.rb
voom-presenters-2.0.0 lib/voom/presenters/plugins/google_maps/google_map.rb