[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie - Test For JPEG Image Over HTTP

neilc

8/17/2006 8:17:00 AM

Hi

I would like to test for the existence of a .jpg image on a remote
server over HTTP.

I have tried 2 different methods but they are not what I need.

Method 1:

require 'open-uri'
if open('http://example.net/path/to/imag...)
# do something
end

Method 2:

require 'net/http'
if Net::HTTP.get 'example.net', '/path/to/image.jpg'
# do something
end

The server where the images are always returns a default page if the
images do not exist so the code above always returns.

What I need is a way to check what is returned is not a html page but is
a .jpg image.

Any help greatly appeciated.
Thanks
Neil



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

4 Answers

Alex Young

8/17/2006 8:25:00 AM

0

Neil Charlton wrote:
> Hi
>
> I would like to test for the existence of a .jpg image on a remote
> server over HTTP.
>
> I have tried 2 different methods but they are not what I need.
>
> Method 1:
>
> require 'open-uri'
> if open('http://example.net/path/to/imag...)
> # do something
> end
>
> Method 2:
>
> require 'net/http'
> if Net::HTTP.get 'example.net', '/path/to/image.jpg'
> # do something
> end
>
> The server where the images are always returns a default page if the
> images do not exist so the code above always returns.
>
> What I need is a way to check what is returned is not a html page but is
> a .jpg image.
>

Oh. Try this:

require 'net/http'

def url_is_a_jpg(url)
u = URI.parse(url)
h = Net::HTTP.new(u.host, u.port)
r = h.head(u.path)
return r.content_type == 'image/jpeg'
end

--
Alex


neilc

8/17/2006 10:39:00 AM

0

Alex Young wrote:
> Neil Charlton wrote:
>> if open('http://example.net/path/to/imag...)
>> The server where the images are always returns a default page if the
>> images do not exist so the code above always returns.
>>
>> What I need is a way to check what is returned is not a html page but is
>> a .jpg image.
>>
>
> Oh. Try this:
>
> require 'net/http'
>
> def url_is_a_jpg(url)
> u = URI.parse(url)
> h = Net::HTTP.new(u.host, u.port)
> r = h.head(u.path)
> return r.content_type == 'image/jpeg'
> end

Hi Alex

Thanks for your help. I can see what you are doing there unfortunately I
get the following error

isjpg.rb:7:in `url_is_a_jpg': undefined method `content_type' for
#<Net::HTTPOK 200 readbody=true> (NoMethodError)

ruby -v is
ruby 1.8.2 (2004-12-25) [x86_64-linux]

Any ideas ?

Thanks
Neil


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

Alex Young

8/17/2006 12:17:00 PM

0

Neil Charlton wrote:
> Alex Young wrote:
>> Neil Charlton wrote:
>>> if open('http://example.net/path/to/imag...)
>>> The server where the images are always returns a default page if the
>>> images do not exist so the code above always returns.
>>>
>>> What I need is a way to check what is returned is not a html page but is
>>> a .jpg image.
>>>
>> Oh. Try this:
>>
>> require 'net/http'
>>
>> def url_is_a_jpg(url)
>> u = URI.parse(url)
>> h = Net::HTTP.new(u.host, u.port)
>> r = h.head(u.path)
>> return r.content_type == 'image/jpeg'
>> end
>
> Hi Alex
>
> Thanks for your help. I can see what you are doing there unfortunately I
> get the following error
>
> isjpg.rb:7:in `url_is_a_jpg': undefined method `content_type' for
> #<Net::HTTPOK 200 readbody=true> (NoMethodError)
>
> ruby -v is
> ruby 1.8.2 (2004-12-25) [x86_64-linux]
>
> Any ideas ?
Is that for a path that does exist, doesn't exist, or both?

--
Alex

Logan Capaldo

8/17/2006 1:29:00 PM

0


On Aug 17, 2006, at 8:16 AM, Alex Young wrote:

> Neil Charlton wrote:
>> Alex Young wrote:
>>> Neil Charlton wrote:
>>>> if open('http://example.net/path/to/imag...)
>>>> The server where the images are always returns a default page if
>>>> the images do not exist so the code above always returns.
>>>>
>>>> What I need is a way to check what is returned is not a html
>>>> page but is a .jpg image.
>>>>
>>> Oh. Try this:
>>>
>>> require 'net/http'
>>>
>>> def url_is_a_jpg(url)
>>> u = URI.parse(url)
>>> h = Net::HTTP.new(u.host, u.port)
>>> r = h.head(u.path)
>>> return r.content_type == 'image/jpeg'
>>> end
>> Hi Alex
>> Thanks for your help. I can see what you are doing there
>> unfortunately I get the following error
>> isjpg.rb:7:in `url_is_a_jpg': undefined method `content_type' for
>> #<Net::HTTPOK 200 readbody=true> (NoMethodError)
>> ruby -v is
>> ruby 1.8.2 (2004-12-25) [x86_64-linux]
>> Any ideas ?
> Is that for a path that does exist, doesn't exist, or both?
>
> --
> Alex
>

Try r.response.content_type == 'image/jpeg' perhaps?