Class: SimpleGeo::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_geo/record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Record

Returns a new instance of Record.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/simple_geo/record.rb', line 5

def initialize(options={})
  options = {
    :created => Time.now,
    :type => 'object',
    :properties => {}
  }.merge(options)

  @id = options[:id]
  @layer = options[:layer]
  @type = options[:type]
  @lat = options[:lat]
  @lon = options[:lon]
  @created = options[:created]
  @properties = options[:properties]
end

Instance Attribute Details

#createdObject

Returns the value of attribute created.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def created
  @created
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def id
  @id
end

#latObject

Returns the value of attribute lat.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def lat
  @lat
end

#layerObject

Returns the value of attribute layer.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def layer
  @layer
end

#lonObject

Returns the value of attribute lon.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def lon
  @lon
end

#propertiesObject

Returns the value of attribute properties.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def properties
  @properties
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/simple_geo/record.rb', line 3

def type
  @type
end

Class Method Details

.parse_geojson_hash(json_hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/simple_geo/record.rb', line 45

def self.parse_geojson_hash(json_hash)
  Record.new(
    :id => json_hash['id'],
    :type => json_hash['properties'].delete('type'),
    :lat => json_hash['geometry']['coordinates'][1],
    :lon => json_hash['geometry']['coordinates'][0],
    :created => Time.at(json_hash['created']),
    :properties => HashUtils.recursively_symbolize_keys(json_hash['properties'])
  )
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/simple_geo/record.rb', line 41

def ==(other)
  other.class == self.class && self.to_hash == other.to_hash
end

#to_hashObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_geo/record.rb', line 21

def to_hash
  {
    :type => 'Feature',
    :id => id,
    :created => created.to_i,
    :geometry => {
      :type => 'Point',
      :coordinates => [ lon, lat ]
    },
    :properties => properties.merge({
      :type => type,
      :layer => layer
    })
  }
end

#to_jsonObject



37
38
39
# File 'lib/simple_geo/record.rb', line 37

def to_json
  self.to_hash.to_json
end