Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
clilent to handle subscriber comunication
Browse files Browse the repository at this point in the history
  • Loading branch information
se3000 committed May 17, 2016
1 parent 87860f9 commit 230ebb4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/models/subscriber_client.rb
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
39 changes: 39 additions & 0 deletions spec/models/subscriber_client_spec.rb
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

0 comments on commit 230ebb4

Please sign in to comment.