storify 0.0.6 → 0.0.7

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: 488d1fb73f0c02593590f124002f103b0ed96fdd
4
- data.tar.gz: 008e7f1650cd761627297de605f2fcfb94760e39
3
+ metadata.gz: 8e5b35c38cdddf4afe33c92a7e6881ffaa67204a
4
+ data.tar.gz: 8812500f6fd457cbab902acb867eb360594f211e
5
5
  SHA512:
6
- metadata.gz: 7b5615dd1ca034c3478c3e41aed0e1f5508ec946c32b3abf58c57325dd3479806104278a9b02880ef00bf1f8a8c3fc56222efbbc54ffb4111e05b5dbd951b93d
7
- data.tar.gz: d9db9630e971f4dca1cacf937fc2757fdc564a649d53c6f5430264573836ad5c858fb68ebbe5fc8d7b0d35d94d7a011c4413e09e2cc1267df706962995988f7a
6
+ metadata.gz: 5242d0dc6441c8f683fd1f29014454094c24b099921e40de4a12f15ca679bf9509ffa5ee1e3f77303acf249a064866142cfbcba4d2e65ef71bc48b048f8a3b09
7
+ data.tar.gz: 0ae3995e36650a02fdddc67945dc00a29b4fc801abad24c477b84587fe5b1d95fec541f8eefbe3742b1e37de709f8b4ebf342aaf3880e223f17c7cd2cd0513e0
@@ -18,50 +18,51 @@ class Storify::Client
18
18
  self
19
19
  end
20
20
 
21
- def userstories(username = @username, options: {})
21
+ def userstories(username = @username, pager: nil, options: {})
22
22
  endpoint = Storify::endpoint(version: options[:version],
23
23
  protocol: options[:protocol],
24
24
  method: :userstories,
25
25
  params: {':username' => username})
26
26
 
27
- pager = add_pagination
27
+ pager = Storify::Pager.new unless pager.is_a?(Storify::Pager)
28
28
  stories = []
29
29
 
30
- while data = call(endpoint, :GET, pager)
30
+ begin
31
+ data = call(endpoint, :GET, pager.to_hash)
31
32
  content = data['content']
32
- break if content['stories'].length == 0
33
33
 
34
34
  content['stories'].each do |s|
35
35
  stories << Storify::Story.new(s)
36
36
  end
37
37
 
38
- pager[:page] += 1
39
- end
38
+ pager.next
39
+ end while pager.has_pages?(content['stories'])
40
40
 
41
41
  stories
42
42
  end
43
43
 
44
- def story(slug, username = @username, options: {})
44
+ def story(slug, username = @username, pager: nil, options: {})
45
45
  params = {':username' => username, ':slug' => slug}
46
46
  endpoint = Storify::endpoint(version: options[:version],
47
47
  protocol: options[:protocol],
48
48
  method: :userstory,
49
49
  params: params)
50
- pager = add_pagination
50
+
51
+ pager = Storify::Pager.new unless pager.is_a?(Storify::Pager)
51
52
  story = nil
52
53
  elements = []
53
54
 
54
- while data = call(endpoint, :GET, pager)
55
+ begin
56
+ data = call(endpoint, :GET, pager.to_hash)
55
57
  story = Storify::Story.new(data['content']) if story.nil?
56
- break if data['content']['elements'].length == 0
57
58
 
58
59
  # create elements
59
60
  data['content']['elements'].each do |e|
60
61
  story.add_element(Storify::Element.new(e))
61
62
  end
62
63
 
63
- pager[:page] += 1
64
- end
64
+ pager.next
65
+ end while pager.has_pages?(data['content']['elements'])
65
66
 
66
67
  story
67
68
  end
@@ -70,13 +71,6 @@ class Storify::Client
70
71
  !@token.nil?
71
72
  end
72
73
 
73
- def add_pagination(page = 1, per_page = 20)
74
- params = {}
75
- params[:page] = page
76
- params[:per_page] = per_page
77
- params
78
- end
79
-
80
74
 
81
75
  private
82
76
 
@@ -0,0 +1,46 @@
1
+ class Storify::Pager
2
+ MIN_PAGE = 1
3
+ MAX_PER_PAGE = 50
4
+
5
+ attr_reader :page, :per_page, :max
6
+
7
+ def initialize(page: 1, per_page: 20, max: 0)
8
+ @max = max.to_i.abs
9
+ self.page=(page)
10
+ self.per_page=(per_page)
11
+ end
12
+
13
+ def per_page=(value)
14
+ @per_page = (value > MAX_PER_PAGE) ? MAX_PER_PAGE : value.to_i.abs
15
+ end
16
+
17
+ def next
18
+ self.page=(@page + 1)
19
+ self
20
+ end
21
+
22
+ def prev
23
+ self.page=(@page - 1)
24
+ self
25
+ end
26
+
27
+ def to_hash
28
+ {:page => @page, :per_page => @per_page}
29
+ end
30
+
31
+ def has_pages?(array = [])
32
+ return false unless array.is_a?(Array)
33
+ return false if (@page > @max && @max != 0)
34
+
35
+ array.length != 0
36
+ end
37
+
38
+ private
39
+
40
+ def page=(value)
41
+ return @page = MIN_PAGE if (value < MIN_PAGE)
42
+ return @page = (@max + 1) if ((value > @max) && @max != 0)
43
+
44
+ @page = value.to_i
45
+ end
46
+ end
data/lib/storify.rb CHANGED
@@ -30,5 +30,6 @@ end
30
30
 
31
31
  require 'storify/story'
32
32
  require 'storify/client'
33
+ require 'storify/pager'
33
34
  require 'storify/element'
34
- require 'storify/apierror'
35
+ require 'storify/apierror'
data/spec/client_spec.rb CHANGED
@@ -18,6 +18,12 @@ describe Storify::Client do
18
18
  options = {:version => :v1, :protocol => :insecure}
19
19
  @client.userstories(@username, options: options).length.should == 2
20
20
  end
21
+
22
+ it "should accept paging options (Pager)" do
23
+ pager = Storify::Pager.new(page: 1, max: 1, per_page: 10)
24
+ stories = @client.userstories('joshuabaer', pager: pager)
25
+ stories.length.should == 10
26
+ end
21
27
  end
22
28
 
23
29
  context "GET /stories/:username/:slug" do
@@ -29,11 +35,18 @@ describe Storify::Client do
29
35
  options = {:version => :v1, :protocol => :insecure}
30
36
  @client.story(@story, options: options).elements.length.should == 3
31
37
  end
38
+
39
+ it "should accept paging options (Page)" do
40
+ pager = Storify::Pager.new(page: 2, max: 3)
41
+ story = @client.story('austin-startup-digest-for-december-9-2014', 'joshuabaer', pager: pager)
42
+ story = story.to_s
43
+
44
+ ['408651138632667136', '409182832234213376'].each {|s| story.include?(s).should be_true }
45
+ end
32
46
  end
33
47
 
34
48
  it "should allow a story to be serialized as text" do
35
49
  story = @client.story('austin-startup-digest-for-december-9-2014', 'joshuabaer')
36
50
  story.should_not eql ""
37
- puts story.to_s
38
51
  end
39
52
  end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe Storify::Pager do
4
+ context "Page" do
5
+ it "should optionally accept a starting page" do
6
+ Storify::Pager.new(page: 10).page.should == 10
7
+ end
8
+
9
+ it "should set the default starting page to 1" do
10
+ Storify::Pager.new.page.should == 1
11
+ end
12
+
13
+ it "should limit the page to the API Min" do
14
+ pager = Storify::Pager.new(page: -1)
15
+ pager.page.should == Storify::Pager::MIN_PAGE
16
+ end
17
+
18
+ it "should optionally allow an artificial max page (inclusive)" do
19
+ pager = Storify::Pager.new(max: 20)
20
+ pager.max.should == 20
21
+ end
22
+
23
+ it "should default the page max to unlimited (0)" do
24
+ pager = Storify::Pager.new
25
+ pager.max.should == 0
26
+ end
27
+
28
+ it "should allow a page to be advanced (forward)" do
29
+ pager = Storify::Pager.new
30
+ pager.next.page.should == 2
31
+ end
32
+
33
+ it "should not allow a page to be advanced beyond (max + 1)" do
34
+ pager = Storify::Pager.new(max: 2)
35
+ pager.next.next.next.page.should == 3
36
+ end
37
+
38
+ it "should allow a page to be advanced (backward)" do
39
+ pager = Storify::Pager.new(page: 2)
40
+ pager.prev.prev.page.should == 1
41
+ end
42
+ end
43
+
44
+ context "Per Page" do
45
+ it "should optionally accept a per page option" do
46
+ Storify::Pager.new(per_page: 15).per_page.should == 15
47
+ end
48
+
49
+ it "should set the default per_page to 20" do
50
+ Storify::Pager.new.per_page.should == 20
51
+ end
52
+
53
+ it "should limit per_page to the API Max" do
54
+ # Constructor
55
+ pager = Storify::Pager.new(per_page: 100)
56
+ pager.per_page.should == Storify::Pager::MAX_PER_PAGE
57
+
58
+ # Setter
59
+ pager.per_page = 500
60
+ pager.per_page.should == Storify::Pager::MAX_PER_PAGE
61
+ end
62
+ end
63
+
64
+ context "Utility" do
65
+ it "should provide access to pagnination as a parameter hash" do
66
+ pager = Storify::Pager.new(page: 13, per_page: 25).to_hash
67
+ pager[:page].should == 13
68
+ pager[:per_page].should == 25
69
+ end
70
+
71
+ it "should know if there are pages left based on content array size" do
72
+ data = {'content' => {'stories' => ['s1', 's2', 's3']}}
73
+ pager = Storify::Pager.new
74
+ pager.has_pages?(data['content']['stories']).should be_true
75
+ pager.has_pages?(data['content'][:invalid]).should be_false
76
+ pager.has_pages?([]).should be_false
77
+ end
78
+
79
+ it "should have no pages left once on the final page" do
80
+ pager = Storify::Pager.new(max: 2)
81
+ pager.next.next.next
82
+ pager.has_pages?(['g']).should be_false
83
+ end
84
+ end
85
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rizwan Tejpar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-10 00:00:00.000000000 Z
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -89,10 +89,12 @@ files:
89
89
  - lib/storify/apierror.rb
90
90
  - lib/storify/client.rb
91
91
  - lib/storify/element.rb
92
+ - lib/storify/pager.rb
92
93
  - lib/storify/story.rb
93
94
  - lib/storify.rb
94
95
  - spec/client_auth_spec.rb
95
96
  - spec/client_spec.rb
97
+ - spec/pager_spec.rb
96
98
  - spec/spec_helper.rb
97
99
  - spec/storify_spec.rb
98
100
  homepage: https://github.com/natural-affinity/storify
@@ -122,5 +124,6 @@ summary: Storify API
122
124
  test_files:
123
125
  - spec/client_auth_spec.rb
124
126
  - spec/client_spec.rb
127
+ - spec/pager_spec.rb
125
128
  - spec/spec_helper.rb
126
129
  - spec/storify_spec.rb