mirror of
https://github.com/nov/fb_graph2
synced 2023-03-27 23:22:15 -04:00
edge summary & more tests
This commit is contained in:
parent
4e9d72703d
commit
a45de9e8e8
5 changed files with 227 additions and 3 deletions
|
@ -1,10 +1,11 @@
|
|||
module FbGraph2
|
||||
class Collection < Array
|
||||
attr_accessor :before, :after, :next, :previous
|
||||
attr_accessor :before, :after, :next, :previous, :order, :total_count
|
||||
|
||||
def initialize(collection = [])
|
||||
collection = normalize collection
|
||||
setup_pagination collection[:paging]
|
||||
paginate collection[:paging]
|
||||
summarize collection[:summary]
|
||||
replace Array(collection[:data])
|
||||
end
|
||||
|
||||
|
@ -24,7 +25,7 @@ module FbGraph2
|
|||
end
|
||||
end
|
||||
|
||||
def setup_pagination(paging)
|
||||
def paginate(paging)
|
||||
cursors = paging.try(:[], :cursors)
|
||||
self.before = cursors.try(:[], :before)
|
||||
self.after = cursors.try(:[], :after)
|
||||
|
@ -32,6 +33,13 @@ module FbGraph2
|
|||
self.previous = params_in paging.try(:[], :previous)
|
||||
end
|
||||
|
||||
def summarize(summary)
|
||||
# NOTE: notifications edge returns "summary" as a blank Array.
|
||||
summary = Hash(summary)
|
||||
self.order = summary.try(:[], :order)
|
||||
self.total_count = summary.try(:[], :total_count)
|
||||
end
|
||||
|
||||
def params_in(url)
|
||||
if url
|
||||
Rack::Utils.parse_nested_query(
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module FbGraph2
|
||||
class Edge < Collection
|
||||
attr_accessor :owner, :edge, :params, :options, :collection
|
||||
delegate :order, :total_count, to: :collection
|
||||
|
||||
def initialize(owner, edge, params = {}, options = {})
|
||||
self.owner = owner
|
||||
|
|
|
@ -41,6 +41,16 @@ describe FbGraph2::Edge::Comments do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when summary requested' do
|
||||
it 'should be summarized' do
|
||||
comments = mock_graph :get, 'post_id/comments', 'post/comments_with_summary', access_token: 'token' do
|
||||
post.comments
|
||||
end
|
||||
comments.order.should == 'chronological'
|
||||
comments.total_count.should == 4
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#comment!' do
|
||||
|
|
148
spec/fb_graph2/edge_spec.rb
Normal file
148
spec/fb_graph2/edge_spec.rb
Normal file
|
@ -0,0 +1,148 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe FbGraph2::Edge do
|
||||
let(:me) { FbGraph2::User.me 'access_token' }
|
||||
let(:post) { FbGraph2::Post.new 'post_id', access_token: 'access_token' }
|
||||
let(:feed) do
|
||||
mock_graph :get, 'me/feed', 'user/feed', access_token: 'access_token' do
|
||||
me.feed
|
||||
end
|
||||
end
|
||||
let(:comments) do
|
||||
mock_graph :get, 'post_id/comments', 'post/comments_with_summary', access_token: 'access_token' do
|
||||
post.comments
|
||||
end
|
||||
end
|
||||
|
||||
describe 'summary' do
|
||||
subject { comments }
|
||||
its(:order) { should == 'chronological' }
|
||||
its(:total_count) { should == 4 }
|
||||
end
|
||||
|
||||
describe 'pagination' do
|
||||
context 'when next/previous-url-based' do
|
||||
describe 'next' do
|
||||
context 'when next page exists' do
|
||||
it 'should fetch next page' do
|
||||
feed.collection.next.should match(limit: '25', until: '1400651347')
|
||||
_next_ = mock_graph :get, 'me/feed', 'blank_collection', access_token: 'access_token', params: {
|
||||
limit: 25,
|
||||
until: '1400651347'
|
||||
} do
|
||||
feed.next
|
||||
end
|
||||
_next_.should be_instance_of FbGraph2::Edge
|
||||
end
|
||||
end
|
||||
|
||||
context 'otherwise' do
|
||||
let(:without_next) do
|
||||
mock_graph :get, 'me/feed', 'blank_collection', access_token: 'access_token', params: {
|
||||
limit: 25,
|
||||
until: '1400651347'
|
||||
} do
|
||||
feed.next
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
without_next.collection.next.should be_blank
|
||||
without_next.next.should be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'previous' do
|
||||
context 'when previous page exists' do
|
||||
it 'should fetch previous page' do
|
||||
feed.collection.previous.should match(limit: '25', since: '1401410484')
|
||||
previous = mock_graph :get, 'me/feed', 'blank_collection', access_token: 'access_token', params: {
|
||||
limit: 25,
|
||||
since: '1401410484'
|
||||
} do
|
||||
feed.previous
|
||||
end
|
||||
previous.should be_instance_of FbGraph2::Edge
|
||||
end
|
||||
end
|
||||
|
||||
context 'otherwise' do
|
||||
let(:without_previous) do
|
||||
mock_graph :get, 'me/feed', 'blank_collection', access_token: 'access_token', params: {
|
||||
limit: 25,
|
||||
since: '1401410484'
|
||||
} do
|
||||
feed.previous
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
without_previous.collection.previous.should be_blank
|
||||
without_previous.previous.should be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when cursors-based' do
|
||||
describe 'next' do
|
||||
context 'when after cursor exists' do
|
||||
it 'should fetch next page' do
|
||||
comments.collection.after.should == 'NA=='
|
||||
_next_ = mock_graph :get, 'post_id/comments', 'blank_collection', access_token: 'access_token', params: {
|
||||
after: 'NA=='
|
||||
} do
|
||||
comments.next
|
||||
end
|
||||
_next_.should be_instance_of FbGraph2::Edge
|
||||
end
|
||||
end
|
||||
|
||||
context 'otherwise' do
|
||||
let(:without_next) do
|
||||
mock_graph :get, 'post_id/comments', 'blank_collection', access_token: 'access_token', params: {
|
||||
after: 'NA=='
|
||||
} do
|
||||
comments.next
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
without_next.collection.after.should be_blank
|
||||
without_next.next.should be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'previous' do
|
||||
context 'when before cursor exists' do
|
||||
it 'should fetch previous page' do
|
||||
comments.collection.before.should == 'MQ=='
|
||||
previous = mock_graph :get, 'post_id/comments', 'blank_collection', access_token: 'access_token', params: {
|
||||
before: 'MQ=='
|
||||
} do
|
||||
comments.previous
|
||||
end
|
||||
previous.should be_instance_of FbGraph2::Edge
|
||||
end
|
||||
end
|
||||
|
||||
context 'otherwise' do
|
||||
let(:without_previous) do
|
||||
mock_graph :get, 'post_id/comments', 'blank_collection', access_token: 'access_token', params: {
|
||||
before: 'MQ=='
|
||||
} do
|
||||
comments.previous
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
without_previous.collection.before.should be_blank
|
||||
without_previous.previous.should be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
57
spec/mock_json/post/comments_with_summary.json
Normal file
57
spec/mock_json/post/comments_with_summary.json
Normal file
|
@ -0,0 +1,57 @@
|
|||
{
|
||||
"data": [{
|
||||
"id": "10152662663892277_10152662666892277",
|
||||
"from": {
|
||||
"id": "579612276",
|
||||
"name": "Nov Matake"
|
||||
},
|
||||
"message": "hummm, it's not \"post\"...",
|
||||
"can_remove": true,
|
||||
"created_time": "2014-08-20T05:34:21+0000",
|
||||
"like_count": 0,
|
||||
"user_likes": false
|
||||
}, {
|
||||
"id": "10152662663892277_10152662686952277",
|
||||
"from": {
|
||||
"id": "579612276",
|
||||
"name": "Nov Matake"
|
||||
},
|
||||
"message": "579612276_10152662663892277 = post\n10152662663892277 = status",
|
||||
"can_remove": true,
|
||||
"created_time": "2014-08-20T05:57:11+0000",
|
||||
"like_count": 0,
|
||||
"user_likes": false
|
||||
}, {
|
||||
"id": "10152662663892277_10152662687272277",
|
||||
"from": {
|
||||
"id": "579612276",
|
||||
"name": "Nov Matake"
|
||||
},
|
||||
"message": "でも多分v2.1完成してない。 http://stackoverflow.com/questions/25270649/facebook-graph-api-v2-1-all-post-photos",
|
||||
"can_remove": true,
|
||||
"created_time": "2014-08-20T05:57:31+0000",
|
||||
"like_count": 0,
|
||||
"user_likes": false
|
||||
}, {
|
||||
"id": "10152662663892277_10152662687387277",
|
||||
"from": {
|
||||
"id": "579612276",
|
||||
"name": "Nov Matake"
|
||||
},
|
||||
"message": "完成さしてからだせや!!",
|
||||
"can_remove": true,
|
||||
"created_time": "2014-08-20T05:57:48+0000",
|
||||
"like_count": 0,
|
||||
"user_likes": false
|
||||
}],
|
||||
"paging": {
|
||||
"cursors": {
|
||||
"after": "NA==",
|
||||
"before": "MQ=="
|
||||
}
|
||||
},
|
||||
"summary": {
|
||||
"order": "chronological",
|
||||
"total_count": 4
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue