Class: Tiqav::Image
- Inherits:
-
Object
- Object
- Tiqav::Image
- Defined in:
- lib/tiqav/image.rb
Instance Attribute Summary collapse
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#glitch ⇒ Object
readonly
Returns the value of attribute glitch.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#permalink ⇒ Object
readonly
Returns the value of attribute permalink.
-
#thumbnail ⇒ Object
readonly
Returns the value of attribute thumbnail.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #exists? ⇒ Boolean
-
#initialize(id, ext = 'jpg') ⇒ Image
constructor
A new instance of Image.
- #save(fname) ⇒ Object
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
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
5 6 7 |
# File 'lib/tiqav/image.rb', line 5 def ext @ext end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
5 6 7 |
# File 'lib/tiqav/image.rb', line 5 def filename @filename end |
#glitch ⇒ Object (readonly)
Returns the value of attribute glitch.
5 6 7 |
# File 'lib/tiqav/image.rb', line 5 def glitch @glitch end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/tiqav/image.rb', line 5 def id @id end |
#permalink ⇒ Object (readonly)
Returns the value of attribute permalink.
5 6 7 |
# File 'lib/tiqav/image.rb', line 5 def permalink @permalink end |
#thumbnail ⇒ Object (readonly)
Returns the value of attribute thumbnail.
5 6 7 |
# File 'lib/tiqav/image.rb', line 5 def thumbnail @thumbnail end |
#url ⇒ Object (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
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 |