Class: MySpace::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/myspace/object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribs = {}) ⇒ Object

Returns a new instance of Object.



19
20
21
# File 'lib/myspace/object.rb', line 19

def initialize(attribs={})
  attribs.each {|key,val| instance_variable_set("@#{key}",val) }
end

Class Method Details

.build_request(params, optParams = nil, format = "json") ⇒ Object

Builds a REST request based on the supplied params and optParams

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/myspace/object.rb', line 53

def build_request(params,optParams=nil,format="json")
  if params == Array && params.length != @numParams
    raise InvalidRequestParams.new(
            "#{self}.get needs #{@numParams} (#{@pathFormat})")
  end

  raise InvalidRequest.new(
    "try a collection of #{self} objects") if @pathFormat.nil?

  begin 
    path = "/#{API_VERSION}#{@pathFormat % params}.#{format}"
  rescue Exception => e
    raise InvalidRequestParams.new(
      "couldn't build a path from params '#{params.join("'")}' \
       and format string #{@pathFormat}. Exception is: (#{e})")
  end

  if optParams 
    raise InvalidRequestParams.new(
      "optParams should be a Hash.") if !optParams.kind_of?(Hash)

    path += "?"

    optParams.each {|k,v| 
      if @optParams && !@optParams.has_key?(k)
        raise InvalidRequestParams.new("option '#{k}' is not valid.") 
      end
      path += "#{k}=#{v}&"
    }
    path.sub!(/&$/,"")
  end
  path
end

.get(params, optParams = nil) ⇒ Object

Performs a get request to the API servers and returns some MySpace object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/myspace/object.rb', line 94

def get(params,optParams=nil)

  path = self.build_request(params,optParams)
  #puts "#{path} pathFormat #{@pathFormat} childType #{@childType} 
  #childBase #{@childBase} numParams #{@numParams}"

  body = MySpace.connection.get_body(path)

  if @dataType == "Hash"
    self.json_to_obj(body)
  elsif @dataType == "Array"
    body.sub!(/\{"Groups"\:/,'{"groups":') # you did not see this
    self.json_to_obj_collection(body)
  else
    raise InvalidRequestParams.new("#{@dataType} is not a valid data type.")
  end
end

.get_raw(params, optParams = nil, format = "json") ⇒ Object

Performs a request to the API servers and returns the raw JSON/XML



88
89
90
91
# File 'lib/myspace/object.rb', line 88

def get_raw(params,optParams=nil,format="json")
  path = self.build_request(params,optParams,format)
  MySpace.connection.get_body(path)
end

.json_to_obj(body) ⇒ Object

Attempts to convert JSON to MySpace objects



25
26
27
28
29
30
31
32
33
# File 'lib/myspace/object.rb', line 25

def json_to_obj(body)
  begin
    obj = self.new(JSON.parse(body)) 
    obj.user = MySpace::User.new(obj.user) if obj.respond_to?("user")
  rescue Exception => e
    raise InvalidResponse, "could not parse json from #{body}: '#{e}'"
  end
  obj
end

.json_to_obj_collection(body) ⇒ Object

Attempts to convert JSON to a MySpace objects (with child collections)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/myspace/object.rb', line 36

def json_to_obj_collection(body)
  begin
    obj = json_to_obj(body)

    col = obj.instance_variable_get "@#{@childBase}"
    raise InvalidResponse, "No '#{@childBase}' in response" if !col

    col = obj.instance_variable_set "@#{@childBase}", 
            col.collect {|o| eval("#{@childType}.new(o)") }

  rescue Exception => e
    raise InvalidResponse, "could not parse json from #{body}: '#{e}'"
  end
  obj
end