From 7a5ec96e4d143279fdda9faa11ee1df43a98275b Mon Sep 17 00:00:00 2001 From: mroloux Date: Mon, 15 Jul 2024 11:55:01 +0200 Subject: [PATCH] Allow fine-grained control over the expanded chart fields (#115) --- lib/seatsio/charts.rb | 25 +++++++++++++++++++++---- lib/seatsio/events.rb | 2 +- lib/seatsio/pagination/cursor.rb | 2 +- test/charts/list_all_charts_test.rb | 27 ++++++++++++--------------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/lib/seatsio/charts.rb b/lib/seatsio/charts.rb index b64a6f4..d4e94b6 100644 --- a/lib/seatsio/charts.rb +++ b/lib/seatsio/charts.rb @@ -113,17 +113,34 @@ def publish_draft_version(chart_key) @http_client.post("charts/#{chart_key}/version/draft/actions/publish") end - def list(chart_filter: nil, tag: nil, expand_events: nil, with_validation: false) + def list(chart_filter: nil, tag: nil, expand_events: false, expand_validation: false, expand_venue_type: false) cursor = Pagination::Cursor.new(Chart, 'charts', @http_client) cursor.set_query_param('filter', chart_filter) cursor.set_query_param('tag', tag) - cursor.set_query_param('expand', 'events') if expand_events - cursor.set_query_param('validation', with_validation) if with_validation - + expand_params = list_expand_params(expand_events, expand_validation, expand_venue_type) + cursor.set_query_param('expand', expand_params) unless expand_params.empty? cursor end + def list_expand_params(expand_events, expand_validation, expand_venue_type) + result = [] + + if expand_events + result.push('events') + end + + if expand_validation + result.push('validation') + end + + if expand_venue_type + result.push('venueType') + end + + result + end + def list_all_tags response = @http_client.get('charts/tags') response['tags'] diff --git a/lib/seatsio/events.rb b/lib/seatsio/events.rb index 93d816c..b5f2ee1 100644 --- a/lib/seatsio/events.rb +++ b/lib/seatsio/events.rb @@ -64,7 +64,7 @@ def retrieve_object_info(key:, label:) def retrieve_object_infos(key:, labels:) url = "events/#{key}/objects" - query_params = URI.encode_www_form(labels.map { |label| ['label', label] }) + query_params = URI.encode_www_form({ label: labels }) response = @http_client.get(url, query_params) response.each do |key, value| response[key] = EventObjectInfo.new(value) diff --git a/lib/seatsio/pagination/cursor.rb b/lib/seatsio/pagination/cursor.rb index 4975079..39dae15 100644 --- a/lib/seatsio/pagination/cursor.rb +++ b/lib/seatsio/pagination/cursor.rb @@ -73,7 +73,7 @@ def keep_running? end def fetch_next_page - response = @http_client.get(@endpoint, @params) + response = @http_client.get(@endpoint, URI.encode_www_form(@params)) if response.nil? || response['items'].empty? @last_response_empty = true diff --git a/test/charts/list_all_charts_test.rb b/test/charts/list_all_charts_test.rb index 60d58c7..84c2000 100644 --- a/test/charts/list_all_charts_test.rb +++ b/test/charts/list_all_charts_test.rb @@ -51,40 +51,37 @@ def test_tag_and_filter assert_equal([chart1.key], keys) end - def test_expand + def test_expand_all chart = @seatsio.charts.create event1 = @seatsio.events.create chart_key: chart.key event2 = @seatsio.events.create chart_key: chart.key - retrieved_charts = @seatsio.charts.list(expand_events: true).to_a + retrieved_charts = @seatsio.charts.list(expand_events: true, expand_validation: true, expand_venue_type: true).to_a assert_instance_of(Seatsio::Event, retrieved_charts[0].events[0]) event_ids = retrieved_charts[0].events.collect {|event| event.id} assert_equal([event2.id, event1.id], event_ids) + assert_equal('MIXED', retrieved_charts[0].venue_type) + assert_not_nil(retrieved_charts[0].validation) end - def test_with_validation - @seatsio.charts.create - - retrieved_charts = @seatsio.charts.list(with_validation: true).to_a - - assert_equal({"errors" => [], "warnings" => []}, retrieved_charts[0].validation) - end - - def test_without_validation - @seatsio.charts.create + def test_expand_none + chart = @seatsio.charts.create + event1 = @seatsio.events.create chart_key: chart.key + event2 = @seatsio.events.create chart_key: chart.key - retrieved_charts = @seatsio.charts.list().to_a + retrieved_charts = @seatsio.charts.list.to_a - assert_equal(nil, retrieved_charts[0].validation) + assert_nil(retrieved_charts[0].events) + assert_nil(retrieved_charts[0].validation) + assert_nil(retrieved_charts[0].venue_type) end def test_without_charts charts = @seatsio.charts.list.to_a assert_equal([], charts) - end def test_chart_amount