1
+ define ( [ "jquery" , "mustache" ] , function ( $ , mustache ) {
2
+ var url_base = "http://reader.livedoor.com" ;
3
+ var auth_url = url_base + "/apps/authorize" ;
4
+ var conn = new LDR . Connect ( auth_url ) ;
5
+
6
+ $ ( "#authorize_button" ) . click ( function ( ) {
7
+ if ( conn . authorized ( ) ) {
8
+ console . log ( "You've authorized." ) ;
9
+ } else {
10
+ conn . authorize ( {
11
+ client_id : location . href ,
12
+ name : "api test" ,
13
+ expires_in : 3600 * 3
14
+ } ) ;
15
+ }
16
+ } ) ;
17
+
18
+ $ ( "#clear_button" ) . click ( function ( ) {
19
+ conn . token = null ;
20
+ conn . token_store . clear ( ) ;
21
+ } ) ;
22
+
23
+ var compiledTemplate = mustache . compile ( $ ( '#tpl' ) . html ( ) ) ;
24
+ $ ( "article.api" ) . each ( function ( ) {
25
+ var api_url = $ ( this ) . find ( "h3" ) . text ( ) ;
26
+ // api definition
27
+ var params = $ ( this ) . find ( ".parameters" ) ;
28
+ if ( params ) {
29
+ var definition = '{"params":' + params . text ( ) + '}' ;
30
+ $ ( this ) . append ( compiledTemplate ( $ . parseJSON ( definition . replace ( / & q u o t ; / ig, '"' ) ) ) ) ;
31
+ var api_name = api_url . replace ( / \/ / ig, '_' ) ;
32
+ $ ( this ) . attr ( "name" , api_name ) ;
33
+ // api post
34
+ var apiForm = $ ( this ) . find ( 'form[name="apiForm"]' ) ;
35
+ $ ( this ) . find ( 'input[name="post"]' ) . click ( function ( ) {
36
+ var callback = function ( res ) {
37
+ $ ( 'article.api[name="' + api_name + '"]' ) . find ( ".result" ) . html ( "<pre>" + JSON . stringify ( res , null , 4 ) + "</pre>" ) ;
38
+ } ;
39
+ var errback = callback ;
40
+ conn . request ( "POST" , api_url + "?" + $ ( apiForm ) . serialize ( ) , { } , callback , errback ) ;
41
+ } ) ;
42
+ }
43
+ } ) ;
44
+ } ) ;
0 commit comments