dribbble-bucket-api 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -27,7 +27,7 @@ Or install it yourself as:
27
27
  buckets.total_entries # => 7
28
28
  buckets.current_page # => 1
29
29
  buckets.next_page # => 2
30
- buckets.previous_page # => 1
30
+ buckets.previous_page # => nil
31
31
 
32
32
  buckets.each do |bucket| # => DribbbleBucketApi::Bucket
33
33
  puts bucket.name
@@ -24,12 +24,20 @@ module DribbbleBucketApi
24
24
  @options[:page] || 1
25
25
  end
26
26
 
27
+ def next_page
28
+ current_page + 1 if current_page < total_pages
29
+ end
30
+
31
+ def previous_page
32
+ current_page - 1 if current_page > 1
33
+ end
34
+
27
35
  def total_entries
28
36
  @total_entries ||= document.css(".buckets .count").text.to_i
29
37
  end
30
38
 
31
39
  def total_pages
32
- (total_entries.to_f / 5).ceil
40
+ total_entries > 0 ? (total_entries.to_f / 5).ceil : 0
33
41
  end
34
42
 
35
43
  private
@@ -13,7 +13,7 @@ module DribbbleBucketApi
13
13
  def shots
14
14
  @shots ||= document.css(".dribbbles > li").map do |shot|
15
15
  # parse shot data from HTML
16
- id = shot["class"].gsub(/^screenshot\-(\d+)$/, "\1").to_i
16
+ id = shot["id"] =~ /^screenshot\-(\d+)$/ && $1.to_i
17
17
  img_src = shot.css(".dribbble-img img").first["src"]
18
18
  url = "http://dribbble.com" + shot.css("a.dribbble-link").first["href"]
19
19
  # pass data into shot object
@@ -24,13 +24,21 @@ module DribbbleBucketApi
24
24
  def current_page
25
25
  @options[:page] || 1
26
26
  end
27
+
28
+ def next_page
29
+ current_page + 1 if current_page < total_pages
30
+ end
31
+
32
+ def previous_page
33
+ current_page - 1 if current_page > 1
34
+ end
27
35
 
28
36
  def total_entries
29
- @total_entries ||= document.css(".main h2.section").text.gsub(/^(\d+).*$/, "\1").to_i
37
+ @total_entries ||= (document.css("#main h2.section").text =~ /^(\d+).*$/ && $1.to_i) || 0
30
38
  end
31
39
 
32
40
  def total_pages
33
- (total_entries.to_f / 15).ceil
41
+ total_entries > 0 ? (total_entries.to_f / 15).ceil : 0
34
42
  end
35
43
 
36
44
  private
@@ -1,3 +1,3 @@
1
1
  module DribbbleBucketApi
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -88,4 +88,50 @@ describe DribbbleBucketApi::BucketIndexParser do
88
88
  expect(subject.total_pages).to eq 6
89
89
  end
90
90
  end
91
+
92
+ describe "#next_page" do
93
+ context "when total_pages > current_page" do
94
+ before do
95
+ subject.stub(:current_page).and_return(2)
96
+ subject.stub(:total_pages).and_return(3)
97
+ end
98
+
99
+ it "should return current_page + 1" do
100
+ expect(subject.next_page).to eq 3
101
+ end
102
+ end
103
+
104
+ context "when total_pages <= current_page" do
105
+ before do
106
+ subject.stub(:current_page).and_return(3)
107
+ subject.stub(:total_pages).and_return(3)
108
+ end
109
+
110
+ it "should return nil" do
111
+ expect(subject.next_page).to be_nil
112
+ end
113
+ end
114
+ end
115
+
116
+ describe "#previous_page" do
117
+ context "when current_page > 1" do
118
+ before do
119
+ subject.stub(:current_page).and_return(3)
120
+ end
121
+
122
+ it "should return current_page - 1" do
123
+ expect(subject.previous_page).to eq 2
124
+ end
125
+ end
126
+
127
+ context "when current_page <= 1" do
128
+ before do
129
+ subject.stub(:current_page).and_return(1)
130
+ end
131
+
132
+ it "should return nil" do
133
+ expect(subject.previous_page).to be_nil
134
+ end
135
+ end
136
+ end
91
137
  end
@@ -0,0 +1,139 @@
1
+ require "spec_helper"
2
+ require_relative "../../lib/dribbble_bucket_api/shot_index_parser"
3
+
4
+ describe DribbbleBucketApi::ShotIndexParser do
5
+ let(:body) do
6
+ %Q(
7
+ <body>
8
+ <div id="main">
9
+ <h2 class="section">212 Shots</h2>
10
+ <ol class="dribbbles group">
11
+ <li id="screenshot-693587" class="group">
12
+ <div class="dribbble">
13
+ <div class="dribbble-shot">
14
+ <div class="dribbble-img">
15
+ <a href="/shots/693587-Timeline" class="dribbble-link"><img alt="Screen_shot_2012-08-18_at_6" src="http://dribbble.s3.amazonaws.com/users/2935/screenshots/693587/screen_shot_2012-08-18_at_6.41.47_pm_teaser.png"></a>
16
+ <a href="/shots/693587-Timeline" class="dribbble-over" style="opacity: 0; ">
17
+ <strong>Timeline</strong>
18
+ <em>August 18, 2012</em>
19
+ </a>
20
+ </div>
21
+ </div>
22
+ </div>
23
+ </li>
24
+
25
+ <li id="screenshot-693929" class="group">
26
+ <div class="dribbble">
27
+ <div class="dribbble-shot">
28
+ <div class="dribbble-img">
29
+ <a href="/shots/693929-Alternate-Timeline" class="dribbble-link"><img alt="Screen_shot_2012-08-18_at_6" src="http://dribbble.s3.amazonaws.com/users/2935/screenshots/693929/screen_shot_2012-08-18_at_6.41.38_pm_teaser.png"></a>
30
+ <a href="/shots/693929-Alternate-Timeline" class="dribbble-over">
31
+ <strong>Alternate Timeline</strong>
32
+ <em>August 19, 2012</em>
33
+ </a>
34
+ </div>
35
+ </div>
36
+ </div>
37
+ </li>
38
+ </ol>
39
+ </div>
40
+ </body>
41
+ )
42
+ end
43
+
44
+ let(:connection) do
45
+ mock("connection", username: "ryantownsend")
46
+ end
47
+
48
+ let(:options) do
49
+ {
50
+ page: 2,
51
+ connection: connection
52
+ }
53
+ end
54
+
55
+ subject do
56
+ DribbbleBucketApi::ShotIndexParser.new(body, options)
57
+ end
58
+
59
+ describe "#shots" do
60
+ it "should return an item for each shot in the list" do
61
+ expect(subject.shots.size).to eq 2
62
+ end
63
+
64
+ it "should return Shot instances" do
65
+ subject.shots.each do |shot|
66
+ expect(shot).to be_kind_of DribbbleBucketApi::Shot
67
+ end
68
+ end
69
+
70
+ it "should parse the ids correctly" do
71
+ ids = subject.shots.map(&:id)
72
+ expect(ids).to eq [693587, 693929]
73
+ end
74
+ end
75
+
76
+ describe "#current_page" do
77
+ it "should return the options page" do
78
+ expect(subject.current_page).to eq 2
79
+ end
80
+ end
81
+
82
+ describe "#next_page" do
83
+ context "when total_pages > current_page" do
84
+ before do
85
+ subject.stub(:current_page).and_return(2)
86
+ subject.stub(:total_pages).and_return(3)
87
+ end
88
+
89
+ it "should return current_page + 1" do
90
+ expect(subject.next_page).to eq 3
91
+ end
92
+ end
93
+
94
+ context "when total_pages <= current_page" do
95
+ before do
96
+ subject.stub(:current_page).and_return(3)
97
+ subject.stub(:total_pages).and_return(3)
98
+ end
99
+
100
+ it "should return nil" do
101
+ expect(subject.next_page).to be_nil
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "#previous_page" do
107
+ context "when current_page > 1" do
108
+ before do
109
+ subject.stub(:current_page).and_return(3)
110
+ end
111
+
112
+ it "should return current_page - 1" do
113
+ expect(subject.previous_page).to eq 2
114
+ end
115
+ end
116
+
117
+ context "when current_page <= 1" do
118
+ before do
119
+ subject.stub(:current_page).and_return(1)
120
+ end
121
+
122
+ it "should return nil" do
123
+ expect(subject.previous_page).to be_nil
124
+ end
125
+ end
126
+ end
127
+
128
+ describe "#total_entries" do
129
+ it "should return count from the HTML document" do
130
+ expect(subject.total_entries).to eq 212
131
+ end
132
+ end
133
+
134
+ describe "#total_pages" do
135
+ it "should return the total / 15" do
136
+ expect(subject.total_pages).to eq 15
137
+ end
138
+ end
139
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dribbble-bucket-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -123,6 +123,7 @@ files:
123
123
  - spec/dribbble_bucket_api/official_api_spec.rb
124
124
  - spec/dribbble_bucket_api/public_site_spec.rb
125
125
  - spec/dribbble_bucket_api/shot_collection_spec.rb
126
+ - spec/dribbble_bucket_api/shot_index_parser_spec.rb
126
127
  - spec/dribbble_bucket_api/shot_spec.rb
127
128
  - spec/dribbble_bucket_api_spec.rb
128
129
  - spec/spec_helper.rb
@@ -158,6 +159,7 @@ test_files:
158
159
  - spec/dribbble_bucket_api/official_api_spec.rb
159
160
  - spec/dribbble_bucket_api/public_site_spec.rb
160
161
  - spec/dribbble_bucket_api/shot_collection_spec.rb
162
+ - spec/dribbble_bucket_api/shot_index_parser_spec.rb
161
163
  - spec/dribbble_bucket_api/shot_spec.rb
162
164
  - spec/dribbble_bucket_api_spec.rb
163
165
  - spec/spec_helper.rb