# What's wrong with this code?

**URL:** <https://www.ruby-forum.com/t/whats-wrong-with-this-code/99297>\
**Category:** Ruby\
**Created:** [July 21, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-code/99297 "2006-07-21T15:59:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [July 21, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-code/99297/1 "2006-07-21T15:59:46Z")

</div>

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](http://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!

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [July 21, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-code/99297/2 "2006-07-21T19:40:28Z")

</div>

On Jul 21, 2006, at 7:00 AM, ry an wrote:

> ```
> response.value
> 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.

> 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=#{api\_key}](http://flickr.com/services/rest/?api_key=#%7Bapi_key%7D)  
&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.flickr.com/#{server}/#{photo_id}_#{secret}.jpg"

```

end  
end  
$ ruby x.rb API\_KEY 50178138@N00 mercury  
[![](https://static.flickr.com/73/187409033_e2b8691c02.jpg) ](https://static.flickr.com/73/187409033_e2b8691c02.jpg)  
[![](https://static.flickr.com/44/187408732_37eb026666.jpg) ](https://static.flickr.com/44/187408732_37eb026666.jpg)  
[![](https://static.flickr.com/48/151487121_a7f27506d9.jpg) ](https://static.flickr.com/48/151487121_a7f27506d9.jpg)  
[![](https://static.flickr.com/53/126211398_cd750a0fd3.jpg) ](https://static.flickr.com/53/126211398_cd750a0fd3.jpg)  
[![](https://static.flickr.com/40/104473626_c98df0235f.jpg) ](https://static.flickr.com/40/104473626_c98df0235f.jpg)  
[![](https://static.flickr.com/32/42799590_b8f1774b1a.jpg) ](https://static.flickr.com/32/42799590_b8f1774b1a.jpg)  
[![](https://static.flickr.com/33/42799509_315fceaa46.jpg) ](https://static.flickr.com/33/42799509_315fceaa46.jpg)  
[![](https://static.flickr.com/32/42799392_56595332d1.jpg) ](https://static.flickr.com/32/42799392_56595332d1.jpg)  
[![](https://static.flickr.com/30/42799302_4c984142ed.jpg) ](https://static.flickr.com/30/42799302_4c984142ed.jpg)  
[![](https://static.flickr.com/33/42799253_52ab0cc23b.jpg) ](https://static.flickr.com/33/42799253_52ab0cc23b.jpg)  
[![](https://static.flickr.com/26/42799227_87135b0f18.jpg) ](https://static.flickr.com/26/42799227_87135b0f18.jpg)  
[![](https://static.flickr.com/10/16179999_0def2efd17.jpg) ](https://static.flickr.com/10/16179999_0def2efd17.jpg)  
$

PS: Matz has ridden in that car.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
