uservoice-ruby 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +1,34 @@
1
1
  module UserVoice
2
2
  class Collection
3
+ PER_PAGE = 500
4
+
3
5
  def initialize(client, query, opts={})
4
6
  @client = client
5
7
  @query = query
6
8
  @limit = opts[:limit] || 2**60
7
- @per_page = [@limit, 500].min
9
+ @per_page = [@limit, PER_PAGE].min
8
10
  @pages = {}
9
11
  end
10
12
 
11
13
  def first
12
- load_record(0)
14
+ self[0]
13
15
  end
14
16
 
15
17
  def last
16
- load_record(size() - 1)
18
+ self[size() - 1]
17
19
  end
18
20
 
19
21
  def size
20
22
  if @response_data.nil?
21
- load_record(0)
23
+ self[0]
22
24
  end
23
- @response_data['total_records']
25
+ [@response_data['total_records'], @limit].min
24
26
  end
25
27
 
26
28
  def map
27
29
  index = 0
28
30
  records = []
29
- while record = load_record(index)
31
+ while record = self[index]
30
32
  records.push(yield record)
31
33
  index += 1
32
34
  end
@@ -40,12 +42,17 @@ module UserVoice
40
42
  value
41
43
  end
42
44
  end
45
+
46
+ def to_a
47
+ each {}
48
+ end
43
49
 
50
+ def [](i)
51
+ load_page((i/PER_PAGE.to_f).floor + 1)[i%PER_PAGE] if (0..@limit-1).include?(i)
52
+ end
53
+
44
54
  private
45
55
 
46
- def load_record(i)
47
- load_page((i/500.0).floor + 1)[i%500]
48
- end
49
56
 
50
57
  def load_page(i)
51
58
  if @pages[i].nil?
@@ -1,3 +1,3 @@
1
1
  module Uservoice
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe UserVoice::Collection do
4
- PER_PAGE = 500
5
- ELEMENTS = 1501 # 4 pages, one record in the last page
6
4
 
7
5
  context 'having an empty result set' do
8
6
  let(:client) do
@@ -23,6 +21,14 @@ describe UserVoice::Collection do
23
21
  raise RuntimeError.new('should be empty')
24
22
  end
25
23
  end
24
+
25
+ it 'should convert to empty array with to_a' do
26
+ @collection.to_a.should == []
27
+ end
28
+
29
+ it 'should not have first record' do
30
+ @collection[0].should == nil
31
+ end
26
32
  end
27
33
 
28
34
  context 'having a list with one element' do
@@ -60,7 +66,7 @@ describe UserVoice::Collection do
60
66
  end
61
67
 
62
68
  it 'should collect ids' do
63
- @client.should_receive(:get).with("/api/v1/suggestions?per_page=#{PER_PAGE}&page=1").once
69
+ @client.should_receive(:get).with("/api/v1/suggestions?per_page=#{UserVoice::Collection::PER_PAGE}&page=1").once
64
70
 
65
71
  @collection.collect do |val|
66
72
  val['id']
@@ -73,15 +79,25 @@ describe UserVoice::Collection do
73
79
  end
74
80
 
75
81
  context 'having a list with 1501 elements' do
82
+ ELEMENTS = 1501 # 4 pages, one record in the last page
76
83
 
77
84
  before do
78
85
  @client = mock()
79
-
86
+
80
87
  4.times.map do |page_index|
81
- @client.stub(:get).with("/api/v1/suggestions?per_page=#{PER_PAGE}&page=#{page_index+1}") do
88
+ page_first_index = UserVoice::Collection::PER_PAGE * page_index + 1
89
+ page_last_index = [UserVoice::Collection::PER_PAGE * (page_index + 1), ELEMENTS].min
90
+
91
+ @client.stub(:get).with("/api/v1/suggestions?per_page=#{UserVoice::Collection::PER_PAGE}&page=#{page_index+1}") do
82
92
  {
83
- "response_data" => {"page"=> page_index+1, "per_page" => PER_PAGE, "total_records" => ELEMENTS, "filter"=>"all", "sort"=>"votes"},
84
- "suggestions"=> (PER_PAGE * page_index + 1).upto([PER_PAGE * (page_index + 1), ELEMENTS].min).map do |idea_index|
93
+ "response_data" => {
94
+ "page"=> page_index+1,
95
+ "per_page" => UserVoice::Collection::PER_PAGE,
96
+ "total_records" => ELEMENTS,
97
+ "filter"=>"all",
98
+ "sort"=>"votes"
99
+ },
100
+ "suggestions"=> page_first_index.upto(page_last_index).map do |idea_index|
85
101
  {
86
102
  "url"=>"http://uservoice-subdomain.uservoice.com/forums/1-general/suggestions/#{idea_index}-idea",
87
103
  "id"=> idea_index,
@@ -99,10 +115,16 @@ describe UserVoice::Collection do
99
115
  end
100
116
 
101
117
  it 'should have correct size' do
102
- @client.should_receive(:get).with("/api/v1/suggestions?per_page=#{PER_PAGE}&page=1").once
118
+ @client.should_receive(:get).with("/api/v1/suggestions?per_page=#{UserVoice::Collection::PER_PAGE}&page=1").once
103
119
  @collection.size.should == ELEMENTS
104
120
  end
105
121
 
122
+ it 'should the size defined by limit' do
123
+ collection = UserVoice::Collection.new(@client, '/api/v1/suggestions', :limit => 1337)
124
+ collection.size.should == 1337
125
+ collection.last['id'].should == 1337
126
+ end
127
+
106
128
  it 'should get last element and array size with two api calls' do
107
129
  @collection.last['id'].should == ELEMENTS
108
130
  @collection.first['id'].should == 1
@@ -122,5 +144,11 @@ describe UserVoice::Collection do
122
144
  val['id']
123
145
  end.should == 1.upto(ELEMENTS).to_a
124
146
  end
147
+
148
+ it 'should convert to array' do
149
+ @collection.to_a.map do |val|
150
+ val['id']
151
+ end.should == 1.upto(ELEMENTS).to_a
152
+ end
125
153
  end
126
154
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uservoice-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: