Module: Contentful::Management::Support

Defined in:
lib/contentful/management/support.rb

Overview

Utility methods used by the contentful management gem

Class Method Summary collapse

Class Method Details

.base_path_for(resource_name) ⇒ Object

Returns the path for a specified resource name.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/contentful/management/support.rb', line 39

def base_path_for(resource_name)
  {
    'Role' => 'roles',
    'Space' => 'spaces',
    'Asset' => 'assets',
    'Entry' => 'entries',
    'Locale' => 'locales',
    'Upload' => 'uploads',
    'ApiKey' => 'api_keys',
    'UIExtension' => 'extensions',
    'Environment' => 'environments',
    'ContentType' => 'content_types',
    'PreviewApiKey' => 'preview_api_keys',
    'SpaceMembership' => 'space_memberships'
  }[resource_name]
end

.deep_hash_merge(query_hash, attribute_hash) ⇒ Object

Merges two hashes with recursion



15
16
17
18
19
20
21
# File 'lib/contentful/management/support.rb', line 15

def deep_hash_merge(query_hash, attribute_hash)
  query_hash.merge(attribute_hash) do |_key, oldval, newval|
    oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
    newval = newval.to_hash if newval.respond_to?(:to_hash)
    oldval.instance_of?(::Hash) && newval.instance_of?(::Hash) ? deep_hash_merge(oldval, newval) : newval
  end
end

.normalize_select!(parameters) ⇒ Object

If the query contains the :select operator, we enforce :sys properties. The SDK requires sys.type to function properly, but as other of our SDKs require more parts of the :sys properties, we decided that every SDK should include the complete :sys block to provide consistency accross our SDKs.



27
28
29
30
31
32
33
34
35
36
# File 'lib/contentful/management/support.rb', line 27

def normalize_select!(parameters)
  return parameters unless parameters.key?(:select)

  parameters[:select] = parameters[:select].split(',').map(&:strip) if parameters[:select].is_a? String
  parameters[:select] = parameters[:select].reject { |p| p.start_with?('sys.') }
  parameters[:select] << 'sys' unless parameters[:select].include?('sys')
  parameters[:select] = parameters[:select].join(',')

  parameters
end

.snakify(object) ⇒ Object

Transforms CamelCase into snake_case (taken from zucker)



9
10
11
12
# File 'lib/contentful/management/support.rb', line 9

def snakify(object)
  snake = String(object).gsub(/(?<!^)[A-Z]/) { "_#{::Regexp.last_match(0)}" }
  snake.downcase
end