class RedditWallpaper
Attributes
downloaded[R]
not_downloaded[R]
path[R]
subreddit[R]
Public Class Methods
new(subreddit, destination_file)
click to toggle source
# File lib/reddit_wallpaper.rb, line 10 def initialize(subreddit, destination_file) @subreddit = subreddit @path = destination_file @downloaded = [] @not_downloaded = [] end
reddit()
click to toggle source
# File lib/reddit_wallpaper.rb, line 6 def self.reddit @reddit ||= Reddit::Api.new end
Public Instance Methods
download_new_image()
click to toggle source
# File lib/reddit_wallpaper.rb, line 19 def download_new_image posts = RedditWallpaper.reddit.browse(subreddit) posts.map(&:url).each do |url| if download? url not_downloaded << url end end download_image not_downloaded.shift end
Private Instance Methods
download?(url)
click to toggle source
Checks that the post url is either a jpg file or hosted on imgur
# File lib/reddit_wallpaper.rb, line 34 def download?(url) url[/(imgur|\.jpe?g\z)/] && !downloaded.include?(url) end
download_image(url)
click to toggle source
# File lib/reddit_wallpaper.rb, line 38 def download_image(url) return unless url # Append .jpg to ensure we are getting the image file from imgur url += ".jpg" unless url[/\.jpe?g\z/] open(path, 'wb') do |file| file << open(url, allow_redirections: :all).read end downloaded << url end