Reading a JSON response from FireFox console - ruby

I have tried searching a bit for an answer but am having trouble, as I think this scenario is a little 'unique' in that the JSON file is coming from a server response after calling a method.
When you input example.Server.method() into the FireFox console manually, you get a JSON response. I want to input this into the console in a ruby script with selenium webdriver, so I can read an important piece of information from the JSON response.
I'm thinking I can call the method like so:
console.log("example.Server.method()")
But where does the JSON response actually 'go', since I want my script to find and parse the information? Calling the method results in a examplename.json file that I need to parse.

You could execute the java script like so
information = driver.execute_script("return example.Server.method()")

Related

AJAX, JQuery, Parse - which one will get me my array?

In my codeacademy and codeschool lessons, I've been fetching data from google rss and simulated twitter feeds.
My newest exercise, however, involves fetching an array of text data from a REST API.
When I try
$.get('https://api.parse.com/1/classes/chats?order=-createdAt', function(x){$('.messages').append('<li>'+x.responseText+'</li>');});
I get
which has the text and username I need. Sort of...
but when I try to alert or console.log either *x.responseText or x.responseText.results I obviously get undefined instead of an array.
What am I missing?
Study more AJAX and I'll find a technique?
Or do I have to send special instructions to the parse server using some commands found here.
You are not using XMLHttpRequest directly, you are using jQuery and it will read the responseText and handle it for you.
Just use x (or, rather, x.results).

Parsing JSON with a Javascript Date object in Ruby

I am using a SOAP API that returns XML but with JSON strings within the response envelope. For most of the API calls this has not been a problem but there is one that returns Javascript new Date objects that is causing problems when using JSON.parse. Here is a simplified example of the response I am getting.
"{\"History\":[ {\"Timestamp\":new Date(1380024020923)}]}"
When using JSON.parse I get the following error.
JSON::ParserError: 399: unexpected token at '{"Timestamp":new Date(1380024020923)}]}'
Is there a nice way to parse this string or am I going to have to use some regex/string trickery? Has anyone come across this way of returning a date object and I would like to understand the advantage?

Obtain XML element's value from REST server response using Ruby

n00b REST question. I'm making a GET request to an API's endpoint and getting the proper XML response. The question I have is, how do I get the value of a particular XML element in the servers REST response using Ruby?
So let's say one of the elements is 'Body' and I want to assign its value 'Blah blah blah' to a variable
Part of the XML response:
<Body>Blah blah blah</Body>
How would I do that with the response? Basically I want to do something like this
variable = params["Body"]
Thanks in advance!
The best solution is to use RestClient or HTTParty and have it parse the response for you.
Otherwise, you'll have to parse the response itself using a library such as Nokogiri:
doc = Nokogiri.XML(response)
variable = doc.at("body").text
You'll want to use an XML parser of some kind.
It sounds like you want something like XmlSimple, which will turn an XML document into ruby arrays and hashes. There's tons of examples of how to use it on the page that has been linked.
One thing to be aware of is that XML to native container mappings are imperfect. If you're dealing with a complex document, you'll likely want to use a more robust parser, like Nokogiri.
If you want full XML Object Mapping, HappyMapper is a decent library, although it isn't very active anymore. It can work with XML from any source, so you'll still want something like the libraries mentioned by #Fitzsimmons or #MarkThomas to do the HTTP request.

Any alternate of drupal_json for non-JSON output

In an AJAX call back in drupal it is normally recommended to use drupal_json() to send data to client. This function converts the raw data into JSON along with HTML encoding.
I want to send the HTML data without encoding to client.
for this I am using following code:
print $html_output;
exit(0);
Is there any recommended or best way in drupal to do so?
If you need to output only the HTML output returned from the menu callback, then the following code is the correct one:
print $html_output;
module_invoke_all('exit');
exit();
If you want your output to appear together the blocks Drupal normally output, then the code needs to be changed to the following:
return $html_output;
That will do the trick. Allthough you should invoke hook_exit first. However this is shortcutting the framework some what, it may work in simple cases but wont work for forms etc.
The only time I have used this method is if I am printing some data which is allready json encoded.

Filtering the result of a JSON.parse response in Ruby (and the JSON gem)

In a little app I'm building, I'm using the the twitter_oauth gem (source of the methods I'm using), which incidentally means I'm dealing with the JSON ruby gem.
I'm using the messages method, whose source is as follows:
def messages(page=1)
oauth_response = access_token.get("/direct_messages.json?page=#{page}")
JSON.parse(oauth_response.body)
end
It parses the JSON produced by Twitter, using the JSON.parse method. Now, what I want to do is filter the response, so as to show only the messages sent by a certain user. In other words, I want to be able to get an accounts message's on a per user basis.
I went through the JSON gem docs, but couldn't find an easy way to do this. I normally don't work with JSON (I prefer XML), but since the Twitter_oauth gem relies on it, I'm forced to learn it (unless I change the gem's source code or overwrite it - not of my preference).
Does any one know a pragmatic way of sorting JSON in Ruby?
Why do you have to work with JSON at all? JSON.parse gives you back deserialized structures (Ruby arrays and hashes), so all you have to do (I presume, you haven't pasted sample JSON output here) is to do select on whatever output JSON.parse (or, in your case, messages) method returns.

Resources