ry an wrote:> I''m trying to interface with the flickr API to pull my photos into
my
> site. I don''t want to use the "flickr.rb" gem,
I''d like to do it the
> other way so I can learn (plus I had trouble loading the gem on my
> server).
Your better off putting this as it''s own file in the lib directory.
Make it a module called FlickerAPI and stick it in lib/flickr_api.rb
Now take that above code and stick it in a class method of that module
#lib/flickr_api.rb
module FlickerAPI
def self.get_photos
...
end
end
Now you can access it easily from "script/console", with the code
"FlickerAPI.get_photos"
Now, right after you get the get_result call, stick a "puts xml" just
to
be sure the API is returning to you the expected result xml. Remove the
line when you are sure the data is being properly retrieved.
Lastly, I think your XML parsing loop is not quite right. Not knowing
the structure of the expected xml, my guess would be that you want
something more like:
REXML::Document.new(xml).root.elements.each(''//photo'') do
|photo|
...
end
And I just realized, is this just in the class itself? or is all this
code in a method called with before_filter? If it''s not in a method
with a before_filter specified for it, then it will only be run once,
and the instance variable will belong to the class, not the instance of
the controller which means it will not get sent to the controller
actions or views at all.
Your aplication.rb should look like this:
class ApplicationController < ActionController::Base
before_filter :get_photos
def get_photos
@photo_ids = FlickrAPI.get_photos
end
end
A long answer for a simple question, but there you go :)
--
Posted via http://www.ruby-forum.com/.