Class: TableSchema::Types::GeoPoint

Inherits:
Base
  • Object
show all
Defined in:
lib/tableschema/types/geopoint.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cast, #initialize, #test

Methods included from Helpers

#deep_symbolize_keys, #get_class_for_type, #read_file, #type_class_lookup

Constructor Details

This class inherits a constructor from TableSchema::Types::Base

Class Method Details

.supported_constraintsObject



9
10
11
12
13
14
15
16
# File 'lib/tableschema/types/geopoint.rb', line 9

def self.supported_constraints
  [
    'required',
    'geopoint',
    'pattern',
    'enum'
  ]
end

Instance Method Details

#cast_array(value) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/tableschema/types/geopoint.rb', line 34

def cast_array(value)
  value = JSON.parse(value, symbolize_names: true) if value.is_a?(::String)
  value = [Float(value[0]), Float(value[1])]
  check_latlng_range(value)
  value
rescue JSON::ParserError, ArgumentError, TypeError
  raise TableSchema::InvalidGeoPointType.new("#{value} is not a valid geopoint")
end

#cast_default(value) ⇒ Object



22
23
24
25
# File 'lib/tableschema/types/geopoint.rb', line 22

def cast_default(value)
  latlng = value.split(',', 2)
  cast_array([latlng[0], latlng[1]])
end

#cast_object(value) ⇒ Object



27
28
29
30
31
32
# File 'lib/tableschema/types/geopoint.rb', line 27

def cast_object(value)
  value = JSON.parse(value, symbolize_names: true) if value.is_a?(::String)
  cast_array([value[:longitude], value[:latitude]])
rescue JSON::ParserError
  raise TableSchema::InvalidGeoPointType.new("#{value} is not a valid geopoint")
end

#nameObject



5
6
7
# File 'lib/tableschema/types/geopoint.rb', line 5

def name
  'geopoint'
end

#typesObject



18
19
20
# File 'lib/tableschema/types/geopoint.rb', line 18

def types
  [::String, ::Array, ::Hash]
end