1
0
Fork 0
mirror of https://github.com/nov/fb_graph2 synced 2023-03-27 23:22:15 -04:00

better api versioning

close #1
This commit is contained in:
nov 2014-06-11 00:53:58 +09:00
parent ce15fe23c3
commit 682816790b
5 changed files with 74 additions and 18 deletions

View file

@ -2,8 +2,9 @@ require 'active_support/all'
require 'rack/oauth2' require 'rack/oauth2'
module FbGraph2 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.api_version = 'v2.0'
self.gem_version = File.read(File.join(__dir__, '../VERSION')).delete("\n\r") self.gem_version = File.read(File.join(__dir__, '../VERSION')).delete("\n\r")
self.logger = Logger.new(STDOUT) self.logger = Logger.new(STDOUT)
@ -11,10 +12,6 @@ module FbGraph2
self.object_classes = Array.new self.object_classes = Array.new
class << self class << self
def root_url
File.join('https://graph.facebook.com', api_version)
end
def debugging? def debugging?
!!self.debugging !!self.debugging
end end

View file

@ -18,13 +18,13 @@ module FbGraph2
self self
end end
def fetch(params = {}) def fetch(params = {}, options = {})
attributes = get params attributes = get params, options
self.class.new(attributes[:id], attributes).authenticate access_token self.class.new(attributes[:id], attributes).authenticate access_token
end end
def self.fetch(identifier, params = {}) def self.fetch(identifier, params = {}, options = {})
new(identifier).fetch params new(identifier).fetch params, options
end end
def edge(edge, params = {}, options = {}) def edge(edge, params = {}, options = {})
@ -85,7 +85,11 @@ module FbGraph2
def build_endpoint(options = {}) def build_endpoint(options = {})
File.join [ 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], options[:edge],
Util.as_identifier(options[:edge_scope]) Util.as_identifier(options[:edge_scope])
].compact.collect(&:to_s) ].compact.collect(&:to_s)

View file

@ -4,6 +4,58 @@ describe FbGraph2::Node do
let(:klass) { FbGraph2::Node } let(:klass) { FbGraph2::Node }
let(:instance) { klass.new 'identifier' } 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 context 'class' do
subject { klass } subject { klass }
it { should_not respond_to :register_attributes } it { should_not respond_to :register_attributes }
@ -14,7 +66,7 @@ describe FbGraph2::Node do
it 'should call API' do it 'should call API' do
expect do expect do
klass.fetch 'foo' klass.fetch 'foo'
end.to request_to '/foo' end.to request_to 'foo'
end end
end end
end end

View file

@ -7,7 +7,7 @@ describe FbGraph2 do
context 'as default' do context 'as default' do
its(:logger) { should be_a Logger } its(:logger) { should be_a Logger }
its(:api_version) { should == 'v2.0' } 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 } its(:object_classes) { should contain_exactly *FbGraph2::Node.subclasses }
it { should_not be_debugging } it { should_not be_debugging }
end end
@ -19,7 +19,7 @@ describe FbGraph2 do
describe '.api_version' do describe '.api_version' do
before { FbGraph2.api_version = 'v2.x' } before { FbGraph2.api_version = 'v2.x' }
its(:root_url) { should == 'https://graph.facebook.com/v2.x' } its(:api_version) { should == 'v2.x' }
end end
describe '.http_client' do describe '.http_client' do

View file

@ -4,7 +4,7 @@ module MockGraph
def mock_graph(method, path, response_path, options = {}) def mock_graph(method, path, response_path, options = {})
stub_request( stub_request(
method, method,
endpoint_for(path) endpoint_for(path, options)
).with( ).with(
request_for(method, options) request_for(method, options)
).to_return( ).to_return(
@ -26,18 +26,21 @@ module MockGraph
response_for(response_path)[:body].read response_for(response_path)[:body].read
end end
def request_to(path, method = :get) def request_to(path, method = :get, options = {})
raise_error { |e| raise_error { |e|
e.should be_instance_of WebMock::NetConnectNotAllowedError e.should be_instance_of WebMock::NetConnectNotAllowedError
e.message.should include("Unregistered request: #{method.to_s.upcase}") 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 end
private private
def endpoint_for(path) def endpoint_for(path, options = {})
File.join(FbGraph2.root_url, path) 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 end
def request_for(method, options = {}) def request_for(method, options = {})