hey-jude-sdk v1.2.4
Hey Jude SDK
Install
npm install hey-jude-sdk
About
Built to wrap the hey jude api for easier integration.
Usage
import library
import { Jude } from 'hey-jude-sdk';
The Jude object
Instantiate a new Jude object and parse in the nessasary parameters
var jude = new Jude(environment: String, program: String, xApiKey: String);
Using jude to make calls for you
The Jude class takes care of instantiation and provides access to the following:
- Auth
- Payments
- Subscriptions
- Tasks
- Misc
- Attachments
- User
Example usage
Here we will make use of Auth through our jude class, each function returns a promise. In this case .then will log the user object.
If an error occurs an array of errors will be parsed with error. In this case .catch will log the array of errors from error
jude.Auth.signIn({ password: 'password', username: 'username' })
.then(user => {
// Logging user object to the console
console.log(user.toString());
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
Keeping the user logged in
You can store the jwtToken from a session, then call resumeFromToken and parse in your saved token. eg.
Get your token when your user leaves the app and store it.
this.mJude.Auth.getToken();
When your user reopens the app call resumeFromToken.
// This method will open the socket connection, bind to chat and return profile.
// Whenever a message is recieved from jude it will call your messageRecievedCallback function
// with a message object
jude.resumeFromToken(resumeToken: String, messageRecievedCallback: Function)
.then(user => {
// Logging user object to the console
console.log(user.toString());
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
###Authentication
The User Object
user: {
id = '12312', //(String)
email = 'test', //(String)
firstName = 'first', //(String)
lastName = 'last', //(String)
fullName = 'first last', //(String)
mobile = '+277872100976', //(String)
profileImage = 'https://image.jpg', //(String)
referralCode = '981920', //(String)
paymentProvider = 'peach', //(String)
country = 'RSA', //(String)
pushNotifications = true, //(Boolean)
greenerChoices = false, //(Boolean)
roadblocks = 'tutorial' // (String)
}
SignOut
jude.Auth.signOut()
.then(data => {
// User logout successful
console.log('User has been logged out');
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
Get Profile
jude.User.getProfile()
.then(user => {
// Logging user object to the console
console.log(user.toString());
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
###Messages
Message object
message:{
// Basic
fromUser = true, //(Boolean) is the message from the user or not
fromUserString = '', //(String)
id = '12312', //(String)
options = ['',''] //(Array of strings)
text = 'Hey this is the message', //(String)
type = 'Text', //(String)
unixTimeStamp = "1526567836", //(String)
// When type is payment
amount = '100.00', //(String)
currency = 'ZAR', //(String)
description = 'description', //(String)
supplier = 'supplier name', //(String)
taskId = 89202, //(Number)
status = 'pending', //(String)
// When type is map
imageUrl = 'https://mapurl', //(String)
latitude = '53.461364', //(String)
longitude = '-2.246373', //(String)
address = 'Asda Hulme Superstore, Princess Road, Manchester, UK', //(String)
// When type is image or document
attachmentId = 12312, //(Number)
extension = 'jpg', //(String)
mime = 'image/jpg', //(String)
size= '40404', //(String)
}
Bind to chat service
// Whenever a message is recieved by the app, messageRecievedCallback will be called with a message object
jude.Auth.bindToChat(Function: messageRecievedCallback);
###Tasks
The task object
task: {
id = '12312',
messageCount = '1',
messageLimit = '0',
messageOffset = '0',
messageOrder = 'asc',
messageTotal = '1',
messages = [
{
fromUser = true, //(Boolean) is the message from the user or not
fromUserString = '', //(String)
id = '12312', //(String)
options = ['',''] //(Array of strings)
text = 'Hey this is the message', //(String)
type = 'Text', //(String)
unixTimeStamp = '"1526567836"' //(String)
},
],
open = true,
status = 'Pending',
title = 'Test Title',
createdAt = '2018-05-17 16:37:16'
}
Creating a new task
jude.Tasks.createTask(title: string, createDefaultMessage: boolean, offset: string, limit: string, messages: [
{
type: string;
text: string;
}
]): Promise
Example:
jude.Tasks.createTask('Test Title', true, "0", "10", [
{
"type": "text",
"text": "This is another message"
}
], )
.then(task => {
// Logging the task object
console.log(task)
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
Retrieving an Existing Task
jude.Tasks.getTask(taskId: Number)
.then(task => {
// Logging the task object
console.log(task)
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
Sending a Message to an Open Task
jude.Tasks.message({ type: 'text', text: 'This is a new message.', taskId: 6210 })
.then(data => {
// Logging message sent
console.log('Send message success');
})
.catch(error => {
// Logging the array of errors
console.log(error.errors);
});
Gets all open tasks
jude.Tasks.getOpenTasks()
.then(tasks => {
// Logging all the open tasks
console.log(tasks);
})
.catch(error => console.log('get open tasks failed '.concat(error.errors)));
Get all closed tasks
jude.Tasks.getClosedTasks()
.then(tasks => {
// Logging all the closed tasks
console.log(tasks);
})
.catch(error => console.log('get closed tasks failed '.concat(error.errors)));
###Attachments
Uploading an Attachment
const media = {
uri: 'Source URl/Path of the file',
mimeType: 'image/jpeg',(mime file type)
name: 'foo-bar.jpg'
}
jude.Attachments.upload(media)
.then(data => console.log('Upload success') }))
.catch(error => console.log('Upload failed') }));
Download
jude.Attachments.download(attachmentId: Number)
.then(blob => {
console.log(blob);
// Do something with your downloaded blob
// ...
})
.catch(error => console.log(error.errors));
Getting an Attachments Details
jude.Attachments.details(attachmentId: Number)
.then(details => console.log(details.toString()) }))
.catch(error => console.log(error.errors));
###Ratings
Rating a task
jude.Tasks.rate(message: String, unixTimeStamp: String, rating: Number, taskId: Number)
.then(details => console.log('Successfully rated task') }))
.catch(error => console.log(error.errors));
Retrieving a Task Rating
jude.Tasks.rating(unixTimeStamp: String, taskId: Number)
.then(data => console.log('Get Rating Success: ', data.task_rating))
.catch(error => console.log(error.errors));