Class: WCC::Contentful::Configuration

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

Constant Summary collapse

ATTRIBUTES =
i[
  access_token
  app_url
  management_token
  space
  environment
  default_locale
  content_delivery
  preview_token
  http_adapter
  sync_cache_store
  webhook_username
  webhook_password
  webhook_jobs
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



88
89
90
91
92
93
94
95
96
97
# File 'lib/wcc/contentful/configuration.rb', line 88

def initialize
  @access_token = ''
  @app_url = ENV['APP_URL']
  @management_token = ''
  @preview_token = ''
  @space = ''
  @default_locale = nil
  @content_delivery = :direct
  @webhook_jobs = []
end

Instance Attribute Details

#content_delivery_paramsObject (readonly)

Returns the value of attribute content_delivery_params.



69
70
71
# File 'lib/wcc/contentful/configuration.rb', line 69

def content_delivery_params
  @content_delivery_params
end

#http_adapter=(value) ⇒ Object (writeonly)

Sets the adapter which is used to make HTTP requests. If left unset, the gem attempts to load either ‘http’ or ‘typhoeus’. You can pass your own adapter which responds to ‘call’, or even a lambda that accepts the following parameters:

->(url, query, headers = {}, proxy = {}) { ... }


86
87
88
# File 'lib/wcc/contentful/configuration.rb', line 86

def http_adapter=(value)
  @http_adapter = value
end

#storeObject

Returns the value of attribute store.



79
80
81
# File 'lib/wcc/contentful/configuration.rb', line 79

def store
  @store
end

Instance Method Details

#content_delivery=(params) ⇒ Object

Defines the method by which content is downloaded from the Contentful CDN.

:direct

‘config.content_delivery = :direct` with the `:direct` method, all queries result in web requests to ’cdn.contentful.com’ via the SimpleClient

:eager_sync

‘config.content_delivery = :eager_sync, [sync_store], [options]` with the `:eager_sync` method, the entire content of the Contentful space is downloaded locally and stored in the configured store. The application is responsible to periodically call `WCC::Contentful.sync!` to keep the store updated. Alternatively, the provided Engine can be mounted to receive a webhook from the Contentful space on publish events:

mount WCC::Contentful::Engine, at: '/wcc/contentful'
:lazy_sync

‘config.content_delivery = :lazy_sync, [cache]` The `:lazy_sync` method is a hybrid between the other two methods. Frequently accessed data is stored in an ActiveSupport::Cache implementation and is kept up-to-date via the Sync API. Any data that is not present in the cache is fetched from the CDN like in the `:direct` method. The application is still responsible to periodically call `sync!` or to mount the provided Engine.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wcc/contentful/configuration.rb', line 51

def content_delivery=(params)
  cd, *cd_params = params
  unless cd.is_a? Symbol
    raise ArgumentError, 'content_delivery must be a symbol, use store= to '\
      'directly set contentful CDN access adapter'
  end

  WCC::Contentful::Store::Factory.new(
    self,
    nil,
    cd,
    cd_params
  ).validate!

  @content_delivery = cd
  @content_delivery_params = cd_params
end

#master?Boolean

Returns true if the currently configured environment is pointing at ‘master`.

Returns:

  • (Boolean)


22
23
24
# File 'lib/wcc/contentful/configuration.rb', line 22

def master?
  !environment.present?
end

#validate!Object

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
# File 'lib/wcc/contentful/configuration.rb', line 99

def validate!
  raise ArgumentError, 'Please provide "space"' unless space.present?
  raise ArgumentError, 'Please provide "access_token"' unless access_token.present?

  webhook_jobs&.each do |job|
    next if job.respond_to?(:call) || job.respond_to?(:perform_later)

    raise ArgumentError, "The job '#{job}' must be an instance of ActiveJob::Base or respond to :call"
  end
end