mirror of
https://github.com/nov/fb_graph2
synced 2023-03-27 23:22:15 -04:00
basic searchable spec
This commit is contained in:
parent
f9bf93f3d0
commit
304975322b
5 changed files with 74 additions and 14 deletions
|
@ -1,19 +1,19 @@
|
|||
module FbGraph2
|
||||
module Searchable
|
||||
def self.search(query, options = {})
|
||||
def self.search(query, access_token, options = {})
|
||||
klass = options.delete(:class) || Searchable
|
||||
collection = Collection.new(
|
||||
Node.new(:search).authenticate(
|
||||
options.delete(:access_token)
|
||||
access_token
|
||||
).send(:get, options.merge(q: query))
|
||||
)
|
||||
yield collection if block_given?
|
||||
Searchable::Result.new(query, klass, options.merge(:collection => collection))
|
||||
Searchable::Result.new(query, access_token, klass, options.merge(:collection => collection))
|
||||
end
|
||||
|
||||
def search(query, options = {})
|
||||
def search(query, access_token, options = {})
|
||||
type = self.to_s.split('::').last.underscore
|
||||
Searchable.search(query, options.merge(:type => type, :class => self)) do |collection|
|
||||
Searchable.search(query, access_token, options.merge(:type => type, :class => self)) do |collection|
|
||||
collection.map! do |obj|
|
||||
self.new(obj[:id], obj.merge(
|
||||
:access_token => options[:access_token]
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
module FbGraph2
|
||||
module Searchable
|
||||
class Result < Collection
|
||||
attr_accessor :query, :klass, :collection, :options
|
||||
attr_accessor :query, :access_token, :klass, :collection, :options
|
||||
|
||||
def initialize(query, klass, options = {})
|
||||
def initialize(query, access_token, klass, options = {})
|
||||
@klass = klass
|
||||
@access_token = access_token
|
||||
@query = query
|
||||
@options = options
|
||||
@collection = options.delete(:collection) || Collection.new
|
||||
|
@ -12,18 +13,18 @@ module FbGraph2
|
|||
end
|
||||
|
||||
def next(_options_ = {})
|
||||
if self.collection.next.present?
|
||||
self.klass.search(self.query, self.options.merge(_options_).merge(self.collection.next))
|
||||
if collection.next.present?
|
||||
klass.search query, access_token, options.merge(_options_).merge(collection.next)
|
||||
else
|
||||
self.class.new(self.query, self.klass)
|
||||
self.class.new query, access_token, klass
|
||||
end
|
||||
end
|
||||
|
||||
def previous(_options_ = {})
|
||||
if self.collection.previous.present?
|
||||
self.klass.search(self.query, self.options.merge(_options_).merge(self.collection.previous))
|
||||
if collection.previous.present?
|
||||
klass.search query, access_token, options.merge(_options_).merge(collection.previous)
|
||||
else
|
||||
self.class.new(self.query, self.klass)
|
||||
self.class.new query, access_token, klass
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
50
spec/fb_graph2/searchable_spec.rb
Normal file
50
spec/fb_graph2/searchable_spec.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe FbGraph2::Searchable do
|
||||
describe 'search' do
|
||||
context 'User' do
|
||||
it 'should return users' do
|
||||
users = mock_graph :get, 'search', 'search/user', access_token: 'token', params: {
|
||||
q: 'nov.matake',
|
||||
type: 'user'
|
||||
} do
|
||||
FbGraph2::User.search 'nov.matake', 'token'
|
||||
end
|
||||
users.should be_instance_of FbGraph2::Searchable::Result
|
||||
users.should_not be_empty
|
||||
users.each do |user|
|
||||
user.should be_instance_of FbGraph2::User
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'pagination' do
|
||||
context 'when first page' do
|
||||
let :page1 do
|
||||
mock_graph :get, 'search', 'search/user', access_token: 'token', params: {
|
||||
q: 'nov.matake',
|
||||
type: 'user'
|
||||
} do
|
||||
FbGraph2::User.search 'nov.matake', 'token'
|
||||
end
|
||||
end
|
||||
|
||||
it 'should have next' do
|
||||
mock_graph :get, 'search', 'blank_collection', access_token: 'token', params: {
|
||||
q: 'nov.matake',
|
||||
type: 'user',
|
||||
__after_id: 'enc_AeyyBJc7uo9gY-nHN9-LM0yG_IKBN5nCmqdkG8b4Ql-COLTnpQ-2NUc-xSDK6VwPhsq2W9Ktnr0zf7dD25Bc9eKT',
|
||||
limit: 5000,
|
||||
offset: 5000
|
||||
} do
|
||||
page1.next
|
||||
end
|
||||
end
|
||||
|
||||
it 'should have no previous' do
|
||||
page1.previous.should be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,7 +8,7 @@ describe FbGraph2 do
|
|||
its(:logger) { should be_a Logger }
|
||||
its(:api_version) { should == 'v2.0' }
|
||||
its(:root_url) { should == 'https://graph.facebook.com' }
|
||||
its(:object_classes) { should contain_exactly *FbGraph2::Node.subclasses }
|
||||
its(:object_classes) { should contain_exactly *FbGraph2::Node.subclasses + [FbGraph2::Place] }
|
||||
it { should_not be_debugging }
|
||||
end
|
||||
|
||||
|
|
9
spec/mock_json/search/user.json
Normal file
9
spec/mock_json/search/user.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"data": [{
|
||||
"name": "Nov Matake",
|
||||
"id": "426118390869327"
|
||||
}],
|
||||
"paging": {
|
||||
"next": "https:\/\/graph.facebook.com\/v2.0\/search?type=user&q=nov.matake&limit=5000&offset=5000&__after_id=enc_AeyyBJc7uo9gY-nHN9-LM0yG_IKBN5nCmqdkG8b4Ql-COLTnpQ-2NUc-xSDK6VwPhsq2W9Ktnr0zf7dD25Bc9eKT"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue