diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 43a1c0e..7925fc6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,9 +15,10 @@ jobs: matrix: ruby: - '3.3.7' + - '3.4' steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 68419b3..40c8b38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [0.1.3] - 2026-08-19 + +- Add `Gslide::Presentation::PRESENTATION_ID_PATTERN` and `Gslide::Presentation.file_id_in`, so callers can validate a presentation id without repeating the pattern + ## [0.1.2] - 2025-05-09 - Add `Gslide::HTTPError`, `Gslide::UnauthorizedError` and `Gslide::QuotaExceededError` #10 diff --git a/Gemfile.lock b/Gemfile.lock index 2008a09..0d81694 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - gslide (0.1.2) + gslide (0.1.3) GEM remote: https://rubygems.org/ diff --git a/lib/gslide/models/presentation.rb b/lib/gslide/models/presentation.rb index 055dd4c..824afea 100644 --- a/lib/gslide/models/presentation.rb +++ b/lib/gslide/models/presentation.rb @@ -7,10 +7,17 @@ class Presentation attr_reader :id - PRESENTATION_PATTERN = %r[/presentation/d/([a-zA-Z0-9\-_]+)?] + PRESENTATION_ID_PATTERN = /[a-zA-Z0-9\-_]+/ + PRESENTATION_PATTERN = %r[/presentation/d/(#{PRESENTATION_ID_PATTERN})?] + + # @param [String] id_or_url a presentation id, or a sharing url holding one. + # @return [String] the presentation id, or the argument when it is not a url. + def self.file_id_in(id_or_url) + (url_id = id_or_url.match(PRESENTATION_PATTERN)) ? url_id[1] : id_or_url + end def initialize(id_or_url, auth: nil) - @id = (url_id = id_or_url.match(PRESENTATION_PATTERN)) ? url_id[1] : id_or_url + @id = self.class.file_id_in(id_or_url) @auth = auth end diff --git a/lib/gslide/version.rb b/lib/gslide/version.rb index 85d7498..e6000cd 100644 --- a/lib/gslide/version.rb +++ b/lib/gslide/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Gslide - VERSION = "0.1.2" + VERSION = "0.1.3" end diff --git a/test/test_gslide.rb b/test/test_gslide.rb index e3be75f..93d177f 100644 --- a/test/test_gslide.rb +++ b/test/test_gslide.rb @@ -20,4 +20,21 @@ def test_initialized_with_presentation_id presentation = Gslide::Presentation.new(presentation_id, auth: nil) assert_equal "1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8", presentation.id end + + def test_file_id_in_url + url = "https://docs.google.com/presentation/d/1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8/edit?usp=sharing" + + assert_equal "1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8", Gslide::Presentation.file_id_in(url) + end + + def test_file_id_in_id + presentation_id = "1W11pzmSEH7EoZXiMITa-cOM1y9Ym6Yby9pj_2l5NPm8" + + assert_equal presentation_id, Gslide::Presentation.file_id_in(presentation_id) + end + + def test_presentation_id_pattern_matches_a_file_id + assert_match(/\A#{Gslide::Presentation::PRESENTATION_ID_PATTERN}\z/, "1W11pzmSEH7EoZ-XiMITa_cOM") + refute_match(/\A#{Gslide::Presentation::PRESENTATION_ID_PATTERN}\z/, "javascript:alert(1)") + end end