PageRenderTime 22ms CodeModel.GetById 7ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 2ms

/flash/echo-nest-flash-api/src/com/ryanberdeen/echonest/api/v3/track/AnalysisLoader.as

http://echo-nest-remix.googlecode.com/
ActionScript | 135 lines | 113 code | 19 blank | 3 comment | 20 complexity | 287657a3def804ddd0cb41ea0eb147b5 MD5 | raw file
  1package com.ryanberdeen.echonest.api.v3.track {
  2  import com.ryanberdeen.echonest.api.v3.EchoNestError;
  3  import com.ryanberdeen.echonest.api.v3.EchoNestErrorEvent;
  4
  5  import flash.events.Event;
  6  import flash.events.EventDispatcher;
  7  import flash.net.URLLoader;
  8  import flash.utils.Timer;
  9
 10  public class AnalysisLoader extends EventDispatcher {
 11    private var trackApi:TrackApi;
 12    private var timer:Timer;
 13    private var apiLoaders:Array;
 14    private var apiLoadersComplete:int;
 15
 16    private var parameters:Object;
 17    private var _properties:Object;
 18
 19    private var _analysis:Object;
 20
 21    public function AnalysisLoader(trackApi:TrackApi):void {
 22      this.trackApi = trackApi;
 23      _properties = TrackApi.ALL_PROPERTIES;
 24      apiLoaders = [];
 25      _analysis = {};
 26    }
 27
 28    public function get analysis():Object {
 29      return _analysis;
 30    }
 31
 32    public function set properties(properties:Array):void {
 33      if (properties == null || properties.length == 0) {
 34        _properties = TrackApi.ALL_PROPERTIES;
 35      }
 36      else {
 37        _properties = properties;
 38      }
 39    }
 40
 41    public function load(parameters:Object):void {
 42      this.parameters = parameters;
 43      getMetadata();
 44    }
 45
 46    public function reload():void {
 47      // TODO exception if currently loading
 48      getMetadata();
 49    }
 50
 51    private function waitForAnalysis(delay:int):void {
 52      timer = new Timer(delay, 1);
 53      timer.addEventListener('timer', function(e:Event):void {
 54        timer = null;
 55        getMetadata();
 56      });
 57      timer.start();
 58    }
 59
 60    private function getMetadata():void {
 61      trackApi.getMetadata(parameters, {
 62        onResponse: metadataResponseHandler,
 63        onEchoNestError: metadataEchoNestErrorHandler,
 64        onError: errorHandler
 65      });
 66    }
 67
 68    private function metadataResponseHandler(metadata:Object):void {
 69      if (metadata.status == 'PENDING') {
 70        // try again in 5 seconds
 71        dispatchEvent(new AnalysisEvent(AnalysisEvent.PENDING));
 72        waitForAnalysis(5000);
 73        return;
 74      }
 75      else if (metadata.status == 'COMPLETE') {
 76        dispatchEvent(new AnalysisEvent(AnalysisEvent.COMPLETE));
 77        loadAnalysis();
 78      }
 79      else if (metadata.status == 'UNKNOWN') {
 80        dispatchEvent(new AnalysisEvent(AnalysisEvent.UNKNOWN));
 81      }
 82      else {
 83        dispatchEvent(new AnalysisEvent(AnalysisEvent.ERROR));
 84      }
 85      analysis.metadata = metadata;
 86    }
 87
 88    private function metadataEchoNestErrorHandler(e:EchoNestError):void {
 89      if (e.code == 11) { // Analysis not ready (please try again in a few minutes)
 90        dispatchEvent(new AnalysisEvent(AnalysisEvent.NOT_READY));
 91        // try again in 15 seconds
 92        waitForAnalysis(15000);
 93      }
 94      else {
 95        echoNestErrorHandler(e);
 96      }
 97    }
 98
 99    private function loadAnalysis():void {
100      apiLoadersComplete = 0;
101
102      for each (var propertyName:String in _properties) {
103        var methodName:String = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
104        var apiLoader:URLLoader = trackApi[methodName](parameters, {
105            onResponse: responseHandler,
106            onResponseArgument: propertyName,
107            onEchoNestError: echoNestErrorHandler,
108            onError: errorHandler
109          }
110        );
111        apiLoaders.push(apiLoader);
112      }
113    }
114
115    private function errorHandler(event:Event):void {
116      dispatchEvent(event);
117    }
118
119    private function echoNestErrorHandler(error:EchoNestError):void {
120      dispatchEvent(error.createEvent());
121    }
122
123    private function responseHandler(propertyName:String, response:Object):void {
124      _analysis[propertyName] = response;
125      apiLoadersComplete++;
126      if (apiLoadersComplete == apiLoaders.length) {
127        analysisLoadedHandler();
128      }
129    }
130
131    private function analysisLoadedHandler():void {
132      dispatchEvent(new Event(Event.COMPLETE));
133    }
134  }
135}