Method: Zuck::FbObject::Write::ClassMethods#create

Defined in:
lib/zuck/fb_object/write.rb

#create(graph, data, parent = nil, path = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zuck/fb_object/write.rb', line 45

def create(graph, data, parent=nil, path=nil)
  raise_if_read_only
  p = path || parent.path

  # We want facebook to return the data of the created object
  data["redownload"] = 1

  # Create
  result = create_connection(graph, p, list_path, data)

  # If the redownload flag was supported, the data is nested by
  # name and id, e.g.
  #
  #     "campaigns" => { "12345" => "data" }
  #
  # Since we only create one at a time, we can just say:
  if d=result['data']
    data = d.values.first.values.first

  # Redownload was not supported, in this case facebook returns
  # just {"id": "12345"}
  elsif result['id']
    data = result

  # Don't know what to do. No id and no data. I need an adult.
  else
    raise "Invalid response received, found neither a data nor id key in #{result}"
  end

  # Return a new instance
  new(graph, data, parent)
end