-
Notifications
You must be signed in to change notification settings - Fork 2
/
plurk-user.vala
259 lines (221 loc) · 8.09 KB
/
plurk-user.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Json;
using Soup;
public class Roguso.User : GLib.Object {
// Minimal data
private static const string KEY_USER_ID = "id";
private static const string KEY_NICK_NAME = "nick_name";
private static const string KEY_DISPLAY_NAME = "display_name";
private static const string KEY_GENDER = "gender";
private static const string KEY_HAS_PROFILE_IMAGE = "has_profile_image";
private static const string KEY_AVATAR = "avatar";
// Full data
private static const string KEY_FULL_NAME = "full_name";
private static const string KEY_LOCATION = "location";
private static const string KEY_DATE_OF_BIRTH = "date_of_birth";
private static const string KEY_RELATIONSHIP = "relationship";
private static const string KEY_PAGE_TITLE = "page_title";
private static const string KEY_RECRUITED = "recruited";
private static const string KEY_KARMA = "karma";
private static const string KEY_IS_PREMIUM = "is_premium";
private static const string KEY_TIMEZONE = "timezone";
public string user_id { get; set; default = null; }
public string nick_name { get; set; default = null; }
public string display_name { get; set; default = null; }
public Gender gender { get; set; default = Gender.NOT_STATING_OR_OTHER; }
public bool has_profile_image { get; set; default = false; }
public int64 avatar_version { get; set; default = 0; }
public string full_name { get; set; default = null; }
public string location { get; set; default = null; }
public unowned Soup.Date date_of_birth { get; set; default = null; }
public Relationship relationship { get; set; default = Relationship.NOT_SAYING; }
public string page_title { get; set; default = null; }
public int64 recruited { get; set; default = 0; }
public double karma { get; set; default = 0.0; }
public bool is_premium { get; set; default = false; }
public static enum Relationship {
NOT_SAYING,
SINGLE,
MARRIED,
DIVORCED,
ENGAGED,
IN_RELATIONSHIP,
COMPLICATED,
WIDOWED,
OPEN_RELATIONSHIP;
public static Relationship from_string(string val) {
switch (val) {
case "single":
return SINGLE;
case "married":
return MARRIED;
case "divorced":
return DIVORCED;
case "engaged":
return ENGAGED;
case "in_relationship":
return IN_RELATIONSHIP;
case "complicated":
return COMPLICATED;
case "widowed":
return WIDOWED;
case "open_relationship":
return OPEN_RELATIONSHIP;
default:
return NOT_SAYING;
}
}
public string to_string() {
switch (this) {
case SINGLE:
return "single";
case MARRIED:
return "married";
case DIVORCED:
return "divorced";
case ENGAGED:
return "engaged";
case IN_RELATIONSHIP:
return "in_relationship";
case COMPLICATED:
return "complicated";
case WIDOWED:
return "widowed";
case OPEN_RELATIONSHIP:
return "open_relationship";
default:
return "not_saying";
}
}
}
public static enum Gender {
MALE = 0,
FEMALE = 1,
NOT_STATING_OR_OTHER = 2;
public static Gender from_int(int64 val) {
switch (val) {
case 0:
return MALE;
case 1:
return FEMALE;
default:
return NOT_STATING_OR_OTHER;
}
}
public int to_int() {
switch (this) {
case MALE:
return 0;
case FEMALE:
return 1;
default:
return 2;
}
}
}
/*
* Constructors
*/
public User() { }
public User.from_data(string buffer) {
this();
Parser parser = new Parser();
try {
parser.load_from_data(buffer);
build(parser.get_root());
} catch ( Error e) {
stdout.printf("Error: %s\n", e.message);
}
}
public User.from_node(Json.Node node) {
this();
build(node);
}
public void load_from_data(string buffer) {
clean();
Parser parser = new Parser();
try {
parser.load_from_data(buffer);
build(parser.get_root());
} catch ( Error e ) {
stdout.printf("Error: %s\n", e.message);
}
}
private void clean() {
user_id = null;
nick_name = null;
display_name = null;
gender = Gender.NOT_STATING_OR_OTHER;
has_profile_image = false;
avatar_version = 0;
full_name = null;
location = null;
date_of_birth = null;
relationship = Relationship.NOT_SAYING;
page_title = null;
recruited = 0;
karma = 0.0;
is_premium = false;
}
private void build(Json.Node node) {
if ( node.get_node_type() != NodeType.OBJECT ) {
return;
}
Json.Object object = node.get_object();
unowned Json.Node node_user_id = object.get_member(KEY_USER_ID);
if ( node_user_id != null ) {
user_id = node_user_id.get_int().to_string();
}
unowned Json.Node node_nick_name = object.get_member(KEY_NICK_NAME);
if ( node_nick_name != null ) {
nick_name = node_nick_name.dup_string();
}
unowned Json.Node node_display_name = object.get_member(KEY_DISPLAY_NAME);
if ( node_display_name != null ) {
display_name = node_display_name.dup_string();
}
unowned Json.Node node_gender = object.get_member(KEY_GENDER);
if ( node_gender != null ) {
gender = Gender.from_int(node_gender.get_int());
}
unowned Json.Node node_has_profile_image = object.get_member(KEY_HAS_PROFILE_IMAGE);
if ( node_has_profile_image != null ) {
has_profile_image = ( node_has_profile_image.get_int() == 1 );
}
unowned Json.Node node_avatar_version = object.get_member(KEY_AVATAR);
if ( node_avatar_version != null && node_avatar_version.get_node_type() == NodeType.VALUE ) {
avatar_version = node_avatar_version.get_int();
}
unowned Json.Node node_full_name = object.get_member(KEY_FULL_NAME);
if ( node_full_name != null ) {
full_name = node_full_name.dup_string();
}
unowned Json.Node node_location = object.get_member(KEY_LOCATION);
if ( node_location != null ) {
location = node_location.dup_string();
}
unowned Json.Node node_date_of_birth = object.get_member(KEY_DATE_OF_BIRTH);
if ( node_date_of_birth != null && node_date_of_birth.get_node_type() != NodeType.NULL ) {
date_of_birth = new Soup.Date.from_string(node_date_of_birth.get_string());
}
unowned Json.Node node_relationship = object.get_member(KEY_RELATIONSHIP);
if ( node_relationship != null ) {
relationship = Relationship.from_string(node_relationship.get_string());
}
unowned Json.Node node_page_title = object.get_member(KEY_PAGE_TITLE);
if ( node_page_title != null ) {
page_title = node_page_title.dup_string();
}
unowned Json.Node node_recruited = object.get_member(KEY_RECRUITED);
if ( node_recruited != null ) {
recruited = node_recruited.get_int();
}
unowned Json.Node node_karma = object.get_member(KEY_KARMA);
if ( node_karma != null ) {
karma = node_karma.get_double();
}
unowned Json.Node node_is_premium = object.get_member(KEY_IS_PREMIUM);
if ( node_is_premium != null ) {
is_premium = node_is_premium.get_boolean();
}
}
}