Class: RandomJpg::Loader::Imgur

Inherits:
Object
  • Object
show all
Defined in:
lib/random_jpg/loader/imgur.rb

Constant Summary collapse

API_URL =
"http://imgur.com/gallery/random.json"

Instance Method Summary collapse

Instance Method Details

#feed(path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/random_jpg/loader/imgur.rb', line 9

def feed(path)
  response = Net::HTTP.get URI(API_URL)
  hash = JSON.parse response
  image = hash["gallery"].select { |i| i["mimetype"] == "image/jpeg"}.first
  image_data = Net::HTTP.get URI("http://i.imgur.com/#{image["hash"]}.jpg")

  File.open(path, "w") do |f|
    f.write image_data
  end
end