Module: Koala::Facebook::GraphAPIMethods

Included in:
GraphAPI, GraphAndRestAPI
Defined in:
lib/koala/graph_api.rb

Instance Method Summary collapse

Instance Method Details

#delete_connections(id, connection_name, args = {}, options = {}) ⇒ Object

Raises:



84
85
86
87
88
# File 'lib/koala/graph_api.rb', line 84

def delete_connections(id, connection_name, args = {}, options = {})
  # Deletes a given connection
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Delete requires an access token"}) unless @access_token
  graph_call("#{id}/#{connection_name}", args, "delete", options)
end

#delete_like(object_id, options = {}) ⇒ Object

Raises:



165
166
167
168
169
# File 'lib/koala/graph_api.rb', line 165

def delete_like(object_id, options = {})
  # Unlikes a given object for the logged-in user
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Unliking requires an access token"}) unless @access_token
  graph_call("#{object_id}/likes", {}, "delete", options)
end

#delete_object(id, options = {}) ⇒ Object

Raises:



64
65
66
67
68
# File 'lib/koala/graph_api.rb', line 64

def delete_object(id, options = {})
  # Deletes the object with the given ID from the graph.
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Delete requires an access token"}) unless @access_token
  graph_call(id, {}, "delete", options)
end

#get_connections(id, connection_name, args = {}, options = {}) ⇒ Object

Connections



72
73
74
75
76
# File 'lib/koala/graph_api.rb', line 72

def get_connections(id, connection_name, args = {}, options = {})
  # Fetchs the connections for given object.
  result = graph_call("#{id}/#{connection_name}", args, "get", options)
  result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
end

#get_object(id, args = {}, options = {}) ⇒ Object

Objects



34
35
36
37
# File 'lib/koala/graph_api.rb', line 34

def get_object(id, args = {}, options = {})
  # Fetchs the given object from the graph.
  graph_call(id, args, "get", options)
end

#get_objects(ids, args = {}, options = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/koala/graph_api.rb', line 39

def get_objects(ids, args = {}, options = {})
  # Fetchs all of the given object from the graph.
  # We return a map from ID to object. If any of the IDs are invalid,
  # we raise an exception.
  graph_call("", args.merge("ids" => ids.join(",")), "get", options)
end

#get_page(params) ⇒ Object

GraphCollection support



196
197
198
199
200
201
# File 'lib/koala/graph_api.rb', line 196

def get_page(params)
  # Pages through a set of results stored in a GraphCollection
  # Used for connections and search results
  result = graph_call(*params)
  result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
end

#get_picture(object, args = {}, options = {}) ⇒ Object

Pictures to delete pictures, use delete_object(photo_id) note: you’ll need the user_photos permission to actually access photos after uploading them



94
95
96
97
98
# File 'lib/koala/graph_api.rb', line 94

def get_picture(object, args = {}, options = {})
  # Gets a picture object, returning the URL (which Facebook sends as a header)
  result = graph_call("#{object}/picture", args, "get", options.merge(:http_component => :headers))
  result["Location"]
end

#graph_call(*args) ⇒ Object

API access



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/koala/graph_api.rb', line 181

def graph_call(*args)
  # Direct access to the Facebook API
  # see any of the above methods for example invocations
  response = api(*args) do |response|
    # check for Graph API-specific errors
    if response.is_a?(Hash) && error_details = response["error"]
      raise APIError.new(error_details)
    end
  end

  response
end

#put_comment(object_id, message, options = {}) ⇒ Object

Comments to delete comments, use delete_object(comment_id) to get comments, use get_connections(object, “likes”)



152
153
154
155
# File 'lib/koala/graph_api.rb', line 152

def put_comment(object_id, message, options = {})
  # Writes the given comment on the given post.
  self.put_object(object_id, "comments", {:message => message}, options)
end

#put_connections(id, connection_name, args = {}, options = {}) ⇒ Object

Raises:



78
79
80
81
82
# File 'lib/koala/graph_api.rb', line 78

def put_connections(id, connection_name, args = {}, options = {})
  # Posts a certain connection
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Write operations require an access token"}) unless @access_token
  graph_call("#{id}/#{connection_name}", args, "post", options)
end

#put_like(object_id, options = {}) ⇒ Object

Likes to get likes, use get_connections(user, “likes”)



160
161
162
163
# File 'lib/koala/graph_api.rb', line 160

def put_like(object_id, options = {})
  # Likes the given post.
  self.put_object(object_id, "likes", {}, options)
end

#put_object(parent_object, connection_name, args = {}, options = {}) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/koala/graph_api.rb', line 46

def put_object(parent_object, connection_name, args = {}, options = {})
  # Writes the given object to the graph, connected to the given parent.
  # See http://developers.facebook.com/docs/api#publishing for all of
  # the supported writeable objects.
  # 
  # For example,
  #     graph.put_object("me", "feed", :message => "Hello, world")
  # writes "Hello, world" to the active user's wall.
  #
  # Most write operations require extended permissions. For example,
  # publishing wall posts requires the "publish_stream" permission. See
  # http://developers.facebook.com/docs/authentication/ for details about
  # extended permissions.
    
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Write operations require an access token"}) unless @access_token
  graph_call("#{parent_object}/#{connection_name}", args, "post", options)
end

#put_picture(*picture_args) ⇒ Object

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/koala/graph_api.rb', line 100

def put_picture(*picture_args)
  # Can be called in multiple ways:
  #
  #   put_picture(file, [content_type], ...)
  #   put_picture(path_to_file, [content_type], ...)
  #
  # You can pass in uploaded files directly from Rails or Sinatra.
  # (See lib/koala/uploadable_io.rb for supported frameworks)
  #
  # Optional parameters can be added to the end of the argument list:
  # - args:       a hash of request parameters (default: {})
  # - target_id:  ID of the target where to post the picture (default: "me")
  # - options:    a hash of http options passed to the HTTPService module
  # 
  #   put_picture(file, content_type, {:message => "Message"}, 01234560)
  #   put_picture(params[:file], {:message => "Message"})
  
  raise KoalaError.new("Wrong number of arguments for put_picture") unless picture_args.size.between?(1, 5)
  
  args_offset = picture_args[1].kind_of?(Hash) || picture_args.size == 1 ? 0 : 1
  
  args      = picture_args[1 + args_offset] || {}
  target_id = picture_args[2 + args_offset] || "me"
  options   = picture_args[3 + args_offset] || {} 
  
  args["source"] = Koala::UploadableIO.new(*picture_args.slice(0, 1 + args_offset))
  
  self.put_object(target_id, "photos", args, options)
end

#put_wall_post(message, attachment = {}, profile_id = "me", options = {}) ⇒ Object

Wall posts To get wall posts, use get_connections(user, “feed”) To delete a wall post, just use delete_object(post_id)



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/koala/graph_api.rb', line 134

def put_wall_post(message, attachment = {}, profile_id = "me", options = {})
  # attachment is a hash describing the wall post
  # (see X for more details)
  # For instance, 
  # 
  #     {"name" => "Link name"
  #      "link" => "http://www.example.com/",
  #      "caption" => "{*actor*} posted a new review",
  #      "description" => "This is a longer description of the attachment",
  #      "picture" => "http://www.example.com/thumbnail.jpg"}

  self.put_object(profile_id, "feed", attachment.merge({:message => message}), options)
end

#search(search_terms, args = {}, options = {}) ⇒ Object

Search



173
174
175
176
177
# File 'lib/koala/graph_api.rb', line 173

def search(search_terms, args = {}, options = {})
  args.merge!({:q => search_terms}) unless search_terms.nil?
  result = graph_call("search", args, "get", options)
  result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
end