cdek_api_client 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e5151f9c6ed46e58d4badbc2e0055c42eea0ebc8f3139451924465197409126
4
- data.tar.gz: 3761dd1c518d946e8a076ac6533dfd8424051f8835db193ea34a87db52db5018
3
+ metadata.gz: 58ba9a5e50a195b86b5736b86d6b4d0cf0b9af02857bed94ab004a5b1cde5819
4
+ data.tar.gz: c719a6ea6706e18a5d887223b57c344070649e980cd37e16c7e1db5a957652f7
5
5
  SHA512:
6
- metadata.gz: c46a9e46713e5702ed3c0f5eb1f004b8d5a3657693b82f922db860201cb365b520d84a68854e7b150f5f82442de04591cc82a8c2e3d5600588430c443cedf64b
7
- data.tar.gz: 4edeb61dbf81279666897852d794dd88ad36ec26ed09d13f0e937290dc6abd1d2ad2ceaef61632ad43e5ce3274f9f350a980ce29e2e092a659e46897471cbc44
6
+ metadata.gz: 7b824446b954f88c27a799681e67a4932c38fe5a61fd7157d92213c194f750d750fdda9dbe287bdc690bf94d3e2a2c101bbe2983e3c06fa5095815870dd54abd
7
+ data.tar.gz: e3ca1b0bb0370819a5fcf5dc44cbfe3bf42fc18e2b185e792ddd2e6622a732afcf050e6dee430abc042f28ab21f831965ba376eb21caf6933b9f01022941fae0
data/CHANGELOG.md ADDED
@@ -0,0 +1,77 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ > [!NOTE]
6
+ > The format is based on [Keep a Changelog],
7
+ > and this project adheres to [Semantic Versioning].
8
+
9
+ ## [Unreleased]
10
+
11
+ /
12
+
13
+ ## [0.3.0] - 2026-01-18
14
+
15
+ ### Added
16
+
17
+ - Added Courier API for managing delivery agreements, courier intakes, and delivery intervals
18
+ - Added Payment API for payment-related operations
19
+ - Added Print API for document printing functionality
20
+ - Added new entities: Agreement, AuthResponse, AuthErrorResponse, Barcode, Check, IntakeAvailableDaysRequest, IntakeAvailableDaysResponse, Intakes, Invoice
21
+ - Improved authentication error handling with dedicated AuthResponse and AuthErrorResponse entities
22
+ - Added comprehensive release checklist (RELEASE.md) following 2026 RubyGems best practices
23
+
24
+ ### Changed
25
+
26
+ - Updated gemspec metadata to follow 2026 RubyGems standards:
27
+ - Fixed all URIs to use HTTPS instead of HTTP
28
+ - Added `bug_tracker_uri` metadata field
29
+ - Added `documentation_uri` metadata field
30
+ - Updated `changelog_uri` to point directly to CHANGELOG.md file
31
+ - Updated GitHub repository description and added relevant topics for better discoverability
32
+ - Fixed GitHub Actions workflow syntax issue in Ruby setup step
33
+
34
+ ## [0.2.1] - 2024-07-22
35
+
36
+ ### Added
37
+
38
+ - Added new commands to the `Makefile` for tagging a new version and pushing it to GitHub.
39
+
40
+ ### Changed
41
+
42
+ - Updated links to Changelog and the Gem homepage in the gemspec.
43
+
44
+ ### Fixed
45
+
46
+ - Fixed issues with previous release details and documentation.
47
+
48
+ ## [0.2.0] - 2024-07-22
49
+
50
+ ### Added
51
+
52
+ - Improved error handling and response parsing in the `Client` class.
53
+ - Updated `README.md` with detailed usage examples, including fetching and saving location data with optional live data fetching.
54
+
55
+ ### Changed
56
+
57
+ - Refactored code structure for better organization.
58
+ - Updated specs for `Client` and `API` classes.
59
+
60
+ ## [0.1.0] - 2024-07-22
61
+
62
+ - Initial release
63
+
64
+ [Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
65
+ [Semantic Versioning]: https://semver.org/spec/v2.0.0.html
66
+
67
+ <!-- Versions -->
68
+
69
+ [unreleased]: https://github.com/SamyRai/cdek_api_client/compare/v0.3.0...HEAD
70
+ [0.3.0]: https://github.com/SamyRai/cdek_api_client/compare/v0.2.1...v0.3.0
71
+ [0.2.1]: https://github.com/SamyRai/cdek_api_client/compare/v0.2.0...v0.2.1
72
+ [0.2.0]: https://github.com/SamyRai/cdek_api_client/compare/v0.1.0...v0.2.0
73
+ [0.1.0]: https://github.com/SamyRai/cdek_api_client/releases/tag/v0.1.0
74
+
75
+ ```
76
+
77
+ ```
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  - [Татарча](README_TAT.md)
9
9
  - [English](README.md)
10
10
 
11
- **Important:** This gem is in the early stages of development and it is shared as it is. Any support for development or feedback is welcome; please check the [Contributing](#contributing) section for more information.
11
+ > [!WARNING] >**Important:** This gem is in the early stages of development and it is shared as it is. Any support for development or feedback is welcome; please check the [Contributing](#contributing) section for more information.
12
12
 
13
13
  ## Overview
14
14
 
@@ -16,10 +16,13 @@ CDEK ([СДЭК](https://www.cdek.ru/)) is a big logistics company in Russia, th
16
16
 
17
17
  The `cdek_api_client` gem offers a clean and robust interface to interact with the CDEK API, ensuring maintainable code with proper validations. This gem supports the following features:
18
18
 
19
- - Creating and tracking orders
20
- - Calculating tariffs
21
- - Retrieving location data (cities, regions, postal codes, and offices)
22
- - Managing webhooks
19
+ - **Order Management**: Creating, tracking, updating, canceling, deleting, and retrieving orders by various identifiers
20
+ - **Tariff Calculation**: Calculating single tariffs, tariff lists, and enhanced tariff calculations with services
21
+ - **Location Services**: Retrieving cities, regions, postal codes, delivery offices, and coordinates
22
+ - **Print/Documents**: Creating and retrieving barcodes, invoices, and other printable documents (PDF)
23
+ - **Courier Services**: Managing delivery agreements and intake requests for courier pickup
24
+ - **Payment Services**: Retrieving payment information, check data, and payment registries
25
+ - **Webhook Management**: Registering, listing, managing, and deleting webhooks for real-time notifications
23
26
 
24
27
  ## Table of Contents
25
28
 
@@ -28,9 +31,15 @@ The `cdek_api_client` gem offers a clean and robust interface to interact with t
28
31
  - [Initialization](#initialization)
29
32
  - [Creating an Order](#creating-an-order)
30
33
  - [Tracking an Order](#tracking-an-order)
34
+ - [Order Management](#order-management)
31
35
  - [Calculating Tariff](#calculating-tariff)
36
+ - [Enhanced Tariff Calculation](#enhanced-tariff-calculation)
32
37
  - [Getting Location Data](#getting-location-data)
38
+ - [Print and Documents](#print-and-documents)
39
+ - [Courier Services](#courier-services)
40
+ - [Payment Services](#payment-services)
33
41
  - [Setting Up Webhooks](#setting-up-webhooks)
42
+ - [Enhanced Webhook Management](#enhanced-webhook-management)
34
43
  - [Fetching and Saving Location Data](#fetching-and-saving-location-data)
35
44
  - [Entities](#entities)
36
45
  - [OrderData](#orderdata)
@@ -38,6 +47,12 @@ The `cdek_api_client` gem offers a clean and robust interface to interact with t
38
47
  - [Sender](#sender)
39
48
  - [Package](#package)
40
49
  - [Item](#item)
50
+ - [Barcode](#barcode)
51
+ - [Invoice](#invoice)
52
+ - [Agreement](#agreement)
53
+ - [Intakes](#intakes)
54
+ - [Check](#check)
55
+ - [Schema Management](#schema-management)
41
56
  - [TODO List](#todo-list)
42
57
  - [Changelog](#changelog)
43
58
  - [Contributing](#contributing)
@@ -272,6 +287,246 @@ rescue => e
272
287
  end
273
288
  ```
274
289
 
290
+ ### Order Management
291
+
292
+ The gem provides comprehensive order management capabilities beyond basic creation and tracking:
293
+
294
+ ```ruby
295
+ # Delete an order
296
+ begin
297
+ response = client.order.delete('order-uuid')
298
+ puts "Order deleted: #{response}"
299
+ rescue => e
300
+ puts "Error deleting order: #{e.message}"
301
+ end
302
+
303
+ # Cancel an order (refusal)
304
+ begin
305
+ response = client.order.cancel('order-uuid')
306
+ puts "Order canceled: #{response}"
307
+ rescue => e
308
+ puts "Error canceling order: #{e.message}"
309
+ end
310
+
311
+ # Update an order
312
+ updated_order_data = # ... create updated order data
313
+ begin
314
+ response = client.order.update(updated_order_data)
315
+ puts "Order updated: #{response}"
316
+ rescue => e
317
+ puts "Error updating order: #{e.message}"
318
+ end
319
+
320
+ # Get order by UUID
321
+ begin
322
+ order_info = client.order.get('order-uuid')
323
+ puts "Order info: #{order_info}"
324
+ rescue => e
325
+ puts "Error getting order: #{e.message}"
326
+ end
327
+
328
+ # Get order by CDEK number
329
+ begin
330
+ order_info = client.order.get_by_cdek_number('123456789')
331
+ puts "Order info: #{order_info}"
332
+ rescue => e
333
+ puts "Error getting order: #{e.message}"
334
+ end
335
+
336
+ # Get order by IM number
337
+ begin
338
+ order_info = client.order.get_by_im_number('ORDER123')
339
+ puts "Order info: #{order_info}"
340
+ rescue => e
341
+ puts "Error getting order: #{e.message}"
342
+ end
343
+
344
+ # Create client return order
345
+ client_return_data = CDEKApiClient::Entities::OrderData.new(
346
+ type: 1,
347
+ tariff_code: 1,
348
+ packages: [package]
349
+ )
350
+ begin
351
+ response = client.order.create_client_return('original-order-uuid', client_return_data)
352
+ puts "Client return created: #{response}"
353
+ rescue => e
354
+ puts "Error creating client return: #{e.message}"
355
+ end
356
+
357
+ # Get intakes for an order
358
+ begin
359
+ intakes = client.order.get_intakes('order-uuid')
360
+ puts "Order intakes: #{intakes}"
361
+ rescue => e
362
+ puts "Error getting intakes: #{e.message}"
363
+ end
364
+ ```
365
+
366
+ ### Print and Documents
367
+
368
+ Generate and retrieve printable documents like barcodes and invoices:
369
+
370
+ ```ruby
371
+ # Create barcode
372
+ barcode_data = CDEKApiClient::Entities::Barcode.with_orders_uuid('order-uuid')
373
+ begin
374
+ response = client.print.create_barcode(barcode_data)
375
+ puts "Barcode created: #{response}"
376
+ rescue => e
377
+ puts "Error creating barcode: #{e.message}"
378
+ end
379
+
380
+ # Get barcode PDF
381
+ begin
382
+ pdf_content = client.print.get_barcode_pdf('barcode-uuid')
383
+ File.write('barcode.pdf', pdf_content)
384
+ rescue => e
385
+ puts "Error getting barcode PDF: #{e.message}"
386
+ end
387
+
388
+ # Create invoice
389
+ invoice_data = CDEKApiClient::Entities::Invoice.with_orders_uuid('order-uuid')
390
+ begin
391
+ response = client.print.create_invoice(invoice_data)
392
+ puts "Invoice created: #{response}"
393
+ rescue => e
394
+ puts "Error creating invoice: #{e.message}"
395
+ end
396
+
397
+ # Get invoice PDF
398
+ begin
399
+ pdf_content = client.print.get_invoice_pdf('invoice-uuid')
400
+ File.write('invoice.pdf', pdf_content)
401
+ rescue => e
402
+ puts "Error getting invoice PDF: #{e.message}"
403
+ end
404
+ ```
405
+
406
+ ### Courier Services
407
+
408
+ Manage delivery agreements and courier intake requests:
409
+
410
+ ```ruby
411
+ # Create delivery agreement
412
+ agreement_data = CDEKApiClient::Entities::Agreement.new(
413
+ cdek_number: '123456789',
414
+ date: '2024-01-17',
415
+ time_from: '10:00',
416
+ time_to: '18:00',
417
+ comment: 'Delivery agreement'
418
+ )
419
+ begin
420
+ response = client.courier.create_agreement(agreement_data)
421
+ puts "Agreement created: #{response}"
422
+ rescue => e
423
+ puts "Error creating agreement: #{e.message}"
424
+ end
425
+
426
+ # Create courier intake request
427
+ intake_data = CDEKApiClient::Entities::Intakes.new(
428
+ cdek_number: '123456789',
429
+ intake_date: '2024-01-17',
430
+ intake_time_from: '10:00',
431
+ intake_time_to: '18:00',
432
+ name: 'Cargo description',
433
+ sender: { name: 'Sender Name', phones: [{ number: '+79001234567' }] },
434
+ from_location: { code: 44, address: 'Pickup Address' }
435
+ )
436
+ begin
437
+ response = client.courier.create_intake(intake_data)
438
+ puts "Intake created: #{response}"
439
+ rescue => e
440
+ puts "Error creating intake: #{e.message}"
441
+ end
442
+
443
+ # Delete intake request
444
+ begin
445
+ response = client.courier.delete_intake('intake-uuid')
446
+ puts "Intake deleted: #{response}"
447
+ rescue => e
448
+ puts "Error deleting intake: #{e.message}"
449
+ end
450
+ ```
451
+
452
+ ### Payment Services
453
+
454
+ Retrieve payment and check information:
455
+
456
+ ```ruby
457
+ # Get payments for a date
458
+ begin
459
+ payments = client.payment.get_payments('2024-01-17')
460
+ puts "Payments: #{payments}"
461
+ rescue => e
462
+ puts "Error getting payments: #{e.message}"
463
+ end
464
+
465
+ # Get checks
466
+ check_data = CDEKApiClient::Entities::Check.new(
467
+ cdek_number: '123456789',
468
+ date: '2024-01-17'
469
+ )
470
+ begin
471
+ checks = client.payment.get_checks(check_data.to_query_params)
472
+ puts "Checks: #{checks}"
473
+ rescue => e
474
+ puts "Error getting checks: #{e.message}"
475
+ end
476
+
477
+ # Get payment registries
478
+ begin
479
+ registries = client.payment.get_registries('2024-01-17')
480
+ puts "Registries: #{registries}"
481
+ rescue => e
482
+ puts "Error getting registries: #{e.message}"
483
+ end
484
+ ```
485
+
486
+ ### Enhanced Tariff Calculation
487
+
488
+ Calculate all available tariffs at once:
489
+
490
+ ```ruby
491
+ # Calculate tariff list
492
+ begin
493
+ tariffs = client.tariff.calculate_list(tariff_data)
494
+ puts "Available tariffs: #{tariffs}"
495
+ rescue => e
496
+ puts "Error calculating tariffs: #{e.message}"
497
+ end
498
+ ```
499
+
500
+ ### Enhanced Webhook Management
501
+
502
+ List and manage webhooks:
503
+
504
+ ```ruby
505
+ # List all webhooks
506
+ begin
507
+ webhooks = client.webhook.list_all
508
+ puts "All webhooks: #{webhooks}"
509
+ rescue => e
510
+ puts "Error listing webhooks: #{e.message}"
511
+ end
512
+
513
+ # Get specific webhook
514
+ begin
515
+ webhook = client.webhook.get('webhook-uuid')
516
+ puts "Webhook: #{webhook}"
517
+ rescue => e
518
+ puts "Error getting webhook: #{e.message}"
519
+ end
520
+
521
+ # Delete webhook by UUID
522
+ begin
523
+ response = client.webhook.delete('webhook-uuid')
524
+ puts "Webhook deleted: #{response}"
525
+ rescue => e
526
+ puts "Error deleting webhook: #{e.message}"
527
+ end
528
+ ```
529
+
275
530
  ### Entities
276
531
 
277
532
  ### OrderData
@@ -338,27 +593,127 @@ Attributes:
338
593
  - `weight` (Integer, required): The weight of the item.
339
594
  - `amount` (Integer, required): The amount of the item.
340
595
 
596
+ ### Barcode
597
+
598
+ Represents barcode creation data for printing.
599
+
600
+ Attributes:
601
+
602
+ - `orders` (Array, required): List of orders for barcode generation.
603
+ - `copy_count` (Integer): Number of copies (default: 1).
604
+ - `format` (String): Print format (A4, A5, A6, A7).
605
+ - `lang` (String): Language code (RUS, ENG, DEU, ITA, TUR, CES, KOR, LIT, LAT).
606
+
607
+ ### Invoice
608
+
609
+ Represents invoice creation data for printing.
610
+
611
+ Attributes:
612
+
613
+ - `orders` (Array, required): List of orders for invoice generation.
614
+ - `copy_count` (Integer): Number of copies (default: 2).
615
+ - `type` (String): Invoice type (tpl_russia, tpl_china, tpl_armenia, tpl_english, tpl_italian, tpl_korean, tpl_latvian, tpl_lithuanian, tpl_german, tpl_turkish, tpl_czech, tpl_thailand, tpl_invoice).
616
+
617
+ ### Agreement
618
+
619
+ Represents delivery agreement data.
620
+
621
+ Attributes:
622
+
623
+ - `cdek_number` (String, required): CDEK order number.
624
+ - `order_uuid` (String): Order UUID.
625
+ - `date` (String, required): Delivery date.
626
+ - `time_from` (String, required): Start time.
627
+ - `time_to` (String, required): End time.
628
+ - `comment` (String): Comment.
629
+ - `delivery_point` (String): Delivery point code.
630
+ - `to_location` (Hash): Delivery location details.
631
+
632
+ ### Intakes
633
+
634
+ Represents courier intake request data.
635
+
636
+ Attributes:
637
+
638
+ - `cdek_number` (String): CDEK order number.
639
+ - `order_uuid` (String): Order UUID.
640
+ - `intake_date` (String, required): Intake date.
641
+ - `intake_time_from` (String, required): Start time.
642
+ - `intake_time_to` (String, required): End time.
643
+ - `lunch_time_from` (String): Lunch start time.
644
+ - `lunch_time_to` (String): Lunch end time.
645
+ - `name` (String, required): Cargo description.
646
+ - `need_call` (Boolean): Whether a call is needed.
647
+ - `comment` (String): Comment for courier.
648
+ - `courier_power_of_attorney` (Boolean): Courier needs power of attorney.
649
+ - `courier_identity_card` (Boolean): Courier needs identity card.
650
+ - `sender` (Hash, required): Sender contact information.
651
+ - `from_location` (Hash, required): Pickup location details.
652
+ - `weight` (Integer): Cargo weight in grams.
653
+ - `length` (Integer): Cargo length in cm.
654
+ - `width` (Integer): Cargo width in cm.
655
+ - `height` (Integer): Cargo height in cm.
656
+
657
+ ### Check
658
+
659
+ Represents check query data.
660
+
661
+ Attributes:
662
+
663
+ - `order_uuid` (String): Order UUID.
664
+ - `cdek_number` (String): CDEK order number.
665
+ - `date` (String): Date for check retrieval.
666
+
667
+ ## Schema Management
668
+
669
+ The gem includes a schema management system to keep API definitions up-to-date. The `pull_cdek_schemas.rb` script fetches the latest OpenAPI schemas from CDEK's documentation and organizes them.
670
+
671
+ ### Pulling Latest Schemas
672
+
673
+ ```ruby
674
+ # Run the schema pull script
675
+ ruby pull_cdek_schemas.rb
676
+
677
+ # This will:
678
+ # 1. Fetch API documentation from CDEK
679
+ # 2. Parse and organize schemas
680
+ # 3. Save to cdek_api_schemas.json
681
+ # 4. Display analysis of available endpoints
682
+ ```
683
+
684
+ ### Schema Analysis
685
+
686
+ The script analyzes and organizes:
687
+ - **5 main API schemas** from CDEK documentation
688
+ - **40 unique endpoints** across all schemas
689
+ - Complete endpoint mapping with HTTP methods
690
+ - Metadata for each schema version
691
+
692
+ ### Automated Updates
693
+
694
+ Run the schema pull script periodically to stay updated with the latest CDEK API changes:
695
+
696
+ ```bash
697
+ # Add to cron or CI/CD pipeline
698
+ 0 2 * * * cd /path/to/gem && ruby pull_cdek_schemas.rb
699
+ ```
700
+
341
701
  ## TODO List
342
702
 
343
703
  - [x] Restructure the codebase for better organization.
344
704
  - [x] Add mappings for CDEK internal codes.
345
- - [ ] Add more API endpoints and data entities.
705
+ - [x] Add more API endpoints and data entities (Order management, Print/Documents, Courier services, Payment services).
706
+ - [x] Implement schema management system.
707
+ - [x] Add comprehensive documentation in English, Russian, and Tatar.
346
708
  - [ ] Check all attributes for required and optional fields.
347
- - [ ] Add documentation for all classes and methods.
709
+ - [x] Add documentation for all classes and methods.
710
+ - [ ] Refine entity validations.
711
+ - [ ] Add more comprehensive error handling.
712
+ - [ ] Add rate limiting support.
348
713
 
349
714
  ## Changelog
350
715
 
351
- ### v0.2.1
352
-
353
- - **Updated**: Changed links to Changelog and the Gem homepage in the gemspec.
354
- - **Added**: Add new commands to a `Makefile` for tagging a new version and pushing it to GitHub.
355
-
356
- ### v0.2.0
357
-
358
- - **Added**: Improved error handling and response parsing in `Client`.
359
- - **Updated**: Code structure for better organization.
360
- - **Updated**: Specs for `Client` and `API` classes.
361
- - **Updated**: `README.md` with detailed usage examples, including fetching and saving location data with optional live data fetching.
716
+ (See [CHANGELOG.md](CHANGELOG.md))
362
717
 
363
718
  ## Contributing
364
719
 
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CDEKApiClient
4
+ module API
5
+ # Handles courier-related API requests (agreements and intakes).
6
+ class Courier
7
+ # Initializes the Courier object.
8
+ #
9
+ # @param client [CDEKApiClient::Client] the client instance.
10
+ def initialize(client)
11
+ @client = client
12
+ end
13
+
14
+ # Creates a delivery agreement.
15
+ #
16
+ # @param agreement_data [CDEKApiClient::Entities::Agreement] the agreement data.
17
+ # @return [Hash] the response from the API.
18
+ def create_agreement(agreement_data)
19
+ response = @client.request('post', 'delivery', body: agreement_data)
20
+ handle_response(response)
21
+ end
22
+
23
+ # Gets agreement information by UUID.
24
+ #
25
+ # @param agreement_uuid [String] the UUID of the agreement.
26
+ # @return [Hash] the agreement information.
27
+ def get_agreement(agreement_uuid)
28
+ validate_uuid(agreement_uuid)
29
+ response = @client.request('get', "delivery/#{agreement_uuid}")
30
+ handle_response(response)
31
+ end
32
+
33
+ # Creates a courier intake request.
34
+ #
35
+ # @param intake_data [CDEKApiClient::Entities::Intakes] the intake data.
36
+ # @return [Hash] the response from the API.
37
+ def create_intake(intake_data)
38
+ response = @client.request('post', 'intakes', body: intake_data)
39
+ handle_response(response)
40
+ end
41
+
42
+ # Gets intake information by UUID.
43
+ #
44
+ # @param intake_uuid [String] the UUID of the intake.
45
+ # @return [Hash] the intake information.
46
+ def get_intake(intake_uuid)
47
+ validate_uuid(intake_uuid)
48
+ response = @client.request('get', "intakes/#{intake_uuid}")
49
+ handle_response(response)
50
+ end
51
+
52
+ # Deletes an intake request by UUID.
53
+ #
54
+ # @param intake_uuid [String] the UUID of the intake to delete.
55
+ # @return [Hash] the response from the API.
56
+ def delete_intake(intake_uuid)
57
+ validate_uuid(intake_uuid)
58
+ response = @client.request('delete', "intakes/#{intake_uuid}")
59
+ handle_response(response)
60
+ end
61
+
62
+ # Gets available days for courier intake.
63
+ #
64
+ # @param request_data [CDEKApiClient::Entities::IntakeAvailableDaysRequest] the request data.
65
+ # @return [Hash] the available days information.
66
+ def create_intake_available_days(request_data)
67
+ response = @client.request('post', 'intakes/availableDays', body: request_data)
68
+ handle_response(response)
69
+ end
70
+
71
+ # Gets available delivery intervals for an order.
72
+ #
73
+ # @param cdek_number [String, nil] the CDEK order number.
74
+ # @param order_uuid [String, nil] the order UUID.
75
+ # @return [Hash] the available delivery intervals.
76
+ def get_delivery_intervals(cdek_number: nil, order_uuid: nil)
77
+ query = {}
78
+ query[:cdek_number] = cdek_number if cdek_number
79
+ query[:order_uuid] = order_uuid if order_uuid
80
+
81
+ response = @client.request('get', 'delivery/intervals', query: query)
82
+ handle_response(response)
83
+ end
84
+
85
+ private
86
+
87
+ # Validates UUID format.
88
+ #
89
+ # @param uuid [String] the UUID to validate.
90
+ # @raise [ArgumentError] if the UUID format is invalid.
91
+ def validate_uuid(uuid)
92
+ @client.validate_uuid(uuid)
93
+ end
94
+
95
+ # Handles the response from the API.
96
+ #
97
+ # @param response [Net::HTTPResponse] the response from the API.
98
+ # @return [Hash] the parsed response.
99
+ def handle_response(response)
100
+ @client.send(:handle_response, response)
101
+ end
102
+ end
103
+ end
104
+ end
@@ -37,23 +37,23 @@ module CDEKApiClient
37
37
  use_live_data ? @client.request('get', 'deliverypoints') : read_data_from_file('offices_mapping.json')
38
38
  end
39
39
 
40
- # Retrieves a list of postal codes.
40
+ # Retrieves a list of postal codes for a specific city.
41
41
  #
42
- # @param city_code [String, nil] the city code to filter postal codes.
42
+ # @param city_code [String] the city code to filter postal codes.
43
43
  # @param use_live_data [Boolean] whether to use live data or cached data.
44
44
  # @return [Array<Hash>] list of postal codes.
45
- def postal_codes(city_code = nil, use_live_data: false)
46
- if use_live_data
47
- if city_code
48
- @client.request('get',
49
- "location/postalcodes?code=#{city_code}")
50
- else
51
- @client.request('get',
52
- 'location/postalcodes')
53
- end
54
- else
55
- read_data_from_file('postal_codes_mapping.json')
45
+ def postal_codes(city_code, use_live_data: false)
46
+ if city_code.nil? || (city_code.respond_to?(:empty?) && city_code.empty?)
47
+ raise ArgumentError,
48
+ 'city_code is required'
56
49
  end
50
+
51
+ response = if use_live_data
52
+ @client.request('get', "location/postalcodes?code=#{city_code}")
53
+ else
54
+ read_data_from_file("postal_codes_#{city_code}_mapping.json")
55
+ end
56
+ response.is_a?(Hash) && response.key?('postal_codes') ? response['postal_codes'] : response
57
57
  end
58
58
 
59
59
  private
@@ -64,7 +64,7 @@ module CDEKApiClient
64
64
  # @return [Hash] the parsed JSON data from the file.
65
65
  def read_data_from_file(filename)
66
66
  file_path = File.join('data', filename)
67
- JSON.parse(File.read(file_path))
67
+ JSON.parse(File.read(file_path, encoding: 'UTF-8'))
68
68
  rescue StandardError => e
69
69
  @client.logger.error("Failed to read data from file: #{e.message}")
70
70
  { 'error' => e.message }