gslide 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b59859f25a2ec7a2e3477e43f1fd9130c2f39cac573da00949db781721e0c00
4
+ data.tar.gz: 3931e3260c7230dc6661baab6b5411c372aef15732a336738dc829f5fa27794f
5
+ SHA512:
6
+ metadata.gz: 7c04e204a3469bbaf902cc32f631476ca8af0d20138f38cfcf146d21ecb0491831333a262418b0eb3e5d95b1d3ab498f1de037d45b7fb34e6bf6360e08b9e5ba
7
+ data.tar.gz: d79224b147b6f6f6dabbfedd9e3f1ab2d49093b2e7622e9333edbe1c41a1d2b59e16b099230ceeee5ff4be11467fc4343971ec444b611c9fe800d30839e39e80
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-03-31
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Kang-Kyu Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Gslide
2
+
3
+ Welcome to Gslide! This is an approach to build a Google Slides file for practical use.
4
+
5
+ ## Installation
6
+
7
+ > Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add gslide --github 'lininglink/gslide'
13
+ bundle install
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ - Get a token with related scopes: `presentations` `presentations.readonly` `drive` `drive.readonly` `drive.file`.
19
+ - Get it from Google Developers [OAuth 2.0 Playground](https://developers.google.com/oauthplayground) for test.
20
+
21
+ ```rb
22
+ token = "ya29.a0AXeO8..."
23
+ id = "1nNv3fyCIrvAs754Zd5c6klCjMhP7D2rmX3T9nFpjW4Q"
24
+ editor = Gslide::Editor.new(token)
25
+ pres = Gslide::Presentation.new(id, auth: editor)
26
+ pres.get
27
+ #=> parsed json of the Google Slides file
28
+ ```
29
+
30
+ ```rb
31
+ editor = Gslide::Editor.new(token)
32
+ editor.insert_presentation(title: "Once upon a time")
33
+ #=> <Gslide::Models::Presentation:0x000000013e6d4108
34
+ # @id="1itXwmlLbyTRa1QKhrsO16OhJPEJUK6eh2d1nqsQfuqY">
35
+ pres = _
36
+ ```
37
+
38
+ ```rb
39
+ pres.batch_update({
40
+ requests: [
41
+ {
42
+ create_slide: {
43
+ slide_layout_reference: {
44
+ predefined_layout: "BLANK"
45
+ }
46
+ }
47
+ }
48
+ ]
49
+ })
50
+ #=> true
51
+
52
+ pres.get_slide_ids
53
+ #=> ["p", "SLIDES_API778986045_0"]
54
+
55
+ pres.batch_update({
56
+ requests: [
57
+ {
58
+ create_image: {
59
+ url: "https://drive.usercontent.google.com/download?id=1fus5psRLzIJjG3A5GAfbqu22cdEczNZQ&authuser=0",
60
+ element_properties: {
61
+ page_object_id: "SLIDES_API778986045_0",
62
+ size: {
63
+ height: { magnitude: 200, unit: "PT" },
64
+ width: { magnitude: 300, unit: "PT" }
65
+ },
66
+ transform: {
67
+ scale_x: 1,
68
+ scale_y: 1,
69
+ translate_x: 100,
70
+ translate_y: 100,
71
+ unit: "PT"
72
+ }
73
+ }
74
+ }
75
+ }
76
+ ]
77
+ })
78
+ #=> true
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ Install the gem (a local copy) and add to the application's Gemfile by executing:
86
+
87
+ ```bash
88
+ bundle add gslide --path '../gslide'
89
+ ```
90
+
91
+ To install this gem onto your local machine, run `bundle install`.
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lininglink/gslide. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/lininglink/gslide/blob/master/CODE_OF_CONDUCT.md).
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
100
+
101
+ ## Code of Conduct
102
+
103
+ Everyone interacting in the Gslide project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/lininglink/gslide/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,28 @@
1
+ require "net/http"
2
+
3
+ module Gslide
4
+ module Concerns
5
+ module Requests
6
+ def get_request(uri, auth_token: "")
7
+ req = Net::HTTP::Get.new(uri.to_s)
8
+ req['Authorization'] = "Bearer #{auth_token}"
9
+
10
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
11
+ http.request(req)
12
+ end
13
+ end
14
+
15
+ def post_request(uri, auth_token: "", body: {})
16
+ req = Net::HTTP::Post.new(uri.to_s)
17
+ req.initialize_http_header 'Content-Type' => 'application/json'
18
+ req['Authorization'] = "Bearer #{auth_token}"
19
+
20
+ req.body = body
21
+
22
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
23
+ http.request(req)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ require "gslide/concerns/requests"
2
+
3
+ module Gslide
4
+ module Models
5
+
6
+ class Editor
7
+ include Concerns::Requests
8
+
9
+ attr_reader :token
10
+
11
+ def initialize(token)
12
+ if token.to_s.empty?
13
+ raise ArgumentError.new("Auth Token must be present")
14
+ end
15
+
16
+ @token = token
17
+ end
18
+
19
+ # @param [Hash] options the body for the POST request.
20
+ # @return [Gslide::Models::Presentation] presentation id.
21
+ # @see https://developers.google.com/slides/api/reference/rest/v1/presentations/create#request-body
22
+ def insert_presentation(options = {})
23
+ uri = URI(GOOGLE_SLIDES + "")
24
+
25
+ # "title" is the only allowed field in the request body
26
+ res = post_request(uri, auth_token: @token, body: options.to_json)
27
+ response_body = JSON(res.body)
28
+
29
+ if response_body["error"]
30
+ raise Gslide::Error.new(response_body["error"]["message"])
31
+ end
32
+ Presentation.new response_body["presentationId"], auth: self
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ require "gslide/models/editor"
2
+ require "gslide/models/presentation"
3
+ require "gslide/models/spreadsheet"
4
+
5
+ module Gslide
6
+ module Models
7
+ GOOGLE_SLIDES = "https://slides.googleapis.com/v1/presentations"
8
+ GOOGLE_SHEETS = "https://sheets.googleapis.com/v4/spreadsheets"
9
+ end
10
+
11
+ include Models
12
+ end
@@ -0,0 +1,50 @@
1
+ require "gslide/concerns/requests"
2
+
3
+ module Gslide
4
+ module Models
5
+ class Presentation
6
+ include Concerns::Requests
7
+
8
+ attr_reader :id
9
+
10
+ def initialize(id, auth: nil)
11
+ @id = id
12
+ @auth = auth
13
+ end
14
+
15
+ # @return [Hash] data from a Google Slides presentation.
16
+ # @see https://developers.google.com/slides/api/reference/rest/v1/presentations/get
17
+ def get
18
+ uri = URI(GOOGLE_SLIDES + "/#{@id}")
19
+
20
+ res = get_request(uri, auth_token: @auth.token)
21
+ JSON(res.body).convert_keys {|k| k.snake_case.to_sym }
22
+ end
23
+
24
+ def link_url
25
+ "https://docs.google.com/presentation/d/#{@id}/edit"
26
+ end
27
+
28
+ # @param [Hash] options the request body.
29
+ # @return True when update successful.
30
+ # @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate#request-body
31
+ def batch_update(options = {})
32
+ uri = URI(GOOGLE_SLIDES + "/#{@id}:batchUpdate")
33
+ request_body = options.convert_keys { |k| k.to_s.lower_camel_case }.to_json
34
+
35
+ res = post_request(uri, auth_token: @auth.token, body: request_body)
36
+ response_body = JSON(res.body)
37
+
38
+ if response_body["error"]
39
+ raise Gslide::Error.new(response_body["error"]["message"])
40
+ end
41
+ response_body["presentationId"] == @id
42
+ end
43
+
44
+ def get_slide_ids
45
+ parsed_body = get
46
+ parsed_body[:slides].collect { |slide| slide[:object_id] }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,88 @@
1
+ require "gslide/concerns/requests"
2
+
3
+ module Gslide
4
+ module Models
5
+ class Spreadsheet
6
+ include Concerns::Requests
7
+
8
+ attr_reader :id
9
+
10
+ def initialize(id, auth: nil)
11
+ @id = id
12
+ @auth = auth
13
+ end
14
+
15
+ # @return [Hash] data from a Google Sheets spreadsheet.
16
+ # @see https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets/get
17
+ def get
18
+ uri = URI(GOOGLE_SHEETS + "/#{@id}")
19
+
20
+ res = get_request(uri, auth_token: @auth.token)
21
+ JSON(res.body).convert_keys {|k| k.snake_case.to_sym }
22
+ end
23
+
24
+ def link_url
25
+ "https://docs.google.com/spreadsheets/d/#{@id}/edit"
26
+ end
27
+
28
+ # @param [Hash] options the request body.
29
+ # @return True when update successful.
30
+ # @see https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate
31
+ def batch_update(options = {})
32
+ uri = URI(GOOGLE_SHEETS + "/#{@id}:batchUpdate")
33
+ request_body = options.convert_keys { |k| k.to_s.lower_camel_case }.to_json
34
+
35
+ res = post_request(uri, auth_token: @auth.token, body: request_body)
36
+ response_body = JSON(res.body)
37
+
38
+ if response_body["error"]
39
+ raise Gslide::Error.new(response_body["error"]["message"])
40
+ end
41
+ response_body["spreadsheetId"] == @id
42
+ end
43
+
44
+ def get_sheets
45
+ parsed_body = get
46
+ parsed_body[:sheets].collect { |h| Sheet.new(h) }
47
+ end
48
+ end
49
+
50
+ class SpreadsheetDraft
51
+ include Concerns::Requests
52
+
53
+ def initialize(auth: nil)
54
+ @auth = auth
55
+ end
56
+
57
+ def create(options = {})
58
+ request_body = options.convert_keys { |k| k.to_s.lower_camel_case }.to_json
59
+
60
+ uri = URI(GOOGLE_SHEETS)
61
+ res = post_request(uri, auth_token: @auth.token, body: request_body)
62
+ response_body = JSON(res.body)
63
+ if response_body["error"]
64
+ raise Gslide::Error.new(response_body["error"]["message"])
65
+ end
66
+ spreadsheet_id = response_body["spreadsheetId"]
67
+ Spreadsheet.new(spreadsheet_id, auth: @auth)
68
+ end
69
+ end
70
+
71
+ class Sheet
72
+ attr_reader :id, :charts
73
+
74
+ def initialize(options = {})
75
+ @id = options[:properties][:sheet_id]
76
+ @charts = options[:charts].collect { |h| Chart.new(h) } if options[:charts]
77
+ end
78
+ end
79
+
80
+ class Chart
81
+ attr_reader :id
82
+
83
+ def initialize(options = {})
84
+ @id = options[:chart_id]
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gslide
4
+ VERSION = "0.1.0"
5
+ end
data/lib/gslide.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gslide/version"
4
+ require_relative "string"
5
+
6
+ require "gslide/models/models"
7
+
8
+ module Gslide
9
+ class Error < StandardError; end
10
+
11
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ def snake_case
5
+ gsub(/::/, '/').
6
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
7
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
8
+ tr("-", "_").
9
+ downcase
10
+ end
11
+
12
+ def lower_camel_case
13
+ split('_').inject { |m, p| m + p.capitalize }
14
+ end
15
+ end
16
+
17
+ class Object
18
+ def convert_keys(&blk)
19
+ case self
20
+ when Array
21
+ self.map { |v| v.convert_keys(&blk) }
22
+ when Hash
23
+ self.map { |k, v| [blk.call(k), v.convert_keys(&blk)] }.to_h
24
+ else
25
+ self
26
+ end
27
+ end
28
+ end
29
+
30
+ # Now, you can do options.convert_keys { |k| k.lower_camel_case }
data/sig/gslide.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Gslide
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gslide
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kang-Kyu Lee
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-03-31 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Allows to modify a Google Slides file by API requests.
13
+ email:
14
+ - kangkyu1111@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - CODE_OF_CONDUCT.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - lib/gslide.rb
25
+ - lib/gslide/concerns/requests.rb
26
+ - lib/gslide/models/editor.rb
27
+ - lib/gslide/models/models.rb
28
+ - lib/gslide/models/presentation.rb
29
+ - lib/gslide/models/spreadsheet.rb
30
+ - lib/gslide/version.rb
31
+ - lib/string.rb
32
+ - sig/gslide.rbs
33
+ homepage: https://github.com/lininglink/gslide
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ allowed_push_host: https://rubygems.org
38
+ homepage_uri: https://github.com/lininglink/gslide
39
+ source_code_uri: https://github.com/lininglink/gslide
40
+ changelog_uri: https://github.com/lininglink/gslide/blob/master/CHANGELOG.md
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.1.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.6.6
56
+ specification_version: 4
57
+ summary: Google Slides API client.
58
+ test_files: []