You are here: Forums Ask a Rails expert Custom Responses w/ 'extra' in...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 31
Sign in to rate this post
|
Hi All, I need to acquire limited data via the ‘XML’ response for a specific item. What I basically need to do is this: 1) retrieve data from ‘http://host/photos.xml’ The caveat is that I need to get the last time from the server itself, and I’m not too ‘knowed up’ on appending information and providing custom responses via XML, etc. Any direction pointing would be most appreciated. Regards, |
Authority 25
Posting Rating 99
Sign in to rate this post
|
I did a little writeup a while back on my weblog about using Builder::XmlMarkup to create custom XML requests. It might help you. http://thebalance.metautonomo.us/2008/03/18/of-badgers-and-xml-custom-xml-serialization-in-rails/ |
Authority 0
Posting Rating 31
Sign in to rate this post
|
Wow. That’s a bit much for my needs, but still quite amazing. That said, all I really need is something like the following:
Is there no way to simply append some information to the response before ‘sending’ it? Does rails provide a mechanism for directly converting a model to XML (without rendering it) that would allow me to then append to the XML ‘string’ and respond with it afterward? Basically, I’m looking for a safe way to say:
|
Authority 25
Posting Rating 99
Sign in to rate this post
|
Oops! Sorry for the overload. Sure there is. Consider this model: class Item < ActiveRecord::Base
def timestamp
Time.now
end
end
We defined a method named timestamp, in this case just returning the current time, but you could certainly read it from your file instead. Here’s how you’d have it included in the XML output. Loading development environment (Rails 2.1.0) >> i = Item.create(:name => "Book") => #<Item id: 1, name: "Book", created_at: "2008-07-03 00:17:50", updated_at: "2 008-07-03 00:17:50"> >> puts i.to_xml(:methods => :timestamp) # :methods tells Rails to serialize the output of other model methods <?xml version="1.0" encoding="UTF-8"?> <item> <created-at type="datetime">2008-07-03T00:17:50Z</created-at> <id type="integer">1</id> <name>Book</name> <updated-at type="datetime">2008-07-03T00:17:50Z</updated-at> <timestamp type="datetime">2008-07-02T20:18:15-04:00</timestamp> </item> => nil Easy! :D Check out http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html for more fun. |
Authority 0
Posting Rating 31
Sign in to rate this post
|
Aaaaaaahhhhhhhhhhhhh! Gotcha. Although I’m not sure I care to have a ‘timestamp’ for each item as I only need it once to let me know when the most recent check occurred. I might even be making this overly complicated. Are we 1000% sure rails doesn’t reuse IDs? If not how can we prevent it? I might simply be able to look at the IDs I’ve already retrieved and simply say ‘grab all items with an id greater than the last highest id’. ...although I must admit, using a timestamp does make me feel all warm and fuzzy as it just feels a bit more accurate. |
Authority 25
Posting Rating 99
Sign in to rate this post
|
It can’t reuse IDs, because IDs are primary keys in the DB, so yes, you could do that as well. :) |
Authority 0
Posting Rating 31
Sign in to rate this post
|
Well, I delete items regularly. After they have been uploaded, Data Entry folks enter the pertinent information, then a backend app gets what it needs and deletes the items from the system. So my fear in that case is that Rails might go back and reuse “old” ids and I might start missing items 1 through whatever when they start repeating because I’m only looking for items 3000 and above. Is that a reasonable fear? |
Authority 25
Posting Rating 99
Sign in to rate this post
|
it doesn’t matter. They won’t be reused in MySQL. If you’re at all concerned, though, just test it. Add a few records, delete one, add some more. Easy test to try. |
Authority 0
Posting Rating 31
Sign in to rate this post
|
Yeah, I’d been testing it, but I wasn’t sure if it was standard MySQL practice or if I was just getting ‘lucky’. Just wanted to make sure. Thanks for the input. |
Authority 25
Posting Rating 99
Sign in to rate this post
|
no problem. :) |
Authority 0
Posting Rating 31
Sign in to rate this post
|
Hrm, how about the same thing, but in ‘reverse’? I understand how to potentially add information, but how do I subtract? For instance, I want to restrict the properties that are presented via xml. Say I have a Person object, and I only want to display the last_name and height via XML and not the first_name, age, etc. Is there a :do_not_include or :include_only directive of sorts? |
Authority 0
Posting Rating 31
Sign in to rate this post
|
Nevermind. I actually went out on a limb and guessed at it: @photos = Photo.all render :text => @photos.to_xml(:only => [:name,:size]) Surprisingly, I saw this nowhere in the docs. |
Authority 25
Posting Rating 99
Sign in to rate this post
|
Yeah, you got it… Sorry, was out of pocket for a bit, didn’t get a chance to respond. |
Ask a Rails expert : activesupport string first method error
Ask a Rails expert : url_for broken ?
Ask a Rails expert : map.routes.rb pls help
Ask a Rails expert : how to h tag in controller
Ask a Rails expert : Problem with Restful routing and partial form
Ask a Rails expert : will_paginate, search and ajax
Ask a Rails expert : Mass Deletes in Admin Section
Ask a Rails expert : RJS partial rendering
Ask a Rails expert : problem configuring a particular route