Sha256: 97c292e07d5ca761cc0d9b56c3ce6df4f54f2149ff824103f66232866be5eb05

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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?

      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

1 entries across 1 versions & 1 rubygems

Version Path
decidim-core-0.1.0 app/services/decidim/static_map_generator.rb