Class: ContentfulRails::WebhooksController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/contentful_rails/webhooks_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

this is where we receive a webhook, via a POST



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/contentful_rails/webhooks_controller.rb', line 10

def create
  # The only things we need to handle in here (for now at least) are entries.
  # If there's been an update or a deletion, we just remove the cached timestamp.
  # The updated_at method which is included in ContentfulModel::Base in this gem
  # will check the cache first before making the call to the API.

  # We can then just use normal Rails russian doll caching without expensive API calls.
  request.format = :json
  update_type = request.headers['HTTP_X_CONTENTFUL_TOPIC']
  content_type_id = params[:sys][:contentType][:sys][:id]
  item_id = params[:sys][:id]
  cache_key = "contentful_timestamp/#{content_type_id}/#{item_id}"

  #delete the cache entry
  if update_type =~ %r(Entry)
    Rails.cache.delete(cache_key)
  end

  #must return an ok
  render nothing: true
end

#debugObject



32
33
34
# File 'app/controllers/contentful_rails/webhooks_controller.rb', line 32

def debug
  render text: "Debug method works ok"
end