RABL and XML formatting - ruby-on-rails

I'm using RABL to format the output of a Rails API I'm creating.
Is there anyway to customize the shape of the XML being produced? For instance, I need to produce an output that uses XML attributes, instead of elements. In other words, this...
<auth status="FAILED" errorcode="UNKNOWN_LOGIN" errormessage=”Error Message” />
Instead of this...
<auth>
<status type="symbol">failed</status>
<errorcode>UNKNOWN_LOGIN</errorcode>
<errormessage>Invalid credentials.</errormessage>
</auth>
Any help will be highly appreciated?

I don't think rabl can support xml attributes, but the gem's author is very helpful. I would pose this question at the gem's git home page.
https://github.com/nesquena/rabl/issues

Related

How to parse a Nokogiri XML Element?

I'm able to narrow in on the area of an HTML document using nokogiri. I need to be able to extract the href from the nokogiri object but I'm not able to figureout how to do this for the life of me. Calling row.css('td > b').to_html method gives me the pretty html representation in string form. But I need to parse this using nokogiri.
"<b>\ntour companies for botswana</b>"
The nokogiri equivalent that I'm unable to extract the url from is below:
[#<Nokogiri::XML::Element:0x3fe972a9deb8 name="b" children=[#<Nokogiri::XML::Element:0x3fe972ad90a8 name="a" attributes=[#<Nokogiri::XML::Attr:0x3fe972ad8ff4 name="href" value="/ShowTopic-g317055-i11941-k10224606-United_Expeditions_tour_company_Maun-Maun_North_West_District.html">, #<Nokogiri::XML::Attr:0x3fe972ad8fe0 name="onclick" value="setPID(34603)">] children=[#<Nokogiri::XML::Text:0x3fe972ad8900 "\nUnited Expeditions tour company, Maun">]>]>]
The snippet above is a confusing bit of nokogiri xml object I guess. But I just want to get the href. How the heck do I do this?
row.css('td > b a').attr('href')
This should do the work. Read more about How to access attributes using Nokogiri.

Rails 3.1 CKEditor gem raw text

I'm using the ckeditor gem and when I use the paste as plain text dialog it encapsulates the text in quotes as well as < p> tags and < br> tags. Is there any way I can tell ckeditor not to use any markup whatsoever when using that option.
What I am trying to accomplish is to have snippets of code within an article. Those snippets are processed using the markdown gem as well as pigments.rb. The following code is what I'm trying to accomplish
```ruby
puts "hello world"
class Hello
end
```
and this is what I'm getting
<p>
```ruby<br />
puts "hello world"
class hello<br />
end<br /></p>
This might be what you're looking for:
http://ckeditor.com/addon/codemirror
I really don't know if the ckeditor has that option.
If your problem is show the information like WYSIWYG in a web with RoR maybe you have to set in the view that the information is html safe (This is for security reasons and is set by default). If you don't do that you will see with the html tags.
You have many ways to do the html safe:
Here is a discussion about which one use:
raw vs. html_safe vs. h to unescape html
Hope that solve your problem.
Codemirror might be your best bet for this. Its like CKeditor but for code.
http://codemirror.net/
You could even write your own mode for it, which if I understand what your trying to do might end up being required.
Have not found anything better than going with Markdown. Just like it is done here on SO

Struts2 <s:url> / <s:param> tags outputting date parameter values that fail in type conversion

I'm struggling with a Struts2 date formatting issue. If I understand correctly, type conversion in Struts2 is locale aware, and any form fields/parameters that map to Date objects should be strings formatted in their Locale specific SHORT format; the default output for a Date object on the value stack is also output as the Locale specific SHORT format (unless overridden with custom formatting).
Although form fields have worked fine with dates, when using the <s:url> tag I can't seem to get the <s:param> tag to encode date parameters correctly. When I try something such as this
<s:url action="foo" >
<s:param name="endDateParam" value="#endDate"/>
</s:url>
the result is pretty obviously not the SHORT format:
/foo.action?endDateParam=Sat+Jan+14+00%3A00%3A00+EST+2012
I re-read the Struts2 documentation but they mostly discuss creating custom date formats in the i18n'ized properties files, which doesn't seem like the right solution.
Any help with this problem would be greatly appreciated.
You can send it like that :
<s:param name="dateFrom">
<s:date name="dateFrom" format="dd.MM.yyyy"/>
</s:param>
You've probably already solved this by formatting a string in the action the way you need it. This I would advise first, unless you are supper stickler for model/view separation or there isn't a one to one mapping between the action and the view in which case this zealousness my be justified.
Lets say you felt the formatting wasn't the business of the action in that case you could use OGNL to it's full effect:
Here is an example that displays the current date (it uses new to construct a new date but you can very easily just replace "new java.util.Date()" with "endDate". It was constructed this way so anyone can just paste it into their JSP without any action dependencies.
<p>
<s:property value="#java.text.DateFormat#getDateInstance(#java.text.DateFormat#SHORT, #java.util.Locale#CANADA).format(new java.util.Date())"/>
</p>
NOTE: requires OGNL static method access to be true. Easiest way to do that is to add the following to struts.xml:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Using OGNL to this level is a bit suspicious but it is easy to read and the intention is clearly view/presentation related. Although it isn't that easy to construct... The easiest way is to write everything as one line of java and then apply ognl syntax rules which you would find here:
http://commons.apache.org/ognl/language-guide.html
Also for quick reference:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html

Library for JSP parsing and manipulation

I am trying to parse a bunch of JSP files and find places with hardcoded strings. E.g.
<h:outputText value="I am hardcoded" styleClass="someClass" />
<my:customTag value="I am hardcoded too" />
Currently I am using jsoup to do so. It seems great as an HTML parser, however if I make changes to the document and write it out to a file all of the case sensitive JSF tags and attributes are changed to lowercase. Are there any Java libraries that can parse a JSP file, let me modify some attribute values, and let me write out the JSP?
Or better yet is there a way to tell jsoup not to change the casing of my elements and attributes?
It may not be the case for other people, but it turns out replacing all of the toLowerCase() calls in jsoup worked well enough for me.

HTML with Prawn

Im trying to use prawn to generate a PDF of a log entry, then entries are stored in bbcode ([b]bold[/b] etc...) and is converted to html on display.
Is there any way to display the html in prawn?
It sounds like prawn-format might be what you're looking for, but you'll need to use an older version of prawn if you want to try it out.
You'll probably get the best results if you use a lexical analyser such as 'Syntax' by Jamis Buck (http://syntax.rubyforge.org/) and parse the interesting parts of the HTML chunk and render those parts to PDF.
You can use http://www.princexml.com/ to convert html docs to css, or use the princely plugin to render a pdf as one of the accepted formats of your view. See http://jimneath.org/2009/02/16/creating-pdf-documents-in-ruby-on-rails/
If you are wanting to use prawn directly I don't think there is a way to just use html directly to convert to pdf.

Resources