From 95fb49137f00f221670be71b6ce318edd0a09e6f Mon Sep 17 00:00:00 2001 From: Emma Flock Date: Fri, 6 Mar 2020 15:01:26 -0800 Subject: [PATCH] Add method to optionally process corrupt data. Provide option in select methods --- lib/booker/client.rb | 33 ++++++++++++++++++++++++++++++--- lib/booker/v4/business_rest.rb | 15 +++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/booker/client.rb b/lib/booker/client.rb index 29757e3..fcf39db 100644 --- a/lib/booker/client.rb +++ b/lib/booker/client.rb @@ -84,7 +84,7 @@ def delete(path, params=nil, body=nil, booker_model=nil) build_resources(booker_resources, booker_model) end - def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true) + def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true, skip_corrupt_pages_without_errors: false) page_size = params[:PageSize] page_number = params[:PageNumber] @@ -98,8 +98,13 @@ def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_al unless results.is_a?(Array) error_msg = "Result from paginated request to #{path} with params: #{params} is not a collection" - raise Booker::MidPaginationError.new(message: error_msg, error_occurred_during_params: params, - results_fetched_prior_to_error: fetched) + + if skip_corrupt_pages_without_errors + results = process_non_corrupt_pages(method: method, path: path, params: params, model: model) + else + raise Booker::MidPaginationError.new(message: error_msg, error_occurred_during_params: params, + results_fetched_prior_to_error: fetched) + end end fetched.concat(results) @@ -119,6 +124,28 @@ def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_al end end + def process_non_corrupt_pages(method:, path:, params:, model: nil) + initial_page_size = params[:PageSize] + initial_page_number = params[:PageNumber] + return [] unless initial_page_size > 1 # if pagesize is one, there are no other non-corrupt pages + + non_corrupt_data = [] + pages_processed = (initial_page_number * initial_page_size) + new_params = params.deep_dup + new_params[:PageSize] = 1 # set to 1 to search for singular pages with noncorrupt data. Optimize noncorrupt data. + + initial_page_size.times do + new_params[:PageNumber] = (pages_processed += 1) + + single_page_results = paginated_request(method: method, path: path, params: new_params, model: model, fetch_all: false) + single_page_results = [] unless results.is_a?(Array) # if page is corrupted: skip + + non_corrupt_data.concat(single_page_results) + end + + non_corrupt_data + end + def get_booker_resources(http_method, path, params=nil, body=nil, booker_model=nil) http_options = request_options(params, body) url = full_url(path) diff --git a/lib/booker/v4/business_rest.rb b/lib/booker/v4/business_rest.rb index 0bfccc4..0dcce12 100644 --- a/lib/booker/v4/business_rest.rb +++ b/lib/booker/v4/business_rest.rb @@ -41,23 +41,25 @@ def find_employees(booker_location_id:, fetch_all: true, params: {}) ) end - def find_orders(booker_location_id:, fetch_all: true, params: {}) + def find_orders(booker_location_id:, fetch_all: true, params: {}, skip_corrupt_pages_without_errors: false) paginated_request( method: :post, path: '/orders', params: build_params({LocationID: booker_location_id}, params, true), model: Booker::V4::Models::Order, - fetch_all: fetch_all + fetch_all: fetch_all, + skip_corrupt_pages_without_errors: skip_corrupt_pages_without_errors ) end - def find_orders_partial(booker_location_id:, fetch_all: true, params: {}) + def find_orders_partial(booker_location_id:, fetch_all: true, params: {}, skip_corrupt_pages_without_errors: false) paginated_request( method: :post, path: '/orders/partial', params: build_params({LocationID: booker_location_id}, params, true), model: Booker::V4::Models::Order, - fetch_all: fetch_all + fetch_all: fetch_all, + skip_corrupt_pages_without_errors: skip_corrupt_pages_without_errors ) end @@ -71,7 +73,7 @@ def find_treatments(booker_location_id:, fetch_all: true, params: {}) ) end - def find_customers(booker_location_id:, fetch_all: true, params: {}) + def find_customers(booker_location_id:, fetch_all: true, params: {}, skip_corrupt_pages_without_errors: false) additional_params = { 'FilterByExactLocationID' => true, 'LocationID' => booker_location_id, @@ -83,7 +85,8 @@ def find_customers(booker_location_id:, fetch_all: true, params: {}) path: '/customers', params: build_params(additional_params, params, true), model: Booker::V4::Models::Customer, - fetch_all: fetch_all + fetch_all: fetch_all, + skip_corrupt_pages_without_errors: skip_corrupt_pages_without_errors ) end