Class: Applocale::GoogleHelper

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_pathObject

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

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_idObject

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_pathObject

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


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)
  authorization = authorize
  begin
    case export_format
      when 'csv'
        service = Google::Apis::SheetsV4::SheetsService.new
        service.client_options.application_name = APPLICATION_NAME
        service.authorization = authorization
        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.expand_path("#{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=#{authorization.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=#{authorization.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.client_options.application_name = APPLICATION_NAME
      service.authorization = authorization
      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