From 9966baddcbca6487ddf5607726581f972605943e Mon Sep 17 00:00:00 2001 From: nov Date: Mon, 5 May 2014 19:19:45 +0900 Subject: [PATCH] refactor attribute registration logic --- lib/fb_graph2/attribute_assigner.rb | 20 +++++++++++++++++++- lib/fb_graph2/edge/friends.rb | 4 +--- lib/fb_graph2/node.rb | 2 +- lib/fb_graph2/page.rb | 10 +++++----- lib/fb_graph2/user.rb | 15 +++++++-------- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/fb_graph2/attribute_assigner.rb b/lib/fb_graph2/attribute_assigner.rb index a2ddc46..904cbf3 100644 --- a/lib/fb_graph2/attribute_assigner.rb +++ b/lib/fb_graph2/attribute_assigner.rb @@ -3,10 +3,13 @@ module FbGraph2 extend ActiveSupport::Concern included do + extend ClassMethods + attr_accessor :raw_attributes cattr_accessor :registered_attributes end def assign(attributes) + self.raw_attributes = attributes self.class.registered_attributes.each do |type, keys| keys.each do |key| raw = attributes[key] @@ -18,13 +21,28 @@ module FbGraph2 Date.parse raw when :time Time.parse raw + when :page + Page.new raw[:id], raw + when :pages + raw.each do |_raw_| + Page.new _raw_[:id], _raw_ + end + when :user + User.new raw[:id], raw when :custom - # TODO: + # NOTE: handle custom attributes in each class end self.send :"#{key}=", attributes[key] end end end end + + module ClassMethods + def register_attributes(attributes) + self.registered_attributes = attributes + send :attr_accessor, *attributes.values.flatten + end + end end end \ No newline at end of file diff --git a/lib/fb_graph2/edge/friends.rb b/lib/fb_graph2/edge/friends.rb index fd83355..6fcedaa 100644 --- a/lib/fb_graph2/edge/friends.rb +++ b/lib/fb_graph2/edge/friends.rb @@ -4,9 +4,7 @@ module FbGraph2 def friends(params = {}) users = self.edge :friends, params users.collect do |user| - User.new(user[:id], user).authenticate( - params[:access_token] || self.access_token - ) + User.new(user[:id], user).authenticate self.access_token end end end diff --git a/lib/fb_graph2/node.rb b/lib/fb_graph2/node.rb index 5a650ec..ba17483 100644 --- a/lib/fb_graph2/node.rb +++ b/lib/fb_graph2/node.rb @@ -5,7 +5,7 @@ module FbGraph2 def initialize(id, attributes = {}) self.id = id - self.raw_attributes = attributes + assign attributes end def authenticate(access_token) diff --git a/lib/fb_graph2/page.rb b/lib/fb_graph2/page.rb index 6c76a7b..55b85f0 100644 --- a/lib/fb_graph2/page.rb +++ b/lib/fb_graph2/page.rb @@ -1,6 +1,6 @@ module FbGraph2 class Page < Node - self.registered_attributes = { + register_attributes( raw: [ :about, :attire, :band_members, :booking_agent, :can_post, :category, :checkins, :company_overview, :current_location, :description, :directed_by, :founded, :general_info, :general_manager, :hometown, @@ -8,16 +8,16 @@ module FbGraph2 :products, :talking_about_count, :username, :website, :were_here_count ], date: [:birthday], + page: [:best_page], custom: [ - :best_page, :category_list, :cover, :context, :hours, :location, :parking, :price_range, :restaurant_services, + :category_list, :cover, :context, :hours, :location, :parking, :price_range, :restaurant_services, :restaurant_specialties ] - } - attr_accessor *registered_attributes.values.flatten + ) def initialize(id, attributes = {}) super - assign attributes + # TODO: handle custom attributes. end def self.me(access_token) diff --git a/lib/fb_graph2/user.rb b/lib/fb_graph2/user.rb index 462272c..73f9508 100644 --- a/lib/fb_graph2/user.rb +++ b/lib/fb_graph2/user.rb @@ -2,7 +2,7 @@ module FbGraph2 class User < Node include Edge::Friends - self.registered_attributes = { + register_attributes( raw: [ :about, :bio, :email, :first_name, :gender, :installed, :is_verified, :link, :locale, :middle_name, :name, :name_format, :political, :quotes, :relationship_status, :religion, @@ -10,16 +10,15 @@ module FbGraph2 ], time: [:updated_time], # NOTE: undocumented attribute date: [:birthday], - custom: [ - :age_range, :context, :cover, :currency, :education, :favorite_athletes, :favorite_teams, - :hometown, :inspirational_people, :languages, :location, :significant_other, :work - ] - } - attr_accessor *registered_attributes.values.flatten + page: [:hometown, :location], + pages: [:favorite_athletes, :favorite_teams, :inspirational_people, :languages], + user: [:significant_other], + custom: [:age_range, :context, :cover, :currency, :education, :work] + ) def initialize(id, attributes = {}) super - assign attributes + # TODO: handle custom attributes. end def self.me(access_token)