Class: OxTenderAbstract::Client
- Inherits:
-
Object
- Object
- OxTenderAbstract::Client
- Includes:
- ContextualLogger
- Defined in:
- lib/oxtenderabstract/client.rb
Overview
Main client for working with Zakupki SOAP API
Instance Method Summary collapse
-
#download_archive_data(archive_url) ⇒ Object
Download and parse archive data.
-
#enhanced_search_tenders(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, include_attachments: true) ⇒ Object
Enhanced search tenders with detailed information extraction.
-
#extract_attachments_from_xml(xml_content) ⇒ Object
Extract attachments info from XML.
-
#get_docs_by_reestr_number(reestr_number:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM) ⇒ Object
Get documents by registry number.
-
#get_docs_by_region(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE) ⇒ Object
Get documents by region and date.
-
#initialize(token: nil) ⇒ Client
constructor
A new instance of Client.
-
#parse_xml_document(xml_content) ⇒ Object
Parse XML document.
-
#search_tenders(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, include_attachments: true) ⇒ Object
Search tenders with full workflow: API -> Archive -> Parse.
-
#search_tenders_with_resume(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, start_from_archive: 0, resume_state: nil, include_attachments: true) ⇒ Object
Search tenders with automatic resume capability Позволяет продолжить загрузку с места паузы при блокировках API.
Methods included from ContextualLogger
included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(token: nil) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 |
# File 'lib/oxtenderabstract/client.rb', line 20 def initialize(token: nil) @token = token || OxTenderAbstract.configuration.token @xml_parser = XmlParser.new @archive_processor = ArchiveProcessor.new validate_token! end |
Instance Method Details
#download_archive_data(archive_url) ⇒ Object
Download and parse archive data
63 64 65 |
# File 'lib/oxtenderabstract/client.rb', line 63 def download_archive_data(archive_url) @archive_processor.download_and_extract(archive_url) end |
#enhanced_search_tenders(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, include_attachments: true) ⇒ Object
Enhanced search tenders with detailed information extraction
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/oxtenderabstract/client.rb', line 175 def enhanced_search_tenders(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, include_attachments: true) log_info "Starting enhanced tender search for region #{org_region}, date #{exact_date}" # Step 1: Get archive URLs from API api_result = get_docs_by_region( org_region: org_region, subsystem_type: subsystem_type, document_type: document_type, exact_date: exact_date ) return api_result if api_result.failure? archive_urls = api_result.data[:archive_urls] return Result.success({ tenders: [], total_archives: 0, total_files: 0 }) if archive_urls.empty? log_info "Found #{archive_urls.size} archives to process" # Step 2: Process each archive with detailed information extraction all_tenders = [] total_files = 0 archive_urls.each_with_index do |archive_url, index| log_info "Processing archive #{index + 1}/#{archive_urls.size}" archive_result = download_archive_data(archive_url) next if archive_result.failure? files = archive_result.data[:files] total_files += files.size # Step 3: Parse XML files from archive with enhanced data extraction xml_files = files.select { |name, _| name.downcase.end_with?('.xml') } xml_files.each do |file_name, file_data| parse_result = parse_xml_document(file_data[:content]) next if parse_result.failure? next unless parse_result.data[:document_type] == :tender tender_data = parse_result.data[:content] next if tender_data[:reestr_number].nil? || tender_data[:reestr_number].empty? # Step 4: Extract additional detailed information if = (file_data[:content]) if .success? tender_data[:attachments] = .data[:attachments] tender_data[:attachments_count] = .data[:total_count] end end # Add metadata tender_data[:source_file] = file_name tender_data[:archive_url] = archive_url tender_data[:processed_at] = Time.now all_tenders << tender_data end end log_info "Enhanced search completed. Found #{all_tenders.size} tenders in #{total_files} files" Result.success({ tenders: all_tenders, total_archives: archive_urls.size, total_files: total_files, processed_at: Time.now, enhanced: true }) end |
#extract_attachments_from_xml(xml_content) ⇒ Object
Extract attachments info from XML
73 74 75 |
# File 'lib/oxtenderabstract/client.rb', line 73 def (xml_content) @xml_parser.(xml_content) end |
#get_docs_by_reestr_number(reestr_number:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM) ⇒ Object
Get documents by registry number
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/oxtenderabstract/client.rb', line 42 def get_docs_by_reestr_number(reestr_number:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM) validate_params!({ reestr_number: reestr_number, subsystem_type: subsystem_type }) request_data = build_reestr_request(reestr_number, subsystem_type) log_info "Requesting documents for registry number: #{reestr_number}, type: #{subsystem_type}" result = execute_soap_request(:get_docs_by_reestr_number, request_data) if result.success? log_info "Success response for #{reestr_number}. Found archives: #{result.data[:archive_urls]&.size || 0}" else log_error "Error for #{reestr_number}: #{result.error}" end result end |
#get_docs_by_region(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE) ⇒ Object
Get documents by region and date
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/oxtenderabstract/client.rb', line 28 def get_docs_by_region(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE) validate_params!({ org_region: org_region, subsystem_type: subsystem_type, document_type: document_type, exact_date: exact_date }) request_data = build_region_request(org_region, subsystem_type, document_type, exact_date) execute_soap_request(:get_docs_by_org_region, request_data) end |
#parse_xml_document(xml_content) ⇒ Object
Parse XML document
68 69 70 |
# File 'lib/oxtenderabstract/client.rb', line 68 def parse_xml_document(xml_content) @xml_parser.parse(xml_content) end |
#search_tenders(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, include_attachments: true) ⇒ Object
Search tenders with full workflow: API -> Archive -> Parse
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/oxtenderabstract/client.rb', line 78 def search_tenders(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, include_attachments: true) log_info "Starting tender search for region #{org_region}, date #{exact_date}, subsystem: #{subsystem_type}, type: #{document_type}" # Step 1: Get archive URLs from API api_result = get_docs_by_region( org_region: org_region, subsystem_type: subsystem_type, document_type: document_type, exact_date: exact_date ) return api_result if api_result.failure? archive_urls = api_result.data[:archive_urls] return Result.success({ tenders: [], total_archives: 0, total_files: 0 }) if archive_urls.empty? log_info "Found #{archive_urls.size} archives to process" # Step 2: Process each archive with error resilience all_tenders = [] total_files = 0 processed_archives = 0 failed_archives = 0 archive_urls.each_with_index do |archive_url, index| log_info "Processing archive #{index + 1}/#{archive_urls.size}" begin archive_result = download_archive_data(archive_url) if archive_result.failure? log_error "Failed to download archive #{index + 1}: #{archive_result.error}" failed_archives += 1 next end processed_archives += 1 files = archive_result.data[:files] total_files += files.size # Step 3: Parse XML files from archive xml_files = files.select { |name, _| name.downcase.end_with?('.xml') } log_debug "Found #{xml_files.size} XML files in archive #{index + 1}" xml_files.each do |file_name, file_data| parse_result = parse_xml_document(file_data[:content]) if parse_result.failure? log_debug "Failed to parse #{file_name}: #{parse_result.error}" next end next unless parse_result.data[:document_type] == :tender tender_data = parse_result.data[:content] next if tender_data[:reestr_number].nil? || tender_data[:reestr_number].empty? # Extract attachments if requested if = (file_data[:content]) if .success? tender_data[:attachments] = .data[:attachments] tender_data[:attachments_count] = .data[:total_count] end end # Add metadata tender_data[:source_file] = file_name tender_data[:archive_url] = archive_url tender_data[:processed_at] = Time.now all_tenders << tender_data rescue StandardError => e log_error "Error processing file #{file_name}: #{e.}" # Continue with other files end rescue StandardError => e log_error "Critical error processing archive #{index + 1}: #{e.}" failed_archives += 1 # Continue with other archives end end log_info "Search completed. Processed: #{processed_archives}/#{archive_urls.size} archives, Failed: #{failed_archives}, Found #{all_tenders.size} tenders in #{total_files} files" Result.success({ tenders: all_tenders, total_archives: archive_urls.size, processed_archives: processed_archives, failed_archives: failed_archives, total_files: total_files, processed_at: Time.now }) end |
#search_tenders_with_resume(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, start_from_archive: 0, resume_state: nil, include_attachments: true) ⇒ Object
Search tenders with automatic resume capability Позволяет продолжить загрузку с места паузы при блокировках API
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/oxtenderabstract/client.rb', line 250 def search_tenders_with_resume(org_region:, exact_date:, subsystem_type: DocumentTypes::DEFAULT_SUBSYSTEM, document_type: DocumentTypes::DEFAULT_DOCUMENT_TYPE, start_from_archive: 0, resume_state: nil, include_attachments: true) log_info "Starting tender search with resume capability for region #{org_region}, date #{exact_date}" log_info "Starting from archive #{start_from_archive}" if start_from_archive > 0 # Восстанавливаем состояние если есть if resume_state log_info "Resuming from previous state: #{resume_state[:processed_archives]} archives processed" all_tenders = resume_state[:tenders] || [] total_files = resume_state[:total_files] || 0 processed_archives = resume_state[:processed_archives] || 0 failed_archives = resume_state[:failed_archives] || 0 archive_urls = resume_state[:archive_urls] else # Step 1: Get archive URLs from API api_result = get_docs_by_region( org_region: org_region, subsystem_type: subsystem_type, document_type: document_type, exact_date: exact_date ) return api_result if api_result.failure? archive_urls = api_result.data[:archive_urls] return Result.success({ tenders: [], total_archives: 0, total_files: 0 }) if archive_urls.empty? all_tenders = [] total_files = 0 processed_archives = 0 failed_archives = 0 end log_info "Found #{archive_urls.size} archives to process (starting from #{start_from_archive})" # Step 2: Process archives starting from specified position (start_from_archive...archive_urls.size).each do |index| archive_url = archive_urls[index] log_info "Processing archive #{index + 1}/#{archive_urls.size}" begin archive_result = download_archive_data(archive_url) if archive_result.failure? # Проверяем, была ли блокировка с автоматическим ожиданием if archive_result.[:error_type] == :blocked && !OxTenderAbstract.configuration.auto_wait_on_block # Возвращаем состояние для возможности продолжения resume_state = { tenders: all_tenders, total_files: total_files, processed_archives: processed_archives, failed_archives: failed_archives, archive_urls: archive_urls, next_archive_index: index } return Result.failure( "Archive download blocked, can resume from archive #{index + 1}", error_type: :blocked, retry_after: 600, resume_state: resume_state ) else log_error "Failed to download archive #{index + 1}: #{archive_result.error}" failed_archives += 1 next end end processed_archives += 1 files = archive_result.data[:files] total_files += files.size # Step 3: Parse XML files from archive xml_files = files.select { |name, _| name.downcase.end_with?('.xml') } log_debug "Found #{xml_files.size} XML files in archive #{index + 1}" xml_files.each do |file_name, file_data| parse_result = parse_xml_document(file_data[:content]) if parse_result.failure? log_debug "Failed to parse #{file_name}: #{parse_result.error}" next end next unless parse_result.data[:document_type] == :tender tender_data = parse_result.data[:content] next if tender_data[:reestr_number].nil? || tender_data[:reestr_number].empty? # Extract attachments if requested if = (file_data[:content]) if .success? tender_data[:attachments] = .data[:attachments] tender_data[:attachments_count] = .data[:total_count] end end # Add metadata tender_data[:source_file] = file_name tender_data[:archive_url] = archive_url tender_data[:processed_at] = Time.now tender_data[:archive_index] = index all_tenders << tender_data rescue StandardError => e log_error "Error processing file #{file_name}: #{e.}" # Continue with other files end rescue StandardError => e log_error "Critical error processing archive #{index + 1}: #{e.}" failed_archives += 1 # Continue with other archives end end log_info "Search completed. Processed: #{processed_archives}/#{archive_urls.size} archives, Failed: #{failed_archives}, Found #{all_tenders.size} tenders in #{total_files} files" Result.success({ tenders: all_tenders, total_archives: archive_urls.size, processed_archives: processed_archives, failed_archives: failed_archives, total_files: total_files, processed_at: Time.now, completed: true }) end |