Take a string representation of JSON and render it as JSON in the broswer - ruby-on-rails

I'm playing around with the Twitter api, and have gotten back super long JSON response. I saved the response as a string in a seperate file, and I want to have Chrome display that string as JSON, so I can collapse/ expand the nested parts in JSON view.
I feel like there should be an easier way to do this rather than temporarily changing my api controller in Rails...any suggestions? This is for a Rails 4 app using Backbone.js in the front end.

Ah, stupid mistake on my part -- I was using one of the referred to chrome extensions, JSONView, and asked this question after being surprised that it wasn't working.
The reason it wasn't working was because contents of the file were not actually in JSON format, they were in a ruby hash.
I was able to fix it by replacing this:
File.open('exampleResponse', 'w') do |file|
file.write(Twitter::SearchResults.new(request).attrs)
end
with this:
File.open('exampleResponse', 'w') do |file|
file.write(Twitter::SearchResults.new(request).attrs.to_json)
end

There are many chrome extensions to view formatted json. I use JsonView and it works fine, but I imagine there are dozens if not hundreds to choose from.

Related

Format dart code as html

I am knocking together a quick debugging view of a backend, as a small set of admin HTML pages (driven by angulardart, but not sure that is critical).
I get back from my XHR call a complex JSON object. I want to see that on the HTML page formatted nicely. It doesn't have to be a great implementation, as its just a debug ui, but the goal is to format the object instead of having it be one long string with no newlines.
I looked at trying to pretty print JSON in dart then putting that inside <pre></pre> tags, as well as just dumping the dart Map object to string (again, inside or not inside <pre></pre> tags. But not getting to where I want.
Even searched pub for something similar, such as a syntax highlighter that would output html, but didn't find something obvious.
Any recommendations?
I think what you're looking for is:
Format your JSON so it's readable
Have syntax highlight
For 1 - This can be done with JsonEncoder with indent
For 2 - You can use the JS lib called HighlightJs pretty easily by appending your formatted json into a marked-up div. (See highlightjs' doc to see what I mean)

using 'puts' to get information from external domain

ive just started with ruby on rails the other day and i was wandering is it possible to using the puts function to get the content of a div from a page on an external page.
something like puts "http://www.example.com #about"
would something like this work ? or would you have to get the entire page and then puts that section that you wanted ?
additionaly if the content on the "example.com" #about div is constantly changing would puts constantly update its output or would it only run the script each time the page is refreshed ?
The open-uri library (for fetching the page) and the Nokogiri gem (for parsing and retrieving specific content) can assist with this.
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('http://www.example.com/'))
puts doc.at('#about').text
puts will not work that way. Ruby makes parsing HTML fairly easy though. Take a look at the Nokogirl library, and you can use xpath queries to get to the div you want to print out. I believe you would need to reopen the file if the div changes, but I'm not positive about that - you can easily test it (or someone here can confirm or reject that statement).

In rails is there a difference between mysite/show/1.js and mysite/show/1.json

In my rails 3 app, if I want a request to return json data does it matter if I use
mysite/show/1.js
or
mysite/show/1.json
I know it seems obvious to use the json version but in my responses they look the same to me.
First of all: It depends on the way you implement the respond_to block.
With 1.json it should be clear that it delivers data as JSON.
1.js could return Javascript that is evaluated by the page that requested it. In the early Rails/Ajax days this used to be done with RJS templates. See http://www.codyfauser.com/2005/11/20/rails-rjs-templates

Using restclient with multipart posts

I'm using restclient for a multipart form to send data to a restful web service (it's Panda video encoding service).
The trick though, is that the file I am passing into restclient (Technoweenie branch) is coming from my own form that a user submits.
So, lets walk through this. A user posts a file to my rails app. In my controller, it receives the file from params[:file]. I then want to pass params[:file] down to Panda using RestClient.
The error I'm getting is on the Panda server follows. I noticed that the file param in the stack trace is in a string as well (which I assume is Panda turning into a string for a nicer stacktrace).
~ Started request handling: Wed Aug 12 18:05:15 +0000 2009
~ Params: {"format"=>"html", "multipart"=>"true", "account_key"=>"SECURE_KEY", "action"=>"upload", "id"=>"SECURE_ID", "controller"=>"videos", "file"=>"#<File:0xcf02ca4>"}
~ 9bfb1750-6998-012c-4509-12313900b0f6: (500 returned to client) InternalServerErrorcan't convert nil into String
/var/local/www/panda/app/models/video.rb:246:in `extname'
/var/local/www/panda/app/models/video.rb:246:in `initial_processing'
/var/local/www/panda/app/controllers/videos.rb:79:in `upload'
I doubt you can really pass a CGI-style upload param from Rails into restclient and expect it to work.
A regular upload in Rails would have quite some extra attributes which do not belong in a posted resource (like the original filename and so on), and a Rails upload contains an IO with the actual file data. Also a file upload object in Rails might be a Tempfile handle and might be a StringIO - depending on the size of the upload.
What you effectively need to do is "repackage" your upload for rest-client to handle it properly, and pass the repackaged and rewound Tempfile object to restclient. Maybe you can get away with just picking the upload object itself instead of the whole params[:file]
Confirm that your restclient action can save locally first. If the action cannot save locally, then you will have a better idea where to look while trouble shooting.
Looks like the problem is with rest-client's posting of the file, check out an alternative method for posting like curb.
Lots of examples for posting multipart form data on this question: Ruby: How to post a file via HTTP as multipart/form-data?

file_field_tag : what was the original file name?

My site allows users to upload csv files for processing. It all works
fine, but on the response I'd like to report something like "Your file
abc.csv processed OK".
Unfortunately I cannot seem to find the actual original file name in
the params, even though Firebug tells me it's part of the post.
Any tips?
Thanks....
Try using debug on the results of your form.
http://guides.rubyonrails.org/debugging_rails_applications.html#debug
As Jarrod mentions in the comments above. Use params[:file].original_filename
Funny thing is, my form has two file upload tags (file1 and file2). One comes in as a ActionController::UploadedTempfile and the other ActionController::UploadedStringIO.
This may be a rails bug but it doesn't matter to me as both have the original_filename method.

Resources