reddit_wallpaper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f59e4c97092153ebdec0864c6219dc5fe13b83fd
4
- data.tar.gz: 0478a417e076a5c9f439d5eccb26d7792bae2039
3
+ metadata.gz: 114bcb054b2e01cae7d3a9edff84d9584ce13dce
4
+ data.tar.gz: 25c1435e6ff0fc26038981df9591abe01d98e769
5
5
  SHA512:
6
- metadata.gz: 0cfc5b6d3060a1277ee7a60ba356d3be31aad9b4bf0e7dacc89a17f9c717ee8c3e1d1f0a87c45bad2a888ece041ad7cd2fd7e9b4c89d0d484b13e26f28b1a977
7
- data.tar.gz: 16a44d81d5fd0df08bf5adcc7d69b3034e383bf70732042ffcc658c9bbfd74876b6aa8a9bb1d4525d2bfc45fdf60809515c034dcf1a22f946b08e500a64e7e73
6
+ metadata.gz: d0fb0af3be8b2e4d2349f7a605c807a9fd9c02339609281791bbe0050cc608cd023a79ea0c0cf88f500ff92659612d18f25f31e929c831ab416c0d7b59b7b3c8
7
+ data.tar.gz: 7d82e5892b7a6a257488c5447328c0ee5bddb6007a004bd9e4b6695b6cfbe89808dacc0f1a1b4f2c067eea32ad06cd1a3b4cb7da3a213c83fe9f487ed708d668
@@ -3,6 +3,10 @@ require 'ruby_reddit_api'
3
3
  require 'open_uri_redirections'
4
4
 
5
5
  class RedditWallpaper
6
+ def self.reddit
7
+ @reddit ||= Reddit::Api.new
8
+ end
9
+
6
10
  def initialize(subreddit, destination_file)
7
11
  @subreddit = subreddit
8
12
  @path = destination_file
@@ -13,10 +17,10 @@ class RedditWallpaper
13
17
  attr_reader :path, :subreddit, :downloaded, :not_downloaded
14
18
 
15
19
  def download_new_image
16
- posts = reddit.browse(subreddit)
17
- posts.each do |post|
18
- if post.url.include?('imgur') && !downloaded.include?(post.url)
19
- not_downloaded << post.url
20
+ posts = RedditWallpaper.reddit.browse(subreddit)
21
+ posts.map(&:url).each do |url|
22
+ if download? url
23
+ not_downloaded << url
20
24
  end
21
25
  end
22
26
 
@@ -25,14 +29,19 @@ class RedditWallpaper
25
29
 
26
30
  private
27
31
 
28
- def reddit
29
- @reddit ||= Reddit::Api.new
32
+ # Checks that the post url is either a jpg file or hosted on imgur
33
+ #
34
+ def download?(url)
35
+ url[/(imgur|\.jpe?g\z)/] && !downloaded.include?(url)
30
36
  end
31
37
 
32
38
  def download_image(url)
33
- # Append .jpg to ensure we are getting the image file
39
+ return unless url
40
+ # Append .jpg to ensure we are getting the image file from imgur
41
+ url += ".jpg" unless url[/\.jpe?g\z/]
42
+
34
43
  open(path, 'wb') do |file|
35
- file << open(url + ".jpg", allow_redirections: :all).read
44
+ file << open(url, allow_redirections: :all).read
36
45
  end
37
46
  downloaded << url
38
47
  end
@@ -0,0 +1,78 @@
1
+ require 'minitest/autorun'
2
+
3
+ require_relative '../lib/reddit_wallpaper'
4
+ require_relative 'support/reddit_stub'
5
+
6
+ describe 'RedditWallpaper' do
7
+ let(:reddit_wallpaper) { RedditWallpaper.new(subreddit, "/dev/null") }
8
+ let(:image_url) { "http://i.imgur.com/Nhg3uHn.jpg" }
9
+ let(:subreddit) { "fancy-subreddit" }
10
+
11
+ it 'downloads the latest image posts' do
12
+ post = MiniTest::Mock.new
13
+ post.expect :url, image_url
14
+
15
+ RedditWallpaper.stub :reddit, Support::RedditStub.new([post]) do
16
+ reddit_wallpaper.download_new_image
17
+ end
18
+
19
+ post.verify
20
+
21
+ reddit_wallpaper.downloaded.must_include image_url
22
+ end
23
+
24
+ it 'loads posts from the specified subreddit' do
25
+ reddit_stub = Support::RedditStub.new
26
+
27
+ RedditWallpaper.stub :reddit, reddit_stub do
28
+ reddit_wallpaper.download_new_image
29
+ end
30
+
31
+ reddit_stub.subreddit.must_equal subreddit
32
+ end
33
+
34
+ it 'does not download non-jpg files' do
35
+ bad_url = "http://www.google.com"
36
+
37
+ post = MiniTest::Mock.new
38
+ post.expect :url, bad_url
39
+
40
+ RedditWallpaper.stub :reddit, Support::RedditStub.new([post]) do
41
+ reddit_wallpaper.download_new_image
42
+ end
43
+
44
+ post.verify
45
+
46
+ reddit_wallpaper.downloaded.wont_include bad_url
47
+ end
48
+
49
+ it 'accepts imgur urls without an extension' do
50
+ imgur_url = "http://imgur.com/Nhg3uHn"
51
+
52
+ post = MiniTest::Mock.new
53
+ post.expect :url, imgur_url
54
+
55
+ RedditWallpaper.stub :reddit, Support::RedditStub.new([post]) do
56
+ reddit_wallpaper.download_new_image
57
+ end
58
+
59
+ post.verify
60
+
61
+ reddit_wallpaper.downloaded.must_include imgur_url + ".jpg"
62
+ end
63
+
64
+ it 'does not mind being redirected' do
65
+ secure_url = "https://imgur.com/Nhg3uHn"
66
+
67
+ post = MiniTest::Mock.new
68
+ post.expect :url, secure_url
69
+
70
+ RedditWallpaper.stub :reddit, Support::RedditStub.new([post]) do
71
+ reddit_wallpaper.download_new_image
72
+ end
73
+
74
+ post.verify
75
+
76
+ reddit_wallpaper.downloaded.must_include secure_url + ".jpg"
77
+ end
78
+ end
@@ -0,0 +1,15 @@
1
+ module Support
2
+ class RedditStub
3
+ def initialize(posts = [])
4
+ @posts = posts
5
+ end
6
+
7
+ attr_reader :subreddit
8
+ attr_accessor :posts
9
+
10
+ def browse(subreddit)
11
+ @subreddit = subreddit
12
+ posts
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reddit_wallpaper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emad Elsaid
@@ -39,6 +39,20 @@ dependencies:
39
39
  - - ~>
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '5.3'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '5.3'
42
56
  description: Download the latest images from your favorite subreddit and set them
43
57
  as your desktop background
44
58
  email:
@@ -53,6 +67,8 @@ files:
53
67
  - README.md
54
68
  - bin/reddit_wallpaper
55
69
  - lib/reddit_wallpaper.rb
70
+ - spec/reddit_wallpaper_spec.rb
71
+ - spec/support/reddit_stub.rb
56
72
  homepage: https://github.com/jordanbyron/reddit_wallpaper
57
73
  licenses:
58
74
  - MIT
@@ -77,5 +93,7 @@ rubygems_version: 2.2.2
77
93
  signing_key:
78
94
  specification_version: 4
79
95
  summary: Auto-changing wallpaper from reddit / imgur images
80
- test_files: []
96
+ test_files:
97
+ - spec/reddit_wallpaper_spec.rb
98
+ - spec/support/reddit_stub.rb
81
99
  has_rdoc: