Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 3.4.7
ruby 3.4.8
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ client.get_tp_rating_plan(tp_id: "cgrates_client_test", id: "Test_Rating_Plan")

client.set_tp_rating_profile(
tp_id: "cgrates_client_test",
id: "Test_Rating_Profile",
load_id: "TEST",
category: "call",
tenant: "cgrates.org",
Expand Down
78 changes: 71 additions & 7 deletions lib/cgrates/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_tp_destination(prefixes:, **)
end

def get_tp_destination(**)
get_tp_resource("APIerSv1.GetTPDestination", **)
tp_resource_request("APIerSv1.GetTPDestination", **)
end

def set_tp_rate(rate_slots:, **)
Expand All @@ -50,7 +50,11 @@ def set_tp_rate(rate_slots:, **)
end

def get_tp_rate(**)
get_tp_resource("APIerSv1.GetTPRate", **)
tp_resource_request("APIerSv1.GetTPRate", **)
end

def remove_tp_rate(**)
tp_resource_request("APIerSv1.RemoveTPRate", **)
end

def set_tp_destination_rate(destination_rates:, **)
Expand All @@ -71,7 +75,11 @@ def set_tp_destination_rate(destination_rates:, **)
end

def get_tp_destination_rate(**)
get_tp_resource("APIerSv1.GetTPDestinationRate", **)
tp_resource_request("APIerSv1.GetTPDestinationRate", **)
end

def remove_tp_destination_rate(**)
tp_resource_request("APIerSv1.RemoveTPDestinationRate", **)
end

def set_tp_rating_plan(rating_plan_bindings:, **)
Expand All @@ -89,11 +97,15 @@ def set_tp_rating_plan(rating_plan_bindings:, **)
end

def get_tp_rating_plan(**)
get_tp_resource("APIerSv1.GetTPRatingPlan", **)
tp_resource_request("APIerSv1.GetTPRatingPlan", **)
end

def remove_tp_rating_plan(**)
tp_resource_request("APIerSv1.RemoveTPRatingPlan", **)
end

def set_tp_rating_profile(rating_plan_activations:, load_id:, category:, subject:, tenant: nil, **)
set_tp_resource("APIerSv1.SetTPRatingProfile", **) do
set_tp_resource("APIerSv1.SetTPRatingProfile", id: nil, **) do
{
"RatingPlanActivations" => rating_plan_activations.map do
{
Expand All @@ -111,14 +123,66 @@ def set_tp_rating_profile(rating_plan_activations:, load_id:, category:, subject
end

def get_tp_rating_profile(tp_id:, load_id:, tenant:, category:, subject:)
get_tp_resource(
tp_resource_request(
"APIerSv1.GetTPRatingProfile",
tp_id:,
id: [ load_id, tenant, category, subject ].join(":"),
id_key: "RatingProfileId"
)
end

def remove_tp_rating_profile(tp_id:, load_id:, tenant:, category:, subject:)
tp_resource_request(
"APIerSv1.RemoveTPRatingProfile",
tp_id:,
id: [ load_id, tenant, category, subject ].join(":"),
id_key: "RatingProfileId"
)
end

def set_account(account:, tenant: nil, **)
api_request(
"APIerSv2.SetAccount",
{
"Tenant" => tenant,
"Account" => account,
**
}
)
end

def get_account(account:, tenant: nil, **)
api_request(
"APIerSv2.GetAccount",
{
"Tenant" => tenant,
"Account" => account,
**
}
)
end

def remove_account(account:, tenant: nil, **)
api_request(
"APIerSv1.RemoveAccount",
{
"Tenant" => tenant,
"Account" => account,
**
}
)
end

def load_tariff_plan_from_stor_db(tp_id:, dry_run: false, validate: true, **)
api_request(
"APIerSv1.LoadTariffPlanFromStorDb",
"TPid" => tp_id,
"DryRun" => dry_run,
"Validate" => validate,
**
)
end

private

def api_request(method, *params)
Expand Down Expand Up @@ -152,7 +216,7 @@ def set_tp_resource(method, tp_id:, id:, &)
api_request(method, { "TPid" => tp_id, "ID" => id }.merge(yield))
end

def get_tp_resource(method, tp_id:, id:, id_key: "ID")
def tp_resource_request(method, tp_id:, id:, id_key: "ID")
api_request(method, { "TPid" => tp_id, id_key => id })
end

Expand Down
95 changes: 95 additions & 0 deletions spec/cgrates/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ module CGRateS
)
)
expect(WebMock).to have_requested_api_method("APIerSv1.GetTPRate")

stub_api_request(result: "OK")
response = client.remove_tp_rate(
tp_id: "cgrates_client_test",
id: "Cambodia_Mobile_Rate"
)

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv1.RemoveTPRate")
end
end

Expand Down Expand Up @@ -174,6 +183,15 @@ module CGRateS
)
)
expect(WebMock).to have_requested_api_method("APIerSv1.GetTPDestinationRate")

stub_api_request(result: "OK")
response = client.remove_tp_destination_rate(
tp_id: "cgrates_client_test",
id: "Cambodia_Mobile_Destination_Rate"
)

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv1.RemoveTPDestinationRate")
end
end

Expand Down Expand Up @@ -226,6 +244,15 @@ module CGRateS
)
)
expect(WebMock).to have_requested_api_method("APIerSv1.GetTPRatingPlan")

stub_api_request(result: "OK")
response = client.remove_tp_rating_plan(
tp_id: "cgrates_client_test",
id: "Test_Rating_Plan"
)

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv1.RemoveTPRatingPlan")
end
end

Expand Down Expand Up @@ -293,9 +320,77 @@ module CGRateS
)
)
expect(WebMock).to have_requested_api_method("APIerSv1.GetTPRatingProfile")

stub_api_request(result: "OK")
response = client.remove_tp_rating_profile(
tp_id: "cgrates_client_test",
load_id: "TEST",
tenant: "cgrates.org",
category: "call",
subject: "my-account"
)

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv1.RemoveTPRatingProfile")
end
end

describe "#set_account" do
it "executes the request" do
client = build_client

stub_api_request(result: "OK")
response = client.set_account(account: "sample-account-sid", tenant: "cgrates.org")

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv2.SetAccount")

stub_api_request(
result: {
"ID" => "cgrates.org:sample-account-sid",
"BalanceMap" => nil,
"UnitCounters" => nil,
"ActionTriggers" => nil,
"AllowNegative" => false,
"Disabled" => false,
"UpdateTime" => "2026-01-08T11:49:44.172931119Z"
}
)

response = client.get_account(
tenant: "cgrates.org",
account: "sample-account-sid"
)

expect(response).to have_attributes(
result: hash_including(
"ID" => "cgrates.org:sample-account-sid",
)
)
expect(WebMock).to have_requested_api_method("APIerSv2.GetAccount")

stub_api_request(result: "OK")
response = client.remove_account(
account: "sample-account-sid",
tenant: "cgrates.org"
)

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv1.RemoveAccount")
end
end

describe "#load_tariff_plan_from_stor_db" do
it "executes the request" do
client = build_client

stub_api_request(result: "OK")
response = client.load_tariff_plan_from_stor_db(tp_id: "cgrates_client_test")

expect(response).to have_attributes(result: "OK")
expect(WebMock).to have_requested_api_method("APIerSv1.LoadTariffPlanFromStorDb")
end
end

it "handles invalid http responses" do
client = build_client
Expand Down