Module: GoogleDrive::Util
- Included in:
- Acl, AclEntry, Collection, File, Session, Session, Spreadsheet, Worksheet
- Defined in:
- lib/google_drive/util.rb
Overview
:nodoc:
Constant Summary collapse
- EXT_TO_CONTENT_TYPE =
{ '.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' }
- IMPORTABLE_CONTENT_TYPE_MAP =
{ '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', }
Class Method Summary collapse
- .concat_url(url, piece) ⇒ Object
- .construct_and_query(args) ⇒ Object
- .construct_query(arg) ⇒ Object
- .convert_params(params) ⇒ Object
- .delegate_api_methods(obj, api_obj, exceptions = []) ⇒ Object
- .encode_query(params) ⇒ Object
- .get_singleton_class(obj) ⇒ Object
- .h(str) ⇒ Object
Class Method Details
.concat_url(url, piece) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/google_drive/util.rb', line 85 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
135 136 137 |
# File 'lib/google_drive/util.rb', line 135 def construct_and_query(args) args.select { |a| a }.map { |a| '(%s)' % construct_query(a) }.join(' and ') end |
.construct_query(arg) ⇒ Object
99 100 101 102 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 |
# File 'lib/google_drive/util.rb', line 99 def construct_query(arg) case arg when String return arg when Array if arg[0].scan(/\?/).size != arg.size - 1 fail( ArgumentError, "The number of placeholders doesn't match the number of arguments: %p" % [arg]) end i = 1 return arg[0].gsub(/\?/) do v = arg[i] i += 1 case v when String "'%s'" % v.gsub(/['\\]/) { '\\' + $& } when Time "'%s'" % v.iso8601 when TrueClass 'true' when FalseClass 'false' else fail(ArgumentError, 'Expected String, Time, true or false, but got %p' % [v]) end end else fail(ArgumentError, 'Expected String or Array, but got %p' % [arg]) end end |
.convert_params(params) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 |
# File 'lib/google_drive/util.rb', line 139 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' fail(ArgumentError, "'%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) fail(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
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/google_drive/util.rb', line 211 def delegate_api_methods(obj, api_obj, exceptions = []) sc = get_singleton_class(obj) names = api_obj.public_methods(false) - exceptions names.each do |name| if !(name.to_s =~ /=$/) sc.__send__(:define_method, name) do api_obj.__send__(name) end end end end |
.encode_query(params) ⇒ Object
81 82 83 |
# File 'lib/google_drive/util.rb', line 81 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
205 206 207 208 209 |
# File 'lib/google_drive/util.rb', line 205 def get_singleton_class(obj) class << obj return self end end |
.h(str) ⇒ Object
94 95 96 97 |
# File 'lib/google_drive/util.rb', line 94 def h(str) # Should also escape "\n" to keep it in cell contents. CGI.escapeHTML(str.to_s).gsub(/\n/, '
') end |