How to read a tweet

by javier (modified: 2017 Jul 03)

How to read a Tweet (Part 3)

In the previous entries, we have learned how to authenticate with Twitter and then how to parse the response

Here we will show you how to read a tweet. We will use the Twitter resource statuses/show.json endpoint, which returns data for a Tweet, given its ID number.

Resource URL

Returns a single Tweet, specified by the id parameter. The Tweet’s author will also be embedded within the Tweet.

Check the documentation for details here: Status Show


In this example, we will also parse JSON response and we will create a new object tweet instance of TWITTER_TWEETS class. First, we will check that the Status code is 200, so we know everything is Ok and we have a response body, in other case we show the response status code.

The following snippet code shows how we do that after we got the Twitter's response, the l_body variable have the JSON response from Twitter. Finally we print out the tweeter details.

if l_response.status = 200 and then attached l_response.body as l_body then if attached {TWITTER_TWEETS} (create {TWITTER_JSON}).show_tweet (l_body) as l_tweet then print (l_tweet.full_out) end else print ("%NResponse: STATUS:" + l_response.status.out) end

Let's see how this work in details. The class TWITTER_JSON is responsible for parsing the JSON input and generate the corresponding Twitter object, in this case, a tweet, after calling the feature show_tweet.

show_tweet (a_string: STRING): detachable TWITTER_TWEETS local err: DEVELOPER_EXCEPTION do if attached parsed_json (a_string) as j then if attached string_value_from_json (j, "error") as l_error then create err err.set_description (l_error) err.raise elseif attached {JSON_ARRAY} json_value (j, "errors") as l_array then create err if attached string_value_from_json (l_array.i_th (1), "message") as l_err_message then err.set_description (l_err_message) end err.raise else Result := twitter_tweets (Void, j) end else print (a_string) end end

This feature will parse the input a_string, representing the Twitter's response, and if everything is ok, will return an object representing a TWITTER_TWEETS using the feature twitter_tweets, that will generate an object tweet from a JSON representation.

If the feature {TWITTER_JSON}).show_tweet (l_body) can't parse the body, it will raise an error.

twitter_tweets (a_tweet: detachable like twitter_tweets; a_json: JSON_VALUE): TWITTER_TWEETS -- Fill `a_tweet' from `a_json' require a_json_attached: a_json /= Void do if a_tweet /= Void then Result := a_tweet else create Result end Result.set_created_at (string_value_from_json (a_json, "created_at")) Result.set_text (string_value_from_json (a_json, "text")) if attached {JSON_OBJECT} json_value (a_json, "user") as l_user then Result.set_user (twitter_user (Void, l_user)) end end

Code Example

Get the code from here:

apis \twitter \twitter_tutorial \read