Class: Viadeo::GraphObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/viadeo/graph_object.rb

Direct Known Subclasses

GraphConnection, Newsfeed, Recommend, ReferenceData, Status

Constant Summary collapse

@@initial_attributes =
Set.new([:id, :access_token])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, options = {}) ⇒ GraphObject

Returns a new instance of GraphObject.



8
9
10
11
12
13
14
15
# File 'lib/viadeo/graph_object.rb', line 8

def initialize(access_token, options={})
  raise Viadeo::MissingAccessTokenError.new("access_token parameter is mandatory") if (access_token.nil? ||  access_token.empty?)
  @access_token = access_token
  @id = options[:id] if options[:id]
  @connection = options[:connection] if options[:connection]
  @values = {}
  @updated_attributes = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/viadeo/graph_object.rb', line 113

def method_missing(name, *args)
  if name.to_s.end_with?('=')
    attr = name.to_s[0...-1].to_sym
    @values[attr] = args[0]
    @updated_attributes.add(attr)
    add_accessors([attr])
    return
  else
    return @values[name] if @values.has_key?(name)
  end
  super
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/viadeo/graph_object.rb', line 5

def access_token
  @access_token
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/viadeo/graph_object.rb', line 5

def id
  @id
end

Class Method Details

.new_from(values, access_token = nil) ⇒ Object



17
18
19
20
# File 'lib/viadeo/graph_object.rb', line 17

def self.new_from(values, access_token=nil)
  obj = self.new(access_token, values)
  obj.refresh_from(values, access_token)
end

Instance Method Details

#[](k) ⇒ Object



55
56
57
58
# File 'lib/viadeo/graph_object.rb', line 55

def [](k)
  k = k.to_sym if k.kind_of?(String)
  @values[k]
end

#[]=(k, v) ⇒ Object



60
61
62
# File 'lib/viadeo/graph_object.rb', line 60

def []=(k, v)
  send(:"#{k}=", v)
end

#data_to_graph_objectObject



84
85
86
# File 'lib/viadeo/graph_object.rb', line 84

def data_to_graph_object
  Util.convert_to_graph_object(@values[:data], access_token)
end

#each(&blk) ⇒ Object



80
81
82
# File 'lib/viadeo/graph_object.rb', line 80

def each(&blk)
  @values.each(&blk)
end

#inspectObject



40
41
42
43
# File 'lib/viadeo/graph_object.rb', line 40

def inspect()
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + MultiJson.dump(@values, :pretty => true)
end

#keysObject



64
65
66
# File 'lib/viadeo/graph_object.rb', line 64

def keys
  @values.keys
end

#refresh_from(values, access_token, partial = false) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/viadeo/graph_object.rb', line 45

def refresh_from(values, access_token, partial=false)
  @access_token = access_token
  add_accessors(values.keys)
  values.each do |k, v|
    @values[k] = v
    @updated_attributes.delete(k)
  end
  self
end

#resetObject



26
27
28
29
# File 'lib/viadeo/graph_object.rb', line 26

def reset
  @values = {}
  @updated_attributes = Set.new
end

#to_hashObject



76
77
78
# File 'lib/viadeo/graph_object.rb', line 76

def to_hash
  @values
end

#to_json(*a) ⇒ Object



72
73
74
# File 'lib/viadeo/graph_object.rb', line 72

def to_json(*a)
  MultiJson.dump(@values)
end

#to_s(*args) ⇒ Object



36
37
38
# File 'lib/viadeo/graph_object.rb', line 36

def to_s(*args)
  MultiJson.dump(@values, :pretty => true)
end

#updated?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/viadeo/graph_object.rb', line 31

def updated?(name=nil)
  return !@updated_attributes.empty? unless name
  return @updated_attributes.include?(name)
end

#urlObject



22
23
24
# File 'lib/viadeo/graph_object.rb', line 22

def url
  self.class.name.downcase.split('::')[-1] 
end

#valuesObject



68
69
70
# File 'lib/viadeo/graph_object.rb', line 68

def values
  @values.values
end