diff --git a/lib/fb_graph2/searchable.rb b/lib/fb_graph2/searchable.rb index c6b4a72..478d3a7 100644 --- a/lib/fb_graph2/searchable.rb +++ b/lib/fb_graph2/searchable.rb @@ -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] diff --git a/lib/fb_graph2/searchable/result.rb b/lib/fb_graph2/searchable/result.rb index 4b0ece2..461a145 100644 --- a/lib/fb_graph2/searchable/result.rb +++ b/lib/fb_graph2/searchable/result.rb @@ -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 diff --git a/spec/fb_graph2/searchable_spec.rb b/spec/fb_graph2/searchable_spec.rb new file mode 100644 index 0000000..22ecfc1 --- /dev/null +++ b/spec/fb_graph2/searchable_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/fb_graph2_spec.rb b/spec/fb_graph2_spec.rb index 5b0fdc3..b38cbc4 100644 --- a/spec/fb_graph2_spec.rb +++ b/spec/fb_graph2_spec.rb @@ -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 diff --git a/spec/mock_json/search/user.json b/spec/mock_json/search/user.json new file mode 100644 index 0000000..e199619 --- /dev/null +++ b/spec/mock_json/search/user.json @@ -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" + } +}