-
Notifications
You must be signed in to change notification settings - Fork 3
KRED-2534 Add get merchant #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MidtransApi | ||
| module Api | ||
| module Merchant | ||
| class Get < MidtransApi::Api::Base | ||
| PATH = 'merchants' | ||
|
|
||
| def get(params, partner_id) | ||
| response = client.get(PATH, params, { | ||
| 'X-PARTNER-ID': partner_id | ||
| }) | ||
|
|
||
| MidtransApi::Model::Merchant::Get.new(response) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MidtransApi | ||
| module Model | ||
| module Merchant | ||
| class Get < MidtransApi::Model::Base | ||
| resource_attributes :merchants | ||
|
|
||
| def resolve_params_attr(attr) | ||
| attr.to_s | ||
| end | ||
|
|
||
| def merchant_list | ||
| return [] unless merchants | ||
|
|
||
| merchants.map do |merchant_data| | ||
| MerchantItem.new(merchant_data) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmn, ini modelnya gak inheret dri model base? kalo ada penambahan attribute di API response ada kemungkinan bakal error ini @syauqimekari, bisa coba tambahin test case ini gak?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oke bang |
||
| end | ||
| end | ||
|
|
||
| # Inner class to represent individual merchant items | ||
| class MerchantItem | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kalo ini dibuat file baru aja gimana? gk perlu di define di model dalem class get, wdyt?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oke bang |
||
| attr_reader :merchant_id, :merchant_name, :merchant_phone_number, :email | ||
|
|
||
| def initialize(merchant_data) | ||
| @merchant_id = merchant_data['merchant_id'] | ||
| @merchant_name = merchant_data['merchant_name'] | ||
| @merchant_phone_number = merchant_data['merchant_phone_number'] | ||
| @email = merchant_data['email'] | ||
| end | ||
|
|
||
| def to_h | ||
| { | ||
| merchant_id: merchant_id, | ||
| merchant_name: merchant_name, | ||
| merchant_phone_number: merchant_phone_number, | ||
| email: email | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| describe MidtransApi::Api::Merchant::Get do | ||
| let(:client) do | ||
| MidtransApi::Client.new( | ||
| client_key: 'secret_key', | ||
| server_key: 'secret_key', | ||
| sandbox: true, | ||
| api_version: :v1 | ||
| ) | ||
| end | ||
|
|
||
| let(:success_response) do | ||
| { | ||
| "status_code": "200", | ||
| "status_message": "Merchants have been successfully retrieved.", | ||
| "merchants": [ | ||
| { | ||
| "merchant_id": "G221991147", | ||
| "merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01", | ||
| "merchant_phone_number": "+621000000000", | ||
| "email": "dhany+39457_79@mekari.com" | ||
| }, | ||
| { | ||
| "merchant_id": "G539217527", | ||
| "merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01", | ||
| "merchant_phone_number": "+621000000000", | ||
| "email": "dhany+39457_80@mekari.com" | ||
| } | ||
| ] | ||
| } | ||
| end | ||
|
|
||
| let(:empty_merchants_response) do | ||
| { | ||
| "status_code": "200", | ||
| "status_message": "No merchants found.", | ||
| "merchants": [] | ||
| } | ||
| end | ||
|
|
||
| let(:missing_merchants_response) do | ||
| { | ||
| "status_code": "200", | ||
| "status_message": "No merchants found." | ||
| } | ||
| end | ||
|
|
||
| let(:merchants_with_empty_fields_response) do | ||
| { | ||
| "status_code": "200", | ||
| "status_message": "Merchants have been successfully retrieved.", | ||
| "merchants": [ | ||
| { | ||
| "merchant_id": "G221991147", | ||
| "merchant_name": "", | ||
| "merchant_phone_number": nil, | ||
| "email": "dhany+39457_79@mekari.com" | ||
| }, | ||
| { | ||
| "merchant_id": "", | ||
| "merchant_name": "Test Merchant", | ||
| "merchant_phone_number": "+621000000000", | ||
| "email": "" | ||
| } | ||
| ] | ||
| } | ||
| end | ||
|
|
||
| let(:merchants_with_missing_fields_response) do | ||
| { | ||
| "status_code": "200", | ||
| "status_message": "Merchants have been successfully retrieved.", | ||
| "merchants": [ | ||
| { | ||
| "merchant_id": "G221991147", | ||
| "email": "dhany+39457_79@mekari.com" | ||
| }, | ||
| { | ||
| "merchant_name": "Test Merchant", | ||
| "merchant_phone_number": "+621000000000" | ||
| } | ||
| ] | ||
| } | ||
| end | ||
|
|
||
| describe '#get' do | ||
| context 'with keyword parameter' do | ||
| it 'returns expected response' do | ||
| partner_id = '739' | ||
| params = { keyword: 'MSN KAP Agus Ubaidillah dan Rekan01' } | ||
|
|
||
| stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") | ||
| .with(query: params) | ||
| .to_return(status: 200, body: success_response.to_json) | ||
|
|
||
| merchant_api = described_class.new(client) | ||
| response = merchant_api.get(params, partner_id) | ||
|
|
||
| expect(response).to be_instance_of MidtransApi::Model::Merchant::Get | ||
| expect(response.merchants).to be_an(Array) | ||
| expect(response.merchants.length).to eq(2) | ||
|
|
||
| # Test merchant_list method | ||
| merchant_list = response.merchant_list | ||
| expect(merchant_list).to be_an(Array) | ||
| expect(merchant_list.length).to eq(2) | ||
|
|
||
| first_merchant = merchant_list.first | ||
| expect(first_merchant.merchant_id).to eq('G221991147') | ||
| expect(first_merchant.merchant_name).to eq('MSN KAP Agus Ubaidillah dan Rekan01') | ||
| expect(first_merchant.merchant_phone_number).to eq('+621000000000') | ||
| expect(first_merchant.email).to eq('dhany+39457_79@mekari.com') | ||
| end | ||
| end | ||
|
|
||
| context 'without keyword parameter' do | ||
| it 'returns expected response' do | ||
| partner_id = '739' | ||
| params = {} | ||
|
|
||
| stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") | ||
| .to_return(status: 200, body: success_response.to_json) | ||
|
|
||
| merchant_api = described_class.new(client) | ||
| response = merchant_api.get(params, partner_id) | ||
|
|
||
| expect(response).to be_instance_of MidtransApi::Model::Merchant::Get | ||
| expect(response.merchants).to be_an(Array) | ||
| end | ||
| end | ||
|
|
||
| context 'when merchants array is empty' do | ||
| it 'returns empty merchant list' do | ||
| partner_id = '739' | ||
| params = {} | ||
|
|
||
| stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") | ||
| .to_return(status: 200, body: empty_merchants_response.to_json) | ||
|
|
||
| merchant_api = described_class.new(client) | ||
| response = merchant_api.get(params, partner_id) | ||
|
|
||
| expect(response).to be_instance_of MidtransApi::Model::Merchant::Get | ||
| expect(response.merchants).to be_an(Array) | ||
| expect(response.merchants).to be_empty | ||
| expect(response.merchant_list).to be_an(Array) | ||
| expect(response.merchant_list).to be_empty | ||
| end | ||
| end | ||
|
|
||
| context 'when merchants field is missing from response' do | ||
| it 'handles missing merchants field gracefully' do | ||
| partner_id = '739' | ||
| params = {} | ||
|
|
||
| stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") | ||
| .to_return(status: 200, body: missing_merchants_response.to_json) | ||
|
|
||
| merchant_api = described_class.new(client) | ||
| response = merchant_api.get(params, partner_id) | ||
|
|
||
| expect(response).to be_instance_of MidtransApi::Model::Merchant::Get | ||
| expect(response.merchants).to be_nil | ||
| expect(response.merchant_list).to be_an(Array) | ||
| expect(response.merchant_list).to be_empty | ||
| end | ||
| end | ||
|
|
||
| context 'when merchant fields are empty or nil' do | ||
| it 'handles empty and nil field values gracefully' do | ||
| partner_id = '739' | ||
| params = {} | ||
|
|
||
| stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") | ||
| .to_return(status: 200, body: merchants_with_empty_fields_response.to_json) | ||
|
|
||
| merchant_api = described_class.new(client) | ||
| response = merchant_api.get(params, partner_id) | ||
|
|
||
| expect(response).to be_instance_of MidtransApi::Model::Merchant::Get | ||
| expect(response.merchants).to be_an(Array) | ||
| expect(response.merchants.length).to eq(2) | ||
|
|
||
| merchant_list = response.merchant_list | ||
| expect(merchant_list.length).to eq(2) | ||
|
|
||
| first_merchant = merchant_list.first | ||
| expect(first_merchant.merchant_id).to eq('G221991147') | ||
| expect(first_merchant.merchant_name).to eq('') | ||
| expect(first_merchant.merchant_phone_number).to be_nil | ||
| expect(first_merchant.email).to eq('dhany+39457_79@mekari.com') | ||
|
|
||
| second_merchant = merchant_list.last | ||
| expect(second_merchant.merchant_id).to eq('') | ||
| expect(second_merchant.merchant_name).to eq('Test Merchant') | ||
| expect(second_merchant.merchant_phone_number).to eq('+621000000000') | ||
| expect(second_merchant.email).to eq('') | ||
| end | ||
| end | ||
|
|
||
| context 'when merchant fields are missing' do | ||
| it 'handles missing field values gracefully' do | ||
| partner_id = '739' | ||
| params = {} | ||
|
|
||
| stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") | ||
| .to_return(status: 200, body: merchants_with_missing_fields_response.to_json) | ||
|
|
||
| merchant_api = described_class.new(client) | ||
| response = merchant_api.get(params, partner_id) | ||
|
|
||
| expect(response).to be_instance_of MidtransApi::Model::Merchant::Get | ||
| expect(response.merchants).to be_an(Array) | ||
| expect(response.merchants.length).to eq(2) | ||
|
|
||
| merchant_list = response.merchant_list | ||
| expect(merchant_list.length).to eq(2) | ||
|
|
||
| first_merchant = merchant_list.first | ||
| expect(first_merchant.merchant_id).to eq('G221991147') | ||
| expect(first_merchant.merchant_name).to be_nil | ||
| expect(first_merchant.merchant_phone_number).to be_nil | ||
| expect(first_merchant.email).to eq('dhany+39457_79@mekari.com') | ||
|
|
||
| second_merchant = merchant_list.last | ||
| expect(second_merchant.merchant_id).to be_nil | ||
| expect(second_merchant.merchant_name).to eq('Test Merchant') | ||
| expect(second_merchant.merchant_phone_number).to eq('+621000000000') | ||
| expect(second_merchant.email).to be_nil | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ini buat apa ya ki?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ini udah standard dari base modelnya bang untuk parsenya