Class: Applocale::GoogleHelper
- Inherits:
-
Object
- Object
- Applocale::GoogleHelper
- Defined in:
- lib/applocale/Core/GoogleHepler/google_helper.rb
Constant Summary collapse
- OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'
- APPLICATION_NAME =
'AppLocale'
- CLIENT_SECRETS_PATH =
'client_secret.json'
- SCOPE =
[Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY, Google::Apis::DriveV3::AUTH_DRIVE, Google::Apis::DriveV3::AUTH_DRIVE_FILE]
Instance Attribute Summary collapse
-
#credential_path ⇒ Object
Returns the value of attribute credential_path.
-
#download_link ⇒ Object
Returns the value of attribute download_link.
-
#spreadsheet_id ⇒ Object
Returns the value of attribute spreadsheet_id.
-
#xlsx_path ⇒ Object
Returns the value of attribute xlsx_path.
Class Method Summary collapse
Instance Method Summary collapse
- #download(sheet_obj_list, export_format:, export_to:) ⇒ Object
-
#initialize(link, credential_path, xlsx_path) ⇒ GoogleHelper
constructor
A new instance of GoogleHelper.
Constructor Details
#initialize(link, credential_path, xlsx_path) ⇒ GoogleHelper
Returns a new instance of GoogleHelper.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 22 def initialize(link, credential_path, xlsx_path) self.download_link = link.to_s.strip self.credential_path = credential_path.to_s.strip self.xlsx_path = xlsx_path.to_s.strip self.spreadsheet_id = GoogleHelper.get_spreadsheet_id(link) if self.download_link.length <= 0 ErrorUtil::ConfigFileInValid.new('[link] is missing in config file ').raise end if self.credential_path.length <= 0 ErrorUtil::ConfigFileInValid.new('[credential_path] is missing in config file ').raise end if self.xlsx_path.length <= 0 ErrorUtil::ConfigFileInValid.new('[xlsx_path] is missing in config file ').raise end end |
Instance Attribute Details
#credential_path ⇒ Object
Returns the value of attribute credential_path.
20 21 22 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 20 def credential_path @credential_path end |
#download_link ⇒ Object
Returns the value of attribute download_link.
20 21 22 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 20 def download_link @download_link end |
#spreadsheet_id ⇒ Object
Returns the value of attribute spreadsheet_id.
20 21 22 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 20 def spreadsheet_id @spreadsheet_id end |
#xlsx_path ⇒ Object
Returns the value of attribute xlsx_path.
20 21 22 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 20 def xlsx_path @xlsx_path end |
Class Method Details
.get_spreadsheet_id(link) ⇒ Object
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 181 def self.get_spreadsheet_id(link) if !link.nil? && link.length > 0 if link.match(/https:\/\/docs.google.com\/spreadsheets\/d\/([^\/]*)/i) if $1.strip.length > 0 return $1.strip end end end ErrorUtil::DownloadFromGoogleFail.new.raise end |
.is_googlelink(link) ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 169 def self.is_googlelink(link) if !link.nil? && link.length > 0 if link.match(/https:\/\/docs.google.com\/spreadsheets\/d\/([^\/]*)/i) if $1.length > 0 return true end end end return false end |
Instance Method Details
#download(sheet_obj_list, export_format:, export_to:) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/applocale/Core/GoogleHepler/google_helper.rb', line 45 def download(sheet_obj_list, export_format:, export_to:) FileUtils.mkdir_p(export_to) unless File.directory?(export_to) remove_old_files(from: export_to) = begin case export_format when 'csv' service = Google::Apis::SheetsV4::SheetsService.new service..application_name = APPLICATION_NAME service. = sheets = service.get_spreadsheet(self.spreadsheet_id).sheets sheetMap = {} sheets.each do |sheet| sheetMap[sheet.properties.title.to_s] = sheet.properties.sheet_id end index = 0 Parallel.each(sheet_obj_list) do |sheet_obj| sheet_name = sheet_obj.sheetname file_path = File.("#{sheet_name}.csv", export_to) if sheetMap[sheet_name].nil? ErrorUtil::SheetNotExist.new(sheet_name).raise end if !sheet_obj.obj.use_export link = "https://docs.google.com/spreadsheets/d/#{self.spreadsheet_id}/gviz/tq?tqx=out:csv&sheet=#{sheet_name}&access_token=#{.access_token}" puts "\nto download sheet: #{sheet_name}\nhttps://docs.google.com/spreadsheets/d/#{self.spreadsheet_id}/gviz/tq?tqx=out:csv&sheet=#{sheet_name}&access_token=xxxxx" csv = open(link) IO.copy_stream(csv, file_path) else if index % 3 == 0 puts "please wait ... " sleep(5) else puts "please wait .. " sleep(2) end link = "https://docs.google.com/spreadsheets/d/#{self.spreadsheet_id}/export?format=csv&gid=#{sheetMap[sheet_name]}&access_token=#{.access_token}" puts "\nto download sheet: #{sheet_name}\nhttps://docs.google.com/spreadsheets/d/#{self.spreadsheet_id}/export?format=csv&gid=#{sheetMap[sheet_name]}&access_token=xxxxx" File.open(file_path, "wb") do |file| file.write open(link).read end index = index + 1 end end when 'xlsx' service = Google::Apis::DriveV3::DriveService.new service..application_name = APPLICATION_NAME service. = service.export_file(self.spreadsheet_id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', download_dest: self.xlsx_path) end if Dir["#{export_to}/*"].any? puts 'Download from google finished'.green else ErrorUtil::DownloadFromGoogleFail.new.raise end rescue Google::Apis::AuthorizationError => e failauth rescue Google::Apis::ClientError => e failauth rescue Google::Apis::ServerError => e failauth rescue => execption ErrorUtil.raise(execption) end end |