Module: Contentful::Support

Defined in:
lib/contentful/support.rb

Overview

Utility methods used by the contentful gem

Class Method Summary collapse

Class Method Details

.link?(value) ⇒ true, false

Checks if value is a link

Parameters:

  • value

Returns:

  • (true, false)


24
25
26
27
# File 'lib/contentful/support.rb', line 24

def link?(value)
  value.is_a?(::Hash) &&
    value.fetch('sys', {}).fetch('type', '') == 'Link'
end

Checks if value is an array of links

Parameters:

  • value

Returns:

  • (true, false)


34
35
36
37
38
# File 'lib/contentful/support.rb', line 34

def link_array?(value)
  return link?(value[0]) if value.is_a?(::Array) && !value.empty?

  false
end

Returns the resource that matches the link

Parameters:

  • link (Hash)
  • includes (::Array)

Returns:

  • (Hash)


46
47
48
49
50
51
# File 'lib/contentful/support.rb', line 46

def resource_for_link(link, includes)
  includes.detect do |i|
    i['sys']['id'] == link['sys']['id'] &&
      i['sys']['type'] == link['sys']['linkType']
  end
end

.snakify(object) ⇒ String

Transforms CamelCase into snake_case (taken from zucker)

Parameters:

  • object (String)

    camelCaseName

Returns:

  • (String)

    snake_case_name



10
11
12
13
14
15
16
17
# File 'lib/contentful/support.rb', line 10

def snakify(object)
  String(object)
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end