diff --git a/README.md b/README.md index 9dd3411..661a67e 100644 --- a/README.md +++ b/README.md @@ -34,38 +34,38 @@ gem install cgrates ```ruby client = CGRateS::Client.new(host: "http://localhost:2080") client.ping -# => # +# => # client.set_tp_destination(tp_id: "cgrates_client_test", id: "Cambodia_Mobile", prefixes: ["85510", "85512", "85597"]) -# => # +# => # client.get_tp_destination(tp_id: "cgrates_client_test", id: "Cambodia_Mobile") -# => # "cgrates_client_test", "ID" => "Cambodia_Mobile", "Prefixes" => ["85510", "85512", "85597"]}> +# => # "cgrates_client_test", "ID" => "Cambodia_Mobile", "Prefixes" => ["85510", "85512", "85597"]}> client.set_tp_rate(tp_id: "cgrates_client_test", id: "Cambodia_Mobile", rate_slots: [{ rate: 0.05, rate_unit: "60s", rate_increment: "60s" }]) -# => # +# => # client.get_tp_rate(tp_id: "cgrates_client_test", id: "Cambodia_Mobile") # => -# # "cgrates_client_test", "ID" => "Cambodia_Mobile", "RateSlots" => [{"ConnectFee" => 0, "Rate" => 0.05, "RateUnit" => "60s", "RateIncrement" => "60s", "GroupIntervalStart" => ""}]}> client.set_tp_destination_rate(tp_id: "cgrates_client_test", id: "Cambodia_Mobile", destination_rates: [{rounding_decimals: 4, rate_id: "Cambodia_Mobile", destination_id: "Cambodia_Mobile", rounding_method: "*up" }]) -# => # +# => # client.get_tp_destination_rate(tp_id: "cgrates_client_test", id: "Cambodia_Mobile") -# "cgrates_client_test", # "ID" => "Cambodia_Mobile", # "DestinationRates" => [{"DestinationId" => "Cambodia_Mobile", "RateId" => "Cambodia_Mobile", "Rate" => nil, "RoundingMethod" => "*up", "RoundingDecimals" => 4, "MaxCost" => 0, "MaxCostStrategy" => ""}]}> client.set_tp_rating_plan(tp_id: "cgrates_client_test", id: "Test_Rating_Plan", rating_plan_bindings: [{ weight: 10, timing_id: "*any", destination_rates_id: "Cambodia_Mobile" }]) -# => # +# => # client.get_tp_rating_plan(tp_id: "cgrates_client_test", id: "Test_Rating_Plan") -# => "cgrates_client_test", "ID" => "Test_Rating_Plan", "RatingPlanBindings" => [{"DestinationRatesId" => "Cambodia_Mobile", "TimingId" => "*any", "Weight" => 10}]}> @@ -83,7 +83,7 @@ client.set_tp_rating_profile( } ] ) -#=> # +#=> # client.get_tp_rating_profile( tp_id: "cgrates_client_test", @@ -92,7 +92,7 @@ client.get_tp_rating_profile( category: "call", subject: "my-account" ) -# => # # "cgrates_client_test", diff --git a/lib/cgrates/client.rb b/lib/cgrates/client.rb index 17ccddf..5064db1 100644 --- a/lib/cgrates/client.rb +++ b/lib/cgrates/client.rb @@ -183,8 +183,55 @@ def load_tariff_plan_from_stor_db(tp_id:, dry_run: false, validate: true, **) ) end + def add_balance(**) + balance_request("APIerSv1.AddBalance", **) + end + + def debit_balance(**) + balance_request("APIerSv1.DebitBalance", **) + end + + def get_cdrs(tenants: [], order_by: "OrderID", extra_args: {}, limit: nil, **) + api_request( + "APIerSv2.GetCDRs", + "Tenants" => tenants, + "OrderBy" => order_by, + "ExtraArgs" => extra_args, + "Limit" => limit, + ** + ) + end + private + def balance_request(method, account:, tenant:, balance_type:, value:, balance:, overwrite: false, action_extra_data: {}, cdrlog: false, **) + api_request( + method, + { + "Account" => account, + "Tenant" => tenant, + "BalanceType" => balance_type, + "Value" => value, + "Balance" => { + "ID" => balance[:id], + "ExpiryTime" => balance.fetch(:expiry_time, "*unlimited"), + "RatingSubject" => balance[:rating_subject], + "Categories" => balance[:categories], + "DestinationIDs" => balance.fetch(:destination_ids, "*any"), + "TimingIDs" => balance[:timing_ids], + "Weight" => balance.fetch(:weight, 10), + "SharedGroups" => balance[:shared_groups], + "Blocker" => balance.fetch(:blocker, false), + "Disabled" => balance.fetch(:disabled, false) + }, + "ActionExtraData" => action_extra_data, + "Overwrite" => overwrite, + "Cdrlog" => cdrlog, + ** + } + ) + end + def api_request(method, *params) response = http_client.post( jsonrpc_endpoint, diff --git a/lib/cgrates/response.rb b/lib/cgrates/response.rb index 5e822be..afc8b52 100644 --- a/lib/cgrates/response.rb +++ b/lib/cgrates/response.rb @@ -1 +1,3 @@ -Response = Data.define(:id, :result) +module CGRateS + Response = Data.define(:id, :result) +end diff --git a/spec/cgrates/client_spec.rb b/spec/cgrates/client_spec.rb index d86b51d..6c06977 100644 --- a/spec/cgrates/client_spec.rb +++ b/spec/cgrates/client_spec.rb @@ -392,6 +392,47 @@ module CGRateS end end + describe "#add_balance" do + it "executes the request" do + client = build_client + + stub_api_request(result: "OK") + response = client.add_balance(account: "sample-account-sid", tenant: "cgrates.org", balance_type: "credit", value: 100, balance: { uuid: "123", id: "456" }) + + expect(response).to have_attributes(result: "OK") + expect(WebMock).to have_requested_api_method("APIerSv1.AddBalance") + end + end + + describe "#debit_balance" do + it "executes the request" do + client = build_client + + stub_api_request(result: "OK") + response = client.debit_balance(account: "sample-account-sid", tenant: "cgrates.org", balance_type: "credit", value: 100, balance: { uuid: "123", id: "456" }) + + expect(response).to have_attributes(result: "OK") + expect(WebMock).to have_requested_api_method("APIerSv1.DebitBalance") + end + end + + describe "#get_cdrs" do + it "executes the request" do + client = build_client + + stub_api_request(result: []) + response = client.get_cdrs( + tenants: [ "cgrates.org" ], + order_by: "OrderID", + extra_args: { "OrderIDStart" => 1 }, + limit: 2 + ) + + expect(response).to have_attributes(result: a_kind_of(Array)) + expect(WebMock).to have_requested_api_method("APIerSv2.GetCDRs") + end + end + it "handles invalid http responses" do client = build_client stub_api_request(status: 500)