Sha256: fc212a1da5d96586334e65a4464ae7f1de5ac1f2f9343dd0a867a591cf11b99e

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 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)
      marshal_load([response, request_params])
    end

    def marshal_load(x)
      @response, @request_params = x
      parse_response
    end

    def marshal_dump
      [@response, @request_params]
    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

2 entries across 2 versions & 1 rubygems

Version Path
geonames_api-0.1.6 lib/geonames_api/entity.rb
geonames_api-0.1.5 lib/geonames_api/entity.rb