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

refactor attribute registration logic

This commit is contained in:
nov 2014-05-05 19:19:45 +09:00
parent 3b3fcc5614
commit 9966baddcb
5 changed files with 33 additions and 18 deletions

View file

@ -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

View file

@ -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

View file

@ -5,7 +5,7 @@ module FbGraph2
def initialize(id, attributes = {})
self.id = id
self.raw_attributes = attributes
assign attributes
end
def authenticate(access_token)

View file

@ -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)

View file

@ -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)