Sha256: 8beeb960550e84f5a439b2a2b73d91a7c9f94aedf284e534b59b868056038d78

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

# Gmaps geocoding module
module GmapsGeocoding
  # Configuration valid keys
  VALID_KEYS = [:url, :output, :key, :address, :latlng, :components, :sensor, :bounds, :language, :region, :place_id, :result_type, :location_type].freeze

  # Valid query parameters
  VALID_QUERY_PARAMS = VALID_KEYS - [:url, :output].freeze

  # Configuration class for GmapsGeocoding API.
  class Config
    # All valid keys is stored in instance attribute
    attr_accessor(*VALID_KEYS)

    # Default configuration values
    DEFAULT_CONFIG = {
      url: 'https://maps.googleapis.com/maps/api/geocode'.freeze,
      output: 'json'.freeze,
      sensor: 'false'.freeze
    }.freeze

    def initialize(opts = {})
      opts = DEFAULT_CONFIG.merge(opts)
      VALID_KEYS.each do |k, _|
        next unless VALID_KEYS.include?(k)
        val = ENV["GOOGLE_MAPS_GEOCODING_#{k.to_s.upcase}"] || opts.delete(k)
        send("#{k}=", val) if val
      end
    end

    # Check if the configuration object is valid
    #
    # @return [true, false] Return _true_ or _false_
    def valid?
      query_valid? && output_param_valid?
    end

    # Check if the output format of the query is set to _json_
    #
    # @return [true, false] Return _true_ or _false_
    def json_format?
      'json'.freeze.eql?(output)
    end

    # Check if the output format of the query is set to _xml_
    #
    # @return [true, false] Return _true_ or _false_
    def xml_format?
      'xml'.freeze.eql?(output)
    end

    private

    # Check if the query is valid
    #
    # According to the specifications: {https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests}
    def query_valid?
      (address && latlng.nil?) ||
        (latlng && address.nil?) ||
        !components.nil?
    end

    # Check if the output format is valid
    #
    # According to the specifications: {https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses}
    def output_param_valid?
      %w(json xml).include?(output)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
gmaps_geocoding-1.3.5 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.3.4 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.3.3 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.3.2 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.3.1 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.3.0 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.2.2 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.2.1 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.2.0 lib/gmaps_geocoding/config.rb
gmaps_geocoding-1.1.4 lib/gmaps_geocoding/config.rb