mogli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Action < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Activity < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Mogli
2
+ class Address < Hashie::Dash
3
+ include Model
4
+
5
+ define_properties :street, :city, :state, :zip, :country, :latitude, :longitude
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Mogli
2
+ class Album < Hashie::Dash
3
+ include Model
4
+
5
+ define_properties :id, :name, :description, :link, :count, :created_time, :updated_time
6
+
7
+ hash_populating_accessor :from, "User","Page"
8
+ has_association :photos, "Photo"
9
+ has_association :comments, "Comment"
10
+
11
+ end
12
+ end
data/lib/mogli/book.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Book < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module Mogli
2
+ class Client
3
+ module User
4
+
5
+
6
+ def user(id)
7
+ get_and_map(id,Mogli::User)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,95 @@
1
+ require "mogli/client/user"
2
+ require "ruby-debug"
3
+
4
+ module Mogli
5
+ class Client
6
+ attr_reader :access_token
7
+ attr_reader :default_params
8
+
9
+ include HTTParty
10
+ include Mogli::Client::User
11
+ class UnrecognizeableClassError < Exception; end
12
+
13
+ def api_path(path)
14
+ "http://graph.facebook.com/#{path}"
15
+ end
16
+
17
+ def initialize(access_token = nil)
18
+ @access_token = access_token
19
+ @default_params = @access_token ? {:access_token=>access_token} : {}
20
+ end
21
+
22
+ def get_and_map(path,klass=nil)
23
+ data = self.class.get(api_path(path),:query=>default_params)
24
+ map_data(data,klass)
25
+ end
26
+
27
+ def get_and_map_url(url,klass=nil)
28
+ data = self.class.get(url)
29
+ map_data(data,klass)
30
+ end
31
+
32
+ def map_data(data,klass=nil)
33
+ raise_error_if_necessary(data)
34
+ hash_or_array = extract_hash_or_array(data,klass)
35
+ hash_or_array = map_to_class(hash_or_array,klass) if klass
36
+ hash_or_array
37
+ end
38
+
39
+
40
+ #protected
41
+
42
+ def extract_hash_or_array(hash_or_array,klass)
43
+ return hash_or_array if hash_or_array.nil? or hash_or_array.kind_of?(Array)
44
+ return extract_fetching_array(hash_or_array,klass) if hash_or_array.has_key?("data")
45
+ return hash_or_array
46
+ end
47
+
48
+ def extract_fetching_array(hash,klass)
49
+ f = Mogli::FetchingArray.new
50
+ f.concat(hash["data"])
51
+ f.client = self
52
+ f.classes = Array(klass)
53
+ if hash["paging"]
54
+ f.next_url = hash["paging"]["next"]
55
+ f.previous_url = hash["paging"]["previous"]
56
+ end
57
+ f
58
+ end
59
+
60
+ def map_to_class(hash_or_array,klass)
61
+ if hash_or_array.kind_of?(Array)
62
+ hash_or_array.map! {|i| create_instance(klass,i)}
63
+ else
64
+ hash_or_array = create_instance(klass,hash_or_array)
65
+ end
66
+ end
67
+
68
+ def create_instance(klass,data)
69
+ klass = determine_class(klass,data)
70
+ if klass.nil?
71
+ raise UnrecognizeableClassError.new("unable to recognize klass for #{klass.inspect} => #{data.inspect}")
72
+ end
73
+ klass.new(data,self)
74
+ end
75
+
76
+ def constantize_string(klass)
77
+ klass.is_a?(String) ? Mogli.const_get(klass) : klass
78
+ end
79
+
80
+ def determine_class(klass_or_klasses,data)
81
+ klasses = Array(klass_or_klasses).map { |k| constantize_string(k)}
82
+ klasses.detect {|klass| klass.recognize?(data)}
83
+ end
84
+
85
+ def raise_error_if_necessary(data)
86
+ if data.kind_of?(Hash)
87
+ if data.keys.size == 1 and data["error"]
88
+ type = data["error"]["type"]
89
+ message = data["error"]["message"]
90
+ raise Exception.new("#{type}: #{message}")
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,10 @@
1
+ module Mogli
2
+ class Comment < Hashie::Dash
3
+ include Model
4
+
5
+ define_properties :id, :message, :created_time
6
+
7
+ hash_populating_accessor :from, "User","Page"
8
+
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module Mogli
2
+ class Event < Hashie::Dash
3
+ include Model
4
+ define_properties :id, :name, :description, :start_time, :end_time, :location, :privacy, :updated_time
5
+
6
+ hash_populating_accessor :venue, "Address"
7
+ hash_populating_accessor :owner, "User", "Page"
8
+ has_association :noreply, "User"
9
+ has_association :mabye, "User"
10
+ has_association :invited, "User"
11
+ has_association :attending, "User"
12
+ has_association :declined, "User"
13
+ has_association :feed, "Post"
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module Mogli
2
+ class FetchingArray < Array
3
+ attr_accessor :next_url, :previous_url, :client, :classes
4
+
5
+ def fetch_next
6
+ return [] if next_url.blank?
7
+ additions = client.get_and_map_url(next_url,classes)
8
+ self.next_url = additions.next_url
9
+ self.concat(additions)
10
+ additions
11
+ end
12
+
13
+ def fetch_previous
14
+ return [] if previous_url.blank?
15
+ additions = client.get_and_map_url(previous_url,classes)
16
+ self.previous_url = additions.previous_url
17
+ self.unshift(*additions)
18
+ additions
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module Mogli
2
+ class Group < Hashie::Dash
3
+ include Model
4
+ define_properties :id, :name, :description, :link, :privacy, :updated_time
5
+
6
+ hash_populating_accessor :owner, "User", "Page"
7
+ hash_populating_accessor :venue, "Address"
8
+
9
+ has_association :feed, "Post"
10
+ has_association :members, "User"
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Interest < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
@@ -0,0 +1,58 @@
1
+ module Mogli
2
+ module Model
3
+ def client=(val)
4
+ @client=val
5
+ end
6
+
7
+ def client
8
+ @client || Mogli::Client.new
9
+ end
10
+
11
+ def initialize(hash={},client=nil)
12
+ self.client=client
13
+ super(hash)
14
+ end
15
+
16
+ def self.included(other)
17
+ other.extend(ClassMethods)
18
+ end
19
+
20
+
21
+ module ClassMethods
22
+
23
+
24
+ def define_properties(*args)
25
+ args.each do |arg|
26
+ property arg
27
+ end
28
+ end
29
+
30
+ def hash_populating_accessor(method_name,*klass)
31
+ define_method "#{method_name}=" do |hash|
32
+ instance_variable_set("@#{method_name}",client.map_data(hash,klass))
33
+ end
34
+ define_method "#{method_name}" do
35
+ instance_variable_get "@#{method_name}"
36
+ end
37
+ end
38
+
39
+ def has_association(name,klass)
40
+ define_method name do
41
+ if (ret=instance_variable_get("@#{name}")).nil?
42
+ ret = client.get_and_map("/#{id}/#{name}",klass)
43
+ instance_variable_set("@#{name}",ret)
44
+ end
45
+ return ret
46
+ end
47
+ end
48
+
49
+ def recognize?(data)
50
+ true
51
+ end
52
+
53
+ def find(id,client=nil)
54
+ (client||Mogli::Client.new).get_and_map(id,self)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Movie < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Music < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
data/lib/mogli/note.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Mogli
2
+ class Note < Hashie::Dash
3
+ include Model
4
+
5
+ define_properties :id, :subject, :message, :created_time, :updated_time, :icon
6
+
7
+ hash_populating_accessor :from, "User", "Page"
8
+
9
+ has_association :comments, "Comment"
10
+
11
+ end
12
+ end
data/lib/mogli/page.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Mogli
2
+ class Page < Hashie::Dash
3
+
4
+ include Model
5
+ define_properties :id, :name, :category
6
+
7
+ hash_populating_accessor :albums, "Album"
8
+ hash_populating_accessor :photos, "Photo"
9
+
10
+ def self.recognize?(hash)
11
+ hash.has_key?("category")
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Mogli
2
+ class Photo < Hashie::Dash
3
+ include Model
4
+ define_properties :id, :name, :picture, :source, :height, :width, :link, :icon,
5
+ :created_time, :updated_time
6
+
7
+ hash_populating_accessor :from, "User","Page"
8
+ end
9
+ end
data/lib/mogli/post.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Mogli
2
+ class Post < Hashie::Dash
3
+
4
+ include Model
5
+ define_properties :id, :to, :message, :picture, :link, :name, :caption,
6
+ :description, :source, :icon, :attribution, :actions, :likes,
7
+ :created_time, :updated_time
8
+
9
+ hash_populating_accessor :actions, "Action"
10
+ hash_populating_accessor :comments, "Comment"
11
+ hash_populating_accessor :from, "User"
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ module Mogli
2
+ class Television < Hashie::Mash
3
+ include Model
4
+
5
+ end
6
+ end
data/lib/mogli/user.rb ADDED
@@ -0,0 +1,20 @@
1
+
2
+ module Mogli
3
+ class User < Hashie::Mash
4
+ include Model
5
+
6
+ def self.recognize?(hash)
7
+ !hash.has_key?("category")
8
+ end
9
+
10
+ has_association :activities,"Activity"
11
+ has_association :albums,"Album"
12
+ has_association :friends, "User"
13
+ has_association :interests, "Interest"
14
+ has_association :music, "Music"
15
+ has_association :books, "Book"
16
+ has_association :movies, "Movie"
17
+ has_association :television, "Television"
18
+ has_association :posts, "Post"
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ module Mogli
2
+ class Video < Hashie::Dash
3
+ include Model
4
+
5
+ define_properties :id, :message, :name, :description, :length, :created_time, :updated_time, :icon, :picture, :embed_html
6
+
7
+ hash_populating_accessor :from, "User", "Page"
8
+
9
+ has_association :comments, "Comment"
10
+
11
+ end
12
+ end
data/lib/mogli.rb ADDED
@@ -0,0 +1,29 @@
1
+ module Mogli
2
+
3
+ end
4
+
5
+ require "httparty"
6
+ require "hashie"
7
+
8
+
9
+ require "mogli/model"
10
+ require "mogli/fetching_array"
11
+ require "mogli/action"
12
+ require "mogli/activity"
13
+ require "mogli/address"
14
+ require "mogli/album"
15
+ require "mogli/book"
16
+ require "mogli/comment"
17
+ require "mogli/event"
18
+ require "mogli/group"
19
+ require "mogli/interest"
20
+ require "mogli/movie"
21
+ require "mogli/music"
22
+ require "mogli/note"
23
+ require "mogli/page"
24
+ require "mogli/photo"
25
+ require "mogli/post"
26
+ require "mogli/television"
27
+ require "mogli/video"
28
+ require "mogli/user"
29
+ require "mogli/client"
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mogli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Mangino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-05-03 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hashie
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: httparty
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.3
34
+ version:
35
+ description: Simple library for accessing the facebook Open Graph API
36
+ email: mmangino@elevatedrails.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/mogli/action.rb
45
+ - lib/mogli/activity.rb
46
+ - lib/mogli/address.rb
47
+ - lib/mogli/album.rb
48
+ - lib/mogli/book.rb
49
+ - lib/mogli/client/user.rb
50
+ - lib/mogli/client.rb
51
+ - lib/mogli/comment.rb
52
+ - lib/mogli/event.rb
53
+ - lib/mogli/fetching_array.rb
54
+ - lib/mogli/group.rb
55
+ - lib/mogli/interest.rb
56
+ - lib/mogli/model.rb
57
+ - lib/mogli/movie.rb
58
+ - lib/mogli/music.rb
59
+ - lib/mogli/note.rb
60
+ - lib/mogli/page.rb
61
+ - lib/mogli/photo.rb
62
+ - lib/mogli/post.rb
63
+ - lib/mogli/television.rb
64
+ - lib/mogli/user.rb
65
+ - lib/mogli/video.rb
66
+ - lib/mogli.rb
67
+ has_rdoc: true
68
+ homepage: http://developers.facebook.com/docs/api
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.5
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Open Graph Library for Ruby
95
+ test_files: []
96
+