class Tiqav::Image

Attributes

ext[R]
filename[R]
glitch[R]
id[R]
thumbnail[R]
url[R]

Public Class Methods

new(id, ext = 'jpg') click to toggle source
# File lib/tiqav/image.rb, line 7
def initialize(id, ext = 'jpg')
  @id = id
  @ext = ext
  @filename = "#{@id}.#{@ext}"
  @url = Addressable::URI.parse "http://img.tiqav.com/#{@filename}"
  @permalink = Addressable::URI.parse "http://tiqav.com/#{@id}"
  @thumbnail = Addressable::URI.parse "http://img.tiqav.com/#{@id}.th.#{ext}"
  @glitch = Addressable::URI.parse "http://img.tiqav.com/#{@id}.glitch"
end

Public Instance Methods

exists?() click to toggle source
# File lib/tiqav/image.rb, line 29
def exists?
  case code = Net::HTTP.start(url.host, url.port).
      request(Net::HTTP::Head.new url.path).
      code.to_i
    when 200
    return true
    when 404
    return false
  end
  raise Error, "HTTP Status #{code} - Bad Response from #{url}"
end
save(fname) click to toggle source
# File lib/tiqav/image.rb, line 17
def save(fname)
  res = Net::HTTP.start(url.host, url.port).
    request(Net::HTTP::Get.new url.path)
  unless res.code.to_i == 200          
    raise Error, "HTTP Status #{res.code} - #{url}"
  end
  File.open(fname,'w+') do |f|
    f.write res.body
  end
  fname
end