Class: MetaGraph::Node
- Inherits:
-
Object
- Object
- MetaGraph::Node
- Includes:
- GraphAccessor, HashAccessor
- Defined in:
- lib/meta_graph/node.rb
Overview
Node class presents Facebook object which is User, Photo, Album, Comment, and so on.
Instance Method Summary collapse
-
#connection(name) ⇒ Object
Get connection data as an array.
-
#id ⇒ Object
Get ID of this object.
-
#initialize(access_token, resource) ⇒ Node
constructor
Create Node instance from a object that read in Resource class instance.
-
#load_resource ⇒ Object
Load this object’s resource from Facebook Graph API and return it.
-
#load_resource? ⇒ Boolean
Check if this object’s resource is loaded.
-
#method_missing(method, *args, &block) ⇒ Object
You can access object fields as calling method.
-
#read(key) ⇒ Object
Read a field value or connection data.
Methods included from HashAccessor
Methods included from GraphAccessor
Constructor Details
#initialize(access_token, resource) ⇒ Node
Create Node instance from a object that read in Resource class instance. Through this class, you can read every fields in a object by accessing to field name such like obj.id, obj.username.
Arguments
- access_token
-
access token to read a data from Graph API. Please set to oauth’s access_token.
- resource
-
Set Resource class instance or hash value. If Resource class instance is set in this argument, fields and connections in this instance are constructed from resource. If hash value is set, then it is set to fields variable in this instance.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/meta_graph/node.rb', line 25 def initialize(access_token, resource) @access_token = access_token if resource.is_a?(Resource) @resource = resource @fields = resource.fields @connections = resource.connections else @fields = resource @connections = {} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
You can access object fields as calling method. When this object has id, name, link, etc, you can access these fields like object.id, object.name, object.link.
In addition, if the object has connections, you can access connections as well, then GraphCollection instance is returned.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/meta_graph/node.rb', line 104 def method_missing(method, *args, &block) data = read(method) return data if data if !load_resource.nil? data = read(method) return data if data end super(method, *args, &block) end |
Instance Method Details
#connection(name) ⇒ Object
Get connection data as an array. Return nil if this ojbect doesn’t have connection specified in name argument.
Argument
- name
-
connection name.
52 53 54 55 56 57 |
# File 'lib/meta_graph/node.rb', line 52 def connection(name) if @connections.key?(name) res = Resource.new(@access_token, @connections[name]) return Collection.new(@access_token, res.data) end end |
#id ⇒ Object
Get ID of this object. Return nil if this object has no id.
41 42 43 |
# File 'lib/meta_graph/node.rb', line 41 def id return @fields[:id] if @fields.key?(:id) end |
#load_resource ⇒ Object
Load this object’s resource from Facebook Graph API and return it. If resource already has loaded or there is no object id, this method do nothing.
71 72 73 74 75 76 77 78 79 |
# File 'lib/meta_graph/node.rb', line 71 def load_resource if id && !load_resource? @resource = Resource.new(@access_token, id) @fields = @resource.fields @connections = @resource.connections end return @resource end |
#load_resource? ⇒ Boolean
Check if this object’s resource is loaded.
62 63 64 |
# File 'lib/meta_graph/node.rb', line 62 def load_resource? !@resource.nil? end |
#read(key) ⇒ Object
Read a field value or connection data. Return nil if you set not exist key name to key argument.
Argument
- key
-
field name or connection name you want to read
88 89 90 91 92 93 94 |
# File 'lib/meta_graph/node.rb', line 88 def read(key) con = connection(key) return con if con value = read_hash(key) return read_graph(@access_token, value) if value end |