From c08f4d3d5ddefeeb1445ae3cff7884ce8deaf97e Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Mon, 14 Apr 2025 10:40:24 -0700 Subject: [PATCH 1/3] Use id_or_url as input of Presentation --- lib/gslide/models/presentation.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/gslide/models/presentation.rb b/lib/gslide/models/presentation.rb index 127573c..4a577e0 100644 --- a/lib/gslide/models/presentation.rb +++ b/lib/gslide/models/presentation.rb @@ -7,8 +7,14 @@ class Presentation attr_reader :id - def initialize(id, auth: nil) - @id = id + PRESENTATION_PATTERN = %r[/presentation/d/([a-zA-Z0-9-_]+)?] + + def initialize(id_or_url, auth: nil) + @id = if url_id = id_or_url.match(PRESENTATION_PATTERN) + url_id[1] + else + id + end @auth = auth end From 49485114bfa7d6ecb1a25718304ce74c7bece54c Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Mon, 14 Apr 2025 10:46:24 -0700 Subject: [PATCH 2/3] Add test for that --- lib/gslide/models/presentation.rb | 4 ++-- test/test_gslide.rb | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/gslide/models/presentation.rb b/lib/gslide/models/presentation.rb index 4a577e0..89dc3b3 100644 --- a/lib/gslide/models/presentation.rb +++ b/lib/gslide/models/presentation.rb @@ -7,13 +7,13 @@ class Presentation attr_reader :id - PRESENTATION_PATTERN = %r[/presentation/d/([a-zA-Z0-9-_]+)?] + PRESENTATION_PATTERN = %r[/presentation/d/([a-zA-Z0-9\-_]+)?] def initialize(id_or_url, auth: nil) @id = if url_id = id_or_url.match(PRESENTATION_PATTERN) url_id[1] else - id + id_or_url end @auth = auth end diff --git a/test/test_gslide.rb b/test/test_gslide.rb index 4d3b7e2..e3be75f 100644 --- a/test/test_gslide.rb +++ b/test/test_gslide.rb @@ -7,7 +7,17 @@ def test_that_it_has_a_version_number refute_nil ::Gslide::VERSION end - # def test_it_does_something_useful - # assert false - # end + def test_initialized_presentation_with_url + url = "https://docs.google.com/presentation/d/1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8/edit?usp=sharing" + + presentation = Gslide::Presentation.new(url, auth: nil) + assert_equal "1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8", presentation.id + end + + def test_initialized_with_presentation_id + presentation_id = "1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8" + + presentation = Gslide::Presentation.new(presentation_id, auth: nil) + assert_equal "1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8", presentation.id + end end From b5f2de52a944b5e82e3229c1fcc1d47a73f93ae4 Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Mon, 14 Apr 2025 10:47:34 -0700 Subject: [PATCH 3/3] This is more readable --- lib/gslide/models/presentation.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/gslide/models/presentation.rb b/lib/gslide/models/presentation.rb index 89dc3b3..325d18a 100644 --- a/lib/gslide/models/presentation.rb +++ b/lib/gslide/models/presentation.rb @@ -10,11 +10,7 @@ class Presentation PRESENTATION_PATTERN = %r[/presentation/d/([a-zA-Z0-9\-_]+)?] def initialize(id_or_url, auth: nil) - @id = if url_id = id_or_url.match(PRESENTATION_PATTERN) - url_id[1] - else - id_or_url - end + @id = (url_id = id_or_url.match(PRESENTATION_PATTERN)) ? url_id[1] : id_or_url @auth = auth end