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", ".htm" =>"text/html", ".html" =>"text/html", ".zip" =>"application/zip", ".swf" =>"application/x-shockwave-flash", }
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
- .new_upload_io(path_or_io, params) ⇒ Object
Class Method Details
.concat_url(url, piece) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/google_drive/util.rb', line 42 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("&") return (url_base || "") + (piece_base || "") + (result_query.empty? ? "" : "?#{result_query}") end |
.construct_and_query(args) ⇒ Object
94 95 96 |
# File 'lib/google_drive/util.rb', line 94 def construct_and_query(args) return args.select(){ |a| a }.map(){ |a| "(%s)" % construct_query(a) }.join(" and ") end |
.construct_query(arg) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/google_drive/util.rb', line 56 def construct_query(arg) case arg when String return arg when Array if arg[0].scan(/\?/).size != arg.size - 1 raise( 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 raise(ArgumentError, "Expected String, Time, true or false, but got %p" % [v]) end end else raise(ArgumentError, "Expected String or Array, but got %p" % [arg]) end end |
.convert_params(params) ⇒ Object
98 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/google_drive/util.rb', line 98 def convert_params(params) str_params = {} for k, v in params str_params[k.to_s()] = v end old_terms = [] new_params = {} for k, v in str_params 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(["title = ?", v]) else old_terms.push(["title 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" if v.to_s() == "false" old_terms.push("trashed = false") end when "ocr", "targetLanguage", "sourceLanguage" raise(ArgumentError, "'%s' parameter is no longer supported." % k) else new_params[k] = v end end if !old_terms.empty? if new_params.has_key?("q") raise(ArgumentError, "Cannot specify both 'q' parameter and old query parameters.") else new_params["q"] = construct_and_query(old_terms) end end return new_params end |
.delegate_api_methods(obj, api_obj, exceptions = []) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/google_drive/util.rb', line 162 def delegate_api_methods(obj, api_obj, exceptions = []) sc = get_singleton_class(obj) names = api_obj.class.keys.keys - exceptions.map(&:to_s) names.each() do |name| sc.__send__(:define_method, name) do api_obj.__send__(name) end end end |
.encode_query(params) ⇒ Object
38 39 40 |
# File 'lib/google_drive/util.rb', line 38 def encode_query(params) return params.map(){ |k, v| CGI.escape(k.to_s()) + "=" + CGI.escape(v.to_s()) }.join("&") end |
.get_singleton_class(obj) ⇒ Object
156 157 158 159 160 |
# File 'lib/google_drive/util.rb', line 156 def get_singleton_class(obj) class << obj return self end end |
.h(str) ⇒ Object
51 52 53 54 |
# File 'lib/google_drive/util.rb', line 51 def h(str) # Should also escape "\n" to keep it in cell contents. return CGI.escapeHTML(str.to_s()).gsub(/\n/, '
') end |
.new_upload_io(path_or_io, params) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/google_drive/util.rb', line 172 def new_upload_io(path_or_io, params) content_type = params[:content_type] || (params[:file_name] ? EXT_TO_CONTENT_TYPE[::File.extname(params[:file_name]).downcase] : nil) || "application/octet-stream" return Google::APIClient::UploadIO.new(path_or_io, content_type) end |