Skip to content

Commit

Permalink
Merge pull request #74 from ioki-mobility/named-user-listing
Browse files Browse the repository at this point in the history
Introduce the named_users listing endpoint
  • Loading branch information
ans-ioki authored Dec 21, 2023
2 parents 2ba130e + 12289c9 commit e2aab06
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

## [0.2.0] - 2023-12-21
- Support for listing (iterating) named users

## [0.1.4] - 2022-09-22

- Transform all is8601 strings to be in UTC
Expand Down
1 change: 1 addition & 0 deletions lib/airship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Airship
require_relative 'airship/api/email_channel_lookup'
require_relative 'airship/api/email_channel_uninstall'
require_relative 'airship/api/email_channel_update'
require_relative 'airship/api/named_users'
require_relative 'airship/api/named_user_associate_email'
require_relative 'airship/api/named_user_lookup'
require_relative 'airship/api/named_user_tags_update'
Expand Down
55 changes: 55 additions & 0 deletions lib/airship/api/named_users.rb
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
2 changes: 1 addition & 1 deletion lib/airship/version.rb
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
154 changes: 154 additions & 0 deletions spec/lib/airship/api/named_users_spec.rb
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

0 comments on commit e2aab06

Please sign in to comment.