Skip to content

Commit

Permalink
Merge 345e957 into fd71ece
Browse files Browse the repository at this point in the history
  • Loading branch information
minaorangina committed Apr 10, 2018
2 parents fd71ece + 345e957 commit b2f6506
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
PUSHER_INSTANCE_ID=your-pusher-instance-id
PUSHER_SECRET_KEY=your-pusher-secret-key
# Replace with your own Pusher Instance ID and Secret Key

export PUSHER_INSTANCE_ID=97c56dfe-58f5-408b-ab3a-158e51a860f2
export PUSHER_SECRET_KEY=3B397552E080252048FE03009C1253A
1 change: 1 addition & 0 deletions lib/pusher/push_notifications/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(config: PushNotifications)
def post(resource, payload = {})
url = build_url(resource)
body = payload.to_json

RestClient::Request.execute(
method: :post, url: url,
payload: body, headers: headers
Expand Down
13 changes: 13 additions & 0 deletions lib/pusher/push_notifications/use_cases/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ module UseCases
class Publish
include Caze

class PublishError < RuntimeError; end

export :call, as: :publish

def initialize(interests:, payload: {})
valid_interest_pattern = /^(_|\-|=|@|,|\.|:|[A-Z]|[a-z]|[0-9])*$/

interests.each do |interest|
if (interest_valid = !valid_interest_pattern.match(interest))
raise PublishError, "Invalid interest name \nMax 164 characters and can only contain ASCII upper/lower-case letters, numbers or one of _-=@,.:"
end
end

raise PublishError, 'Must provide at least one interest' if interests.length == 0
raise PublishError, "Number of interests #{interests.length} exceeds maximum of 100" if interests.length > 100

@interests = interests
@payload = payload
end
Expand Down
48 changes: 48 additions & 0 deletions spec/cassettes/publishes/valid_interests.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions spec/pusher/push_notifications/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,61 @@
end
end
end

context 'when interest name is invalid' do
let(:interests) {['lovely-valid-interest', 'hey €€ ***']}

it 'warns an interest name is invalid' do
expect { send_notification }.to raise_error(
Pusher::PushNotifications::UseCases::Publish::PublishError
).with_message(
"Invalid interest name \nMax 164 characters and can only contain ASCII upper/lower-case letters, numbers or one of _-=@,.:"
)
end
end

context 'when no interests provided' do
let(:interests) {[]}

it 'warns to provide at least one interest' do
expect { send_notification }.to raise_error(
Pusher::PushNotifications::UseCases::Publish::PublishError
).with_message('Must provide at least one interest')
end
end

context 'when 100 interests provided' do
int_array = (1..100).to_a.shuffle
test_interests = int_array.map do |num|
"interest-#{num.to_s}"
end

let(:interests) { test_interests }

it 'sends the notification' do
VCR.use_cassette('publishes/valid_interests') do
response = send_notification

expect(response).to be_ok
end
end
end

context 'when too many interests provided' do
int_array = (1..101).to_a.shuffle
test_interests = int_array.map do |num|
"interest-#{num.to_s}"
end

let(:interests) { test_interests }

it 'raises an error' do
VCR.use_cassette('publishes/valid_interests') do
expect { send_notification }.to raise_error(
Pusher::PushNotifications::UseCases::Publish::PublishError
).with_message("Number of interests #{interests.length} exceeds maximum of 100")
end
end
end
end
end

0 comments on commit b2f6506

Please sign in to comment.