Skip to content

Commit

Permalink
add error check method to response; formatting for get() and post() r…
Browse files Browse the repository at this point in the history
…edo; expose href of request object for clear-view of latest OpenGraph niceness
  • Loading branch information
mccolin committed Sep 23, 2011
1 parent e94fb9d commit 94b8d71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/doesopengraph/graph_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
module DoesOpenGraph
class GraphRequest

attr_reader :api, :method, :path, :params
attr_reader :api, :method, :path, :params, :href

# Build a Request object from its component parts
def initialize(api, method, path, params={})
@api = api
@method = method
@path = path
@params = params
@href = nil
end


# Perform the request
def request
base = @api.access_token ? GraphAPI::HTTPS_GRAPH_ENDPOINT : GraphAPI::HTTP_GRAPH_ENDPOINT
href = File.join(base, @path)
@href = File.join(base, @path)

if !%w(get post delete).include?(@method.to_s)
raise InvalidRequestMethod.new("Invalid HTTP method #{@method} passed to request") and return nil
Expand All @@ -30,7 +31,7 @@ def request
params[:access_token] = @api.access_token if @api.access_token

begin
response = Typhoeus::Request.send(@method, href, :params=>@params)
response = Typhoeus::Request.send(@method, @href, :params=>@params)
puts "RESPONSE RECEIVED FROM FACEBOOK ON REQUEST TO PATH #{@path}:\n#{response.body}\n\n"

return GraphResponse.new(response.body, self)
Expand Down
19 changes: 18 additions & 1 deletion lib/doesopengraph/graph_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def reload
return self
end

# Is this response an error?
def error?
keys.include?(:error)
end

# What is the error return from Facebook in this response?
def error_message
@object.error ? @object.error.message : nil
end

# Introspect on the connections available to this node
def introspect
raise IncapableOfUpdateMethods.new("Cannot update content without stored request") if request.nil?
Expand All @@ -58,9 +68,16 @@ def introspect
# Get a connection of this node
def get(connection, params={})
raise IncapableOfUpdateMethods.new("Cannot update content without stored request") if request.nil?
request.api.get(object.id, connection, params={})
request.api.get(object.id, connection, params)
end

# Post to a connection of this node
def post(connection, params={})
raise IncapableOfUpdateMethods.new("Cannot update content without stored request") if request.nil?
request.api.post(object.id, connection, params)
end


# Load the next page of the response if paging is available
def next_page; page("next"); end
def previous_page; page("previous"); end
Expand Down

0 comments on commit 94b8d71

Please sign in to comment.