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

stop retry when 401, and support future v2.x

This commit is contained in:
nov 2014-05-04 01:45:32 +09:00
parent f6aee7496e
commit 0383c1ef66
6 changed files with 31 additions and 21 deletions

View file

@ -2,14 +2,18 @@ require 'active_support/all'
require 'rack/oauth2' require 'rack/oauth2'
module FbGraph2 module FbGraph2
VERSION = File.read(File.join(__dir__, '../VERSION')).delete("\n\r") cattr_accessor :api_version, :gem_version, :logger, :debugging, :_http_config_
ROOT_URL = 'https://graph.facebook.com/v2.0'
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 = Logger.new(STDOUT)
self.logger.progname = 'FbGraph2' self.logger.progname = 'FbGraph2'
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
@ -20,10 +24,13 @@ module FbGraph2
def http_client(access_token = nil) def http_client(access_token = nil)
_http_client_ = HTTPClient.new( _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.delete_if do |filter|
_http_client_.request_filter << RequestFilters::Debugger.new if self.debugging? 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_config_.try(:call, _http_client_)
_http_client_ _http_client_
end end
@ -35,6 +42,11 @@ module FbGraph2
end end
require 'fb_graph2/node' require 'fb_graph2/node'
Dir[File.dirname(__FILE__) + '/fb_graph2/*.rb'].each do |file| [
require file '.',
'request_filter'
].each do |dir|
Dir[File.join(__dir__, 'fb_graph2', dir, '*.rb')].each do |file|
require file
end
end end

View file

@ -22,7 +22,7 @@ module FbGraph2
protected protected
def http_client def http_client
FbGraph2.http_client access_token FbGraph2.http_client(access_token)
end end
def get(params = {}, options = {}) def get(params = {}, options = {})
@ -34,11 +34,11 @@ module FbGraph2
private private
def build_endpoint(options = {}) def build_endpoint(options = {})
File.join([ File.join [
File.join(ROOT_URL, id.to_s), File.join(FbGraph2.root_url, id.to_s),
options[:connection], options[:connection],
options[:connection_scope] options[:connection_scope]
].compact.collect(&:to_s)) ].compact.collect(&:to_s)
end end
def build_params(params = {}) def build_params(params = {})
@ -48,10 +48,11 @@ module FbGraph2
def handle_response def handle_response
response = yield response = yield
_response_ = MultiJson.load(response.body).with_indifferent_access _response_ = MultiJson.load(response.body).with_indifferent_access
if (200...300).include?(response.status) case response.status
when 200...300
_response_ _response_
else else
Exception.handle_structured_response(response.status, _response_, response.headers) raise response.body
end end
rescue MultiJson::DecodeError rescue MultiJson::DecodeError
raise Exception.new(response.status, "Unparsable Response: #{response.body}") raise Exception.new(response.status, "Unparsable Response: #{response.body}")

View file

@ -1,5 +1,5 @@
module FbGraph2 module FbGraph2
module RequestFilters module RequestFilter
class Authenticator < Rack::OAuth2::AccessToken::Authenticator class Authenticator < Rack::OAuth2::AccessToken::Authenticator
def initialize(access_token) def initialize(access_token)
_access_token_ = case access_token _access_token_ = case access_token

View file

@ -1,5 +1,5 @@
module FbGraph2 module FbGraph2
module RequestFilters module RequestFilter
class Debugger class Debugger
def filter_request(request) def filter_request(request)
started = "======= [FbGraph2] API REQUEST STARTED =======" started = "======= [FbGraph2] API REQUEST STARTED ======="

View file

@ -1,3 +0,0 @@
Dir[File.dirname(__FILE__) + '/request_filters/*.rb'].each do |file|
require file
end

View file

@ -5,7 +5,7 @@ describe FbGraph2::Node do
it 'should support access_token option' do it 'should support access_token option' do
FbGraph2::Node.new( FbGraph2::Node.new(
'matake', :access_token => 'access_token' 'matake', :access_token => 'access_token'
).access_token.should be_a Rack::OAuth2::AccessToken ).access_token.should == 'access_token'
end end
it 'should store raw attributes' do it 'should store raw attributes' do