Skip to content

Commit

Permalink
Beginning of code to make and parse responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mmangino committed Apr 30, 2010
1 parent af3e4f1 commit e86fcc6
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Todo.txt
@@ -0,0 +1,17 @@
0) exchange access_token for access token

1) get user data
* basic info
* work info
* education
* interested in
* meeting for
2) annotate connections
* friends
* activities
* interests
* music
* books
* television

3) add things that require permission with exception if not granted
3 changes: 3 additions & 0 deletions init.rb
@@ -0,0 +1,3 @@
require "httparty"
require "hashie"
require "ogli"
6 changes: 6 additions & 0 deletions lib/ogli.rb
@@ -0,0 +1,6 @@
module Ogli

end

require "ogli/user"
require "ogli/client"
53 changes: 53 additions & 0 deletions lib/ogli/client.rb
@@ -0,0 +1,53 @@
require "ogli/client/user"

module Ogli
class Client
attr_reader :access_token
attr_reader :default_params

def api_path(path)
"http://graph.facebook.com/#{path}"
end

def initialize(access_token = nil)
@access_token = access_token
@default_params = @access_token ? {:access_token=>access_token} : {}
end

def map_data(data,klass=nil)
raise_error_if_necessary(data)
hash_or_array = extract_hash_or_array(data)
hash_or_array = map_to_class(hash_or_array,klass) if klass
hash_or_array
end

include HTTParty
include User

protected

def extract_hash_or_array(hash_or_array)
return hash_or_array if hash_or_array.kind_of?(Array)
return hash_or_array["data"] if hash_or_array.keys.size == 1 and hash_or_array.has_key?("data")
return hash_or_array
end

def map_to_class(hash_or_array,klass)
if hash_or_array.kind_of?(Array)
hash_or_array = hash_or_array.map {|i| klass.new(i)}
else
hash_or_array = klass.new(hash_or_array)
end
end

def raise_error_if_necessary(data)
if data.kind_of?(Hash)
if data.keys.size == 1 and data["error"]
type = data["error"]["type"]
message = data["error"]["message"]
raise Exception.new("#{type}: #{message}")
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/ogli/client/user.rb
@@ -0,0 +1,11 @@
module Ogli
class Client
module User


def user(id)
self.class.get(api_path(id))
end
end
end
end
5 changes: 5 additions & 0 deletions lib/ogli/user.rb
@@ -0,0 +1,5 @@
module Ogli
class User < Hashie::Mash

end
end
9 changes: 9 additions & 0 deletions spec/client/client_user_spec.rb
@@ -0,0 +1,9 @@
require "spec_helper"
describe Ogli::Client do
let :client do
Ogli::Client.new
end
# it "fetches a user by id" do
# client.user(12451752).should == {:id=>12451752}
# end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,2 @@
require "rubygems"
require (Pathname(__FILE__).dirname + '../init')

0 comments on commit e86fcc6

Please sign in to comment.