diff --git a/lib/fb_graph2.rb b/lib/fb_graph2.rb index 3ace01c..89df2b4 100644 --- a/lib/fb_graph2.rb +++ b/lib/fb_graph2.rb @@ -2,8 +2,9 @@ require 'active_support/all' require 'rack/oauth2' module FbGraph2 - cattr_accessor :api_version, :gem_version, :logger, :debugging, :_http_config_, :object_classes + cattr_accessor :root_url, :api_version, :gem_version, :logger, :debugging, :_http_config_, :object_classes + self.root_url = 'https://graph.facebook.com' self.api_version = 'v2.0' self.gem_version = File.read(File.join(__dir__, '../VERSION')).delete("\n\r") self.logger = Logger.new(STDOUT) @@ -11,10 +12,6 @@ module FbGraph2 self.object_classes = Array.new class << self - def root_url - File.join('https://graph.facebook.com', api_version) - end - def debugging? !!self.debugging end diff --git a/lib/fb_graph2/node.rb b/lib/fb_graph2/node.rb index 92067a6..412a9eb 100644 --- a/lib/fb_graph2/node.rb +++ b/lib/fb_graph2/node.rb @@ -18,13 +18,13 @@ module FbGraph2 self end - def fetch(params = {}) - attributes = get params + def fetch(params = {}, options = {}) + attributes = get params, options self.class.new(attributes[:id], attributes).authenticate access_token end - def self.fetch(identifier, params = {}) - new(identifier).fetch params + def self.fetch(identifier, params = {}, options = {}) + new(identifier).fetch params, options end def edge(edge, params = {}, options = {}) @@ -85,7 +85,11 @@ module FbGraph2 def build_endpoint(options = {}) File.join [ - File.join(FbGraph2.root_url, id.to_s), + File.join( + FbGraph2.root_url, + options[:api_version] || FbGraph2.api_version, + id.to_s + ), options[:edge], Util.as_identifier(options[:edge_scope]) ].compact.collect(&:to_s) diff --git a/spec/fb_graph2/node_spec.rb b/spec/fb_graph2/node_spec.rb index dce1af8..e276f5b 100644 --- a/spec/fb_graph2/node_spec.rb +++ b/spec/fb_graph2/node_spec.rb @@ -4,6 +4,58 @@ describe FbGraph2::Node do let(:klass) { FbGraph2::Node } let(:instance) { klass.new 'identifier' } + describe 'API Versioning' do + before do + @original = FbGraph2.api_version + end + + after do + FbGraph2.api_version = @original + end + + describe 'via global setting' do + before do + FbGraph2.api_version = 'v2.x' + end + + describe '#fetch' do + it 'should use api_version globally' do + expect do + instance.fetch + end.to request_to 'v2.x/identifier', :get, api_version_in_path: true + end + end + + describe '#edge' do + it 'should use api_version globally' do + expect do + instance.edge :foo + end.to request_to 'v2.x/identifier/foo', :get, api_version_in_path: true + end + end + end + + describe 'via per-call option' do + describe '#fetch' do + it 'should use api_version locally' do + expect do + instance.fetch nil, api_version: 'v2.y' + end.to request_to 'v2.y/identifier', :get, api_version_in_path: true + FbGraph2.api_version.should == @original + end + end + + describe '#edge' do + it 'should use api_version locally' do + expect do + instance.edge :foo, {}, api_version: 'v2.y' + end.to request_to 'v2.y/identifier/foo', :get, api_version_in_path: true + FbGraph2.api_version.should == @original + end + end + end + end + context 'class' do subject { klass } it { should_not respond_to :register_attributes } @@ -14,7 +66,7 @@ describe FbGraph2::Node do it 'should call API' do expect do klass.fetch 'foo' - end.to request_to '/foo' + end.to request_to 'foo' end end end diff --git a/spec/fb_graph2_spec.rb b/spec/fb_graph2_spec.rb index 55849c2..f249981 100644 --- a/spec/fb_graph2_spec.rb +++ b/spec/fb_graph2_spec.rb @@ -7,7 +7,7 @@ describe FbGraph2 do context 'as default' do its(:logger) { should be_a Logger } its(:api_version) { should == 'v2.0' } - its(:root_url) { should == 'https://graph.facebook.com/v2.0' } + its(:root_url) { should == 'https://graph.facebook.com' } its(:object_classes) { should contain_exactly *FbGraph2::Node.subclasses } it { should_not be_debugging } end @@ -19,7 +19,7 @@ describe FbGraph2 do describe '.api_version' do before { FbGraph2.api_version = 'v2.x' } - its(:root_url) { should == 'https://graph.facebook.com/v2.x' } + its(:api_version) { should == 'v2.x' } end describe '.http_client' do diff --git a/spec/spec_helper/mock_graph.rb b/spec/spec_helper/mock_graph.rb index f48b967..7b313e4 100644 --- a/spec/spec_helper/mock_graph.rb +++ b/spec/spec_helper/mock_graph.rb @@ -4,7 +4,7 @@ module MockGraph def mock_graph(method, path, response_path, options = {}) stub_request( method, - endpoint_for(path) + endpoint_for(path, options) ).with( request_for(method, options) ).to_return( @@ -26,18 +26,21 @@ module MockGraph response_for(response_path)[:body].read end - def request_to(path, method = :get) + def request_to(path, method = :get, options = {}) raise_error { |e| e.should be_instance_of WebMock::NetConnectNotAllowedError e.message.should include("Unregistered request: #{method.to_s.upcase}") - e.message.should include(endpoint_for path) + e.message.should include(endpoint_for path, options) } end private - def endpoint_for(path) - File.join(FbGraph2.root_url, path) + def endpoint_for(path, options = {}) + api_version = unless options[:api_version_in_path] + options[:api_version] || FbGraph2.api_version + end + File.join FbGraph2.root_url, api_version.to_s, path end def request_for(method, options = {})