-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from ioki-mobility/named-user-listing
Introduce the named_users listing endpoint
- Loading branch information
Showing
5 changed files
with
214 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Airship | ||
module Api | ||
# see also documentation: | ||
# - Add Custom Events => https://docs.airship.com/api/ua/#operation/api/named_users/get | ||
class NamedUsers < Base | ||
receives :app_key | ||
receives :token | ||
|
||
receives :page | ||
receives :page_size | ||
|
||
class << self | ||
def each(options = {}) | ||
raise ArgumentError, 'argument must be a Hash' unless options.is_a?(Hash) | ||
|
||
page = 0 | ||
page_size = options[:page_size] || 1000 | ||
|
||
loop do | ||
page += 1 | ||
|
||
operation_instance = new(options.merge(page: page, page_size: page_size)) | ||
result = operation_instance.call | ||
named_users = Array(result['named_users']) | ||
|
||
named_users.each do |named_user| | ||
yield named_user | ||
end | ||
|
||
return if named_users.size < page_size | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def api_endpoint | ||
'named_users' | ||
end | ||
|
||
def request_parameters | ||
{ | ||
page: page || 1, | ||
page_size: page_size || 1000 | ||
} | ||
end | ||
|
||
def process_request | ||
process_get_request | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Airship | ||
VERSION = '0.1.5' | ||
VERSION = '0.2.0' | ||
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,154 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Airship::Api::NamedUsers do | ||
subject { described_class.call(operation_params) } | ||
|
||
let(:operation_params) do | ||
{ | ||
app_key: app_key, | ||
token: token, | ||
page: page, | ||
page_size: page_size | ||
} | ||
end | ||
|
||
let(:app_key) { 'airship_app_andromeda' } | ||
let(:token) { '***TOKEN***' } | ||
let(:page) { 3 } | ||
let(:page_size) { 2 } | ||
|
||
let(:expected_endpoint) { 'named_users' } | ||
let(:expected_full_path) do | ||
described_class::AIRSHIP_API_BASE_URL + expected_endpoint + "?page=#{page}&page_size=#{page_size}" | ||
end | ||
|
||
let(:response_status) { 200 } | ||
let(:response_body) do | ||
<<-JSON | ||
{ | ||
"named_users": [ | ||
{ | ||
"named_user_id": "harry.potter", | ||
"channels": [{ | ||
"channel_id": "70c0b58f-942f-4b27-b4e3-13f47a80ab28", | ||
"device_type": "email" | ||
}] | ||
}, | ||
{ | ||
"named_user_id": "ron.weasley", | ||
"channels": [{ | ||
"channel_id": "9000b48c-245c-cac7-b2e1-12f47a9abab1f", | ||
"device_type": "email" | ||
}] | ||
} | ||
] | ||
} | ||
JSON | ||
end | ||
|
||
let(:request_tracker) do | ||
proc do |api_endpoint| | ||
api_endpoint | ||
end | ||
end | ||
|
||
let(:error_tracker) do | ||
proc do |api_endpoint, response_code| | ||
[api_endpoint, response_code] | ||
end | ||
end | ||
|
||
before do | ||
allow(Airship.config).to receive(:request_tracker).and_return(request_tracker) | ||
allow(Airship.config).to receive(:error_tracker).and_return(error_tracker) | ||
|
||
stub_request(:get, expected_full_path) | ||
.with( | ||
headers: { | ||
'Accept' => 'application/vnd.urbanairship+json; version=3', | ||
'Authorization' => "Bearer #{token}", | ||
'Content-Type' => 'application/json', | ||
'X-Ua-Appkey' => app_key | ||
} | ||
) | ||
.to_return(status: response_status, body: response_body) | ||
end | ||
|
||
it 'is expected to succeed' do | ||
expect { subject }.not_to raise_error | ||
end | ||
|
||
it 'returns the json response-body' do | ||
expect(subject).to eq JSON.parse(response_body) | ||
end | ||
|
||
it 'tracks the request with configured tracker' do | ||
expect(request_tracker).to receive(:call).with(expected_endpoint) | ||
subject | ||
end | ||
|
||
it 'doesn\'t track an error with configured tracker' do | ||
expect(error_tracker).not_to receive(:call) | ||
subject | ||
end | ||
|
||
context 'with a failing HTTP response' do | ||
let(:response_status) { 401 } | ||
let(:response_body) do | ||
'{"ok":false,"error":"Unauthorized","error_code":40101}' | ||
end | ||
|
||
it 'is expected not to succeed' do | ||
expect { subject }.to raise_error Airship::Api::Unauthorized | ||
end | ||
|
||
it 'tracks the request and the according error with configured trackers' do | ||
expect(request_tracker).to receive(:call).with(expected_endpoint) | ||
expect(error_tracker).to receive(:call).with(expected_endpoint, response_status) | ||
expect { subject }.to raise_error Airship::Api::Unauthorized | ||
end | ||
end | ||
|
||
describe '#each_batch' do | ||
let(:page_size) { 2 } | ||
|
||
let(:expected_full_path_1) { described_class::AIRSHIP_API_BASE_URL + expected_endpoint + '?page=1&page_size=2' } | ||
let(:expected_full_path_2) { described_class::AIRSHIP_API_BASE_URL + expected_endpoint + '?page=2&page_size=2' } | ||
|
||
let(:response_body_2) do | ||
<<-JSON | ||
{ | ||
"named_users": [ | ||
{ | ||
"named_user_id": "wonder.woman", | ||
"channels": [{ | ||
"channel_id": "0b5870cf-9f42-24b7-b34e-817a3f48b20a", | ||
"device_type": "email" | ||
}] | ||
} | ||
] | ||
} | ||
JSON | ||
end | ||
|
||
let(:response_body_3) { '{"named_users": []}' } | ||
|
||
before do | ||
stub_request(:get, expected_full_path_1).to_return(status: response_status, body: response_body) | ||
stub_request(:get, expected_full_path_2).to_return(status: response_status, body: response_body_2) | ||
end | ||
|
||
it 'makes multiple requests and yields the single named user records' do | ||
all_data = [] | ||
|
||
described_class.each(page_size: page_size) do |data| | ||
all_data << data | ||
end | ||
|
||
expect(all_data.size).to eq(3) | ||
expect(all_data[0]['named_user_id']).to eq('harry.potter') | ||
expect(all_data[1]['named_user_id']).to eq('ron.weasley') | ||
expect(all_data[2]['named_user_id']).to eq('wonder.woman') | ||
end | ||
end | ||
end |