about summary refs log tree commit diff
path: root/app/services/pubsubhubbub
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-05-09 14:48:30 -0400
committerEugen Rochko <eugen@zeonfederated.com>2017-05-09 20:48:30 +0200
commit682507bc3c954239c11934faddcd3ba2934a4d4d (patch)
treec550803b7443c740a8b6f22d6ab2c9f9d19c5786 /app/services/pubsubhubbub
parent441d6dc734d2590b14ca010076496e652d6ef676 (diff)
Specs for pubsub subscribe service (#2951)
* Add spec for pubsubhubbub/subscribe

* Refactor pubsubhubbub/subscribe service
Diffstat (limited to 'app/services/pubsubhubbub')
-rw-r--r--app/services/pubsubhubbub/subscribe_service.rb57
1 files changed, 52 insertions, 5 deletions
diff --git a/app/services/pubsubhubbub/subscribe_service.rb b/app/services/pubsubhubbub/subscribe_service.rb
index 3642b4eca..67d7f6598 100644
--- a/app/services/pubsubhubbub/subscribe_service.rb
+++ b/app/services/pubsubhubbub/subscribe_service.rb
@@ -1,14 +1,61 @@
 # frozen_string_literal: true
 
 class Pubsubhubbub::SubscribeService < BaseService
+  URL_PATTERN = /\A#{URI.regexp(%w(http https))}\z/
+
+  attr_reader :account, :callback, :secret, :lease_seconds
+
   def call(account, callback, secret, lease_seconds)
-    return ['Invalid topic URL',        422] if account.nil?
-    return ['Invalid callback URL',     422] unless !callback.blank? && callback =~ /\A#{URI.regexp(%w(http https))}\z/
-    return ['Callback URL not allowed', 403] if DomainBlock.blocked?(Addressable::URI.parse(callback).normalize.host)
+    @account = account
+    @callback = callback
+    @secret = secret
+    @lease_seconds = lease_seconds
+
+    process_subscribe
+  end
+
+  private
+
+  def process_subscribe
+    case subscribe_status
+    when :invalid_topic
+      ['Invalid topic URL', 422]
+    when :invalid_callback
+      ['Invalid callback URL', 422]
+    when :callback_not_allowed
+      ['Callback URL not allowed', 403]
+    when :valid
+      confirm_subscription
+      ['', 202]
+    end
+  end
 
-    subscription = Subscription.where(account: account, callback_url: callback).first_or_create!(account: account, callback_url: callback)
+  def subscribe_status
+    if account.nil?
+      :invalid_topic
+    elsif !valid_callback?
+      :invalid_callback
+    elsif blocked_domain?
+      :callback_not_allowed
+    else
+      :valid
+    end
+  end
+
+  def confirm_subscription
+    subscription = locate_subscription
     Pubsubhubbub::ConfirmationWorker.perform_async(subscription.id, 'subscribe', secret, lease_seconds)
+  end
+
+  def valid_callback?
+    callback.present? && callback =~ URL_PATTERN
+  end
+
+  def blocked_domain?
+    DomainBlock.blocked? Addressable::URI.parse(callback).normalize.host
+  end
 
-    ['', 202]
+  def locate_subscription
+    Subscription.where(account: account, callback_url: callback).first_or_create!(account: account, callback_url: callback)
   end
 end