Class: Gslide::Models::Presentation

Inherits:
Object
  • Object
show all
Includes:
Concerns::Requests
Defined in:
lib/gslide/models/presentation.rb

Constant Summary collapse

PRESENTATION_ID_PATTERN =
/[a-zA-Z0-9\-_]+/
PRESENTATION_PATTERN =
%r[/presentation/d/(#{PRESENTATION_ID_PATTERN})?]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Requests

#get_request, #post_request

Constructor Details

#initialize(id_or_url, auth: nil) ⇒ Presentation

Returns a new instance of Presentation.



19
20
21
22
# File 'lib/gslide/models/presentation.rb', line 19

def initialize(id_or_url, auth: nil)
  @id = self.class.file_id_in(id_or_url)
  @auth = auth
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/gslide/models/presentation.rb', line 8

def id
  @id
end

Class Method Details

.file_id_in(id_or_url) ⇒ String

Returns the presentation id, or the argument when it is not a url.

Parameters:

  • id_or_url (String)

    a presentation id, or a sharing url holding one.

Returns:

  • (String)

    the presentation id, or the argument when it is not a url.



15
16
17
# File 'lib/gslide/models/presentation.rb', line 15

def self.file_id_in(id_or_url)
  (url_id = id_or_url.match(PRESENTATION_PATTERN)) ? url_id[1] : id_or_url
end

Instance Method Details

#batch_update(options = {}) ⇒ Object

Returns True when update successful.

Parameters:

  • options (Hash) (defaults to: {})

    the request body.

Returns:

  • True when update successful.

See Also:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gslide/models/presentation.rb', line 40

def batch_update(options = {})
  retries ||= 0

  uri = URI(GOOGLE_SLIDES + "/#{@id}:batchUpdate")
  request_body = options.convert_keys { |k| k.to_s.lower_camel_case }.to_json

  response_body = post_request(uri, auth_token: @auth.token, body: request_body)
  response_body["presentationId"] == @id
rescue Gslide::QuotaExceededError
  if (retries += 1) < 3
    sleep 10 + retries * retries * 10

    retry
  end
  # all retries failed, raise exception
  raise
end

#getHash

Returns data from a Google Slides presentation.

Returns:

  • (Hash)

    data from a Google Slides presentation.

See Also:



26
27
28
29
30
31
# File 'lib/gslide/models/presentation.rb', line 26

def get
  uri = URI(GOOGLE_SLIDES + "/#{@id}")

  response_body = get_request(uri, auth_token: @auth.token)
  response_body.convert_keys {|k| k.snake_case.to_sym }
end

#get_slide_idsObject



58
59
60
61
# File 'lib/gslide/models/presentation.rb', line 58

def get_slide_ids
  parsed_body = get
  parsed_body[:slides].collect { |slide| slide[:object_id] }
end


33
34
35
# File 'lib/gslide/models/presentation.rb', line 33

def link_url
  "https://docs.google.com/presentation/d/#{@id}/edit"
end