Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
mmangino committed Apr 30, 2010
1 parent e86fcc6 commit 177467b
Show file tree
Hide file tree
Showing 19 changed files with 309 additions and 25 deletions.
26 changes: 11 additions & 15 deletions Todo.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
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
1) make klass arg be a proc that can determine type of object (for page/user on posts)
2) Handle paging in associations
3)

3) add things that require permission with exception if not granted


allow embedding into other classes
class User < ActiveRecord::Base
acts_as_ogli :id=>:facebook_id,:class=>Ogli::User
end

allows user.facebook.activities
10 changes: 10 additions & 0 deletions lib/ogli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@ module Ogli

end

require "ogli/model"
require "ogli/action"
require "ogli/activity"
require "ogli/book"
require "ogli/comment"
require "ogli/interest"
require "ogli/movie"
require "ogli/music"
require "ogli/post"
require "ogli/television"
require "ogli/user"
require "ogli/client"
6 changes: 6 additions & 0 deletions lib/ogli/action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Ogli
class Action < Hashie::Mash
include Model

end
end
4 changes: 4 additions & 0 deletions lib/ogli/activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Ogli
class Activity < Hashie::Mash
end
end
6 changes: 6 additions & 0 deletions lib/ogli/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Ogli
class Book < Hashie::Mash
include Model

end
end
23 changes: 17 additions & 6 deletions lib/ogli/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Client
attr_reader :access_token
attr_reader :default_params

include HTTParty
include Ogli::Client::User


def api_path(path)
"http://graph.facebook.com/#{path}"
end
Expand All @@ -14,32 +18,39 @@ def initialize(access_token = nil)
@default_params = @access_token ? {:access_token=>access_token} : {}
end

def get_and_map(path,klass=nil)
map_data(self.class.get(api_path(path),:query=>default_params),klass)
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 if hash_or_array.nil? or hash_or_array.kind_of?(Array)
return hash_or_array["data"] if 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)}
hash_or_array = hash_or_array.map {|i| create_instance(klass,i)}
else
hash_or_array = klass.new(hash_or_array)
hash_or_array = create_instance(klass,hash_or_array)
end
end

def create_instance(klass,data)
klass = Ogli.const_get(klass) if klass.is_a?(String)
klass.new(data,self)
end

def raise_error_if_necessary(data)
if data.kind_of?(Hash)
if data.keys.size == 1 and data["error"]
Expand Down
2 changes: 1 addition & 1 deletion lib/ogli/client/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module User


def user(id)
self.class.get(api_path(id))
get_and_map(id,Ogli::User)
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/ogli/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Ogli
class Comment < Hashie::Dash
include Model

define_properties :id, :message, :created_time

hash_populating_accessor :from, "User"

end
end
6 changes: 6 additions & 0 deletions lib/ogli/interest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Ogli
class Interest < Hashie::Mash
include Model

end
end
50 changes: 50 additions & 0 deletions lib/ogli/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Ogli
module Model
def client=(val)
@client=val
end

def client
@client || Ogli::Client.new
end

def initialize(hash={},client=nil)
self.client=client
super(hash)
end

def self.included(other)
other.extend(ClassMethods)
end


module ClassMethods


def define_properties(*args)
args.each do |arg|
property arg
end
end

def hash_populating_accessor(name,klass)
define_method "#{name}=" do |hash|
instance_variable_set("@#{name}",client.map_data(hash,klass))
end
define_method "#{name}" do
instance_variable_get "@#{name}"
end
end

def has_association(name,klass)
define_method name do
if (ret=instance_variable_get("@#{name}")).nil?
ret = client.get_and_map("/#{id}/#{name}",klass)
instance_variable_set("@#{name}",ret)
end
return ret
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/ogli/movie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Ogli
class Movie < Hashie::Mash
include Model

end
end
6 changes: 6 additions & 0 deletions lib/ogli/music.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Ogli
class Music < Hashie::Mash
include Model

end
end
12 changes: 12 additions & 0 deletions lib/ogli/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Ogli
class Post < Hashie::Dash
include Model
define_properties :id, :to, :message, :picture, :link, :name, :caption,
:description, :source, :icon, :attribution, :actions, :likes,
:created_time, :updated_time

hash_populating_accessor :actions, "Action"
hash_populating_accessor :comments, "Comment"
hash_populating_accessor :from, "User"
end
end
6 changes: 6 additions & 0 deletions lib/ogli/television.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Ogli
class Television < Hashie::Mash
include Model

end
end
10 changes: 10 additions & 0 deletions lib/ogli/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@

module Ogli
class User < Hashie::Mash
include Model

has_association :activities,Ogli::Activity
has_association :friends, Ogli::User
has_association :interests, Ogli::Interest
has_association :music, Ogli::Music
has_association :books, Ogli::Book
has_association :movies, Ogli::Movie
has_association :television, Ogli::Television
has_association :posts, Ogli::Post
end
end
9 changes: 6 additions & 3 deletions spec/client/client_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
let :client do
Ogli::Client.new
end
# it "fetches a user by id" do
# client.user(12451752).should == {:id=>12451752}
# end

it "fetches a user by id" do
client.should_receive(:get_and_map).with(12451752,Ogli::User).and_return("user")
client.user(12451752).should == "user"
end

end
91 changes: 91 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require "spec_helper"
describe Ogli::Client do

let :client do
Ogli::Client.new
end


describe "creation" do
it "allows creating with an access_token" do
client = Ogli::Client.new("myaccesstoken")
client.access_token.should == "myaccesstoken"
end

it "sets the access_token into the default params" do
client = Ogli::Client.new("myaccesstoken")
client.default_params.should == {:access_token=>"myaccesstoken"}
end

it "allow creation with no access token" do
client = Ogli::Client.new
client.access_token.should be_nil
end

it "doesn't include the access_token param when not passed" do
client = Ogli::Client.new
client.default_params.should == {}
end
end

describe "Making requests" do

end

describe "result mapping" do

let :user_data do
{"id"=>12451752, "first_name"=>"Mike", "last_name"=>"Mangino" }
end

it "returns the raw value with no class specified" do
client.map_data(user_data).should be_an_instance_of(Hash)
end

it "returns the array if no class is specified and there is only a data parameter" do
client.map_data({"data"=>[user_data,user_data]}).should be_an_instance_of(Array)
end


it "creates an instance of the class when specified" do
user = client.map_data(user_data,Ogli::User)
user.should be_an_instance_of(Ogli::User)
user.id.should == 12451752
end

it "creates an array of instances when the data is an array" do
users = client.map_data([user_data,user_data],Ogli::User)
users.should be_an_instance_of(Array)
users.each {|i| i.should be_an_instance_of(Ogli::User) }
users.size.should == 2
end

it "creates an array of instances when the data is just a hash with a single data parameter" do
users = client.map_data({"data"=>[user_data,user_data]},Ogli::User)
users.should be_an_instance_of(Array)
users.each {|i| i.should be_an_instance_of(Ogli::User) }
users.size.should == 2
end

it "sets the client in the newly created instance" do
user = client.map_data(user_data,Ogli::User)
user.client.should == client
end

it "raises an exception when there is just an error" do
lambda do
client.map_data({"error"=>{"type"=>"OAuthAccessTokenException","message"=>"An access token is required to request this resource."}})
end.should raise_error
end

it "should set the message on the exception" do
begin
client.map_data({"error"=>{"type"=>"OAuthAccessTokenException","message"=>"An access token is required to request this resource."}})
fail "Exception not raised!"
rescue Exception => e
e.message.should == "OAuthAccessTokenException: An access token is required to request this resource."
end
end
end

end
20 changes: 20 additions & 0 deletions spec/post_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "spec_helper"

describe Ogli::Post do

it "populates from as the user from a hash" do
post = Ogli::Post.new("from"=> {"id" => "12451752", "name" => "Mike Mangino"})
post.from.should be_an_instance_of(Ogli::User)
post.from.id.should == "12451752"
post.from.name.should == "Mike Mangino"
end

it "populates comments from a hash array" do
post = Ogli::Post.new ({"comments"=>{"data"=>[{"id"=>1,"message"=>"message1"},{"id"=>2}]}},Ogli::Client.new("my_api_key"))
post.comments.size.should == 2
post.comments.each {|c| c.should be_an_instance_of(Ogli::Comment)}
post.comments.first.id.should == 1
post.comments.first.client.access_token.should == "my_api_key"
end

end
Loading

0 comments on commit 177467b

Please sign in to comment.