Sha256: 7f4e5cd5dc345c562d87596a7d7a6bab5f350453215291b724d498415de6b492

Contents?: true

Size: 1.14 KB

Versions: 16

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require "httparty"

module Decidim
  # This class generates a url to create a static map image for a geocoded resource
  class StaticMapGenerator
    def initialize(resource, options = {})
      @resource = resource
      @options = options

      @options[:zoom] ||= 15
      @options[:width] ||= 120
      @options[:height] ||= 120
    end

    def data
      return if Decidim.geocoder.nil? || @resource.blank?

      Rails.cache.fetch(@resource.cache_key) do
        request = HTTParty.get(uri, headers: { "Referer" => organization.host })
        request.body
      end
    end

    private

    def uri
      params = {
        c: "#{@resource.latitude}, #{@resource.longitude}",
        z: @options[:zoom],
        w: @options[:width],
        h: @options[:height],
        f: "1",
        app_id: Decidim.geocoder.fetch(:here_app_id),
        app_code: Decidim.geocoder.fetch(:here_app_code)
      }

      URI.parse(Decidim.geocoder.fetch(:static_map_url)).tap do |uri|
        uri.query = URI.encode_www_form params
      end
    end

    def organization
      @organization ||= @resource.feature.organization
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-core-0.10.1 app/services/decidim/static_map_generator.rb
decidim-core-0.10.0 app/services/decidim/static_map_generator.rb
decidim-core-0.9.3 app/services/decidim/static_map_generator.rb
decidim-core-0.9.2 app/services/decidim/static_map_generator.rb
decidim-core-0.9.1 app/services/decidim/static_map_generator.rb
decidim-core-0.9.0 app/services/decidim/static_map_generator.rb
decidim-core-0.8.4 app/services/decidim/static_map_generator.rb
decidim-core-0.8.3 app/services/decidim/static_map_generator.rb
decidim-core-0.8.2 app/services/decidim/static_map_generator.rb
decidim-core-0.8.1 app/services/decidim/static_map_generator.rb
decidim-core-0.8.0 app/services/decidim/static_map_generator.rb
decidim-core-0.7.4 app/services/decidim/static_map_generator.rb
decidim-core-0.7.3 app/services/decidim/static_map_generator.rb
decidim-core-0.7.2 app/services/decidim/static_map_generator.rb
decidim-core-0.7.1 app/services/decidim/static_map_generator.rb
decidim-core-0.7.0 app/services/decidim/static_map_generator.rb