From e86fcc6d24f0c0f5a7bfb9e180bf7a72000888ea Mon Sep 17 00:00:00 2001 From: Mike Mangino Date: Fri, 30 Apr 2010 09:41:31 -0400 Subject: [PATCH] Beginning of code to make and parse responses --- Todo.txt | 17 +++++++++++ init.rb | 3 ++ lib/ogli.rb | 6 ++++ lib/ogli/client.rb | 53 +++++++++++++++++++++++++++++++++ lib/ogli/client/user.rb | 11 +++++++ lib/ogli/user.rb | 5 ++++ spec/client/client_user_spec.rb | 9 ++++++ spec/spec_helper.rb | 2 ++ 8 files changed, 106 insertions(+) create mode 100644 Todo.txt create mode 100644 init.rb create mode 100644 lib/ogli.rb create mode 100644 lib/ogli/client.rb create mode 100644 lib/ogli/client/user.rb create mode 100644 lib/ogli/user.rb create mode 100644 spec/client/client_user_spec.rb diff --git a/Todo.txt b/Todo.txt new file mode 100644 index 0000000..67c8fb3 --- /dev/null +++ b/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 diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..5e3a5be --- /dev/null +++ b/init.rb @@ -0,0 +1,3 @@ +require "httparty" +require "hashie" +require "ogli" diff --git a/lib/ogli.rb b/lib/ogli.rb new file mode 100644 index 0000000..3ad90d9 --- /dev/null +++ b/lib/ogli.rb @@ -0,0 +1,6 @@ +module Ogli + +end + +require "ogli/user" +require "ogli/client" \ No newline at end of file diff --git a/lib/ogli/client.rb b/lib/ogli/client.rb new file mode 100644 index 0000000..87b65b1 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/ogli/client/user.rb b/lib/ogli/client/user.rb new file mode 100644 index 0000000..0ebf101 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/ogli/user.rb b/lib/ogli/user.rb new file mode 100644 index 0000000..7ecd269 --- /dev/null +++ b/lib/ogli/user.rb @@ -0,0 +1,5 @@ +module Ogli + class User < Hashie::Mash + + end +end \ No newline at end of file diff --git a/spec/client/client_user_spec.rb b/spec/client/client_user_spec.rb new file mode 100644 index 0000000..162dab6 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e69de29..3c942ee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -0,0 +1,2 @@ +require "rubygems" +require (Pathname(__FILE__).dirname + '../init')