This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clilent to handle subscriber comunication
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class SubscriberClient | ||
|
||
def self.notify(subscriber_id, params) | ||
subscriber = Subscriber.find(subscriber_id) | ||
new(subscriber).notify(params) | ||
end | ||
|
||
def initialize(subscriber) | ||
@subscriber = subscriber | ||
end | ||
|
||
def notify(body) | ||
json = JSON.parse(post(body)) | ||
|
||
if json['acknowledged_at'].blank? | ||
raise "Subscriber did not acknowledge: #{json['errors']}" | ||
end | ||
end | ||
|
||
|
||
private | ||
|
||
attr_reader :subscriber | ||
|
||
def post(body) | ||
HTTParty.post(subscriber.notification_url, { | ||
body: body | ||
}).body | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
describe SubscriberClient, type: :model do | ||
let(:subscriber) { factory_create :subscriber } | ||
let(:params) { {baz: SecureRandom.hex} } | ||
|
||
describe ".notify" do | ||
context "when the response includes an acknowledged at time" do | ||
let(:response) { double body: {acknowledged_at: Time.now.to_i}.to_json } | ||
|
||
it "posts to the subscriber's notification URL" do | ||
expect(HTTParty).to receive(:post) | ||
.with(subscriber.notification_url, { | ||
body: params | ||
}) | ||
.and_return(response) | ||
|
||
expect { | ||
SubscriberClient.notify(subscriber.id, params) | ||
}.not_to raise_error | ||
end | ||
end | ||
|
||
context "when the response does NOT include an acknowledged at time" do | ||
let(:response) { double body: {errors: '["all of the errors"]'}.to_json } | ||
|
||
it "posts to the subscriber's notification URL" do | ||
expect(HTTParty).to receive(:post) | ||
.with(subscriber.notification_url, { | ||
body: params | ||
}) | ||
.and_return(response) | ||
|
||
expect { | ||
SubscriberClient.notify(subscriber.id, params) | ||
}.to raise_error 'Subscriber did not acknowledge: ["all of the errors"]' | ||
end | ||
end | ||
end | ||
|
||
end |