Class: OpenSocial::Person

Inherits:
Base
  • Object
show all
Defined in:
lib/opensocial/person.rb

Overview

Acts as a wrapper for an OpenSocial person.

The Person class takes input JSON as an initialization parameter, and iterates through each of the key/value pairs of that JSON. For each key that is found, an attr_accessor is constructed, allowing direct access to the value. Each value is stored in the attr_accessor, either as a String, Fixnum, Hash, or Array.

Instance Method Summary collapse

Methods inherited from Base

#add_attr

Constructor Details

#initialize(json) ⇒ Person

Initializes the Person based on the provided json fragment. If no JSON is provided, an empty object (with no attributes) is created.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/opensocial/person.rb', line 38

def initialize(json)
  if json
    json.each do |key, value|
      proper_key = key.underscore
      begin
        self.send("#{proper_key}=", value)
      rescue NoMethodError
        add_attr(proper_key)
        self.send("#{proper_key}=", value)
      end
    end
  end
end

Instance Method Details

#long_nameObject

Returns the complete name of the Person, to the best ability, given available fields.



54
55
56
57
58
59
60
61
62
# File 'lib/opensocial/person.rb', line 54

def long_name
  if @name && @name["givenName"] && @name["familyName"]
    return @name["givenName"] + " " + @name["familyName"]
  elsif @nickname
    return @nickname
  else
    return ""
  end
end

#short_nameObject

Returns the first name or nickname of the Person, to the best ability, given available fields.



66
67
68
69
70
71
72
73
74
# File 'lib/opensocial/person.rb', line 66

def short_name
  if @name && @name["givenName"]
    return @name["givenName"]
  elsif @nickname
    return @nickname
  else
    return ""
  end
end