diff options
author | Matt Jankowski <mjankowski@thoughtbot.com> | 2017-05-30 20:15:09 -0400 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2017-05-31 02:15:09 +0200 |
commit | 83435c49ea4f31d80d81658d8faa69ed5350e26f (patch) | |
tree | 8166430a7729713ee1b0bc4a38d83967c06d3ecc /app/controllers | |
parent | 93de41b39b5aa287671cdf80ee26b5b96e0cf612 (diff) |
Clean up api/subscriptions controller (#3448)
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/api/subscriptions_controller.rb | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index 135a5632e..dd2f42aab 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -5,18 +5,15 @@ class Api::SubscriptionsController < ApiController respond_to :txt def show - if @account.subscription(api_subscription_url(@account.id)).valid?(params['hub.topic']) - @account.update(subscription_expires_at: Time.now.utc + (params['hub.lease_seconds'] || 86_400).to_i.seconds) - render plain: HTMLEntities.new.encode(params['hub.challenge']), status: 200 + if subscription.valid?(params['hub.topic']) + @account.update(subscription_expires_at: future_expires) + render plain: encoded_challenge, status: 200 else head 404 end end def update - body = request.body.read - subscription = @account.subscription(api_subscription_url(@account.id)) - if subscription.verify(body, request.headers['HTTP_X_HUB_SIGNATURE']) ProcessingWorker.perform_async(@account.id, body.force_encoding('UTF-8')) end @@ -26,6 +23,28 @@ class Api::SubscriptionsController < ApiController private + def subscription + @_subscription ||= @account.subscription( + api_subscription_url(@account.id) + ) + end + + def body + @_body ||= request.body.read + end + + def encoded_challenge + HTMLEntities.new.encode(params['hub.challenge']) + end + + def future_expires + Time.now.utc + lease_seconds_or_default + end + + def lease_seconds_or_default + (params['hub.lease_seconds'] || 86_400).to_i.seconds + end + def set_account @account = Account.find(params[:id]) end |