Back

Step by Step Guide to Building a Google Slides API Integration in Ruby

Aug 2, 20247 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to spice up your presentations programmatically? Let's dive into the world of Google Slides API using the nifty google-apis-slides_v1 package. This guide will walk you through the process of creating, modifying, and managing Google Slides presentations with Ruby. Buckle up!

Prerequisites

Before we jump in, make sure you've got these bases covered:

  • A Ruby environment (you've got this, right?)
  • A Google Cloud project (create one if you haven't already)
  • API credentials (grab 'em from your Google Cloud Console)

Installation

First things first, let's get that gem installed:

gem install google-apis-slides_v1

Easy peasy!

Authentication

Time to get cozy with OAuth 2.0. Here's a quick snippet to get you started:

require 'google/apis/slides_v1' require 'googleauth' require 'googleauth/stores/file_token_store' OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze scope = 'https://www.googleapis.com/auth/presentations' client_id = Google::Auth::ClientId.from_file('path/to/client_secrets.json') token_store = Google::Auth::Stores::FileTokenStore.new(file: 'path/to/tokens.yaml') authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store) credentials = authorizer.get_credentials('default') if credentials.nil? url = authorizer.get_authorization_url(base_url: OOB_URI) puts "Open this URL in your browser and enter the resulting code:" puts url code = gets credentials = authorizer.get_and_store_credentials_from_code( user_id: 'default', code: code, base_url: OOB_URI ) end

Initializing the Slides API Client

Now that we're authenticated, let's create our Slides service object:

service = Google::Apis::SlidesV1::SlidesService.new service.authorization = credentials

Basic Operations

Creating a New Presentation

Let's make some slides!

presentation = Google::Apis::SlidesV1::Presentation.new(title: 'My Awesome Presentation') presentation = service.create_presentation(presentation) puts "Created presentation with ID: #{presentation.presentation_id}"

Opening an Existing Presentation

Already have a presentation? No problem:

presentation_id = 'your-presentation-id' presentation = service.get_presentation(presentation_id)

Adding a New Slide

Let's add a slide to our presentation:

requests = [ { create_slide: { object_id_prop: 'my-unique-slide-id', insertion_index: 1, slide_layout_reference: { predefined_layout: 'TITLE_AND_TWO_COLUMNS' } } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))

Working with Slide Elements

Adding Text Boxes

Time to add some text:

requests = [ { create_shape: { object_id_prop: 'my-text-box', shape_type: 'TEXT_BOX', element_properties: { page_object_id: 'my-unique-slide-id', size: { width: { magnitude: 300, unit: 'PT' }, height: { magnitude: 100, unit: 'PT' } }, transform: { scale_x: 1, scale_y: 1, translate_x: 350, translate_y: 100, unit: 'PT' } } } }, { insert_text: { object_id_prop: 'my-text-box', text: 'Hello, Google Slides API!' } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))

Inserting Images

Let's add some visual flair:

requests = [ { create_image: { url: 'https://example.com/image.jpg', element_properties: { page_object_id: 'my-unique-slide-id', size: { width: { magnitude: 300, unit: 'PT' }, height: { magnitude: 300, unit: 'PT' } }, transform: { scale_x: 1, scale_y: 1, translate_x: 350, translate_y: 100, unit: 'PT' } } } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))

Modifying Slide Content

Updating Text

Need to change that text? No sweat:

requests = [ { delete_text: { object_id_prop: 'my-text-box', text_range: { type: 'ALL' } } }, { insert_text: { object_id_prop: 'my-text-box', text: 'Updated text!' } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))

Advanced Operations

Applying Templates

Want to use a template? Here's how:

template_presentation_id = 'your-template-id' requests = [ { apply_presentation_theme: { theme_id: template_presentation_id } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))

Error Handling and Best Practices

Always wrap your API calls in error handling:

begin # Your API call here rescue Google::Apis::Error => e puts "An error occurred: #{e.message}" end

And remember, batch your requests when possible to improve performance!

Conclusion

There you have it! You're now equipped to create stunning presentations programmatically with Ruby and the Google Slides API. The possibilities are endless, so go forth and automate those slides!

For more in-depth info, check out the official Google Slides API documentation. Happy coding!