Skip to content

Commit

Permalink
switched to ES7 async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
wurmr committed Feb 17, 2017
1 parent 4124f0f commit 8ac5f31
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
15 changes: 15 additions & 0 deletions lib/authenticate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import rp from 'request-promise-native'
const jarRequest = rp.defaults({jar: true})

const authenticate = async ({email, password}) => {
const form = {
'login_email': email,
'login_password': password,
'do_login': 'LOGIN'
}

// Login
await jarRequest.post({ uri: 'http://app.trackpin.com/', form })
}

export default authenticate
35 changes: 14 additions & 21 deletions lib/checkDoorStatus.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import request from 'request'
import cheerio from 'cheerio'
import rp from 'request-promise-native'
import authenticate from './authenticate'
const jarRequest = rp.defaults({jar: true})

const jarRequest = request.defaults({jar: true})
const checkDoorStatus = async (credentials) => {
await authenticate(credentials)

const checkDoorStatus = ({email, password}) => {
return new Promise(resolve => {
const formData = {
'login_email': email,
'login_password': password,
'do_login': 'LOGIN'
}
const options = {
uri: 'http://app.trackpin.com/index.php/app/main',
transform: body => cheerio.load(body)
}

jarRequest.post({url: 'http://app.trackpin.com/', form: formData},
(error, response) => {
if (!error && response.statusCode === 200) {
jarRequest.get('http://app.trackpin.com/index.php/app/main', (e, r, b) => {
const $ = cheerio.load(b)
const status = $('#door_status b').text()
const lastPin = $('td.activity_title').first().contents().get(0).data
resolve({status, lastPin})
})
}
})
})
// Get data
const $ = await jarRequest.get(options)
const status = $('#door_status b').text()
const lastPin = $('td.activity_title').first().contents().get(0).data
return {status, lastPin}
}

export default checkDoorStatus
41 changes: 18 additions & 23 deletions lib/listPins.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import request from 'request'
import rp from 'request-promise-native'
import cheerio from 'cheerio'
import authenticate from './authenticate'

const jarRequest = request.defaults({jar: true})
const jarRequest = rp.defaults({jar: true})

const listPins = ({email, password}) => {
return new Promise(resolve => {
const formData = {
'login_email': email,
'login_password': password,
'do_login': 'LOGIN'
}
const listPins = async (credentials) => {
await authenticate(credentials)

jarRequest.post({url: 'http://app.trackpin.com/', form: formData},
(error, response) => {
if (!error && response.statusCode === 200) {
jarRequest.get('http://app.trackpin.com/index.php/app/pins', (e, r, b) => {
const $ = cheerio.load(b)
const result = $('td.pin_title')
.contents()
.map((i, el) => el.data)
.filter((i, pin) => pin !== 'Remotes')
resolve(result)
})
}
})
})
const options = {
uri: 'http://app.trackpin.com/index.php/app/pins',
transform: body => cheerio.load(body)
}

// Get data
const $ = await jarRequest.get(options)
// parse out the pin titles and remove "Remotes"
const result = $('td.pin_title')
.contents()
.map((i, el) => el.data)
.filter((i, pin) => pin !== 'Remotes')
return result
}

export default listPins
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"license": "MIT",
"dependencies": {
"cheerio": "^0.22.0",
"request": "^2.79.0"
"request": "^2.79.0",
"request-promise-native": "^1.0.3"
},
"devDependencies": {
"babel-cli": "^6.22.2",
Expand Down

0 comments on commit 8ac5f31

Please sign in to comment.