Skip to content

Commit

Permalink
Merge pull request #11 from wahni/google-play-info-fix
Browse files Browse the repository at this point in the history
Fix parsing of Google Play information
  • Loading branch information
wahni committed Apr 4, 2018
2 parents 3251da9 + 7c9069f commit 8ad42f2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* reviews-to-slack
* Copyright(c) 2016 Niklas Wahlén
* Copyright(c) 2018 Niklas Wahlén
* MIT Licensed
*/

Expand Down Expand Up @@ -141,13 +141,13 @@ exports.setupGooglePlayAppInformation = function (config, appInformation, callba
request.get(appInformation.appLink, function (error, response, body) {
if (error) {
console.error('WARNING: [' + config.appId + '] Could not fetch app information, ' + error)
} else {
} else if (body) {
const $ = cheerio.load(body)
appInformation.appName = $('.id-app-title').text().trim()
appInformation.appName = $('[itemprop="name"]').text().trim()
if (config.debug) console.log('INFO: [' + config.appId + '] Fetched app name: ' + appInformation.appName)

var webpIcon = $('.cover-image').attr('src')
if (!webpIcon.startsWith('http')) {
var webpIcon = $('[itemprop="image"]').attr('src')
if (typeof webpIcon === 'string' && !webpIcon.startsWith('http')) {
webpIcon = 'https:' + webpIcon
}
appInformation.appIcon = webpIcon + '-no-tmp.png' // Force png as Slack currently cannot display the WebP image.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reviews-to-slack",
"version": "2.1.0",
"version": "2.1.1",
"description": "Post app reviews to Slack",
"scripts": {
"test": "npm run lint && npm run test-unit",
Expand Down
38 changes: 34 additions & 4 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,10 @@ describe('The module', function () {
})

it('sets up Google Play app information when not overriden', function * () {
const config = {
appId: 'com.mock.id'
}
const config = { appId: 'com.mock.id' }
var appInformation = {}

const body = '<body><span class="id-app-title"> Expected App Title </span><img class="cover-image" src="cover-image-src"></img></body>'
const body = '<body><h1 itemprop="name"><span> Expected App Title </span></h1><img src="cover-image-src" itemprop="image"></img></body>'

const requestGetStub = this.sandbox.stub(request, 'get').callsFake(function (url, callback) {
callback(null, null, body)
Expand All @@ -254,4 +252,36 @@ describe('The module', function () {
expect(appInformation.appName).to.eql('Expected App Title')
expect(appInformation.appIcon).to.eql('https:cover-image-src-no-tmp.png')
})

it('handels empty Google Play page body gracefully', function * () {
const config = { appId: 'com.mock.id' }
var appInformation = {}

const requestGetStub = this.sandbox.stub(request, 'get').callsFake(function (url, callback) {
callback(null, null, '<body></body>')
})

reviews.setupGooglePlayAppInformation(config, appInformation, function () {})

expect(requestGetStub.callCount).to.eql(1)
expect(appInformation.appLink).to.eql('https://play.google.com/store/apps/details?id=com.mock.id')
expect(appInformation.appName).to.eql('')
expect(appInformation.appIcon).to.eql('undefined-no-tmp.png')
})

it('handels null Google Play page response gracefully', function * () {
const config = { appId: 'com.mock.id' }
var appInformation = {}

const requestGetStub = this.sandbox.stub(request, 'get').callsFake(function (url, callback) {
callback(null, null, null)
})

reviews.setupGooglePlayAppInformation(config, appInformation, function () {})

expect(requestGetStub.callCount).to.eql(1)
expect(appInformation.appLink).to.eql('https://play.google.com/store/apps/details?id=com.mock.id')
expect(appInformation.appName).to.eql(undefined)
expect(appInformation.appIcon).to.eql(undefined)
})
})

0 comments on commit 8ad42f2

Please sign in to comment.