Module: Contentful::DatabaseImporter::ResourceCoercions

Included in:
Resource
Defined in:
lib/contentful/database_importer/resource_coercions.rb

Overview

Coercion methods for Resource

Instance Method Summary collapse

Instance Method Details

#asset_id_from_name(name) ⇒ Object



114
115
116
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



29
30
31
32
33
34
35
36
# 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) ⇒ Object



45
46
47
48
49
50
# 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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



38
39
40
41
42
43
# 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) ⇒ Object



25
26
27
# File 'lib/contentful/database_importer/resource_coercions.rb', line 25

def coerce_integer(value)
  value.to_i
end

#coerce_location(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



21
22
23
# File 'lib/contentful/database_importer/resource_coercions.rb', line 21

def coerce_number(value)
  value.to_f
end

#coerce_object(value) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object Also known as: coerce_string, coerce_text



15
16
17
# File 'lib/contentful/database_importer/resource_coercions.rb', line 15

def coerce_symbol(value)
  value.to_s
end

#create_associated_asset(name, value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# 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