Class: GoogleSheetProcessor
- Defined in:
- lib/Processors/GoogleSheetProcessor.rb
Instance Attribute Summary collapse
-
#formatValues ⇒ Object
Returns the value of attribute formatValues.
-
#googleAPI ⇒ Object
Returns the value of attribute googleAPI.
-
#keywordsInclude ⇒ Object
Returns the value of attribute keywordsInclude.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#ratingsInclude ⇒ Object
Returns the value of attribute ratingsInclude.
-
#sheetId ⇒ Object
Returns the value of attribute sheetId.
-
#sheetInsertAt ⇒ Object
Returns the value of attribute sheetInsertAt.
-
#sheetInsertType ⇒ Object
Returns the value of attribute sheetInsertType.
-
#sheetName ⇒ Object
Returns the value of attribute sheetName.
-
#spreadsheetID ⇒ Object
Returns the value of attribute spreadsheetID.
-
#territoriesInclude ⇒ Object
Returns the value of attribute territoriesInclude.
-
#timeZoneOffset ⇒ Object
Returns the value of attribute timeZoneOffset.
Attributes inherited from Processor
#baseExecutePath, #config, #configFilePath
Instance Method Summary collapse
-
#initialize(config, configFilePath, baseExecutePath) ⇒ GoogleSheetProcessor
constructor
A new instance of GoogleSheetProcessor.
- #processReviews(reviews, platform) ⇒ Object
Methods inherited from Processor
Constructor Details
#initialize(config, configFilePath, baseExecutePath) ⇒ GoogleSheetProcessor
Returns a new instance of GoogleSheetProcessor.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 13 def initialize(config, configFilePath, baseExecutePath) @config = config @configFilePath = configFilePath @baseExecutePath = baseExecutePath @logger = ZLogger.new(baseExecutePath) keyFilePath = Helper.unwrapRequiredParameter(config, "googleSheetAPIKeyFilePath") if Pathname.new(keyFilePath).absolute? configDir = File.dirname(configFilePath) keyFilePath = "#{configDir}#{keyFilePath}" end @googleAPI = GoogleAPI.new(keyFilePath, baseExecutePath, ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/spreadsheets"]) @keywordsInclude = [] if !config["keywordsInclude"].nil? @keywordsInclude = config["keywordsInclude"] end @ratingsInclude = [] if !config["ratingsInclude"].nil? @ratingsInclude = config["ratingsInclude"] end @territoriesInclude = [] if !config["territoriesInclude"].nil? @territoriesInclude = config["territoriesInclude"] end @timeZoneOffset = Helper.unwrapRequiredParameter(config, "googleSheetTimeZoneOffset") @spreadsheetID = Helper.unwrapRequiredParameter(config, "googleSpreadsheetID") sheetInsertStyle = Helper.unwrapRequiredParameter(config, "googleSheetInsertStyle") if !sheetInsertStyle.is_a? Array raise "googleSheetInsertStyle must specify as Array in GoogleSheetProcessor." end sheetInsertStyles = {} sheetInsertStyle.each do |value| value.keys.each do |key| sheetInsertStyles[key] = value[key] end end @sheetInsertType = Helper.unwrapRequiredParameter(sheetInsertStyles, "type") if sheetInsertType != "insert" && sheetInsertType != "append" raise "googleSheetInsertStyle.type only accept insert or append in GoogleSheetProcessor." elsif sheetInsertType == "insert" @sheetInsertAt = Helper.unwrapRequiredParameter(sheetInsertStyles, "at").to_i @sheetId = Helper.unwrapRequiredParameter(sheetInsertStyles, "sheetID") elsif sheetInsertType == "append" @sheetName = Helper.unwrapRequiredParameter(sheetInsertStyles, "sheetName") end @formatValues = [] if !config["values"].nil? @formatValues = config["values"] end puts "[GoogleSheetProcessor] Init Success." end |
Instance Attribute Details
#formatValues ⇒ Object
Returns the value of attribute formatValues.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def formatValues @formatValues end |
#googleAPI ⇒ Object
Returns the value of attribute googleAPI.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def googleAPI @googleAPI end |
#keywordsInclude ⇒ Object
Returns the value of attribute keywordsInclude.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def keywordsInclude @keywordsInclude end |
#logger ⇒ Object
Returns the value of attribute logger.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def logger @logger end |
#ratingsInclude ⇒ Object
Returns the value of attribute ratingsInclude.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def @ratingsInclude end |
#sheetId ⇒ Object
Returns the value of attribute sheetId.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def sheetId @sheetId end |
#sheetInsertAt ⇒ Object
Returns the value of attribute sheetInsertAt.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def sheetInsertAt @sheetInsertAt end |
#sheetInsertType ⇒ Object
Returns the value of attribute sheetInsertType.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def sheetInsertType @sheetInsertType end |
#sheetName ⇒ Object
Returns the value of attribute sheetName.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def sheetName @sheetName end |
#spreadsheetID ⇒ Object
Returns the value of attribute spreadsheetID.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def spreadsheetID @spreadsheetID end |
#territoriesInclude ⇒ Object
Returns the value of attribute territoriesInclude.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def territoriesInclude @territoriesInclude end |
#timeZoneOffset ⇒ Object
Returns the value of attribute timeZoneOffset.
11 12 13 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11 def timeZoneOffset @timeZoneOffset end |
Instance Method Details
#processReviews(reviews, platform) ⇒ Object
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/Processors/GoogleSheetProcessor.rb', line 77 def processReviews(reviews, platform) if reviews.length < 1 return reviews end filterReviews = reviews if .length > 0 filterReviews = filterReviews.select{ |review| .map{ || .to_i }.include? review. } end if territoriesInclude.length > 0 filterReviews = filterReviews.select{ |review| territoriesInclude.map{ |territory| territory.upcase }.include? review.territory.upcase } end if keywordsInclude.length > 0 keywordsInclude.select{ |keywordsInclude| keywordsInclude != "" }.each do |keywordInclude| filterReviews = filterReviews.select{ |review| review.body.include? keywordInclude } end end values = [] sortedFilterReviews = filterReviews if sheetInsertType == "insert" sortedFilterReviews = sortedFilterReviews.sort! { |a, b| b.createdDateTimestamp <=> a.createdDateTimestamp } end sortedFilterReviews.each do |review| cols = [] formatValues.each do |formatValue| formatValue = renderReview(formatValue, review, timeZoneOffset) cols.append(formatValue) end values.append(cols) end page = 1 limit = 100 values.each_slice(limit) do |value| puts "[GoogleSheetProcessor] Insert rows page:(#{page}/#{(values.length/limit).ceil + 1}) #{sheetInsertType} to #{spreadsheetID}" page += 1 if sheetInsertType == "insert" googleAPI.request("https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheetID}:batchUpdate", "POST", { "requests": [ { "insertRange": { "range": { "sheetId": sheetId, "startRowIndex": sheetInsertAt, "endRowIndex": sheetInsertAt + value.length }, "shiftDimension": "ROWS" } }, { "pasteData": { "data": rowsToString(value), "type": "PASTE_NORMAL", "delimiter": ",", "coordinate": { "sheetId": sheetId, "rowIndex": sheetInsertAt, } } } ] }) elsif sheetInsertType == "append" googleAPI.request("https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheetID}/values/#{sheetName}!A1:append?valueInputOption=RAW", "POST", {:values => value}) end end return reviews end |