to_xml giving strange results - ruby-on-rails

When I do that
{"New York"=>33, :Versailles => 3231}.to_xml
I get
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<hash>
<Versailles type=\"integer\">3231</Versailles>
<New York type=\"integer\">33</New York>
</hash>
I would have expected rails to dasherize "New York", no?

This issue is closed via the merging of pull request 445 : https://github.com/rails/rails/pull/445
Spaces will now be dasherized (and the private _dasherize method is enhanced to handle spaces.)
{"New York"=>33}.to_xml will result in
..<New-York type=\"integer\">33</New-York>..
Thanks for putting up this lighthouse ticket & the stackoverflow question (which added more info in the discussions); with the help of all the information provided, I was able to make my first rails commit!

I'm seeing the same thing too.
According to the docs the :dasherize option to to_xml should do the trick.
Some configuration is available through options. [...]
This behavior can be controlled with :only, :except, :skip_instruct, :skip_types, :dasherize and :camelize [...]
The default is to dasherize all column names, but you can disable this setting :dasherize to false. Setting :camelize to true will camelize all column names - this also overrides :dasherize.
So, it looks like, at a minimum:
asdf.to_xml(:dasherize => true)
should do it, and adding the :camelize option should force it.
=> "<?xml version="1.0" encoding="UTF-8"?>\n<Hash>\n <New York type="integer">33</New York>\n <Versailles type="integer">3231</Versailles>\n</Hash>\n"
So, maybe someone can 'splain the anomoly.

Normally database fields don't have spaces in them, so your example in the context of #to_xml is a garbage-in-garbage-out situation.

Related

Invalid XML path keys still evaluate in Rest Assured

My example web service is returning following XML.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errorResponse>
<errorCode>Wrong ID</errorCode>
<errorId>2</errorId>
</errorResponse>
Following tests are passed.
response.then().body("errorResponse.errorId", Matchers.is("2"));
response.then().body("errorResponse.errorCode", Matchers.is("Wrong ID"));
response.then().body("errorResponse1.errorCode", Matchers.is("Wrong ID"));
response.then().body("errorResponse2.errorCode", Matchers.is("Wrong ID"));
I understand that first two tests are fine, what I am not getting is why the last two are getting passed?
Rest-assured uses its xml-path library to evaluate your hamcrest matcher and that library contains the XMLAssertion class which does the actual checking.
The source can be found on GitHub: https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/main/groovy/io/restassured/assertion/XMLAssertion.groovy
At line 60 of this file you can see it removes the part of the search key before the first dot, because it recognizes we're evaluating from a root node.
Hence your key:
"errorResponse3.errorCode"
becomes
".errorCode"
So it turns out it doesn't matter what this initial path looks like, it assumes it's the name of the root node and discards it anyway.

https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("ARSARS")&env=store://datatables.org/alltableswithkeys

I have been using the following URL for the past 3 years without issue. However, it has stopped returning results.
URL:
https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("ARSARS")&env=store://datatables.org/alltableswithkeys
Now returns the following:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="0" yahoo:created="2017-11-02T09:33:25Z" yahoo:lang="en-AU">
<results/>
</query><!-- total: 9 -->
Notice how there are no results, it simply has the tag "results".
I have tried on this many different computers and browsers. I have also tried changing the currency combinations, but no luck.
Can anybody spot what I am doing wrong?
Yes, it looks like Yahoo has discontinued their Currency Converter API service.
I found a different site : https://currencylayer.com/
They let you request currency rates (1 USD = 168 other currencies). If you make less than 1000 requests per month, it is free (if you need more, they have different subscriptions : https://currencylayer.com/product).
You just need to sign up and receive your own Access Code. Once you have that, then you simply make the call http://apilayer.net/api/live?access_key= and it will return JSON of the other currency rates.
They also have code examples in PHP, JavaScript, and Java at https://currencylayer.com/documentation
Found it very easy to get started using their API right away.
I think the API is down.
I am similarly receiving "results"=nullfor the query:
http://query.yahooapis.com/v1/public/yql?q=select+%2A+from+yahoo.finance.xchange+where+pair+in+%28%22GBPEUR%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
Setting diagnostics=true in the request yields:
`
[execution-start-time] => 7
[execution-stop-time] => 12
[execution-time] => 5
[http-status-code] => 999
[http-status-message] => Request denied
[content] => http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=snl1d1t1ab
`
If anyone knows more than me about what this might imply I'd be glad to hear it!
Yahoo is aware of this issue and their engineers are working on it:
ref: https://forums.yahoo.net/t5/Yahoo-Finance-help/http-download-finance-yahoo-com-d-quotes-csv-s-GOOG-amp-f/td-p/387096
Very sadly and quite outrageously Yahoo decided to stop this service without any warning.
See admin message here
So many services depend on it, it's like Google saying they would suddenly stop their maps API... At this point I am blocking yahoo in our DNS so no one in our company will ever use Yahoo again since they are not a reliable entity.

How Can I Make ActiveResource XML Parsing More Consistent?

I'm using ActiveResource to consume a REST webservice provided by Redmine (a bug-tracking tool). That webservice produces XML like the following:
<custom_field name="Issue Owner" id="15">Fred Fake</custom_field>
<custom_field name="Needs Printing" id="16">0</custom_field>
<custom_field name="Review Assignee" id="17">Fran Fraud</custom_field>
<custom_field name="Released On" id="20"></custom_field>
<custom_field name="Client Facing" id="21">0</custom_field>
<custom_field name="Type" id="22">Bug</custom_field>
<custom_field name="QA Assignee" id="23"></custom_field>
<custom_field name="Company Name" id="26"></custom_field>
<custom_field name="QA Notes" id="27"></custom_field>
<custom_field name="Failed QA Attempts" id="28">2</custom_field>
However, when ActiveResource parses that, and I iterate through the results printing them out, I get:
Fred Fake
0
Fran Fraud
#<Redmine::Issue::CustomFields::CustomField:0x5704e95d>
0
Bug
#<Redmine::Issue::CustomFields::CustomField:0x32fd963>
#<Redmine::Issue::CustomFields::CustomField:0x3a68f437>
#<Redmine::Issue::CustomFields::CustomField:0x407964d6>
2
That's right, it throws out all of the attribute info from anything with a value, but keeps the attribute info from the empty elements.
Needless to say, this makes things rather difficult when you're trying to find the value for id 15 (or whatever). Now I can reference things by their position, but that's very brittle, because those elements are likely to change in the future. I assume there has to be some way to make ActiveResource keep the attribute info, but since I'm not doing anything special.
(My ActiveResource extension is just five lines long: it extends ActiveResource, defines the url, username and password of the service, and that's it).
So, does anyone know how I can make ActiveResource not parse this XML so strangely?
This is a known issue with ActiveResource apparently:
https://github.com/rails/rails/issues/588
Unfortunately, nothing appears to be done about it & the issue was closed. If you're feeling up to it, the Rails 3 code for updating ActiveResource and Hash.from_xml to preserve all attributes are all in the gist below and you could create a tailored version in your Redmine module to fix it:
https://gist.github.com/971598
Update:
An alternative, as it appears ActiveResource will not be part of Rails 4 core and will be spun out as a separate gem, would be to use an alternative ORM for REST APIs, like Her.
Her allows you to use a custom parser for your XML. This is an example custom parser called Redmine::ParseXML:
https://gist.github.com/3879418
So then all you need to do is create a file like config/initializers/her.rb:
Her::API.setup :url => "https://api.xxxxx.org" do |connection|
connection.use Faraday::Request::UrlEncoded
connection.use Redmine::ParseXML
connection.use Faraday::Adapter::NetHttp
end
and you get a Hash like the following:
#<Redmine::Issue(issues) issues={:attributes=>{:type=>"array", :count=>1640},
:issue=>{:id=>4326,
:project=>{:attributes=>{:name=>"Redmine", :id=>1}},
:tracker=>{:attributes=>{:name=>"Feature", :id=>2}},
:status=>{:attributes=>{:name=>"New", :id=>1}},
:priority=>{:attributes=>{:name=>"Normal", :id=>4}},
:author=>{:attributes=>{:name=>"John Smith", :id=>10106}},
:category=>{:attributes=>{:name=>"Email notifications", :id=>9}},
:subject=>"\n Aggregate Multiple Issue Changes for Email Notifications\n ",
:description=>"\n This is not to be confused with another useful proposed feature that\n would do digest emails for notifications.\n ",
:start_date=>"2009-12-03",
:due_date=>{},
:done_ratio=>0,
:estimated_hours=>{},
:custom_fields=>{
:custom_field=>[
{:attributes=>{:name=>"Issue Owner", :id=>15}, "value"=>"Fred Fake"},
{:attributes=>{:name=>"Needs Printing", :id=>16}, "value"=>0},
{:attributes=>{:name=>"Review Assignee", :id=>17}, "value"=>"Fran Fraud"},
{:attributes=>{:name=>"Released On", :id=>20}},
{:attributes=>{:name=>"Client Facing", :id=>21}, "value"=>0},
{:attributes=>{:name=>"Type", :id=>22}, "value"=>"Bug"},
{:attributes=>{:name=>"QA Assignee", :id=>23}},
{:attributes=>{:name=>"Company Name", :id=>26}},
{:attributes=>{:name=>"QA Notes", :id=>27}},
{:attributes=>{:name=>"Failed QA Attempts", :id=>28}, "value"=>2}]},
:created_on=>"Thu Dec 03 15:02:12 +0100 2009",
:updated_on=>"Sun Jan 03 12:08:41 +0100 2010"}}>

Feed Parsing In Rails

I am working on Ubuntu 10.04 and I am using feed-zirra to parse RSS feeds and I have MySQL database.
I am trying to parse RSS feeds from Times of India Top Stories. There seems to be problem with the first link, I am sure TOI guys will correct it soon. But anyway, I dont want to face similar error later so thats why I want to ask you guys how to solve this problem.
Just look at this and especially look for link
<item>
<title>CWG: Abhinav Bindra, Gagan Narang win first Gold for India</title
<description>Abhinav Bindra and Gagan Narang on Tuesday bagged Gold for the men's 10 m air rifle pair's event, getting India its first gold in the 19th Commonwealth Games.</description>
<link>/cwgarticleshow/6688747.cms</link>
<guid>/cwgarticleshow/6688747.cms</guid>
<pubDate>Tue, 05 Oct 2010 04:57:46 GMT</pubDate>
</item>
The link is <link>/cwgarticleshow/6688747.cms</link>
Now, when I click the link, in the view.. its getting routed to http://localhost:3000/cwgarticleshow/6688747.cms instead of http://timesofindia.indiatimes.com/cwgarticleshow/6688747.cms
And the error I am getting is
**Routing Error**
No route matches "/cwgarticleshow/6688747.cms" with {:method=>:get}
How do I correct this type of Error?
Looking forward for your help and support
Thanks
You just need to prepend http://timesofindia.indiatimes.com to the link tag value and you'll be ok.
You can use URI class. You can, for example, define following method
require "uri"
def repair_link(feed_link)
uri = URI.parse(feed_link)
uri.scheme ||= "http"
uri.host ||= "timesofindia.indiatimes.com"
uri.to_s
end
It will set the scheme and host part of the URL if they are not already filled. So if you call it for normal link (like http://foo/bar.cms) then nothing will be changed.
And last thing - you probably should catch exception somewhere as the #parse method raises exception InvalidURIError in case of invalid URI. But it's up to you how you will deal with it.

com.jcraft.jsch.JSchException: Auth cancel

I'm trying to write an Ant script to retrieve an URL via port tunnelling.
It works great when I use a password (the names xxxx'd out for privacy):
<project default="main">
<target name="main">
<sshsession host="xxxx"
username="xxxx"
password="xxxx">
<LocalTunnel lport="1080" rhost="xxxx" rport="80"/>
<sequential>
<get src="http://localhost:1080/xxxx" dest="/tmp/xxxx"/>
</sequential>
</sshsession>
</target>
</project>
But it doesn't work when I use a keyfile, like this:
<sshsession host="xxxx"
username="xxxx"
keyfile="/Users/xxxx/.ssh/id_dsa"
passphrase="xxxx">
<LocalTunnel lport="1080" rhost="xxxx" rport="80"/>
<sequential>
<get src="http://localhost:1080/xxxx" dest="/tmp/xxxx"/>
</sequential>
</sshsession>
I get this exception:
/tmp/build.xml:8: com.jcraft.jsch.JSchException: Auth cancel
at com.jcraft.jsch.Session.connect(Session.java:451)
at com.jcraft.jsch.Session.connect(Session.java:150)
at org.apache.tools.ant.taskdefs.optional.ssh.SSHBase.openSession(SSHBase.java:223)
I'm sure I'm using the correct keyfile (I've tried using the wrong name, which gives a legitimate FileNotFoundException).
I can successfully ssh from the command line without being prompted for a password.
I'm sure I'm using the correct passphrase for the keyfile.
What's the cause of this error and what can I do about it?
I debugged the code. This was failing because my private key was failing authentication; JSch silently fell back to password authentication, which was canceled, because I didn't specify a password.
JSch error handling sucks a lot. Retrace your steps, regenerate a (separate) private key file, use ssh -i to guarantee you're using the right file, and keep your fingers crossed.
To get the jsch connection to work, you must specify the paths to both the known_hosts file and to the file containing the private key. This is done using the setKnownHosts and addIdentity methods.
jsch.setKnownHosts("/path/to/.ssh/known_hosts");
jsch.addIdentity("/path/to/.ssh/id_rsa");
If the key has a passphrase, you can add it to the addIdentity argument list:
jsch.addIdentity("/path/to/.ssh/id_rsa", myPassPhrase);
See Javadocs
I had the same issue while using sshexec task. I added passphrase attibute too and it worked fine. create a passphrase for your private key and add this as a attribute in your task. Also don't forget to convert your private key to open ssh format if you generated the key using puttygen on windows.
There is a brand new fork of Jsch out now. The exception handling is far more comprehensive. No more swallowing or defaulting. Head over to https://github.com/vngx/vngx-jsch to check it out. If something doesn't work the way you expect, please raise it as an issue, or send a pull request as we are actively maintaining it. We are also looking to get it up on the maven central repos soon.
I had a similar Issue today. So i thought i will share my solution aswell. I got the same exception but the problem was in fact that i had a umlaut within my password. after choosing a new password without it everything worked fine.

Resources