diff --git a/lib/fb_graph2.rb b/lib/fb_graph2.rb index fc02dd9..04fa07f 100644 --- a/lib/fb_graph2.rb +++ b/lib/fb_graph2.rb @@ -2,14 +2,18 @@ require 'active_support/all' require 'rack/oauth2' module FbGraph2 - VERSION = File.read(File.join(__dir__, '../VERSION')).delete("\n\r") - ROOT_URL = 'https://graph.facebook.com/v2.0' + cattr_accessor :api_version, :gem_version, :logger, :debugging, :_http_config_ - cattr_accessor :logger, :debugging, :_http_config_ + self.api_version = 'v2.0' + self.gem_version = File.read(File.join(__dir__, '../VERSION')).delete("\n\r") self.logger = Logger.new(STDOUT) self.logger.progname = 'FbGraph2' class << self + def root_url + File.join('https://graph.facebook.com', api_version) + end + def debugging? !!self.debugging end @@ -20,10 +24,13 @@ module FbGraph2 def http_client(access_token = nil) _http_client_ = HTTPClient.new( - agent_name: "FbGraph2 (#{VERSION})" + agent_name: "FbGraph2 (#{gem_version})" ) - _http_client_.request_filter << RequestFilters::Authenticator.new(access_token) if access_token.present? - _http_client_.request_filter << RequestFilters::Debugger.new if self.debugging? + _http_client_.request_filter.delete_if do |filter| + filter.is_a? HTTPClient::WWWAuth + end + _http_client_.request_filter << RequestFilter::Authenticator.new(access_token) if access_token.present? + _http_client_.request_filter << RequestFilter::Debugger.new if self.debugging? _http_config_.try(:call, _http_client_) _http_client_ end @@ -35,6 +42,11 @@ module FbGraph2 end require 'fb_graph2/node' -Dir[File.dirname(__FILE__) + '/fb_graph2/*.rb'].each do |file| - require file -end \ No newline at end of file +[ + '.', + 'request_filter' +].each do |dir| + Dir[File.join(__dir__, 'fb_graph2', dir, '*.rb')].each do |file| + require file + end +end diff --git a/lib/fb_graph2/node.rb b/lib/fb_graph2/node.rb index c696578..b6892f3 100644 --- a/lib/fb_graph2/node.rb +++ b/lib/fb_graph2/node.rb @@ -22,7 +22,7 @@ module FbGraph2 protected def http_client - FbGraph2.http_client access_token + FbGraph2.http_client(access_token) end def get(params = {}, options = {}) @@ -34,11 +34,11 @@ module FbGraph2 private def build_endpoint(options = {}) - File.join([ - File.join(ROOT_URL, id.to_s), + File.join [ + File.join(FbGraph2.root_url, id.to_s), options[:connection], options[:connection_scope] - ].compact.collect(&:to_s)) + ].compact.collect(&:to_s) end def build_params(params = {}) @@ -48,10 +48,11 @@ module FbGraph2 def handle_response response = yield _response_ = MultiJson.load(response.body).with_indifferent_access - if (200...300).include?(response.status) + case response.status + when 200...300 _response_ else - Exception.handle_structured_response(response.status, _response_, response.headers) + raise response.body end rescue MultiJson::DecodeError raise Exception.new(response.status, "Unparsable Response: #{response.body}") diff --git a/lib/fb_graph2/request_filters/authenticator.rb b/lib/fb_graph2/request_filter/authenticator.rb similarity index 93% rename from lib/fb_graph2/request_filters/authenticator.rb rename to lib/fb_graph2/request_filter/authenticator.rb index efe3b66..ea4e9b5 100644 --- a/lib/fb_graph2/request_filters/authenticator.rb +++ b/lib/fb_graph2/request_filter/authenticator.rb @@ -1,5 +1,5 @@ module FbGraph2 - module RequestFilters + module RequestFilter class Authenticator < Rack::OAuth2::AccessToken::Authenticator def initialize(access_token) _access_token_ = case access_token diff --git a/lib/fb_graph2/request_filters/debugger.rb b/lib/fb_graph2/request_filter/debugger.rb similarity index 94% rename from lib/fb_graph2/request_filters/debugger.rb rename to lib/fb_graph2/request_filter/debugger.rb index 519dd9a..87d0030 100644 --- a/lib/fb_graph2/request_filters/debugger.rb +++ b/lib/fb_graph2/request_filter/debugger.rb @@ -1,5 +1,5 @@ module FbGraph2 - module RequestFilters + module RequestFilter class Debugger def filter_request(request) started = "======= [FbGraph2] API REQUEST STARTED =======" diff --git a/lib/fb_graph2/request_filters.rb b/lib/fb_graph2/request_filters.rb deleted file mode 100644 index 80c1777..0000000 --- a/lib/fb_graph2/request_filters.rb +++ /dev/null @@ -1,3 +0,0 @@ -Dir[File.dirname(__FILE__) + '/request_filters/*.rb'].each do |file| - require file -end \ No newline at end of file diff --git a/spec/fb_graph2/node_spec.rb b/spec/fb_graph2/node_spec.rb index 9c30242..4270a46 100644 --- a/spec/fb_graph2/node_spec.rb +++ b/spec/fb_graph2/node_spec.rb @@ -5,7 +5,7 @@ describe FbGraph2::Node do it 'should support access_token option' do FbGraph2::Node.new( 'matake', :access_token => 'access_token' - ).access_token.should be_a Rack::OAuth2::AccessToken + ).access_token.should == 'access_token' end it 'should store raw attributes' do