Class: Zoho::Sheet
- Inherits:
-
Object
- Object
- Zoho::Sheet
- Defined in:
- lib/zoho/sheet.rb
Instance Attribute Summary collapse
-
#cli ⇒ Object
Returns the value of attribute cli.
-
#update_lock ⇒ Object
Returns the value of attribute update_lock.
-
#wb_id ⇒ Object
Returns the value of attribute wb_id.
-
#wb_name ⇒ Object
Returns the value of attribute wb_name.
Class Method Summary collapse
Instance Method Summary collapse
- #download(format = 'csv') ⇒ Object
-
#initialize(workbook_id) ⇒ Sheet
constructor
A new instance of Sheet.
- #replace_with(file_name, contenttype = 'csv') ⇒ Object
- #update(new_row = "") ⇒ Object
Constructor Details
#initialize(workbook_id) ⇒ Sheet
Returns a new instance of Sheet.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zoho/sheet.rb', line 32 def initialize(workbook_id) raise "Zoho::Sheet uninitialized" if @@auth_params.nil? @cli = Zoho::Client.new(@@auth_params) all_docs = self.class.list all_wbs = all_docs['workbooks']['workbook'] @details = all_wbs.select{|sheet| sheet["workbookId"] == workbook_id || sheet['workbookName'] == workbook_id }[0] @wb_id = @details['workbookId'] @wb_name = @details['workbookName'] @update_lock = @details['updateLock'] @created_time = @details['createdTime'] end |
Instance Attribute Details
#cli ⇒ Object
Returns the value of attribute cli.
4 5 6 |
# File 'lib/zoho/sheet.rb', line 4 def cli @cli end |
#update_lock ⇒ Object
Returns the value of attribute update_lock.
7 8 9 |
# File 'lib/zoho/sheet.rb', line 7 def update_lock @update_lock end |
#wb_id ⇒ Object
Returns the value of attribute wb_id.
5 6 7 |
# File 'lib/zoho/sheet.rb', line 5 def wb_id @wb_id end |
#wb_name ⇒ Object
Returns the value of attribute wb_name.
6 7 8 |
# File 'lib/zoho/sheet.rb', line 6 def wb_name @wb_name end |
Class Method Details
.auth_with(auth_params) ⇒ Object
9 10 11 |
# File 'lib/zoho/sheet.rb', line 9 def self.auth_with(auth_params) @@auth_params = auth_params.merge({:servicename => 'ZohoSheet', :FROM_AGENT => 'true'}) end |
.list(args = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/zoho/sheet.rb', line 13 def self.list(args = nil) raise "Zoho::Sheet uninitialized" if @@auth_params.nil? cli = Zoho::Client.new(@@auth_params) sheets_list_url = "https://sheet.zoho.com/api/private/json/books?apikey=#{cli.api_key}&ticket=#{cli.ticket}" if args args_in_url_format = "" args.each_pair{|k,v| args_in_url_format << "#{k}=#{v}"} sheets_list_url << "&" + args_in_url_format end resp = RestClient.get(sheets_list_url) JSON.parse(resp)['response']['result'] end |
Instance Method Details
#download(format = 'csv') ⇒ Object
26 27 28 29 30 |
# File 'lib/zoho/sheet.rb', line 26 def download(format = 'csv') raise "Zoho::Sheet uninitialized" if @@auth_params.nil? cli = Zoho::Client.new(@@auth_params) resp = RestClient.get "https://sheet.zoho.com/api/private/#{format}/download/#{wb_id}?apikey=#{cli.api_key}&ticket=#{cli.ticket}" end |
#replace_with(file_name, contenttype = 'csv') ⇒ Object
46 47 48 49 50 |
# File 'lib/zoho/sheet.rb', line 46 def replace_with(file_name, contenttype = 'csv') raise "Zoho::Sheet uninitialized" if @@auth_params.nil? cli = Zoho::Client.new(@@auth_params) resp = RestClient.post "https://sheet.zoho.com/api/private/json/savebook/#{wb_id}?apikey=#{cli.api_key}&ticket=#{cli.ticket}&contentType=#{contenttype}&updateLock=#{update_lock}&isOverwrite=true", :content => File.new(file_name, 'rb') end |
#update(new_row = "") ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/zoho/sheet.rb', line 52 def update(new_row = "") # New row is a CSV row. # The fields have to be enclosed with \"<field>\" existing_data = download || "" max_commas = existing_data.split("\n").inject(0){|memo, line| memo > line.count(",") ? memo : line.count(",") } new_row += "," * (max_commas - new_row.count(',')) updated_data = existing_data + "\n" + new_row + "\n" File.open("/tmp/#{@wb_name}.csv", 'w') {|f| f.write(updated_data) } replace_with("/tmp/#{@wb_name}.csv") end |