class GooglePlay

Attributes

cacheFile[RW]
iconEmoji[RW]
ignoreKeywords[RW]
jsonKeyFilePath[RW]
notifyWebHookUrl[RW]
packageName[RW]
username[RW]

Public Class Methods

new(android) click to toggle source
# File lib/GooglePlay.rb, line 11
def initialize(android)
  @packageName = android['packageName']
  @jsonKeyFilePath = android['jsonKeyFilePath']
  @notifyWebHookUrl = android['notifyWebHookUrl']
  @iconEmoji = android['iconEmoji']
  @username = android['username']
  @ignoreKeywords = android['ignoreKeywords']
  @cacheFile = "#{$lib}/.cache/.androidLastModified"
end

Public Instance Methods

run() click to toggle source
# File lib/GooglePlay.rb, line 21
def run()
  app = Google::Apis::AndroidpublisherV3::AndroidPublisherService.new
  app.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
    json_key_io: File.open(jsonKeyFilePath),
    scope: 'https://www.googleapis.com/auth/androidpublisher')

  lastModified = getLastModified()
  newLastModified = lastModified
  
  reviews = []
  isFirst = true
  
  remoteReviews = app.list_reviews(packageName).reviews
  remoteReviews.each { |remoteReview|
    result = {
      "reviewId" => remoteReview.review_id,
      "reviewer" => remoteReview.author_name,
      "androidOsVersion" => remoteReview.comments[0].user_comment.android_os_version,
      "appVersionCode" => remoteReview.comments[0].user_comment.app_version_code,
      "appVersionName" => remoteReview.comments[0].user_comment.app_version_name,
      "lastModified" => remoteReview.comments[0].user_comment.last_modified.seconds.to_i,
      "reviewerLanguage" => remoteReview.comments[0].user_comment.reviewer_language,
      "starRating" => remoteReview.comments[0].user_comment.star_rating.to_i,
      "text" => remoteReview.comments[0].user_comment.text.strip
    }

    if isFirst
      isFirst = false
      newLastModified = result["lastModified"]
    end
    
    if result["lastModified"] > lastModified && lastModified != 0
      reviews.append(result)
    else
      break
    end
  }

  reviews.sort! { |a, b|  a["lastModified"] <=> b["lastModified"] }
  sendMessagesToSlack(reviews)
  saveLastModified(newLastModified)

  return lastModified
end
sendMessagesToSlack(reviews) click to toggle source
# File lib/GooglePlay.rb, line 66
def sendMessagesToSlack(reviews)
  slack = Slack.new(notifyWebHookUrl)

  reviews.each { |review|
    if ignoreKeywords != nil
      ignore = false
      ignoreKeywords.each { |ignoreKeyword|
        if review["text"].include? ignoreKeyword
          ignore = true
        end
      }
      next if ignore
    end

    rating = review["starRating"]
    color = rating >= 4 ? "good" : (rating >= 2 ? "warning" : "danger")
    date = "Created at: #{Time.at(review["lastModified"]).to_datetime}"

    stars = "★" * rating + "☆" * (5 - rating)

    attachment = Slack::Payload::Attachment.new

    attachment.color = color
    attachment.fallback = "#{stars}"
    attachment.title = "#{stars}"
    attachment.text = review["text"]
    attachment.author_name = review["reviewer"]
    attachment.footer = "Android(#{review["androidOsVersion"]}) - v#{review["appVersionName"]}(#{review["appVersionCode"]}) - #{review["reviewerLanguage"]} - #{date} - <https://play.google.com/store/apps/details?id=#{packageName}&reviewId=#{review["reviewId"]}|Go To Google Play>"
    
    payload = Slack::Payload.new
    payload.icon_emoji = iconEmoji
    payload.username = username
    payload.attachments = [attachment]

    slack.pushMessage(payload)
  }
 
end

Private Instance Methods

getLastModified() click to toggle source
# File lib/GooglePlay.rb, line 105
def getLastModified() 
  if File.exists?(cacheFile)
    lastModifiedFile = File.open(cacheFile)
    return lastModifiedFile.read.to_i
  else
    return 0
  end
end
saveLastModified(lastModified) click to toggle source
# File lib/GooglePlay.rb, line 114
def saveLastModified(lastModified)
  File.write(cacheFile, lastModified, mode: "w+")
end