class TableSchema::Types::GeoPoint
Public Class Methods
supported_constraints()
click to toggle source
# File lib/tableschema/types/geopoint.rb, line 9 def self.supported_constraints [ 'required', 'geopoint', 'pattern', 'enum' ] end
Public Instance Methods
cast_array(value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
name()
click to toggle source
# File lib/tableschema/types/geopoint.rb, line 5 def name 'geopoint' end
types()
click to toggle source
# File lib/tableschema/types/geopoint.rb, line 18 def types [::String, ::Array, ::Hash] end
Private Instance Methods
check_latlng_range(geopoint)
click to toggle source
# File lib/tableschema/types/geopoint.rb, line 45 def check_latlng_range(geopoint) longitude = geopoint[0] latitude = geopoint[1] if longitude >= 180 or longitude <= -180 raise TableSchema::InvalidGeoPointType.new("longtitude should be between -180 and 180, found `#{longitude}`") elsif latitude >= 90 or latitude <= -90 raise TableSchema::InvalidGeoPointType.new("longtitude should be between -90 and 90, found `#{latitude}`") end end