Module: GoogleDrive::Util Private

Included in:
Acl, AclEntry, Collection, File, Session, Session, Spreadsheet, Worksheet
Defined in:
lib/google_drive/util.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

EXT_TO_CONTENT_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  '.csv' => 'text/csv',
  '.tsv' => 'text/tab-separated-values',
  '.tab' => 'text/tab-separated-values',
  '.doc' => 'application/msword',
  '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  '.ods' => 'application/x-vnd.oasis.opendocument.spreadsheet',
  '.odt' => 'application/vnd.oasis.opendocument.text',
  '.rtf' => 'application/rtf',
  '.sxw' => 'application/vnd.sun.xml.writer',
  '.txt' => 'text/plain',
  '.xls' => 'application/vnd.ms-excel',
  '.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  '.pdf' => 'application/pdf',
  '.png' => 'image/png',
  '.ppt' => 'application/vnd.ms-powerpoint',
  '.pps' => 'application/vnd.ms-powerpoint',
  '.pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  '.htm' => 'text/html',
  '.html' => 'text/html',
  '.zip' => 'application/zip',
  '.swf' => 'application/x-shockwave-flash'
}.freeze
IMPORTABLE_CONTENT_TYPE_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  'application/x-vnd.oasis.opendocument.presentation' => 'application/vnd.google-apps.presentation',
  'text/tab-separated-values' => 'application/vnd.google-apps.spreadsheet',
  'image/jpeg' => 'application/vnd.google-apps.document',
  'image/bmp' => 'application/vnd.google-apps.document',
  'image/gif' => 'application/vnd.google-apps.document',
  'application/vnd.ms-excel.sheet.macroenabled.12' => 'application/vnd.google-apps.spreadsheet',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => 'application/vnd.google-apps.document',
  'application/vnd.ms-powerpoint.presentation.macroenabled.12' => 'application/vnd.google-apps.presentation',
  'application/vnd.ms-word.template.macroenabled.12' => 'application/vnd.google-apps.document',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'application/vnd.google-apps.document',
  'image/pjpeg' => 'application/vnd.google-apps.document',
  'application/vnd.google-apps.script+text/plain' => 'application/vnd.google-apps.script',
  'application/vnd.ms-excel' => 'application/vnd.google-apps.spreadsheet',
  'application/vnd.sun.xml.writer' => 'application/vnd.google-apps.document',
  'application/vnd.ms-word.document.macroenabled.12' => 'application/vnd.google-apps.document',
  'application/vnd.ms-powerpoint.slideshow.macroenabled.12' => 'application/vnd.google-apps.presentation',
  'text/rtf' => 'application/vnd.google-apps.document',
  'text/plain' => 'application/vnd.google-apps.document',
  'application/vnd.oasis.opendocument.spreadsheet' => 'application/vnd.google-apps.spreadsheet',
  'application/x-vnd.oasis.opendocument.spreadsheet' => 'application/vnd.google-apps.spreadsheet',
  'image/png' => 'application/vnd.google-apps.document',
  'application/x-vnd.oasis.opendocument.text' => 'application/vnd.google-apps.document',
  'application/msword' => 'application/vnd.google-apps.document',
  'application/pdf' => 'application/vnd.google-apps.document',
  'application/json' => 'application/vnd.google-apps.script',
  'application/x-msmetafile' => 'application/vnd.google-apps.drawing',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => 'application/vnd.google-apps.spreadsheet',
  'application/vnd.ms-powerpoint' => 'application/vnd.google-apps.presentation',
  'application/vnd.ms-excel.template.macroenabled.12' => 'application/vnd.google-apps.spreadsheet',
  'image/x-bmp' => 'application/vnd.google-apps.document',
  'application/rtf' => 'application/vnd.google-apps.document',
  'application/vnd.openxmlformats-officedocument.presentationml.template' => 'application/vnd.google-apps.presentation',
  'image/x-png' => 'application/vnd.google-apps.document',
  'text/html' => 'application/vnd.google-apps.document',
  'application/vnd.oasis.opendocument.text' => 'application/vnd.google-apps.document',
  'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'application/vnd.google-apps.presentation',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'application/vnd.google-apps.spreadsheet',
  'application/vnd.google-apps.script+json' => 'application/vnd.google-apps.script',
  'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'application/vnd.google-apps.presentation',
  'application/vnd.ms-powerpoint.template.macroenabled.12' => 'application/vnd.google-apps.presentation',
  'text/csv' => 'application/vnd.google-apps.spreadsheet',
  'application/vnd.oasis.opendocument.presentation' => 'application/vnd.google-apps.presentation',
  'image/jpg' => 'application/vnd.google-apps.document',
  'text/richtext' => 'application/vnd.google-apps.document'
}.freeze

Class Method Summary collapse

Class Method Details

.concat_url(url, piece) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
93
94
95
96
# File 'lib/google_drive/util.rb', line 88

def concat_url(url, piece)
  (url_base, url_query) = url.split(/\?/, 2)
  (piece_base, piece_query) = piece.split(/\?/, 2)
  result_query =
    [url_query, piece_query].select { |s| s && !s.empty? }.join('&')
  (url_base || '') +
    (piece_base || '') +
    (result_query.empty? ? '' : "?#{result_query}")
end

.construct_and_query(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
152
153
# File 'lib/google_drive/util.rb', line 149

def construct_and_query(args)
  args
    .select { |a| a }.map { |a| format('(%s)', construct_query(a)) }
    .join(' and ')
end

.construct_query(arg) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/google_drive/util.rb', line 103

def construct_query(arg)
  case arg

  when String
    arg

  when Array
    if arg[0].scan(/\?/).size != arg.size - 1
      raise(
        ArgumentError,
        format(
          "The number of placeholders doesn't match the number of " \
          'arguments: %p',
          arg
        )
      )
    end
    i = 1
    arg[0].gsub(/\?/) do
      v = arg[i]
      i += 1
      case v
      when String
        format("'%s'", v.gsub(/['\\]/) { '\\' + $& })
      when Time
        format("'%s'", v.iso8601)
      when TrueClass
        'true'
      when FalseClass
        'false'
      else
        raise(
          ArgumentError,
          format('Expected String, Time, true or false, but got %p', v)
        )
      end
    end

  else
    raise(
      ArgumentError, format('Expected String or Array, but got %p', arg)
    )

  end
end

.convert_params(params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/google_drive/util.rb', line 155

def convert_params(params)
  str_params = {}
  params.each do |k, v|
    str_params[k.to_s] = v
  end

  old_terms = []
  new_params = {}
  str_params.each do |k, v|
    case k
    when 'q'
      new_params[:q] = construct_query(v)

    # Parameters in the old API.
    when 'title'
      if str_params['title-exact'].to_s == 'true'
        old_terms.push(['name = ?', v])
      else
        old_terms.push(['name contains ?', v])
      end
    when 'title-exact'
    # Skips it. It is handled above.
    when 'opened-min'
      old_terms.push(['lastViewedByMeDate >= ?', v])
    when 'opened-max'
      old_terms.push(['lastViewedByMeDate <= ?', v])
    when 'edited-min'
      old_terms.push(['modifiedDate >= ?', v])
    when 'edited-max'
      old_terms.push(['modifiedDate <= ?', v])
    when 'owner'
      old_terms.push(['? in owners', v])
    when 'writer'
      old_terms.push(['? in writers', v])
    when 'reader'
      old_terms.push(['? in readers', v])
    when 'showfolders'
      if v.to_s == 'false'
        old_terms.push("mimeType != 'application/vnd.google-apps.folder'")
      end
    when 'showdeleted'
      old_terms.push('trashed = false') if v.to_s == 'false'
    when 'ocr', 'targetLanguage', 'sourceLanguage'
      raise(
        ArgumentError, format("'%s' parameter is no longer supported.", k)
      )
    else
      # e.g., 'pageToken' -> :page_token
      new_key = k
                .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
                .gsub(/([a-z\d])([A-Z])/, '\1_\2')
                .downcase
                .intern
      new_params[new_key] = v
    end
  end

  unless old_terms.empty?
    if new_params.key?(:q)
      raise(
        ArgumentError,
        "Cannot specify both 'q' parameter and old query parameters."
      )
    else
      new_params[:q] = construct_and_query(old_terms)
    end
  end

  new_params
end

.delegate_api_methods(obj, api_obj, exceptions = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



232
233
234
235
236
237
238
239
240
241
# File 'lib/google_drive/util.rb', line 232

def delegate_api_methods(obj, api_obj, exceptions = [])
  sc = get_singleton_class(obj)
  names = api_obj.public_methods(false) - exceptions
  names.each do |name|
    next if name.to_s =~ /=$/
    sc.__send__(:define_method, name) do
      api_obj.__send__(name)
    end
  end
end

.encode_query(params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



82
83
84
85
86
# File 'lib/google_drive/util.rb', line 82

def encode_query(params)
  params
    .map { |k, v| CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s) }
    .join('&')
end

.get_singleton_class(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



226
227
228
229
230
# File 'lib/google_drive/util.rb', line 226

def get_singleton_class(obj)
  class << obj
    return self
  end
end

.h(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
# File 'lib/google_drive/util.rb', line 98

def h(str)
  # Should also escape "\n" to keep it in cell contents.
  CGI.escapeHTML(str.to_s).gsub(/\n/, '&#x0a;')
end