Sha256: e716377b1218aa31833eea9283e306cad2a98aa1e0ac3f0c684761b5f221b703

Contents?: true

Size: 877 Bytes

Versions: 18

Compression:

Stored size: 877 Bytes

Contents

# Using the :resource_mapping configuration, you can register your own classes to be used
# for the results from contentful.
#
# The key of the resource_mapping hash defines the resource type (object["sys"]["type"]) and the value is:
# - the Class to use
# - a Proc, that returns the Class to use
# - a Symbol for a method of the ResourceBuilder object

require 'contentful'

class MyBetterArray < Contentful::Array
  # e.g. define more methods that you need
  def last
    items.last
  end
end

client = Contentful::Client.new(
  space: 'cfexampleapi',
  access_token: 'b4c0n73n7fu1',
  resource_mapping: {
    'Array' => MyBetterArray,
    'Asset' => ->(_json_object)do
      # might return different class if some criteria is matched
      Contentful::Asset
    end,
  }
)

assets = client.assets
p assets.class # => MyBetterArray
p assets.last # => #<Contentful::Asset ...

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
contentful-1.2.2 examples/resource_mapping.rb
contentful-1.2.1 examples/resource_mapping.rb
contentful-1.2.0 examples/resource_mapping.rb
contentful-1.1.1 examples/resource_mapping.rb
contentful-1.1.0 examples/resource_mapping.rb
contentful-1.0.2 examples/resource_mapping.rb
contentful-1.0.1 examples/resource_mapping.rb
contentful-1.0.0 examples/resource_mapping.rb
contentful-0.12.0 examples/resource_mapping.rb
contentful-0.11.0 examples/resource_mapping.rb
contentful-0.10.0 examples/resource_mapping.rb
contentful-0.9.0 examples/resource_mapping.rb
contentful-0.8.0 examples/resource_mapping.rb
contentful-0.7.0 examples/resource_mapping.rb
contentful-0.6.0 examples/resource_mapping.rb
contentful-0.5.0 examples/resource_mapping.rb
contentful-0.4.0 examples/resource_mapping.rb
contentful-0.3.5 examples/resource_mapping.rb