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
|
[
// Flickr API Connector for Lasso
// (c) 2007 Pieter Claerhout, SHpartners
// www.shpartners.com
// Define the FlickrAPI type
Define_Type(
'shp_FlickrApi',
-prototype,
-description='Implements the Flickr API .'
);
// The local variables to use
local: 'ApiKey' = string;
// onCreate
Define_Tag: 'oncreate', -req='ApiKey';
// Remember the API key
self->ApiKey = #ApiKey;
/Define_Tag;
// Define the flickrRequest tag
Define_Tag: 'Request', -Required='Method', -Required='Params', -EncodeNone;
// The rest url for Flickr
local:'flickrRestUrl' = 'http://api.flickr.com/services/rest/';
// Construct the url parameters
local:'urlParams' = (Array: 'method'=#Method, 'api_key'=self->ApiKey, 'nojsoncallback'=1, 'format'='json');
Iterate(#Params->Keys, (local: 'key'));
#urlParams->Insert(#key=#Params->find(#key));
/Iterate;
// Perform the Flickr Request
local:'Result' = (Include_URL: #flickrRestUrl, -GETParams=#urlParams);
// Decode the result
local:'DecodedResult' = self->DecodeJSON(#Result);
// Check the status of the request
If: #DecodedResult->find('stat') == 'fail';
Fail: #DecodedResult->find('code'), #DecodedResult->find('message');
/If;
// Return the decoded result
Return: #DecodedResult;
/Define_Tag;
// Define the flickrPhotoUrl tag
Define_Tag: 'PhotoUrl', -Required='Photo', -Required='Size', -EncodeNone;
// The list of known sizes
local:'KnownSizes' = (Map: 'square'='_s', 'thumb'='_t', 'small'='_m', 'medium'='', 'large'='_b');
// Get the size
If: #KnownSizes->Find(#Size) == null;
local:'photo_size' = '';
Else;
local:'photo_size' = #KnownSizes->Find(#Size);
/If;
// Construct the url
local:'Url' = "http://farm" + #Photo->find('farm') + ".static.flickr.com/" + #Photo->find('server') + "/" + #Photo->find('id') + "_" + #Photo->find('secret') + #photo_size + ".jpg";
// Return the url
Return: #Url;
/Define_Tag;
// Get the page url for a photo
Define_Tag: 'PhotoPage', -Required='Photo', -Required='UserId', -EncodeNone;
// Return the url
Return: 'http://www.flickr.com/photos/' + #UserId + '/' + #Photo->find('id');
/Define_Tag;
// Get information about a person
Define_Tag: 'PeopleGetInfo', -Required='UserId', -EncodeNone;
// Perform the Flickr request
local:'Result' = self->Request(
-Method = 'flickr.people.getInfo',
-Params = (Map: 'user_id' = #UserId)
);
// Return the result
Return: #Result->find('person');
/Define_Tag;
// Custom JSON decoder (because of a bug in the default library that doesn't always interpret numbers correctly)
Define_Tag: 'DecodeJSON', -Required='value';
(#value == '') ? Return: Null;
Define_Tag: 'consume_string', -Required='ibytes';
Local: 'obytes' = bytes;
local: 'temp' = 0;
While: ((#temp := #ibytes->(export8bits: #temp)) != 34);
#obytes->(import8bits: #temp);
(#temp == 92) ? #obytes->(import8bits: #ibytes->export8bits); // Escape \
/While;
Local: 'output' = ((String: #obytes)->(Replace: '\\"', '\"') & (Replace: '\\r', '\r') & (Replace: '\\n', '\n') & (Replace: '\\t', '\t') & (Replace: '\\f', '\f') & (Replace: '\\b', '\b') &);
If: #output->(BeginsWith: '<LassoNativeType>') && #output->(EndsWith: '</LassoNativeType>');
Local: 'temp' = #output - '<LassoNativeType>' - '</LassoNativeType>';
Local: 'output' = null;
Protect;
#output->(Deserialize: #temp);
/Protect;
/If;
Return: @#output;
/Define_Tag;
Define_Tag: 'consume_token', -Required='ibytes', -required='temp';
Local: 'obytes' = bytes->(import8bits: #temp) &;
local: 'delimit' = (array: 9, 10, 13, 32, 44, 58, 93, 125); // \t\r\n ,:]}
While: (#delimit !>> (#temp := #ibytes->export8bits));
#obytes->(import8bits: #temp);
/While;
Local: 'output' = (String: #obytes);
If: (#output == 'true') || (#output == 'false');
Return: (Boolean: #output);
Else: (#output == 'null');
Return: Null;
Else: (String_IsNumeric: #output);
Return: (#output >> '.') ? (Decimal: #output) | (Integer: #output);
/If;
Return: @#output;
/Define_Tag;
Define_Tag: 'consume_array', -Required='ibytes';
Local: 'output' = array;
local: 'delimit' = (array: 9, 10, 13, 32, 44); // \t\r\n ,
local: 'temp' = 0;
While: ((#temp := #ibytes->export8bits) != 93); // ]
If: (#delimit >> #temp);
Else: (#temp == 34); // "
#output->(insert: (consume_string: @#ibytes));
Else: (#temp == 91); // [
#output->(insert: (consume_array: @#ibytes));
Else: (#temp == 123); // {
#output->(insert: (consume_object: @#ibytes));
Else;
#output->(insert: (consume_token: @#ibytes, @#temp));
(#temp == 93) ? Loop_Abort;
/If;
/While;
Return: @#output;
/Define_Tag;
Define_Tag: 'consume_object', -Required='ibytes';
Local: 'output' = map;
local: 'delimit' = (array: 9, 10, 13, 32, 44); // \t\r\n ,
local: 'temp' = 0;
local: 'key' = null;
local: 'val' = null;
While: ((#temp := #ibytes->export8bits) != 125); // }
If: (#delimit >> #temp);
// Discard whitespace
Else: (#key !== null) && (#temp == 34); // "
#output->(insert: #key = (consume_string: @#ibytes));
#key = null;
Else: (#key !== null) && (#temp == 91); // [
#output->(insert: #key = (consume_array: @#ibytes));
#key = null;
Else: (#key !== null) && (#temp == 123); // {
#output->(insert: #key = (consume_object: @#ibytes));
#key = null;
Else: (#key !== null);
#output->(insert: #key = (consume_token: @#ibytes, @#temp));
(#temp == 125) ? Loop_abort;
#key = null;
Else;
#key = (consume_string: @#ibytes);
while(#delimit >> (#temp := #ibytes->export8bits));
/while;
#temp != 58 ? Loop_Abort;
/If;
/While;
If: (#output >> '__jsonclass__') && (#output->(Find: '__jsonclass__')->(isa: 'array')) && (#output->(Find: '__jsonclass__')->size >= 2) && (#output->(Find: '__jsonclass__')->First == 'deserialize');
Return: #output->(find: '__jsonclass__')->Second->First;
Else: (#output >> 'native') && (#output >> 'comment') && (#output->(find: 'comment') == 'http://www.lassosoft.com/json');
Return: #output->(find: 'native');
/If;
Return: @#output;
/Define_Tag;
Local: 'ibytes' = (bytes: #value);
Local: 'start' = 1;
#ibytes->removeLeading(BOM_UTF8);
Local: 'temp' = #ibytes->export8bits;
If: (#temp == 91); // [
Local: 'output' = (consume_array: @#ibytes);
Return: @#output;
Else: (#temp == 123); // {
Local: 'output' = (consume_object: @#ibytes);
Return: @#output;
/If;
/Define_Tag;
/Define_Type;
]
|