module GoogleDrive::Util

@api private

Constants

EXT_TO_CONTENT_TYPE
IMPORTABLE_CONTENT_TYPE_MAP

Public Instance Methods

concat_url(url, piece) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = []) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/google_drive/util.rb, line 226
def get_singleton_class(obj)
  class << obj
    return self
  end
end
h(str) click to toggle source
# 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