Sha256: 571a2fc78f1fd8911881d85c3dfacde99f03c7e0a77d8d39f35f73bea9680114

Contents?: true

Size: 1.88 KB

Versions: 29

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'timezone/lookup/geonames'
require 'timezone/lookup/google'
require 'timezone/lookup/test'
require 'timezone/net_http_client'

module Timezone
  # Configure timezone lookups.
  module Lookup
    class << self
      MISSING_LOOKUP = 'No lookup configured'.freeze
      private_constant :MISSING_LOOKUP

      # Returns the lookup object
      #
      # @return [#lookup] the lookup object
      # @raise [Timezone::Error::InvalidConfig] if the lookup has not
      #   been configured
      def lookup
        @lookup || raise(::Timezone::Error::InvalidConfig, MISSING_LOOKUP)
      end

      # Configure a lookup object
      #
      # @param lookup [:google, :geonames, :test] use a built-in lookup
      # @param lookup [Class] a custom lookup class
      # @yieldparam [OpenStruct] an object on which to set configuration
      #   options
      #
      # @return [#lookup] the lookup object
      def config(lookup)
        options = OptionSetter.new(lookup)
        yield(options.config) if block_given?
        @lookup = options.make_lookup
      end
    end

    # Responsible for collecting options in the DSL and creating
    # lookup objects using those options.
    class OptionSetter
      LOOKUPS = {
        geonames: ::Timezone::Lookup::Geonames,
        google: ::Timezone::Lookup::Google,
        test: ::Timezone::Lookup::Test
      }.freeze

      INVALID_LOOKUP = 'Invalid lookup specified'.freeze

      attr_reader :config

      def initialize(lookup)
        if lookup.is_a?(Symbol)
          lookup = LOOKUPS.fetch(lookup) do
            raise ::Timezone::Error::InvalidConfig, INVALID_LOOKUP
          end
        end

        @lookup = lookup

        @config = OpenStruct.new
      end

      def make_lookup
        config.request_handler ||= ::Timezone::NetHTTPClient
        @lookup.new(config)
      end
    end

    private_constant :OptionSetter
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
timezone-1.3.25 lib/timezone/lookup.rb
timezone-1.3.24 lib/timezone/lookup.rb
timezone-1.3.23 lib/timezone/lookup.rb
timezone-1.3.22 lib/timezone/lookup.rb
timezone-1.3.21 lib/timezone/lookup.rb
timezone-1.3.20 lib/timezone/lookup.rb
timezone-1.3.19 lib/timezone/lookup.rb
timezone-1.3.18 lib/timezone/lookup.rb
timezone-1.3.17 lib/timezone/lookup.rb
timezone-1.3.16 lib/timezone/lookup.rb
timezone-1.3.15 lib/timezone/lookup.rb
timezone-1.3.14 lib/timezone/lookup.rb
timezone-1.3.13 lib/timezone/lookup.rb
timezone-1.3.12 lib/timezone/lookup.rb
timezone-1.3.11 lib/timezone/lookup.rb
timezone-1.3.10 lib/timezone/lookup.rb
timezone-1.3.9 lib/timezone/lookup.rb
timezone-1.3.8 lib/timezone/lookup.rb
timezone-1.3.7 lib/timezone/lookup.rb
timezone-1.3.6 lib/timezone/lookup.rb