|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require "httparty" |
| 4 | +require "cgi" |
| 5 | +require "json" |
4 | 6 |
|
5 | 7 | require_relative "outscraper/version" |
6 | 8 |
|
@@ -391,5 +393,89 @@ def yellowpages_search(query, location: 'New York, NY', limit: 100, region: nil, |
391 | 393 | webhook: webhook |
392 | 394 | }).parsed_response['data'] |
393 | 395 | end |
| 396 | + |
| 397 | + def postAPIRequest(path, parameters = {}) |
| 398 | + payload = parameters || {} |
| 399 | + |
| 400 | + response = self.class.post( |
| 401 | + path, |
| 402 | + headers: { 'Content-Type' => 'application/json' }, |
| 403 | + body: payload.to_json |
| 404 | + ).parsed_response |
| 405 | + |
| 406 | + response.is_a?(Hash) && response.key?('data') ? response['data'] : response |
| 407 | + end |
| 408 | + |
| 409 | + def businessesSearch( |
| 410 | + filters: {}, |
| 411 | + limit: 10, |
| 412 | + include_total: false, |
| 413 | + cursor: nil, |
| 414 | + fields: nil, |
| 415 | + async_request: false, |
| 416 | + ui: false, |
| 417 | + webhook: nil |
| 418 | + ) |
| 419 | + payload = { |
| 420 | + filters: (filters || {}), |
| 421 | + limit: limit, |
| 422 | + include_total: include_total, |
| 423 | + cursor: cursor, |
| 424 | + fields: fields ? Array(fields) : nil, |
| 425 | + async: async_request, |
| 426 | + ui: ui, |
| 427 | + webhook: webhook |
| 428 | + }.compact |
| 429 | + |
| 430 | + postAPIRequest('/businesses', payload) |
| 431 | + end |
| 432 | + |
| 433 | + def businessesIterSearch(filters: {}, limit: 10, fields: nil, include_total: false) |
| 434 | + cursor = nil |
| 435 | + all_items = [] |
| 436 | + |
| 437 | + loop do |
| 438 | + page = businessesSearch( |
| 439 | + filters: filters, |
| 440 | + limit: limit, |
| 441 | + include_total: include_total, |
| 442 | + cursor: cursor, |
| 443 | + fields: fields, |
| 444 | + async_request: false |
| 445 | + ) |
| 446 | + |
| 447 | + items = page.is_a?(Hash) ? (page['items'] || []) : [] |
| 448 | + break if !items.is_a?(Array) || items.empty? |
| 449 | + |
| 450 | + all_items.concat(items) |
| 451 | + |
| 452 | + has_more = !!(page['has_more']) |
| 453 | + next_cursor = page['next_cursor'] |
| 454 | + break if !has_more || next_cursor.nil? || next_cursor.to_s.strip.empty? |
| 455 | + |
| 456 | + cursor = next_cursor.to_s |
| 457 | + end |
| 458 | + |
| 459 | + all_items |
| 460 | + end |
| 461 | + |
| 462 | + def businessesGet(business_id, fields: nil, async_request: false, ui: false, webhook: nil) |
| 463 | + raise ArgumentError, 'business_id is required' if business_id.nil? || business_id.to_s.strip.empty? |
| 464 | + |
| 465 | + fields_param = |
| 466 | + if fields.is_a?(Array) |
| 467 | + fields.map(&:to_s).join(',') |
| 468 | + else |
| 469 | + fields |
| 470 | + end |
| 471 | + |
| 472 | + response = self.class.get("/businesses/#{CGI.escape(business_id.to_s)}", query: { |
| 473 | + fields: fields_param, |
| 474 | + async: async_request, |
| 475 | + ui: ui, |
| 476 | + webhook: webhook |
| 477 | + }.compact).parsed_response |
| 478 | + response.is_a?(Hash) && response.key?('data') ? response['data'] : response |
| 479 | + end |
394 | 480 | end |
395 | 481 | end |
0 commit comments