8000 Add metadata to user resource #97 by roundrop · Pull Request #100 · roundrop/facebook4j · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add metadata to user resource #97 #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/FacebookResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ public interface FacebookResponse {
* @see <a href="https://developers.facebook.com/docs/reference/api/#Introspection">Graph API#Introspection - Facebook Developers</a>
*/
interface Metadata {
Metadata.Fields getFields();
String getType();
Metadata.Connections getConnections();

interface Fields {
List<Field> getFields();

interface Field {
String getName();
String getDescription();
String getType();
}
}

interface Connections {
URL getURL(String connectionName);
Expand Down
2 changes: 1 addition & 1 deletion facebook4j-core/src/main/java/facebook4j/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author Ryuji Yamashita - roundrop at gmail.com
*/
public interface User {
public interface User extends FacebookResponse {
String getId();
String getName();
String getFirstName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,142 @@

package facebook4j.internal.json;

import facebook4j.FacebookException;
import facebook4j.FacebookResponse.Metadata;
import facebook4j.internal.org.json.JSONArray;
import facebook4j.internal.org.json.JSONException;
import facebook4j.internal.org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import facebook4j.FacebookException;
import facebook4j.FacebookResponse.Metadata;
import facebook4j.internal.org.json.JSONException;
import facebook4j.internal.org.json.JSONObject;
import static facebook4j.internal.util.z_F4JInternalParseUtil.*;

/*package*/ final class MetadataJSONImpl implements Metadata, java.io.Serializable {
private static final long serialVersionUID = 2544362523876092964L;

private static final long serialVersionUID = 47702530016158838L;

private Metadata.Fields fields;
private String type;
private Metadata.Connections connections;

/*package*/MetadataJSONImpl(JSONObject json) throws FacebookException {
try {
JSONObject connectionsJSONObject = json.getJSONObject("connections");
connections = new ConnectionsJSONImpl(connectionsJSONObject);
fields = new FieldsJSONImpl(json.getJSONArray("fields"));
type = getRawString("type", json);
connections = new ConnectionsJSONImpl(json.getJSONObject("connections"));
} catch (JSONException jsone) {
throw new FacebookException(jsone.getMessage(), jsone);
}
}

public Fields getFields() {
return fields;
}

public String getType() {
return type;
}

public Metadata.Connections getConnections() {
return connections;
}

private final class FieldsJSONImpl implements Metadata.Fields, java.io.Serializable {
private static final long serialVersionUID = -4785397260262958674L;

private List<Metadata.Fields.Field> fields = new ArrayList<Metadata.Fields.Field>();

/*package*/FieldsJSONImpl(JSONArray jsonArray) throws FacebookException {
for (int i = 0; i < jsonArray.length(); i++){
JSONObject fieldJSONObject = null;
try {
fieldJSONObject = jsonArray.getJSONObject(i);
} catch (JSONException jsone) {
throw new FacebookException(jsone.getMessage(), jsone);
}
fields.add(new FieldJSONImpl(fieldJSONObject));
}
}

public List<Metadata.Fields.Field> getFields() {
return fields;
}

private final class FieldJSONImpl implements Metadata.Fields.Field, java.io.Serializable {
private static final long serialVersionUID = -8533365818279231831L;

private String name;
private String description;
private String type;

private FieldJSONImpl(JSONObject json) throws FacebookException {
try {
if (!json.isNull("name")) {
this.name = json.getString("name");
}
if (!json.isNull("description")) {
this.description = json.getString("description");
}
if (!json.isNull("type")) {
this.type = json.getString("type");
}
} catch (JSONException jsone) {
throw new FacebookException(jsone.getMessage(), jsone);
}
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public String getType() {
return type;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof FieldJSONImpl)) return false;

FieldJSONImpl fieldJSON = (FieldJSONImpl) o;

if (description != null ? !description.equals(fieldJSON.description) : fieldJSON.description != null)
return false;
if (name != null ? !name.equals(fieldJSON.name) : fieldJSON.name != null) return false;
if (type != null ? !type.equals(fieldJSON.type) : fieldJSON.type != null) return false;

return true;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "FieldJSONImpl{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", type='" + type + '\'' +
'}';
}
}
}

private final class ConnectionsJSONImpl implements Metadata.Connections, java.io.Serializable {
private static final long serialVersionUID = -826235388607408320L;

Expand All @@ -73,5 +178,29 @@ public URL getURL(String connectionName) {
public List<String> getConnectionNames() {
return Arrays.asList(map.keySet().toArray(new String[map.size()]));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ConnectionsJSONImpl)) return false;

ConnectionsJSONImpl that = (ConnectionsJSONImpl) o;

if (map != null ? !map.equals(that.map) : that.map != null) return false;

return true;
}

@Override
public int hashCode() {
return map != null ? map.hashCode() : 0;
}

@Override
public String toString() {
return "ConnectionsJSONImpl{" +
"map=" + map +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
*
* @author Ryuji Yamashita - roundrop at gmail.com
*/
/*package*/ class UserJSONImpl implements User, Comparable<User>, java.io.Serializable {
private static final long serialVersionUID = 3839339196757459703L;
/*package*/ class UserJSONImpl extends FacebookResponseImpl implements User, Comparable<User>, java.io.Serializable {
private static final long serialVersionUID = 4973575756610859126L;

private String id;
private String name;
Expand Down Expand Up @@ -83,6 +83,7 @@
private User.AgeRange ageRange;

/*package*/UserJSONImpl(HttpResponse res, Configuration conf) throws FacebookException {
super(res);
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.clearThreadLocalMap();
}
Expand Down
9 changes: 9 additions & 0 deletions facebook4j-core/src/test/java/facebook4j/UserMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ public void hometown_string() throws Exception {
assertThat(user.getHometown().getId(), is(nullValue()));
assertThat(user.getHometown().getName(), is("La Plata"));
}

@Test
public void withMetadata() throws Exception {
facebook.setMockJSON("mock_json/user/metadata.json");
User user = facebook.getUser("BillGates", new Reading().metadata());

assertThat(user.getMetadata(), is(notNullValue()));
assertThat(user.getMetadata().getType(), is("page"));
}
}

public static class getPictureURL extends MockFacebookTestBase {
Expand Down
Loading
0