Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Custom Responses w/ 'extra' in...

Replytotopic

Custom Responses w/ 'extra' information...?

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
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’
2) write the time of last retrieval to a file
3) use that retrieved time the next time I request 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,
Michael

 
Ernie

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/

 
Profile

Authority 0
Posting Rating 0
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:

respond_to do |format|
      format.html
      format.xml  { render :xml => @captchas
            //AND APPEND THE TIME TO THE RESPONSE HERE }
end

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:

Item.find(my_conditions_here).to_xml + current_time_as_xml
 
Ernie

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.

 
Profile

Authority 0
Posting Rating 0
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.

 
Ernie

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. :)

 
Profile

Authority 0
Posting Rating 0
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?

 
Ernie

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.

 
Profile

Authority 0
Posting Rating 0
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.

 
Ernie

Authority 25
Posting Rating 99
Sign in to rate this post

no problem. :)

 
Profile

Authority 0
Posting Rating 0
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?

 
Profile

Authority 0
Posting Rating 0
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.

 
Ernie

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.

Replytotopic

Other Recent Topics

Ask a Rails expert : Use Rails to develop sites for both Designer and Programmer

Ask a Rails expert : Rails+RS232

Ask a Rails expert : Is this a good way to add Admin section

Ask a Rails expert : RSS feed maker in rails 2.1

Ask a Rails expert : Syncing with ugly legacy databases

Ask a Rails expert : juggernaut Error

Ask a Rails expert : gem "chronic" error

Ask a Rails expert : gem install error

Ask a Rails expert : need your help or views for distributed programming with ruby

Ask a Rails expert : how to refresh ruby files without restart production server

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel