Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

issue# (TFJ-734)

  • Loading branch information...
commit 56bac036d33094f2c0f21a37afa9b8c46cd7f707 1 parent 807fd09
Naoya Hatayama ApplePedlar authored
1  readme.txt
@@ -47,6 +47,7 @@ marr-masaaki <marr fiveflavors at gmail.com> @marr
47 47 matsumo <matsumo at ce.ns0.it>
48 48 Max Penet <m at qbits.cc> @mpenet
49 49 Mocel <docel77 at gmail.com> @Mocel
  50 +Naoya Hatayama <applepedlar at gmail.com> @ApplePedlar
50 51 Ngoc Dao <ngocdaothanh@gmail.com> @ngocdaothanh
51 52 Nobutoshi Ogata <n-ogata at cnt.biglobe.co.jp> @nobu666
52 53 Nicolas Bouillon <nicolas at bouil.org>
9 twitter4j-core/src/main/java/twitter4j/User.java
@@ -225,4 +225,13 @@
225 225 * @since Twitter4J 2.1.4
226 226 */
227 227 boolean isFollowRequestSent();
  228 +
  229 + /**
  230 + * Returns URL entities for user description.
  231 + *
  232 + * @return URL entities for user description
  233 + * @since Twitter4J 3.0.4
  234 + */
  235 + URLEntity[] getDescriptionUrlEntities();
  236 +
228 237 }
27 twitter4j-core/src/main/java/twitter4j/internal/json/UserJSONImpl.java
@@ -41,6 +41,7 @@
41 41 private String screenName;
42 42 private String location;
43 43 private String description;
  44 + private URLEntity[] descriptionUrlEntities;
44 45 private boolean isContributorsEnabled;
45 46 private String profileImageUrl;
46 47 private String profileImageUrlHttps;
@@ -104,6 +105,23 @@ private void init(JSONObject json) throws TwitterException {
104 105 screenName = getRawString("screen_name", json);
105 106 location = getRawString("location", json);
106 107 description = getRawString("description", json);
  108 +
  109 + // descriptionUrlEntities <=> entities/descriptions/urls[]
  110 + if (!json.isNull("entities")) {
  111 + JSONObject entitiesJSON = json.getJSONObject("entities");
  112 + if (!entitiesJSON.isNull("description")) {
  113 + JSONObject descriptionEntitiesJSON = entitiesJSON.getJSONObject("description");
  114 + if (!descriptionEntitiesJSON.isNull("urls")) {
  115 + JSONArray urlsArray = descriptionEntitiesJSON.getJSONArray("urls");
  116 + int len = urlsArray.length();
  117 + descriptionUrlEntities = new URLEntity[len];
  118 + for (int i = 0; i < len; i++) {
  119 + descriptionUrlEntities[i] = new URLEntityJSONImpl(urlsArray.getJSONObject(i));
  120 + }
  121 + }
  122 + }
  123 + }
  124 +
107 125 isContributorsEnabled = getBoolean("contributors_enabled", json);
108 126 profileImageUrl = getRawString("profile_image_url", json);
109 127 profileImageUrlHttps = getRawString("profile_image_url_https", json);
@@ -508,6 +526,14 @@ public boolean isFollowRequestSent() {
508 526 return isFollowRequestSent;
509 527 }
510 528
  529 + /**
  530 + * {@inheritDoc}
  531 + */
  532 + @Override
  533 + public URLEntity[] getDescriptionUrlEntities() {
  534 + return descriptionUrlEntities;
  535 + }
  536 +
511 537 /*package*/
512 538 static PagableResponseList<User> createPagableUserList(HttpResponse res, Configuration conf) throws TwitterException {
513 539 try {
@@ -626,4 +652,5 @@ public String toString() {
626 652 ", isFollowRequestSent=" + isFollowRequestSent +
627 653 '}';
628 654 }
  655 +
629 656 }
57 twitter4j-core/src/test/java/twitter4j/internal/json/UserJSONImplTest.java
... ... @@ -0,0 +1,57 @@
  1 +/*
  2 + * Copyright 2007 Yusuke Yamamoto
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + *
  16 + * @author Naoya Hatayama - applepedlar at gmail.com
  17 + */
  18 +
  19 +package twitter4j.internal.json;
  20 +
  21 +import twitter4j.TwitterException;
  22 +import twitter4j.URLEntity;
  23 +import twitter4j.internal.org.json.JSONException;
  24 +import twitter4j.internal.org.json.JSONObject;
  25 +import junit.framework.TestCase;
  26 +
  27 +public class UserJSONImplTest extends TestCase {
  28 +
  29 + public void testGetDescriptionUrlEntities1() throws JSONException, TwitterException {
  30 + String rawJson = "{\"id\":219570539,\"id_str\":\"219570539\",\"name\":\"\\u9577\\u3044\\u9577\\u3044\\u9577\\u3044\\u9577\\u3044\\u3042\\u3042\\u3042\\u3042\\u3042\\u3042\\u304b\\u3055\\u305f\\u306a\\u306f\\u3046\",\"screen_name\":\"gjmp10\",\"location\":\"\\u4eac\\u90fd\\u5e9c\\u4eac\\u90fd\\u5e02\\u6771\\u5c71\\u533a\\u4e09\\u6761\\u901a\\u5357\\u4e8c\\u7b4b\\u76ee\\u767d\\u5ddd\\u7b4b\\u897f\\u5165\\u30eb\\u4e8c\\u4e01\\u76ee\",\"description\":\"aa\\n09012345678\\n@kouda12345 #test http:\\/\\/t.co\\/n578Jr6S http:\\/\\/t.co\\/i2cX7pEY http:\\/\\/t.co\\/PlItS8jh 0123456789\\n\\u3066\\u3059\\u3068 @gjmp9\",\"url\":\"https:\\/\\/www.google.co.jp\\/search?q=adgjm\",\"entities\":{\"url\":{\"urls\":[{\"url\":\"https:\\/\\/www.google.co.jp\\/search?q=adgjm\",\"expanded_url\":null,\"indices\":[0,39]}]},\"description\":{\"urls\":[{\"url\":\"http:\\/\\/t.co\\/n578Jr6S\",\"expanded_url\":\"http:\\/\\/jigtwi.jp\\/\",\"display_url\":\"jigtwi.jp\",\"indices\":[33,53]},{\"url\":\"http:\\/\\/t.co\\/i2cX7pEY\",\"expanded_url\":\"http:\\/\\/twitpic.com\\/7tx0wk\",\"display_url\":\"twitpic.com\\/7tx0wk\",\"indices\":[54,74]},{\"url\":\"http:\\/\\/t.co\\/PlItS8jh\",\"expanded_url\":\"http:\\/\\/twitter.com\\/ak_12\\/status\\/12813969077051392\",\"display_url\":\"twitter.com\\/ak_12\\/status\\/1\\u2026\",\"indices\":[79,99]}]}},\"protected\":false,\"followers_count\":9,\"friends_count\":13,\"listed_count\":6,\"created_at\":\"Thu Nov 25 06:48:26 +0000 2010\",\"favourites_count\":249,\"utc_offset\":32400,\"time_zone\":\"Osaka\",\"geo_enabled\":true,\"verified\":false,\"statuses_count\":964,\"lang\":\"ja\",\"status\":{\"created_at\":\"Wed Dec 05 09:03:31 +0000 2012\",\"id\":276250432630833152,\"id_str\":\"276250432630833152\",\"text\":\"test3 http:\\/\\/t.co\\/oUG7DSeN\",\"source\":\"\\u003ca href=\\\"http:\\/\\/jigtwi.jp\\/?p=1\\\" rel=\\\"nofollow\\\"\\u003ejigtwi\\u003c\\/a\\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweet_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[],\"media\":[{\"id\":276250432635027456,\"id_str\":\"276250432635027456\",\"indices\":[6,26],\"media_url\":\"http:\\/\\/pbs.twimg.com\\/media\\/A9VwTgpCQAAInTL.jpg\",\"media_url_https\":\"https:\\/\\/pbs.twimg.com\\/media\\/A9VwTgpCQAAInTL.jpg\",\"url\":\"http:\\/\\/t.co\\/oUG7DSeN\",\"display_url\":\"pic.twitter.com\\/oUG7DSeN\",\"expanded_url\":\"http:\\/\\/twitter.com\\/gjmp10\\/status\\/276250432630833152\\/photo\\/1\",\"type\":\"photo\",\"sizes\":{\"medium\":{\"w\":600,\"h\":901,\"resize\":\"fit\"},\"thumb\":{\"w\":150,\"h\":150,\"resize\":\"crop\"},\"small\":{\"w\":340,\"h\":510,\"resize\":\"fit\"},\"large\":{\"w\":1024,\"h\":1537,\"resize\":\"fit\"}}}]},\"favorited\":false,\"retweeted\":false,\"possibly_sensitive\":false},\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"FF8F00\",\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/729801918\\/e6f0992a68eda21a2809efbec2f88050.jpeg\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/729801918\\/e6f0992a68eda21a2809efbec2f88050.jpeg\",\"profile_background_tile\":true,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2934786416\\/e6f0992a68eda21a2809efbec2f88050_normal.jpeg\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2934786416\\/e6f0992a68eda21a2809efbec2f88050_normal.jpeg\",\"profile_link_color\":\"0084B4\",\"profile_sidebar_border_color\":\"FFFFFF\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":false,\"follow_request_sent\":false,\"notifications\":false}";
  31 + JSONObject json = new JSONObject(rawJson);
  32 + UserJSONImpl user = new UserJSONImpl(json);
  33 +
  34 + URLEntity[] descriptionUrlEntities = user.getDescriptionUrlEntities();
  35 + assertNotNull(descriptionUrlEntities);
  36 + assertEquals(3, descriptionUrlEntities.length);
  37 +
  38 + assertEquals("http://jigtwi.jp/", descriptionUrlEntities[0].getExpandedURL());
  39 + assertEquals("jigtwi.jp", descriptionUrlEntities[0].getDisplayURL());
  40 + assertEquals("http://t.co/n578Jr6S", descriptionUrlEntities[0].getURL());
  41 + assertEquals(33, descriptionUrlEntities[0].getStart());
  42 + assertEquals(53, descriptionUrlEntities[0].getEnd());
  43 +
  44 + assertEquals("http://twitpic.com/7tx0wk", descriptionUrlEntities[1].getExpandedURL());
  45 + assertEquals("twitpic.com/7tx0wk", descriptionUrlEntities[1].getDisplayURL());
  46 + assertEquals("http://t.co/i2cX7pEY", descriptionUrlEntities[1].getURL());
  47 + assertEquals(54, descriptionUrlEntities[1].getStart());
  48 + assertEquals(74, descriptionUrlEntities[1].getEnd());
  49 +
  50 + assertEquals("http://twitter.com/ak_12/status/12813969077051392", descriptionUrlEntities[2].getExpandedURL());
  51 + assertEquals("twitter.com/ak_12/status/1…", descriptionUrlEntities[2].getDisplayURL());
  52 + assertEquals("http://t.co/PlItS8jh", descriptionUrlEntities[2].getURL());
  53 + assertEquals(79, descriptionUrlEntities[2].getStart());
  54 + assertEquals(99, descriptionUrlEntities[2].getEnd());
  55 + }
  56 +
  57 +}

0 comments on commit 56bac03

Please sign in to comment.
Something went wrong with that request. Please try again.