diff --git a/lib/gslide/models/presentation.rb b/lib/gslide/models/presentation.rb index 127573c..325d18a 100644 --- a/lib/gslide/models/presentation.rb +++ b/lib/gslide/models/presentation.rb @@ -7,8 +7,10 @@ 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 = (url_id = id_or_url.match(PRESENTATION_PATTERN)) ? url_id[1] : id_or_url @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