From 3b3fcc5614e2c7589b1a45bc763f8a42ad083006 Mon Sep 17 00:00:00 2001 From: nov Date: Mon, 5 May 2014 18:59:05 +0900 Subject: [PATCH] first edge, and registered_attribute handler module --- lib/fb_graph2.rb | 3 +++ lib/fb_graph2/attribute_assigner.rb | 30 ++++++++++++++++++++++ lib/fb_graph2/collection.rb | 20 +++++++++++++++ lib/fb_graph2/edge.rb | 18 +++++++++++++ lib/fb_graph2/edge/friends.rb | 14 +++++++++++ lib/fb_graph2/node.rb | 39 +++++++++++++++++++++-------- lib/fb_graph2/page.rb | 27 ++++++++++++++++++++ lib/fb_graph2/user.rb | 27 ++++++-------------- 8 files changed, 148 insertions(+), 30 deletions(-) create mode 100644 lib/fb_graph2/attribute_assigner.rb create mode 100644 lib/fb_graph2/collection.rb create mode 100644 lib/fb_graph2/edge.rb create mode 100644 lib/fb_graph2/edge/friends.rb create mode 100644 lib/fb_graph2/page.rb diff --git a/lib/fb_graph2.rb b/lib/fb_graph2.rb index 04fa07f..21fad8d 100644 --- a/lib/fb_graph2.rb +++ b/lib/fb_graph2.rb @@ -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' diff --git a/lib/fb_graph2/attribute_assigner.rb b/lib/fb_graph2/attribute_assigner.rb new file mode 100644 index 0000000..a2ddc46 --- /dev/null +++ b/lib/fb_graph2/attribute_assigner.rb @@ -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 \ No newline at end of file diff --git a/lib/fb_graph2/collection.rb b/lib/fb_graph2/collection.rb new file mode 100644 index 0000000..14f7cd8 --- /dev/null +++ b/lib/fb_graph2/collection.rb @@ -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 diff --git a/lib/fb_graph2/edge.rb b/lib/fb_graph2/edge.rb new file mode 100644 index 0000000..1babafb --- /dev/null +++ b/lib/fb_graph2/edge.rb @@ -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 \ No newline at end of file diff --git a/lib/fb_graph2/edge/friends.rb b/lib/fb_graph2/edge/friends.rb new file mode 100644 index 0000000..fd83355 --- /dev/null +++ b/lib/fb_graph2/edge/friends.rb @@ -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 \ No newline at end of file diff --git a/lib/fb_graph2/node.rb b/lib/fb_graph2/node.rb index 3e70c07..5a650ec 100644 --- a/lib/fb_graph2/node.rb +++ b/lib/fb_graph2/node.rb @@ -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 diff --git a/lib/fb_graph2/page.rb b/lib/fb_graph2/page.rb new file mode 100644 index 0000000..6c76a7b --- /dev/null +++ b/lib/fb_graph2/page.rb @@ -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 \ No newline at end of file diff --git a/lib/fb_graph2/user.rb b/lib/fb_graph2/user.rb index 1ab5186..462272c 100644 --- a/lib/fb_graph2/user.rb +++ b/lib/fb_graph2/user.rb @@ -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 \ No newline at end of file