Class: GoogleSheetProcessor

Inherits:
Processor show all
Defined in:
lib/Processors/GoogleSheetProcessor.rb

Instance Attribute Summary collapse

Attributes inherited from Processor

#baseExecutePath, #config, #configFilePath

Instance Method Summary collapse

Methods inherited from Processor

#renderReview

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

#formatValuesObject

Returns the value of attribute formatValues.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def formatValues
  @formatValues
end

#googleAPIObject

Returns the value of attribute googleAPI.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def googleAPI
  @googleAPI
end

#keywordsIncludeObject

Returns the value of attribute keywordsInclude.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def keywordsInclude
  @keywordsInclude
end

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def logger
  @logger
end

#ratingsIncludeObject

Returns the value of attribute ratingsInclude.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def ratingsInclude
  @ratingsInclude
end

#sheetIdObject

Returns the value of attribute sheetId.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def sheetId
  @sheetId
end

#sheetInsertAtObject

Returns the value of attribute sheetInsertAt.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def sheetInsertAt
  @sheetInsertAt
end

#sheetInsertTypeObject

Returns the value of attribute sheetInsertType.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def sheetInsertType
  @sheetInsertType
end

#sheetNameObject

Returns the value of attribute sheetName.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def sheetName
  @sheetName
end

#spreadsheetIDObject

Returns the value of attribute spreadsheetID.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def spreadsheetID
  @spreadsheetID
end

#territoriesIncludeObject

Returns the value of attribute territoriesInclude.



11
12
13
# File 'lib/Processors/GoogleSheetProcessor.rb', line 11

def territoriesInclude
  @territoriesInclude
end

#timeZoneOffsetObject

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 ratingsInclude.length > 0
        filterReviews = filterReviews.select{ |review| ratingsInclude.map{ |rating| rating.to_i }.include? review.rating }
    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