-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
8 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
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
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
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module Crowdin | ||
module ApiResources | ||
module Notifications | ||
def send_notification_to_authenticated_user(query = {}) | ||
%i[message].each do |param| | ||
query[param] || raise_parameter_is_required_error(param) | ||
end | ||
|
||
request = Web::Request.new( | ||
connection, | ||
:post, | ||
"#{config.target_api_url}/notify", | ||
{ params: query } | ||
) | ||
Web::SendRequest.new(request).perform | ||
end | ||
|
||
def send_notification_to_organization_members(query = {}) | ||
enterprise_mode? || raise_only_for_enterprise_mode_error | ||
|
||
%i[message].each do |param| | ||
query[param] || raise_parameter_is_required_error(param) | ||
end | ||
|
||
request = Web::Request.new( | ||
connection, | ||
:post, | ||
"#{config.target_api_url}/notify", | ||
{ params: query } | ||
) | ||
Web::SendRequest.new(request).perform | ||
end | ||
|
||
def send_notifications_to_project_members(query = {}, project_id = config.project_id) | ||
project_id || raise_project_id_is_required_error | ||
|
||
%i[message].each do |param| | ||
query[param] || raise_parameter_is_required_error(param) | ||
end | ||
|
||
request = Web::Request.new( | ||
connection, | ||
:post, | ||
"#{config.target_api_url}/projects/#{project_id}/notify", | ||
{ params: query } | ||
) | ||
Web::SendRequest.new(request).perform | ||
end | ||
end | ||
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
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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Crowdin::ApiResources::Notifications do | ||
describe 'Default endpoints' do | ||
describe '#send_notification_to_authenticated_user' do | ||
it 'when request are valid', :default do | ||
stub_request(:post, "https://api.crowdin.com/#{target_api_url}/notify") | ||
.with(body: { 'message' => 'New notification message' }) | ||
query = { message: 'New notification message' } | ||
response = @crowdin.send_notification_to_authenticated_user(query) | ||
expect(response).to eq(200) | ||
end | ||
|
||
it 'raises ArgumentError when request is missing required query parameter', :default do | ||
expect do | ||
@crowdin.send_notification_to_authenticated_user({}) | ||
end.to raise_error(ArgumentError, ':message is required') | ||
end | ||
end | ||
|
||
describe '#send_notifications_to_project_members' do | ||
let(:project_id) { 1 } | ||
|
||
it 'when request are valid', :default do | ||
stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/notify") | ||
.with(body: { 'message' => 'New notification message' }) | ||
query = { message: 'New notification message' } | ||
response = @crowdin.send_notifications_to_project_members(query, project_id) | ||
expect(response).to eq(200) | ||
end | ||
|
||
it 'raises ArgumentError when request is missing required query parameter', :default do | ||
expect do | ||
@crowdin.send_notifications_to_project_members({}, project_id) | ||
end.to raise_error(ArgumentError, ':message is required') | ||
end | ||
|
||
it 'raises ArgumentError when request is missing project_id parameter', :default do | ||
query = { message: 'New notification message' } | ||
expect do | ||
@crowdin.send_notifications_to_project_members(query, nil) | ||
end.to raise_error(ArgumentError, ':project_id is required in parameters or while Client initialization') | ||
end | ||
end | ||
end | ||
|
||
describe 'Enterprise endpoints' do | ||
describe '#send_notification_to_organization_members' do | ||
it 'when request are valid', :enterprise do | ||
stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/notify") | ||
.with(body: { 'message' => 'New notification message' }) | ||
query = { message: 'New notification message' } | ||
response = @crowdin.send_notification_to_organization_members(query) | ||
expect(response).to eq(200) | ||
end | ||
|
||
it 'raises ArgumentError when request is missing required query parameter', :enterprise do | ||
expect do | ||
@crowdin.send_notification_to_organization_members({}) | ||
end.to raise_error(ArgumentError, ':message is required') | ||
end | ||
end | ||
end | ||
end |