Sha256: 8e8d9b779a9c3657db3e42a96adb759ed7bb3515eef1273b4d1099323ee6f411

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

module GeoNamesAPI
  class Object
        
    def self.find(*names)
      params, n = {}, 0
      self::ID.each { |i| params[i] = names[n]; n+= 1 }
      new(where(params).first)
    end
    
    def self.url(params={})
      GeoNamesAPI.url + self::METHOD + GeoNamesAPI.params.merge(params).to_url
    end

    def initialize(response)
      parse(response)
    end 

    def create_attribute(attribute)
      attr_name = attribute.underscore.to_sym
      self.class.send(:attr_accessor, attr_name) unless respond_to?(attr_name)
      attr_name
    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
    
    def parse(response)
      response.each do |key, value|
        attr_name = create_attribute(key)
        value = set_default_type(value)
        send("#{attr_name}=", value)
      end 
    end
     
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geonames_api-0.0.2 lib/geonames_api/object.rb