Class: Tiqav::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/tiqav/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, ext = 'jpg') ⇒ Image

Returns a new instance of Image.



7
8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def ext
  @ext
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def filename
  @filename
end

#glitchObject (readonly)

Returns the value of attribute glitch.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def glitch
  @glitch
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def id
  @id
end

Returns the value of attribute permalink.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def permalink
  @permalink
end

#thumbnailObject (readonly)

Returns the value of attribute thumbnail.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def thumbnail
  @thumbnail
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/tiqav/image.rb', line 5

def url
  @url
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)

Raises:



29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# 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