Rails Log Analyzer - Rails and Flex with JSON 7

I started to write a small Rails Log Analyzer that provides some insight on how a given application is used. I’ve just spent three hours so far, so not too much to show, but I have found the integration of Flex with Rails for read-only purpose of the different time series pretty straight forward.
In two words…
RAILS: data.to_json
FLEX: JSON.decode(String(srv.lastResult));
On the Rails side
The controller simply transforms the Hash return by the model into a json textual representation.
class DataController < ApplicationController
def overview
render :text => Hit.overview_data.to_json
end
endThis is an extract of the method that returns a Hash that contains the time series in an Array.
def Hit.overview_data
result = {}
result[:header] = {:period => {:start => Hit.minimum(:time).to_s(:db), :end => Hit.maximum(:time).to_s(:db)}}
result[:sessions_series] =
{:by_day => Hit.data_serie(Hit.count(:session, :group => :day, :conditions => 'controller <> "HeartbeatController"'), "sessions by day") }
result
endOn the Flex side
import com.macromedia.serialization.json.*;
private function resultHandler(event:ResultEvent) : void
{
status = "Loaded. Parsing data...";
var result:Object = JSON.decode(String(srv.lastResult));
header = result.header;
ts = getSerie(result.sessions_series.by_day.data);
}
<mx:HTTPService id="srv" url="http://10.37.129.2:3000/data/overview" result="resultHandler(event)" />
The service is invoked by the following actionscript call
srv.send()JSON doesn’t support Date objects out of the box, but it’s a nice way to exchange complex data such a Hash and Map between Rails and Flex.

Hi,
why the to_json donts use a ” in the variables names…
ex:
{ name : “Daniel” }
and the corrects is { “name” : “Daniel” }
You have any ideia ?
JSON describes javascript objects and {name:”Daniel”} is a valid javascript object with an attribute of named ‘name’. I.e. You can declare and refere to the object and it’s attributes in the following manner:
Note you can also think of the {} as an hasmap and use a string as key, i.e. var person = {‘name’:’Daniel’}; This has the same effect. I hope this answers your question.
Hi, fine article but I have encountered some issues similar to Daniel above. Rails method to_json has output without ” as Daniel S. suggested which is fine by definition, but that JSON decoder which I use doesnt seem to parse it well (it is the one from corelib).
If I fake JSON response and hardcode it in rails controller without using to_json (something like [{“name” : “Fluke” ....}]) everything is just fine. I ve played with this for some time and I am getting kinda desperate, so any help would be appreciated since you obviously get it to work. Thx in Advance and have a nice day
Ok me again. It is really funny that after two days of tweaking and experimenting I finaly dare to ask someone for help and in next ten minutes I found the solution nearly accidentaly. It seems that though something like x = { name:”Fluke”} works fine (tried it in Firebug), it violates strict specification (RFC whatever). There is even a ticket in rails Trac for this issue (http://dev.rubyonrails.org/ticket/8762) but solution is pretty simple
ActiveSupport::JSON.unquote_hash_key_identifiers = false
After putting this in my controller everything started working like a charm so thx anyway and I hope this helps anyone.
Thanks for following up your question, this will be useful to the people that will encounter this issue. I am doing more using xml communication these days, so I wasn’t bugged by this.
Thanks Luke – your solution solved me some hours :-)
I will use this idea for parts of my flex and ruby website.
Will make it live for people, when it is live.
~r http://www.pilotoutlook.com