mirror of
https://github.com/nov/fb_graph2
synced 2023-03-27 23:22:15 -04:00
refactored attribute assigner
This commit is contained in:
parent
50b5f82b09
commit
1f40116d88
11 changed files with 41 additions and 65 deletions
|
@ -1,5 +1,6 @@
|
|||
require 'active_support'
|
||||
require 'active_support/core_ext'
|
||||
require 'active_support/concern'
|
||||
require 'rack/oauth2'
|
||||
|
||||
module FbGraph2
|
||||
|
@ -10,9 +11,12 @@ module FbGraph2
|
|||
self.gem_version = File.read(File.join(__dir__, '../VERSION')).strip
|
||||
self.logger = Logger.new(STDOUT)
|
||||
self.logger.progname = 'FbGraph2'
|
||||
self.object_classes = Array.new
|
||||
|
||||
class << self
|
||||
def object_classes
|
||||
FbGraph2::Node.descendants
|
||||
end
|
||||
|
||||
def debugging?
|
||||
!!self.debugging
|
||||
end
|
||||
|
|
|
@ -8,26 +8,28 @@ module FbGraph2
|
|||
end
|
||||
|
||||
module ClassMethods
|
||||
def register_attributes(attributes)
|
||||
@registered_attributes ||= {}
|
||||
attributes.each do |type, keys|
|
||||
@registered_attributes[type] ||= []
|
||||
@registered_attributes[type] += keys
|
||||
end
|
||||
send :attr_accessor, *attributes.values.flatten
|
||||
end
|
||||
|
||||
def registered_attributes
|
||||
@registered_attributes
|
||||
end
|
||||
attr_reader :registered_attributes
|
||||
|
||||
def inherited(child)
|
||||
super
|
||||
child.register_attributes registered_attributes
|
||||
end
|
||||
|
||||
def registered_attributes
|
||||
@registered_attributes ||= {}
|
||||
end
|
||||
|
||||
def register_attributes(attributes)
|
||||
attributes.each do |type, keys|
|
||||
registered_attributes[type] ||= []
|
||||
registered_attributes[type] += keys
|
||||
end
|
||||
send :attr_accessor, *attributes.values.flatten
|
||||
end
|
||||
end
|
||||
|
||||
def assign(attributes)
|
||||
self.raw_attributes = attributes
|
||||
Array(self.class.registered_attributes).each do |type, keys|
|
||||
keys.each do |key|
|
||||
if attributes.include? key
|
||||
|
@ -91,9 +93,11 @@ module FbGraph2
|
|||
Collection.new(raw).collect! do |_raw_|
|
||||
Struct::Tag.new _raw_
|
||||
end
|
||||
else
|
||||
when :custom
|
||||
# NOTE: handle these attributes in each class
|
||||
next
|
||||
else
|
||||
raise "unknown attribute type #{type}"
|
||||
end
|
||||
self.send :"#{key}=", value
|
||||
end
|
||||
|
|
|
@ -1,17 +1,12 @@
|
|||
module FbGraph2
|
||||
class Node
|
||||
attr_accessor :id, :access_token, :raw_attributes
|
||||
include AttributeAssigner
|
||||
attr_accessor :id, :access_token
|
||||
alias_method :identifier, :id
|
||||
|
||||
def self.inherited(klass)
|
||||
klass.send :include, AttributeAssigner
|
||||
FbGraph2.object_classes << klass
|
||||
end
|
||||
|
||||
def initialize(id, attributes = {})
|
||||
self.id = id
|
||||
self.raw_attributes = attributes
|
||||
assign attributes if respond_to? :assign
|
||||
assign attributes
|
||||
authenticate attributes[:access_token] if attributes.include? :access_token
|
||||
end
|
||||
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
module FbGraph2
|
||||
class Struct
|
||||
def self.inherited(klass)
|
||||
klass.send :include, AttributeAssigner
|
||||
end
|
||||
include AttributeAssigner
|
||||
|
||||
def initialize(attributes = {})
|
||||
assign attributes if respond_to? :assign
|
||||
assign attributes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ module FbGraph2
|
|||
class Rating < Struct
|
||||
register_attributes(
|
||||
raw: [:has_rating, :has_review, :rating, :review_text],
|
||||
datetime: [:created_time],
|
||||
time: [:created_time],
|
||||
user: [:reviewer],
|
||||
custom: [:open_graph_story]
|
||||
)
|
||||
|
|
|
@ -2,21 +2,19 @@ require 'spec_helper'
|
|||
|
||||
describe FbGraph2::App do
|
||||
describe '.app' do
|
||||
let(:klass) { FbGraph2::App }
|
||||
|
||||
it 'should not call API' do
|
||||
expect do
|
||||
app = klass.app 'token'
|
||||
app.should be_instance_of klass
|
||||
app = described_class.app 'token'
|
||||
app.should be_instance_of described_class
|
||||
end.not_to request_to 'app'
|
||||
end
|
||||
|
||||
context 'when fetched' do
|
||||
it 'should call API' do
|
||||
app = mock_graph :get, 'app', 'app/app' do
|
||||
klass.app('token').fetch
|
||||
described_class.app('token').fetch
|
||||
end
|
||||
app.should be_instance_of klass
|
||||
app.should be_instance_of described_class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe FbGraph2::Node do
|
||||
let(:klass) { FbGraph2::Node }
|
||||
let(:instance) { klass.new 'identifier' }
|
||||
let(:instance) { described_class.new 'identifier' }
|
||||
|
||||
describe 'API Versioning' do
|
||||
before do
|
||||
|
@ -56,15 +55,8 @@ describe FbGraph2::Node do
|
|||
end
|
||||
end
|
||||
|
||||
context 'class' do
|
||||
subject { klass }
|
||||
it { should_not respond_to :register_attributes }
|
||||
it { should_not respond_to :registered_attributes }
|
||||
end
|
||||
|
||||
context 'instance' do
|
||||
subject { instance }
|
||||
it { should_not respond_to :assign }
|
||||
|
||||
describe '#initialize' do
|
||||
its(:id) { should == 'identifier' }
|
||||
|
|
|
@ -6,17 +6,7 @@ module FbGraph2
|
|||
end
|
||||
|
||||
describe FbGraph2::NodeSubClass do
|
||||
let(:klass) { FbGraph2::NodeSubClass }
|
||||
let(:instance) { klass.new 'identifier' }
|
||||
|
||||
context 'class' do
|
||||
subject { klass }
|
||||
it { should respond_to :register_attributes }
|
||||
it { should respond_to :registered_attributes }
|
||||
end
|
||||
|
||||
context 'instance' do
|
||||
subject { instance }
|
||||
it { should respond_to :assign }
|
||||
it 'should be included in FbGraph2.object_classes' do
|
||||
FbGraph2.object_classes.should include described_class
|
||||
end
|
||||
end
|
|
@ -1,11 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe FbGraph2::RequestFilter::Authenticator do
|
||||
let(:klass) { FbGraph2::RequestFilter::Authenticator }
|
||||
|
||||
let(:endpoint) { 'https://graph.facebook.com/matake' }
|
||||
let(:request) { HTTP::Message.new_request(:get, URI.parse(endpoint)) }
|
||||
let(:request_filter) { klass.new token }
|
||||
let(:request_filter) { described_class.new token }
|
||||
|
||||
context 'when String given' do
|
||||
let(:token) { 'token' }
|
||||
|
|
|
@ -2,27 +2,25 @@ require 'spec_helper'
|
|||
|
||||
describe FbGraph2::User do
|
||||
describe '.me' do
|
||||
let(:klass) { FbGraph2::User }
|
||||
|
||||
it 'should not call API' do
|
||||
expect do
|
||||
me = klass.me 'token'
|
||||
me.should be_instance_of klass
|
||||
me = described_class.me 'token'
|
||||
me.should be_instance_of described_class
|
||||
end.not_to request_to 'me'
|
||||
end
|
||||
|
||||
context 'when fetched' do
|
||||
it 'should call API' do
|
||||
me = mock_graph :get, 'me', 'user/me' do
|
||||
klass.me('token').fetch
|
||||
described_class.me('token').fetch
|
||||
end
|
||||
me.should be_instance_of klass
|
||||
me.should be_instance_of described_class
|
||||
end
|
||||
|
||||
context 'when ext attrs included' do
|
||||
it 'should success to parse' do
|
||||
me = mock_graph :get, 'me', 'user/me_with_ext_attrs' do
|
||||
klass.me('token').fetch
|
||||
described_class.me('token').fetch
|
||||
end
|
||||
[
|
||||
:age_range, :context, :currency, :devices
|
||||
|
|
|
@ -8,7 +8,6 @@ describe FbGraph2 do
|
|||
its(:logger) { should be_a Logger }
|
||||
its(:api_version) { should == 'v2.3' }
|
||||
its(:root_url) { should == 'https://graph.facebook.com' }
|
||||
its(:object_classes) { should contain_exactly *FbGraph2::Node.subclasses + [FbGraph2::Place] }
|
||||
it { should_not be_debugging }
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue