Class: Koala::Facebook::GraphAPI
- Inherits:
-
Object
- Object
- Koala::Facebook::GraphAPI
- Defined in:
- lib/koala.rb
Instance Method Summary collapse
- #api(path, args = {}, verb = "get") ⇒ Object
- #delete_object(id) ⇒ Object
- #get_connections(id, connection_name, args = {}) ⇒ Object
- #get_object(id, args = {}) ⇒ Object
- #get_objects(ids, args = {}) ⇒ Object
-
#initialize(access_token = nil) ⇒ GraphAPI
constructor
initialize with an access token.
- #put_comment(object_id, message) ⇒ Object
- #put_like(object_id) ⇒ Object
- #put_object(parent_object, connection_name, args = {}) ⇒ Object
- #put_wall_post(message, attachment = {}, profile_id = "me") ⇒ Object
- #query(fql) ⇒ Object
- #search(search_terms, args = {}) ⇒ Object
Constructor Details
#initialize(access_token = nil) ⇒ GraphAPI
initialize with an access token
67 68 69 |
# File 'lib/koala.rb', line 67 def initialize(access_token = nil) @access_token = access_token end |
Instance Method Details
#api(path, args = {}, verb = "get") ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/koala.rb', line 152 def api(path, args = {}, verb = "get") # Fetches the given path in the Graph API. args["access_token"] = @access_token if @access_token # make the request via the provided service result = Koala.make_request(path, args, verb) # Facebook sometimes sends results like "true" and "false", which aren't strictly object # and cause JSON.parse to fail # so we account for that response = JSON.parse("[#{result}]")[0] # check for errors if response.is_a?(Hash) && error_details = response["error"] raise GraphAPIError.new(error_details) end response end |
#delete_object(id) ⇒ Object
142 143 144 145 |
# File 'lib/koala.rb', line 142 def delete_object(id) # Deletes the object with the given ID from the graph. api(id, {}, "delete") end |
#get_connections(id, connection_name, args = {}) ⇒ Object
83 84 85 86 |
# File 'lib/koala.rb', line 83 def get_connections(id, connection_name, args = {}) # Fetchs the connections for given object. api("#{id}/#{connection_name}", args) end |
#get_object(id, args = {}) ⇒ Object
71 72 73 74 |
# File 'lib/koala.rb', line 71 def get_object(id, args = {}) # Fetchs the given object from the graph. api(id, args) end |
#get_objects(ids, args = {}) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/koala.rb', line 76 def get_objects(ids, args = {}) # 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. api("", args.merge("ids" => ids.join(","))) end |
#put_comment(object_id, message) ⇒ Object
132 133 134 135 |
# File 'lib/koala.rb', line 132 def put_comment(object_id, ) # Writes the given comment on the given post. self.put_object(object_id, "comments", {:message => }) end |
#put_like(object_id) ⇒ Object
137 138 139 140 |
# File 'lib/koala.rb', line 137 def put_like(object_id) # Likes the given post. self.put_object(object_id, "likes") end |
#put_object(parent_object, connection_name, args = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/koala.rb', line 88 def put_object(parent_object, connection_name, args = {}) # Writes the given object to the graph, connected to the given parent. # # For example, # # graph.put_object("me", "feed", :message => "Hello, world") # # writes "Hello, world" to the active user's wall. Likewise, this # will comment on a the first post of the active user's feed: # # feed = graph.get_connections("me", "feed") # post = feed["data"][0] # graph.put_object(post["id"], "comments", :message => "First!") # # See http://developers.facebook.com/docs/api#publishing for all of # the supported writeable objects. # # 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 GraphAPIError.new({"type" => "KoalaMissingAccessToken", "message" => "Write operations require an access token"}) unless @access_token api("#{parent_object}/#{connection_name}", args, "post") end |
#put_wall_post(message, attachment = {}, profile_id = "me") ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/koala.rb', line 114 def put_wall_post(, = {}, profile_id = "me") # Writes a wall post to the given profile's wall. # # We default to writing to the authenticated user's wall if no # profile_id is specified. # # attachment adds a structured attachment to the status message being # posted to the Wall. It should be a dictionary of the form: # # {"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", .merge({:message => })) end |
#query(fql) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/koala.rb', line 172 def query(fql) args = { "query" => fql, "format" => "json", } # Adds access_token if available args["access_token"] = @access_token if @access_token response = Koala.make_request('method/fql.query', args, 'get', false) # Facebook sometimes sends results like "true" and "false", which aren't strictly object # and cause JSON.parse to fail # so we account for that response = JSON.parse("[#{response}]")[0] # check for errors if response.is_a?(Hash) && response["error_code"] raise RestAPIError.new(response) end response end |
#search(search_terms, args = {}) ⇒ Object
147 148 149 150 |
# File 'lib/koala.rb', line 147 def search(search_terms, args = {}) # Searches for a given term api("search", args.merge({:q => search_terms})) end |