Sha256: 56920de04144cbc186dcac39d4225523532a246d7c9bbbe4f5f913019f741ad6

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

require 'forwardable'

module GeoNamesAPI
  class Entity
    include Enumerable
    extend Forwardable
    def_delegator :geonames, :each
    alias_method :size, :count
    attr_reader :request_params

    def initialize(response, request_params = nil)
      @response = response
      @request_params = request_params
      parse_response
    end

    def parse_response
      @response.keys.each { |ea| parse_attr(ea) }
    end

    def parse_attr(key)
      return unless @response.has_key? key

      aliases = []
      value = @response[key]
      parsed_value = case (key)
        when 'geonames', 'streetSegment', 'postalcodes'
          aliases = [:geonames, :results, :postalcodes]
          value.map { |ea| self.class.new(ea) }
        when 'alternateNames'
          AlternateNames.new(value)
        when 'timezone'
          TimeZone.new(value)
        else
          set_default_type(value)
      end

      attr_name = create_attribute(key, *aliases)
      instance_variable_set(attr_name, parsed_value)
    end

    def create_attribute(attribute, *attribute_aliases)
      attr_name = attribute.underscore.to_sym
      self.class.send(:attr_reader, attr_name) unless respond_to?(attr_name)

      attribute_aliases.each do |ea|
        self.class.send(:alias_method, ea, attr_name) unless respond_to?(ea)
      end

      "@#{attr_name}".to_sym
    end

    def set_default_type(value)
      case value
        when /\A-?\d+\Z/
          value.to_i
        when /\A-?\d*\.\d*\Z/
          value.to_f
        else
          value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
geonames_api-0.1.4 lib/geonames_api/entity.rb
geonames_api-0.1.3 lib/geonames_api/entity.rb
geonames_api-0.1.2 lib/geonames_api/entity.rb