mirror of
https://github.com/nov/fb_graph2
synced 2023-03-27 23:22:15 -04:00
first edge, and registered_attribute handler module
This commit is contained in:
parent
e8d0d50de2
commit
3b3fcc5614
8 changed files with 148 additions and 30 deletions
|
@ -41,7 +41,10 @@ module FbGraph2
|
|||
end
|
||||
end
|
||||
|
||||
require 'fb_graph2/attribute_assigner'
|
||||
require 'fb_graph2/node'
|
||||
require 'fb_graph2/collection'
|
||||
require 'fb_graph2/edge'
|
||||
[
|
||||
'.',
|
||||
'request_filter'
|
||||
|
|
30
lib/fb_graph2/attribute_assigner.rb
Normal file
30
lib/fb_graph2/attribute_assigner.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module FbGraph2
|
||||
module AttributeAssigner
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
cattr_accessor :registered_attributes
|
||||
end
|
||||
|
||||
def assign(attributes)
|
||||
self.class.registered_attributes.each do |type, keys|
|
||||
keys.each do |key|
|
||||
raw = attributes[key]
|
||||
if raw.present?
|
||||
value = case type
|
||||
when :raw
|
||||
raw
|
||||
when :date
|
||||
Date.parse raw
|
||||
when :time
|
||||
Time.parse raw
|
||||
when :custom
|
||||
# TODO:
|
||||
end
|
||||
self.send :"#{key}=", attributes[key]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
20
lib/fb_graph2/collection.rb
Normal file
20
lib/fb_graph2/collection.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module FbGraph2
|
||||
class Collection < Array
|
||||
attr_reader :previous, :next, :total_count, :unread_count, :updated_time, :cursors
|
||||
|
||||
def initialize(collection = nil)
|
||||
collection = case collection
|
||||
when Hash
|
||||
collection
|
||||
when Array
|
||||
{
|
||||
data: collection,
|
||||
count: collection.size
|
||||
}
|
||||
else
|
||||
raise ArgumentError.new("Invalid collection")
|
||||
end
|
||||
replace Array(collection[:data])
|
||||
end
|
||||
end
|
||||
end
|
18
lib/fb_graph2/edge.rb
Normal file
18
lib/fb_graph2/edge.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
module FbGraph2
|
||||
class Edge < Collection
|
||||
attr_accessor :owner, :edge, :params, :options, :collection
|
||||
|
||||
def initialize(owner, edge, params = {}, options = {})
|
||||
self.owner = owner
|
||||
self.edge = edge
|
||||
self.params = params
|
||||
self.options = options
|
||||
self.collection = options.delete(:collection) || Collection.new
|
||||
replace collection
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Dir[File.join(__dir__, 'edge/*.rb')].each do |file|
|
||||
require file
|
||||
end
|
14
lib/fb_graph2/edge/friends.rb
Normal file
14
lib/fb_graph2/edge/friends.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
module FbGraph2
|
||||
class Edge
|
||||
module Friends
|
||||
def friends(params = {})
|
||||
users = self.edge :friends, params
|
||||
users.collect do |user|
|
||||
User.new(user[:id], user).authenticate(
|
||||
params[:access_token] || self.access_token
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,22 +1,36 @@
|
|||
module FbGraph2
|
||||
class Node
|
||||
include AttributeAssigner
|
||||
attr_accessor :id, :access_token, :raw_attributes
|
||||
|
||||
def initialize(id, attributes = {})
|
||||
self.id = id
|
||||
self.access_token = attributes[:access_token]
|
||||
self.raw_attributes = attributes
|
||||
end
|
||||
|
||||
def fetch(params = {}, options = {})
|
||||
self.access_token ||= options[:access_token]
|
||||
_fetched_ = get params, options
|
||||
_fetched_[:access_token] ||= self.access_token
|
||||
self.class.new(_fetched_[:id], _fetched_)
|
||||
def authenticate(access_token)
|
||||
self.access_token = access_token
|
||||
self
|
||||
end
|
||||
|
||||
def self.fetch(identifier, options = {})
|
||||
new(identifier).fetch(options)
|
||||
def fetch(params = {})
|
||||
attributes = get params
|
||||
self.class.new(attributes[:id], attributes).authenticate access_token
|
||||
end
|
||||
|
||||
def self.fetch(identifier, params = {})
|
||||
new(identifier).fetch params
|
||||
end
|
||||
|
||||
def edge(edge, params = {}, options = {})
|
||||
Edge.new(
|
||||
self,
|
||||
edge,
|
||||
params,
|
||||
options.merge(
|
||||
collection: edge_for(edge, params, options)
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
protected
|
||||
|
@ -33,11 +47,16 @@ module FbGraph2
|
|||
|
||||
private
|
||||
|
||||
def edge_for(edge, params = {}, options = {})
|
||||
collection = get params, options.merge(:edge => edge)
|
||||
Collection.new collection
|
||||
end
|
||||
|
||||
def build_endpoint(options = {})
|
||||
File.join [
|
||||
File.join(FbGraph2.root_url, id.to_s),
|
||||
options[:connection],
|
||||
options[:connection_scope]
|
||||
options[:edge],
|
||||
options[:edge_scope]
|
||||
].compact.collect(&:to_s)
|
||||
end
|
||||
|
||||
|
|
27
lib/fb_graph2/page.rb
Normal file
27
lib/fb_graph2/page.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
module FbGraph2
|
||||
class Page < Node
|
||||
self.registered_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,
|
||||
:is_permanently_closed, :is_published, :is_unclaimed, :likes, :link, :mission, :name, :phone, :press_contact,
|
||||
:products, :talking_about_count, :username, :website, :were_here_count
|
||||
],
|
||||
date: [:birthday],
|
||||
custom: [
|
||||
:best_page, :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
|
||||
end
|
||||
|
||||
def self.me(access_token)
|
||||
new(:me).authenticate access_token
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,8 @@
|
|||
module FbGraph2
|
||||
class User < Node
|
||||
@@attributes = {
|
||||
include Edge::Friends
|
||||
|
||||
self.registered_attributes = {
|
||||
raw: [
|
||||
:about, :bio, :email, :first_name, :gender, :installed, :is_verified, :link, :locale,
|
||||
:middle_name, :name, :name_format, :political, :quotes, :relationship_status, :religion,
|
||||
|
@ -13,30 +15,15 @@ module FbGraph2
|
|||
:hometown, :inspirational_people, :languages, :location, :significant_other, :work
|
||||
]
|
||||
}
|
||||
attr_accessor *@@attributes.values.flatten
|
||||
attr_accessor *registered_attributes.values.flatten
|
||||
|
||||
def initialize(id, attributes = {})
|
||||
super
|
||||
@@attributes.each do |type, keys|
|
||||
keys.each do |key|
|
||||
raw = attributes[key]
|
||||
if raw.present?
|
||||
value = case type
|
||||
when :raw
|
||||
raw
|
||||
when :date
|
||||
Date.parse raw
|
||||
when :time
|
||||
Time.parse raw
|
||||
end
|
||||
self.send :"#{key}=", attributes[key]
|
||||
end
|
||||
end
|
||||
end
|
||||
assign attributes
|
||||
end
|
||||
|
||||
def self.me(access_token)
|
||||
new(:me, access_token: access_token)
|
||||
new(:me).authenticate access_token
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue