grizzly-weibo 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,6 +37,12 @@ friends.first.screen_name #=> "Fred Chang"
37
37
  friends.first.gender #=> "m"
38
38
  ```
39
39
 
40
+ You can convert a user object to a hash if you need to however use . syntax where you can. Makes the code look cleaner
41
+
42
+ ```ruby
43
+ friends.first.to_h["screen_name"] #=> "Fred Chang"
44
+ ```
45
+
40
46
  ### Friends (Bilateral)
41
47
  Returns a list of your bilateral friends on Weibo. This list has users who are a fan of you and your are a fan of.
42
48
 
@@ -45,3 +51,28 @@ friends = client.bilateral_friends(user_id)
45
51
  ```
46
52
 
47
53
  This method returns the same friend object that ```client.friends``` returns.
54
+
55
+ ### Cursors
56
+ Weibo API supports paging. Both the ```friends``` and ```bilateral_friends``` are paginated. This means that when we
57
+ call these methods we have a cursor object returned. This cursor object can be iterated over. Iterating in this way will
58
+ only iterate over the first 50 results
59
+
60
+ ```ruby
61
+ friends = client.friends(user_id)
62
+ friends.each do |friend|
63
+ ...
64
+ end
65
+ ```
66
+
67
+ For something a little more robust we can iterate over pages as well. This will get all of a users friends iterating
68
+ over them in blocks of fifties. Note that each time you access friends.next_page an new request to the weibo api will be
69
+ generated.
70
+
71
+ ```ruby
72
+ friends = client.friends(user_id)
73
+ while friends.next_page? #Loops untill end of collection
74
+ friends.next_page do |friend|
75
+ ... # Loops 50 times
76
+ end
77
+ end
78
+ ```
@@ -1,6 +1,6 @@
1
1
  require 'grizzly/request'
2
2
  require 'grizzly/client'
3
+ require 'grizzly/cursor'
3
4
  require 'grizzly/user'
4
5
  require 'grizzly/errors/no_access_token'
5
6
  require 'grizzly/errors/weibo_api'
6
-
@@ -6,28 +6,11 @@ module Grizzly
6
6
  end
7
7
 
8
8
  def friends(user_id)
9
- response = perfrom_request(:get, '/friendships/friends', {:access_token => @access_token, :uid => user_id})
10
- users = []
11
- response["users"].each do |user|
12
- users << Grizzly::User.new(user)
13
- end
14
- users
9
+ Grizzly::Cursor.new(Grizzly::User, "/friendships/friends", {:access_token => @access_token, :uid => user_id})
15
10
  end
16
11
 
17
12
  def bilateral_friends(user_id)
18
- response = perfrom_request(:get, '/friendships/friends/bilateral', {:access_token => @access_token, :uid => user_id})
19
- users = []
20
- response["users"].each do |user|
21
- users << Grizzly::User.new(user)
22
- end
23
- users
24
- end
25
-
26
- private
27
-
28
- def perfrom_request(method, url, options)
29
- request = Grizzly::Request.new(method, url, options)
30
- request.response
13
+ Grizzly::Cursor.new(Grizzly::User, "/friendships/friends/bilateral", {:access_token => @access_token, :uid => user_id})
31
14
  end
32
15
  end
33
16
  end
@@ -0,0 +1,47 @@
1
+ module Grizzly
2
+ class Cursor
3
+ include Enumerable
4
+ ITEMS_PER_PAGE = 50
5
+
6
+ def initialize(domain_object, url, options, items_per_page = ITEMS_PER_PAGE)
7
+ @domain_object, @url, @options, @items_per_page = domain_object, url, options, items_per_page
8
+ @current_page = 1
9
+ fetch_current_page
10
+ end
11
+
12
+ #Each will return the first page of results
13
+ def each(&block)
14
+ @items.each { |i| block.call i }
15
+ end
16
+
17
+ #Next page will return the result set
18
+ def next_page(&block)
19
+ @items.each { |i| block.call i }
20
+ @current_page += 1
21
+ fetch_current_page
22
+ end
23
+
24
+ def next_page?
25
+ (@current_page * @items_per_page) < @total_items
26
+ end
27
+
28
+ private
29
+
30
+ def page_request(url, options)
31
+ request = Grizzly::Request.new(:get, url, options)
32
+ request.response
33
+ end
34
+
35
+ def fetch_current_page
36
+ options = @options.merge({ :page => @current_page, :count => @items_per_page })
37
+ response = page_request(@url, options)
38
+ @total_items = response["total_number"]
39
+
40
+ @items = []
41
+ response[@domain_object::API_COLLECTION_NAME].each do |domain_object|
42
+ @items << @domain_object.new(domain_object)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -1,5 +1,7 @@
1
1
  module Grizzly
2
2
  class User
3
+
4
+ API_COLLECTION_NAME = "users"
3
5
 
4
6
  def initialize(user_data)
5
7
  @data = user_data
@@ -13,5 +15,9 @@ module Grizzly
13
15
  end
14
16
  end
15
17
 
18
+ def to_h
19
+ @data
20
+ end
21
+
16
22
  end
17
23
  end
@@ -1,3 +1,3 @@
1
1
  module Grizzly
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grizzly-weibo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-26 00:00:00.000000000Z
12
+ date: 2012-03-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -100,6 +100,7 @@ extensions: []
100
100
  extra_rdoc_files: []
101
101
  files:
102
102
  - lib/grizzly/client.rb
103
+ - lib/grizzly/cursor.rb
103
104
  - lib/grizzly/errors/no_access_token.rb
104
105
  - lib/grizzly/errors/weibo_api.rb
105
106
  - lib/grizzly/request.rb
@@ -122,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
123
  version: '0'
123
124
  segments:
124
125
  - 0
125
- hash: 1587233218334402021
126
+ hash: 2776003271979443730
126
127
  required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  none: false
128
129
  requirements: