module Contentful::DatabaseImporter::ResourceCoercions
Coercion methods for Resource
Public Instance Methods
asset_id_from_name(name)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 114 def asset_id_from_name(name) Support.snake_case(name.gsub(/[^\w ]/i, '_')) end
coerce(field_definition, value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 5 def coerce(field_definition, value) return if value.nil? type = field_definition[:type] return coerce_array(field_definition, value) if type == :array send("coerce_#{type}".to_sym, value) end
coerce_array(field_definition, value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 29 def coerce_array(field_definition, value) item_type = field_definition[:item_type] raise "Can't coerce nested arrays" if item_type == :array value = value.split(',').map(&:strip) if value.is_a?(::String) value.map { |v| coerce({ type: item_type }, v) } end
coerce_array_location(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 45 def coerce_array_location(value) { lat: value[0], lon: value[1] } end
coerce_asset(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 101 def coerce_asset(value) raise 'Only URL Strings supported for Assets' unless value.is_a?(String) name = value.split('/').last.split('.').first create_associated_asset(name, value) { linkType: 'Asset', id: asset_id_from_name(name) } end
coerce_boolean(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 65 def coerce_boolean(value) # rubocop:disable Style/DoubleNegation !!value # rubocop:enable Style/DoubleNegation end
coerce_date(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 71 def coerce_date(value) case value when Time, Date, DateTime value.iso8601 when String value else raise "Can't coerce #{value} to ISO8601 Date" end end
coerce_hash_location(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 38 def coerce_hash_location(value) { lat: value.fetch(:lat, nil) || value.fetch(:latitude, nil), lon: value.fetch(:lon, nil) || value.fetch(:longitude, nil) } end
coerce_integer(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 25 def coerce_integer(value) value.to_i end
coerce_location(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 52 def coerce_location(value) return coerce_hash_location(value) if value.is_a?(::Hash) return coerce_array_location(value) if value.is_a?(::Array) if value.is_a?(::String) && value.include?(',') parts = value.split(',').map(&:strip).map(&:to_f) return coerce_array_location(parts) end raise "Can't coerce #{value} to Location" end
coerce_number(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 21 def coerce_number(value) value.to_f end
coerce_object(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 82 def coerce_object(value) return value if value.is_a?(::Hash) raise "Can't coerce #{value} to JSON Object" end
coerce_symbol(value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 15 def coerce_symbol(value) value.to_s end
Also aliased as: coerce_string, coerce_text
create_associated_asset(name, value)
click to toggle source
# File lib/contentful/database_importer/resource_coercions.rb, line 88 def create_associated_asset(name, value) extension = value.split('.').last associated_assets << { id: asset_id_from_name(name), title: name, file: { filename: name, url: value, contentType: MimeMagic.by_extension(extension).type } } end