Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit bae8182

Browse files
committed
Added additional function to the tweet, follow and unfollow functions (they now also accept just a string instead of requiring an object). - Thanks to Will Honey (Tibfib) for the suggestion.
When sending a tweet, the string will become the tweet message. When following or unfollowing, the string will become the username you wish to follow/unfollow.
1 parent f80a128 commit bae8182

9 files changed

+394
-212
lines changed

README.md

+247-200
Large diffs are not rendered by default.

old stuff/docs & how to use.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Name: xTwitter for webOS
2+
Author: Joshua Spohr (dawm) / Biocandy Labs
3+
4+
Version: 1.0
5+
6+
7+
How to use:
8+
Initial setup is simple, put the following code in your assistant setup(), using the objects below to
9+
to initialize xTwitter.
10+
11+
-- code --
12+
appKeys = {
13+
consumerKey: '',
14+
consumerSecret: '',
15+
follow: {
16+
username: 'biocandy',
17+
//OR
18+
userid: '105638521'
19+
}
20+
};
21+
userKeys = {
22+
username: '',
23+
authorized: false,
24+
token: '',
25+
secret: ''
26+
};
27+
-- code --
28+
29+
A variable named 'Twitter' is already defined at the bottom of this file, but its empty so you will have to assign
30+
it to our library when you're ready to use it.
31+
32+
-- code --
33+
if (Twitter.isAuthorized === undefined) {
34+
// if you store your appKeys or userKeys in a cookie you should load them up now.
35+
Twitter = new xTwitter(appKeys, userKeys);
36+
}
37+
-- code --
38+
39+
You will want to save 'userKeys' after successfull authorization with twitter (in a cookie or db).
40+
The object 'appKeys' above is static data about your app, and should be hard coded.
41+
42+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43+
!! You must have xAuth access for your twitter application (see http://dev.twitter.com/pages/xauth). !!
44+
!! If you do not have xAuth access this library will not work properly. !!
45+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46+
47+
To authorize your app to use a users twitter use the function:
48+
Twitter.authorize(username,password);
49+
50+
~~ Optionally you can pass a callback function to be called,
51+
~~ it will return the data required for userKeys on successful authorization
52+
~~ and will return FALSE on a failure.
53+
54+
Twitter.authorize(username,password,callback);
55+
56+
To send a tweet you use the function:
57+
Twitter.tweet(message);
58+
59+
To follow a user on twitter use the function:
60+
Twitter.follow({screen_name: 'twitterUsername'});
61+
- OR -
62+
Twitter.follow({user_id: TwitterUserID});
63+
64+
To follow yourself/company (via the data in appKeys) use the function:
65+
Twitter.followMe();
66+
67+
To clear all data currently associated with xTwitter use the function:
68+
Twitter.logout();
69+
70+
71+
###############
72+
# Sample code #
73+
###############
74+
The code below checks to see if the user has been authorized, if they have not authorized
75+
with your app it sends them to the twitter authorization scene. If they are authorized it
76+
sends the tweet 'Test.'
77+
78+
-- code --
79+
if (Twitter.isAuthorized === false) {
80+
this.controller.stageController.pushScene('twitter', appKeys);
81+
}
82+
else {
83+
Twitter.tweet('Test.')
84+
}
85+
-- code --
86+
87+
Once authorized (or failed authorization) the app pop's back to the previous scene with
88+
2 arguments 'from' and 'userKeys'. You will need to catch them in your scene's activate()
89+
function in order to save them for future use. Below is sample code that does just that.
90+
91+
-- code --
92+
MySceneAssistant.prototype.activate(event) {
93+
if (event.from == 'xTwitter') {
94+
if (event.userKeys != false) {
95+
// your save method goes here cookie or database.
96+
}
97+
}
98+
}
99+
-- code --
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Komodo Project File - DO NOT EDIT -->
3+
<project id="E0AEB7CB-6E96-4A75-99F2-CEB262F1DF39" kpf_version="4" name="Sample Application.kpf">
4+
<preference-set idref="E0AEB7CB-6E96-4A75-99F2-CEB262F1DF39">
5+
<boolean id="import_live">1</boolean>
6+
</preference-set>
7+
</project>
Binary file not shown.
Binary file not shown.

sampleApplication/src/app/assistants/main-assistant.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,32 @@ MainAssistant.prototype.setup = function() {
7373
this.controller.setupWidget("buttonAuth", {},
7474
this.buttonAuthModel = {
7575
label: "Authorize this app",
76-
disabled: false
76+
disabled: (Twitter.isLoaded !== undefined) ? false : true
7777
}
7878
);
7979
this.controller.setupWidget("buttonTweet", {},
8080
this.buttonTweetModel = {
8181
label: "Send a test Tweet",
8282
// If we are already authorized lets enable this button.
83-
disabled: (this.userKeys.authorized === true) ? false : true
83+
disabled: (this.userKeys.authorized === true && Twitter.isLoaded !== undefined) ? false : true
8484
}
8585
);
8686
this.controller.setupWidget("buttonFollow", {},
8787
this.buttonFollowModel = {
8888
label: "Follow @dawm",
8989
// If we are already authorized lets enable this button.
90-
disabled: (this.userKeys.authorized === true) ? false : true
90+
disabled: (this.userKeys.authorized === true && Twitter.isLoaded !== undefined) ? false : true
9191
}
9292
);
9393

94+
// Double checking if library was loaded, if not the keys were not entered.
95+
if (Twitter.isLoaded === undefined) {
96+
this.appMenuModel.items[2].disabled = true;
97+
this.controller.modelChanged(this.appMenuModel);
98+
this.controller.get('info').innerHTML = 'Did you enter your Consumer Key/Secret? Library not loaded!';
99+
}
100+
101+
// Dump our userKey data into our showKeys div
94102
this.controller.get('showKeys').innerHTML = Object.toJSON(this.userKeys);
95103
};
96104
MainAssistant.prototype.aboutToActivate = function(callback) {
@@ -201,7 +209,9 @@ MainAssistant.prototype.logout = function() {
201209
token: '',
202210
secret: ''
203211
};
204-
Twitter.logout();
212+
if (Twitter.isLoaded !== undefined) {
213+
Twitter.logout();
214+
}
205215
this.cookie.remove();
206216

207217
// Disable the tweet button

sampleApplication/src/app/models/xTwitter/xTwitter.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function xTwitter(appKeys, userKeys) {
2828
Author: Joshua Spohr (dawm) / Biocandy Labs
2929
3030
Twitter: @dawm / @biocandy
31-
Version: 1.0
31+
GitHub: https://github.com/dawm/xTwitter
32+
Version: 1.0.5
3233
*/
3334

3435
// OAuth / xAuth
@@ -137,7 +138,15 @@ xTwitter.prototype.logout = function(name) {
137138
};
138139
xTwitter.prototype.tweet = function(parameters, callback) {
139140

140-
if (parameters !== undefined) {
141+
// Thanks Will Honey (Tibfib) for the suggestion of checking the variable for its type
142+
// If parameters is a string we build our parameters object using the string as the status
143+
// If parameters is an object we do nothing and let Twitter complain
144+
if (parameters !== undefined && (typeof(parameters) == 'object' || typeof(parameters) == 'string')) {
145+
146+
if (typeof(parameters) == 'string') {
147+
parameters = { status: parameters };
148+
}
149+
141150
// A full list of parameters can be found here -> http://dev.twitter.com/doc/post/statuses/update
142151
var tweetParameters = OAuth.formEncode(parameters);
143152

@@ -179,7 +188,12 @@ xTwitter.prototype.tweet = function(parameters, callback) {
179188
};
180189
xTwitter.prototype.follow = function(parameters, callback) {
181190

182-
if (parameters !== undefined) {
191+
if (parameters !== undefined && (typeof(parameters) == 'object' || typeof(parameters) == 'string')) {
192+
193+
if (typeof(parameters) == 'string') {
194+
parameters = { screen_name: parameters };
195+
}
196+
183197
// A full list of parameters can be found here -> http://dev.twitter.com/doc/post/friendships/create
184198
var followParameters = OAuth.formEncode(parameters);
185199

@@ -221,7 +235,12 @@ xTwitter.prototype.follow = function(parameters, callback) {
221235
};
222236
xTwitter.prototype.unfollow = function(parameters, callback) {
223237

224-
if (parameters !== undefined) {
238+
if (parameters !== undefined && (typeof(parameters) == 'object' || typeof(parameters) == 'string')) {
239+
240+
if (typeof(parameters) == 'string') {
241+
parameters = { screen_name: parameters };
242+
}
243+
225244
// A full list of parameters can be found here -> http://dev.twitter.com/doc/post/friendships/destroy
226245
var unfollowParameters = OAuth.formEncode(parameters);
227246

sampleApplication/src/app/views/main/main-scene.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<div x-mojo-element="Button" id="buttonAuth" class="buttonClass" name="buttonAuth"></div>
33
<div x-mojo-element="Button" id="buttonTweet" class="buttonClass" name="buttonTweet"></div>
44
<div x-mojo-element="Button" id="buttonFollow" class="buttonClass" name="buttonFollow"></div>
5-
<p>
5+
66
<div id="info" style="color: #000;"></div>
7-
<p>
8-
<div id="showKeys"></div>
7+
<hr />
8+
<div id="showKeys" style="color: #000; font-size:12px"></div>
99
</div>

sampleApplication/src/appinfo.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "com.biocandy.xtwittersample",
3-
"version": "1.0.0",
3+
"version": "1.0.5",
44
"vendor": "Biocandy Labs",
55
"vendorurl": "http://www.biocandy.com",
66
"type": "web",

0 commit comments

Comments
 (0)