class GoogleCells::Spreadsheet

Public Class Methods

all()
Alias for: list
copy(key, opts={}) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 32
def copy(key, opts={})
  params = {}
  body = nil
  { :writers_can_share => 'writersCanShare',
    :title  => 'title' }.each do |sym,str|
    next unless opts[sym]
    body ||= {}
    body[str] = opts.delete(sym)
  end
  if body
    params[:body] = body.to_json
    params[:headers] = {'Content-Type' => 'application/json'}
  end
  res = request(:post, copy_uri(key), params)
  s = get(res.data['id'])
end
delete(key) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 69
def delete(key)
  request(:delete, file_uri(key))
end
get(key) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 26
def get(key)
  res = request(:get, worksheets_uri(key))
  args = parse_from_entry(Nokogiri.parse(res.body), key)
  Spreadsheet.new(args)
end
list() click to toggle source
# File lib/google_cells/spreadsheet.rb, line 15
def list
  spreadsheets = []
  each_entry do |entry|
    args = parse_from_entry(entry)
    spreadsheets << Spreadsheet.new(args)
  end
  spreadsheets
end
Also aliased as: all
share(key, params) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 49
def share(key, params)
  body = {}
  [:role, :type, :value].each do |sym|
    body[sym.to_s] = params.delete(sym)
  end
  params[:body] = body.to_json

  params[:url_params] = {}
  params[:url_params]['sendNotificationEmails'] = params.delete(
    :send_notification_emails) if !params[:send_notification_emails].
    to_s.empty?
  params[:url_params]['emailMessage'] = params.delete(
    :email_message) if params[:email_message]

  params[:headers] = {'Content-Type' => 'application/json'}
  
  res = request(:post, permissions_uri(key), params)
  true
end
subscribe(key, params) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 84
def subscribe(key, params)
  body = {"type" => "web_hook"}
  [:id, :address, :token, :expiration, :type].each do |sym|
    body[sym.to_s] = params.delete(sym) if params[sym]
  end
  drive = GoogleCells.client.discovered_api('drive', 'v2')
  res = GoogleCells.client.execute!(
    :api_method => drive.files.watch,
    :body_object => body,
    :parameters => { 'fileId' => key })
  res.data['resourceId']
end
unsubscribe(params) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 73
def unsubscribe(params)
  body = {}
  body['id'] = params.delete(:id) if params[:id]
  body['resourceId'] = params.delete(:resource_id) if params[:resource_id]
  drive = GoogleCells.client.discovered_api('drive', 'v2')
  GoogleCells.client.execute!(
    :api_method => drive.channels.stop,
    :body_object => body )
  true
end

Private Class Methods

parse_from_entry(entry, key=nil) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 175
def self.parse_from_entry(entry, key=nil)
  url ||= entry.css("link").select{|el| el['rel'] == 'alternate'}.
    first['href']
  key =  url[/key=.+/] ? url[/key=.+/][4..-1] : url[/[^\/]{44}/]
  { title: entry.css("title").first.text,
    key: key,
    updated_at: entry.css("updated").first.text,
    author: Author.new(
      name: entry.css("author/name").first.text,
      email: entry.css("author/email").first.text
    )
  }
end

Public Instance Methods

copy(opts={}) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 114
def copy(opts={})
  self.class.copy(self.key, opts)
end
defold(folder_key) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 127
def defold(folder_key)
  klass = self.class
  res = klass.request(:delete, klass.child_uri(folder_key, self.key))
  @folders = nil
  true
end
delete() click to toggle source
# File lib/google_cells/spreadsheet.rb, line 110
def delete
  self.class.delete(self.key)
end
enfold(folder_key) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 118
def enfold(folder_key)
  return true if @folders && @folders.select{|f| f.key == folder_key}.first
  body = {'id' => self.key}.to_json
  res = self.class.request(:post, self.class.folder_uri(folder_key), 
    :body => body, :headers => {'Content-Type' => 'application/json'})
  @folders << Folder.new(spreadsheet:self, key:folder_key) if @folders
  true
end
folders() click to toggle source
# File lib/google_cells/spreadsheet.rb, line 134
def folders
  return @folders if @folders
  res = self.class.request(:get, self.class.file_uri(key))
  data = JSON.parse(res.body)
  return @folders = [] if data['parents'].nil?
  @folders = data['parents'].map do |f|
    Folder.new(spreadsheet: self, key:f['id'])
  end
end
revisions() click to toggle source
# File lib/google_cells/spreadsheet.rb, line 166
def revisions
  @revisions ||= Revision.list(self.key).map do |r|
    r.instance_variable_set(:@spreadsheet, self)
    r
  end
end
unsubscribe(params) click to toggle source
# File lib/google_cells/spreadsheet.rb, line 106
def unsubscribe(params)
  self.class.unsubscribe(params)
end
worksheets() click to toggle source
# File lib/google_cells/spreadsheet.rb, line 144
def worksheets
  return @worksheets if @worksheets
  @worksheets = []
  self.class.each_entry(worksheets_uri) do |entry|
    args = {
      title: entry.css("title").text,
      updated_at: entry.css("updated").text,
      cells_uri: entry.css(
        "link[rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']"
        )[0]["href"],
      lists_uri: entry.css(
        "link[rel='http://schemas.google.com/spreadsheets/2006#listfeed']"
        )[0]["href"],
      row_count: entry.css("gs|rowCount").text.to_i,
      col_count: entry.css("gs|colCount").text.to_i,
      spreadsheet: self
    }
    @worksheets << Worksheet.new(args)
  end
  return @worksheets
end