class EzOAuth2 constructor: ( @root_url, @customer_id, @customer_secret, @endpoint_uri ) -> id = Ti.Utils.md5HexDigest( @root_url + @customer_id + @customer_secret + @endpoint_uri ) @access_token = Ti.App.Properties.getString( id + '_access', null ) @refresh_token = Ti.App.Properties.getString( id + '_refresh', null ) @update_stamp = Ti.App.Properties.getString( id + '_updated', null ) @expiry_time = Ti.App.Properties.getInt( id + '_expires', 3600 ) Ti.API.debug( 'EzOAuth2 object initialized.' ) saveSettings: -> id = Ti.Utils.md5HexDigest( @root_url + @customer_id + @customer_secret + @endpoint_uri ) Ti.App.Properties.setString( id + '_access', @access_token ) Ti.App.Properties.setString( id + '_refresh', @refresh_token ) Ti.App.Properties.setString( id + '_updated', @update_stamp ) Ti.App.Properties.setInt( id + '_expires', @expiry_time ) getAuthView: ( success_function, cancel_function )-> authUrl = @root_url + '/oauth/authorize' + '?response_type=token' + '&client_id=' + @customer_id + '&redirect_uri=' + encodeURIComponent( @endpoint_uri ) Ti.API.debug( 'authUrl: ' + authUrl ) wv = Ti.UI.createWebView( url: authUrl ) wv.parent_obj = this wv.success_function = success_function wv.cancel_function = cancel_function wv.addEventListener( 'beforeload', @processOAuth2Response ) @auth_web = wv @auth_btn = Ti.UI.createButton( left: 0 top: 0 title: "Cancel" ) @auth_btn.click_function = cancel_function @auth_btn.addEventListener( 'click', (e) -> e.source.auth_view.hide() e.source.click_function( e ) ) @auth_view = Ti.UI.createView() @auth_view.add( @auth_web ) @auth_view.add( @auth_btn ) @auth_web.auth_view = @auth_view @auth_btn.auth_view = @auth_view return @auth_view doRequest: ( url, success_function, type = 'GET' ) -> if @access_token is null Ti.API.warning( 'No access token.' ) return false if Ti.Network.networkType is Ti.Network.NETWORK_NONE Ti.API.warning( 'No Internet connection.' ) return false xhr = Ti.Network.createHTTPClient( onerror: -> Ti.API.error( 'ERROR communicating. (' + this.responseText + ')' ) onload: success_function ) xhr.open( type, @root_url + '/api/ezp/v1' + url ) xhr.setRequestHeader( 'Authorization', 'OAuth ' + @access_token ) xhr.setRequestHeader( 'Accept', 'application/json' ) xhr.send() getNode: ( node_id, success_function ) -> @doRequest( '/content/node/' + node_id, success_function ) getNodeFields: ( node_id, success_function ) -> @doRequest( '/content/node/' + node_id + '/fields', success_function ) getNodeField: ( node_id, field_id, success_function ) -> @doRequest( '/content/node/' + node_id + '/field/' + field_id, success_function ) getNodeList: ( root_node_id, success_function, offset = null, limit = null, sortKey = null, sortType = null ) -> req_url = '/content/node/' + root_node_id + '/list' req_url += '/offset/' + offset if offset? req_url += '/limit/' + limit if limit? if sortKey? req_url += '/sort/' + sortKey req_url += '/' + sortType if sortType? @doRequest( req_url, success_function ) getGetParameters: ( url ) -> # borrowed and modified from: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html vars = {} pairs = url.slice( url.indexOf( '?' ) + 1 ).split( '&' ) for pair in pairs Ti.API.debug( 'Now splitting: "' + pair + '"' ) pair_split = pair.split( '=' ) vars[ pair_split[0] ] = pair_split[1] return vars processOAuth2Response: ( e ) -> Ti.API.debug( '[beforeload] URL: ' + e.url ) Ti.API.debug( 'Endpoint URI: ' + e.source.parent_obj.endpoint_uri ) Ti.API.debug( 'Index:' + e.url.indexOf( e.source.parent_obj.endpoint_uri ) ) if e.url.indexOf( e.source.parent_obj.endpoint_uri ) is 0 Ti.API.info( 'ENDPOINT URL DETECTED!!!!' ) e.source.parent_obj.final_url = e.url params = e.source.parent_obj.getGetParameters( e.url ) e.source.parent_obj.access_token = params['access_token'] e.source.parent_obj.refresh_token = params['refresh_token'] e.source.parent_obj.expiry_time = params['expires_in'] e.source.parent_obj.update_stamp = Math.round( +new Date() / 1000 ) e.source.auth_view.hide() Ti.API.info( 'Got new token. Will expire in ' + params['expires_in'] + ' seconds.' ) e.source.success_function( e )