Class: Contentful::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/client.rb

Overview

The client object is initialized with a space and a key and then used for querying resources from this space. See README for details

Constant Summary

DEFAULT_CONFIGURATION =

Default configuration for Contentful::Client

{
  secure: true,
  raise_errors: true,
  dynamic_entries: :manual,
  api_url: 'cdn.contentful.com',
  api_version: 1,
  authentication_mechanism: :header,
  resource_builder: ResourceBuilder,
  resource_mapping: {},
  entry_mapping: {},
  default_locale: 'en-US',
  raw_mode: false,
  gzip_encoded: true,
  logger: false,
  log_level: Logger::INFO,
  proxy_host: nil,
  proxy_username: nil,
  proxy_password: nil,
  proxy_port: nil,
  max_rate_limit_retries: 1,
  max_rate_limit_wait: 60
}
RATE_LIMIT_RESET_HEADER_KEY =

Rate Limit Reset Header Key

'x-contentful-ratelimit-reset'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_configuration = {}) ⇒ Client

Returns a new instance of Client

Parameters:

  • given_configuration (Hash) (defaults to: {})

Options Hash (given_configuration):

  • :space (String)

    Required

  • :access_token (String)

    Required

  • :api_url (String)

    Modifying this to 'preview.contentful.com' gives you access to our Preview API

  • :api_version (String)
  • :default_locale (String)
  • :proxy_host (String)
  • :proxy_username (String)
  • :proxy_password (String)
  • :proxy_port (Number)
  • :max_rate_limit_retries (Number)
  • :max_rate_limit_wait (Number)
  • :gzip_encoded (Boolean)
  • :raw_mode (Boolean)
  • :logger (false, ::Logger)
  • :log_level (::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR)
  • :raise_errors (Boolean)
  • :dynamic_entries (::Array<String>)
  • :resource_mapping (::Hash<String, Contentful::Resource>)
  • :entry_mapping (::Hash<String, Contentful::Resource>)

See Also:

  • https://github.com/contentful/contentful.rb#client-configuration-options


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/contentful/client.rb', line 73

def initialize(given_configuration = {})
  @configuration = default_configuration.merge(given_configuration)
  normalize_configuration!
  validate_configuration!
  setup_logger

  if configuration[:dynamic_entries] == :auto
    update_dynamic_entry_cache!
  else
    @dynamic_entry_cache = {}
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration



40
41
42
# File 'lib/contentful/client.rb', line 40

def configuration
  @configuration
end

#dynamic_entry_cacheObject (readonly)

Returns the value of attribute dynamic_entry_cache



40
41
42
# File 'lib/contentful/client.rb', line 40

def dynamic_entry_cache
  @dynamic_entry_cache
end

#loggerObject (readonly)

Returns the value of attribute logger



40
41
42
# File 'lib/contentful/client.rb', line 40

def logger
  @logger
end

#proxyObject (readonly)

Returns the value of attribute proxy



40
41
42
# File 'lib/contentful/client.rb', line 40

def proxy
  @proxy
end

Instance Method Details

#asset(id, query = {}) ⇒ Contentful::Asset

Gets a specific asset

Parameters:

  • id (String)
  • query (Hash) (defaults to: {})

Returns:



162
163
164
# File 'lib/contentful/client.rb', line 162

def asset(id, query = {})
  Request.new(self, '/assets', query, id).get
end

#assets(query = {}) ⇒ Contentful::Array<Contentful::Asset>

Gets a collection of assets

Parameters:

  • query (Hash) (defaults to: {})

Returns:



171
172
173
174
# File 'lib/contentful/client.rb', line 171

def assets(query = {})
  normalize_select!(query)
  Request.new(self, '/assets', query).get
end

#content_type(id, query = {}) ⇒ Contentful::ContentType

Gets a specific content type

Parameters:

  • id (String)
  • query (Hash) (defaults to: {})

Returns:



123
124
125
# File 'lib/contentful/client.rb', line 123

def content_type(id, query = {})
  Request.new(self, '/content_types', query, id).get
end

#content_types(query = {}) ⇒ Contentful::Array<Contentful::ContentType>

Gets a collection of content types

Parameters:

  • query (Hash) (defaults to: {})

Returns:



132
133
134
# File 'lib/contentful/client.rb', line 132

def content_types(query = {})
  Request.new(self, '/content_types', query).get
end

#entries(query = {}) ⇒ Contentful::Array<Contentful::Entry>

Gets a collection of entries

Parameters:

  • query (Hash) (defaults to: {})

Returns:



151
152
153
154
# File 'lib/contentful/client.rb', line 151

def entries(query = {})
  normalize_select!(query)
  Request.new(self, '/entries', query).get
end

#entry(id, query = {}) ⇒ Contentful::Entry

Gets a specific entry

Parameters:

  • id (String)
  • query (Hash) (defaults to: {})

Returns:



142
143
144
# File 'lib/contentful/client.rb', line 142

def entry(id, query = {})
  Request.new(self, '/entries', query, id).get
end

#space(query = {}) ⇒ Contentful::Space

Gets the client's space

Parameters:

  • query (Hash) (defaults to: {})

Returns:



113
114
115
# File 'lib/contentful/client.rb', line 113

def space(query = {})
  Request.new(self, '', query).get
end

#sync(options = { initial: true }) ⇒ Contentful::Sync

Note:

You will need to call #each_page or #first_page on it

Create a new synchronisation object

Parameters:

  • options (Hash, String) (defaults to: { initial: true })

    Options or Sync URL

Returns:



295
296
297
# File 'lib/contentful/client.rb', line 295

def sync(options = { initial: true })
  Sync.new(self, options)
end