Class: Twitorious::Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Image

Returns a new instance of Image.



10
11
12
# File 'lib/twitorious/image.rb', line 10

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/twitorious/image.rb', line 8

def filename
  @filename
end

Class Method Details

.from_url(url, store) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/twitorious/image.rb', line 14

def self.from_url(url, store)
  hash = Digest::MD5.hexdigest(url).to_s
  path = File.expand_path(store)
  file = File.join(path, hash)
  site = URI.parse(url)
  
  return Image.new(file) if File.exists?(file)
  
  site = URI.parse(url)
  http = Net::HTTP.new(site.host, site.port)
  http.use_ssl = (site.scheme == 'https')
  req  = Net::HTTP::Get.new(site.path, { "User-Agent" => "Twitorious/#{Twitorious::VERSION}"})
  res  = http.request(req)
  
  return nil unless res.class == Net::HTTPOK
  File.mkdirs(path) if !File.exists?(path)
  
  File.open(file, "wb+") do |f|
    f.puts res.body
  end
  
  return Image.new(file)
end