Created
August 28, 2012 15:45
-
-
Save ayush/3499276 to your computer and use it in GitHub Desktop.
Main.as
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import com.wordnik.api.batch.*; | |
import com.wordnik.api.client.*; | |
import com.wordnik.api.entity.*; | |
import com.wordnik.api.entity.word.*; | |
import com.wordnik.api.event.*; | |
import flash.display.Sprite; | |
import flash.text.TextField; | |
public class Main extends Sprite | |
{ | |
private const apiToken: String = "[your-token]"; | |
private const hostName: String = "api.wordnik.com"; | |
private var text:TextField = new TextField(); | |
public function Main() | |
{ | |
// Textfield | |
text.width = 400; | |
text.height = 600; | |
addChild(text); | |
// Credentials | |
var wordnikCredentials: ApiUserCredentials = new ApiUserCredentials(apiToken); | |
wordnikCredentials.hostName = hostName; | |
const wordnikClient: WordnikClient = new WordnikClient(wordnikCredentials); | |
// Add listeners to wait for a response from a call | |
wordnikClient.addEventListener(ApiClientEvent.DEFINITIONS_RESPONSE_EVENT, onWordDefinitions); | |
// Make the call | |
wordnikClient.getWordDefinitions("freedom"); | |
// To make multiple calls in one shot use the batch feature: | |
// Create a batch | |
const batch: Batch = new Batch(); | |
batch.add(new OpGetDefinitions(2)); | |
batch.add(new OpGetExamples()); | |
batch.add(new OpGetRelatedWords()); | |
// Execute it | |
wordnikClient.addEventListener(ApiClientEvent.BATCH_EXECUTE_RESPONSE_EVENT, onBatchResponse); | |
wordnikClient.execute("gargantuan", batch); | |
} | |
private function onWordDefinitions(e: ApiClientEvent): void { | |
if(validateResponse("WordDefinitions", e)) { | |
trace(e); | |
const word: Word = e.response.payload as Word; | |
showMessage("Got " + word.definitions.length + " definitions"); | |
} | |
} | |
private function onBatchResponse(e: ApiClientEvent): void { | |
if(validateResponse("BatchResponse", e)) { | |
trace(e); | |
const word: Word = e.response.payload as Word; | |
const wordDefinition: WordDefinition = word.definitions[0]; | |
showMessage(wordDefinition.word); | |
showMessage(wordDefinition.text); | |
} | |
} | |
private function validateResponse(method: String, e: ApiClientEvent): Boolean { | |
if(e.response.isSuccess) { | |
showMessage("\n" + method + " Succeeded"); | |
return true; | |
} else { | |
showMessage("\n" + method + " FAILED"); | |
return false; | |
} | |
} | |
private function showMessage(message: String): void { | |
text.appendText("\n"); | |
text.appendText(message); | |
trace(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment