-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlipJS.js
124 lines (97 loc) · 2.63 KB
/
BlipJS.js
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
/*****
BlipJS v1.0
A dead-simple, client-side way to grab info via the Blipfoto API.
Copyright 2011 Graham Bradley
This work is licensed under a Creative Commons Attribution-Noncommercial 3.0
Unported License (http://creativecommons.org/licenses/by-nc/3.0/).
*****/
(function(G, D){
var name = 'BlipJS',
me = function(key, conf){
this.key = key;
this.conf = conf || {
baseURL : 'http://api.blipfoto.com/',
version : 2,
test : 0
};
this._requestID = 0;
};
me._sendRequest = function(requestID, url, opt){
var script = D.createElement('script'),
id = name+requestID;
me._requestCache[id] = function(json){
var delScript = D.getElementById(id);
if (delScript){
delScript.parentNode.removeChild(delScript);
}
delete me._requestCache[id];
if (json.error && json.error.code){
if (opt.error){
opt.error(json.error);
}
}
else if (opt.complete){
opt.complete(json.data);
}
};
script.id = id;
script.src = url+'&callback='+name+'._requestCache["'+id+'"]';
D.getElementsByTagName('head')[0].appendChild(script);
};
me._requestCache = {};
me.prototype = {
get : function(resource, opt){
return this._request('get', resource, opt);
},
search : function(query, callback, max){
return this._request('get', 'search', {
params : {max : max || 12, query : encodeURIComponent(query)},
complete : callback
});
},
view : function(view, callback, max){
return this._request('get', 'view', {
params : {max : max || 12, view : view},
complete : callback
});
},
entry : function(which, callback, what){
var params = {data : (what || ['display_name','journal_title','entry_id','date','title','image']).join(',')};
if (typeof which == 'number'){
params.entry_id = which;
}
else {
params.entry_date = 'latest';
params.display_name = which;
}
return this._request('get', 'entry', {
params : params,
complete : function(data){
callback(data[0]);
}
});
},
_request : function(method, resource, opt){
var opt = opt || {},
url = this.conf.baseURL+method+'/'+resource+'/?'+this._getParams(opt.params || {});
me._sendRequest(this._requestID++, url, opt);
},
_getParams : function(params){
params.api_key = this.key;
params.version = this.conf.version;
params.format = 'JSON';
if (this.test){
defaults.test = 1;
}
var qs = [],
k;
for (k in params){
if (params.hasOwnProperty(k)){
qs.push(k+'='+params[k]);
}
}
return qs.join('&');
}
};
G[name] = me;
})(window, document);