What's wrong with this code?

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). Anyway, here’s what I have:

#Application.rb controller

def get_response(path, http)
  request = Net::HTTP::Get.new(path)
  response = http.request(request)
  response.value
  response.body
end

path = 

“/services/rest/?api_key=API_KEY&method=flickr.photos.search&user_id=USER_ID&tags=TAG”
http = Net::HTTP.new(‘flickr.com’)

@photo_ids = Array.new
http.start do |http|
  xml = get_response(path, http)
  REXML::Document.new(xml).root.get_elements('photo').each do 

|photo|
id = photo.attributes[‘id’]
@photo_ids << id
end
end

#Application.rhtml layout

<% for p in @photo_ids %>
<%= p %>
<% end %>

This just doesn’t do anything. It doesn’t give me errors, but it the
@photo_ids variable is empty. I’ve also tried <%= p.id %> and so on.

Can someone see something wrong with this code? Thanks!

Same for the ‘if’ on the ‘If @en_points > @ur_points’ line. Ruby is
case sensitive

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 :slight_smile:

Sorry I mean “while” :frowning:

now = Time.newthir = Time.new + 20While now != thir num = 500 +
rand(300) puts “#{num}” thir = Time.new + 60else now =
Time.newend

Again with the capital “w” for “while”. Also what is the “else”
supposed to pair up with (hint it is usually an “if” or “unless” or
“case … when”)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs