[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What's wrong with this code?

ry an

7/21/2006 2:00:00 PM

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!

--
Posted via http://www.ruby-....

1 Answer

Eric Hodel

7/21/2006 5:37:00 PM

0

On Jul 21, 2006, at 7:00 AM, 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). 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|

I don't know if this gives you every photo or not. I think the path
is wrong.

> 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!

The easiest way to do this is to use OpenURI.

$ cat x.rb require 'open-uri'
require 'rexml/document'

api_key = ARGV.shift
user_id = ARGV.shift
tag = ARGV.shift

uri = "http://flickr.com/services/rest/...{api_key}
&method=flickr.photos.search&user_id=#{user_id}&tags=#{tag}"

open uri do |res|
xml = REXML::Document.new res.read
xml.elements['/rsp/photos'].each_element do |photo|
photo_id = photo.attributes['id']
server = photo.attributes['server']
secret = photo.attributes['secret']

puts "http://static.fl...{server}/#{photo_id}_#{secret}.jpg"
end
end
$ ruby x.rb API_KEY 50178138@N00 mercury
http://static.flickr.com/73/187409033_e2b8...
http://static.flickr.com/44/187408732_37eb...
http://static.flickr.com/48/151487121_a7f2...
http://static.flickr.com/53/126211398_cd75...
http://static.flickr.com/40/104473626_c98d...
http://static.flickr.com/32/42799590_b8f1...
http://static.flickr.com/33/42799509_315f...
http://static.flickr.com/32/42799392_5659...
http://static.flickr.com/30/42799302_4c98...
http://static.flickr.com/33/42799253_52ab...
http://static.flickr.com/26/42799227_8713...
http://static.flickr.com/10/16179999_0def...
$

PS: Matz has ridden in that car.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...