Class: JsonTableSchema::Types::GeoPoint

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cast, #initialize, #set_format, #test

Methods included from Helpers

#convert_to_boolean, #false_values, #get_class_for_type, #true_values, #type_class_lookup

Constructor Details

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

Class Method Details

.supported_constraintsObject



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

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

Instance Method Details

#cast_array(value) ⇒ Object



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

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

#cast_default(value) ⇒ Object



21
22
23
24
# File 'lib/jsontableschema/types/geopoint.rb', line 21

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

#cast_object(value) ⇒ Object



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

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

#nameObject



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

def name
  'geopoint'
end

#typesObject



17
18
19
# File 'lib/jsontableschema/types/geopoint.rb', line 17

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