Skip to content

Commit 6d30376

Browse files
committed
KRED-2534 improve
1 parent 3d99351 commit 6d30376

3 files changed

Lines changed: 108 additions & 22 deletions

File tree

lib/midtrans_api/model/merchant/get.rb

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative 'item'
4+
35
module MidtransApi
46
module Model
57
module Merchant
@@ -14,28 +16,7 @@ def merchant_list
1416
return [] unless merchants
1517

1618
merchants.map do |merchant_data|
17-
MerchantItem.new(merchant_data)
18-
end
19-
end
20-
21-
# Inner class to represent individual merchant items
22-
class MerchantItem
23-
attr_reader :merchant_id, :merchant_name, :merchant_phone_number, :email
24-
25-
def initialize(merchant_data)
26-
@merchant_id = merchant_data['merchant_id']
27-
@merchant_name = merchant_data['merchant_name']
28-
@merchant_phone_number = merchant_data['merchant_phone_number']
29-
@email = merchant_data['email']
30-
end
31-
32-
def to_h
33-
{
34-
merchant_id: merchant_id,
35-
merchant_name: merchant_name,
36-
merchant_phone_number: merchant_phone_number,
37-
email: email
38-
}
19+
Item.new(merchant_data)
3920
end
4021
end
4122
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module MidtransApi
4+
module Model
5+
module Merchant
6+
class Item < MidtransApi::Model::Base
7+
attr_reader :merchant_id, :merchant_name, :merchant_phone_number, :email
8+
9+
def initialize(merchant_data)
10+
@merchant_id = merchant_data['merchant_id']
11+
@merchant_name = merchant_data['merchant_name']
12+
@merchant_phone_number = merchant_data['merchant_phone_number']
13+
@email = merchant_data['email']
14+
end
15+
16+
def to_h
17+
{
18+
merchant_id: merchant_id,
19+
merchant_name: merchant_name,
20+
merchant_phone_number: merchant_phone_number,
21+
email: email
22+
}
23+
end
24+
end
25+
end
26+
end
27+
end

spec/lib/midtrans_api/api/merchant/get_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,34 @@
8686
}
8787
end
8888

89+
let(:merchants_with_unexpected_fields_response) do
90+
{
91+
"status_code": "200",
92+
"status_message": "Merchants have been successfully retrieved.",
93+
"unexpected_root_field": "some_value",
94+
"merchants": [
95+
{
96+
"merchant_id": "G221991147",
97+
"merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01",
98+
"merchant_phone_number": "+621000000000",
99+
"email": "dhany+39457_79@mekari.com",
100+
"unexpected_field": "unexpected_value",
101+
"another_unexpected_field": {
102+
"nested": "data"
103+
}
104+
},
105+
{
106+
"merchant_id": "G539217527",
107+
"merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01",
108+
"merchant_phone_number": "+621000000000",
109+
"email": "dhany+39457_80@mekari.com",
110+
"extra_field": 12345,
111+
"boolean_field": true
112+
}
113+
]
114+
}
115+
end
116+
89117
describe '#get' do
90118
context 'with keyword parameter' do
91119
it 'returns expected response' do
@@ -232,5 +260,55 @@
232260
expect(second_merchant.email).to be_nil
233261
end
234262
end
263+
264+
context 'when response contains unexpected fields' do
265+
it 'ignores unexpected fields and processes only expected ones' do
266+
partner_id = '739'
267+
params = {}
268+
269+
stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
270+
.to_return(status: 200, body: merchants_with_unexpected_fields_response.to_json)
271+
272+
merchant_api = described_class.new(client)
273+
response = merchant_api.get(params, partner_id)
274+
275+
expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
276+
expect(response.merchants).to be_an(Array)
277+
expect(response.merchants.length).to eq(2)
278+
279+
# Verify that unexpected root-level fields are ignored
280+
expect(response).not_to respond_to(:unexpected_root_field)
281+
282+
merchant_list = response.merchant_list
283+
expect(merchant_list.length).to eq(2)
284+
285+
# Test first merchant - should only have expected fields
286+
first_merchant = merchant_list.first
287+
expect(first_merchant.merchant_id).to eq('G221991147')
288+
expect(first_merchant.merchant_name).to eq('MSN KAP Agus Ubaidillah dan Rekan01')
289+
expect(first_merchant.merchant_phone_number).to eq('+621000000000')
290+
expect(first_merchant.email).to eq('dhany+39457_79@mekari.com')
291+
292+
# Verify that unexpected merchant fields are ignored
293+
expect(first_merchant).not_to respond_to(:unexpected_field)
294+
expect(first_merchant).not_to respond_to(:another_unexpected_field)
295+
296+
# Test second merchant - should only have expected fields
297+
second_merchant = merchant_list.last
298+
expect(second_merchant.merchant_id).to eq('G539217527')
299+
expect(second_merchant.merchant_name).to eq('MSN KAP Agus Ubaidillah dan Rekan01')
300+
expect(second_merchant.merchant_phone_number).to eq('+621000000000')
301+
expect(second_merchant.email).to eq('dhany+39457_80@mekari.com')
302+
303+
# Verify that unexpected merchant fields are ignored
304+
expect(second_merchant).not_to respond_to(:extra_field)
305+
expect(second_merchant).not_to respond_to(:boolean_field)
306+
307+
# Verify to_h method only returns expected fields
308+
first_merchant_hash = first_merchant.to_h
309+
expect(first_merchant_hash.keys).to contain_exactly(:merchant_id, :merchant_name, :merchant_phone_number, :email)
310+
expect(first_merchant_hash[:merchant_id]).to eq('G221991147')
311+
end
312+
end
235313
end
236314
end

0 commit comments

Comments
 (0)