mogli 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mogli.rb CHANGED
@@ -15,6 +15,7 @@ require "mogli/address"
15
15
  require "mogli/album"
16
16
  require "mogli/book"
17
17
  require "mogli/comment"
18
+ require "mogli/education"
18
19
  require "mogli/event"
19
20
  require "mogli/group"
20
21
  require "mogli/interest"
@@ -26,5 +27,6 @@ require "mogli/photo"
26
27
  require "mogli/post"
27
28
  require "mogli/television"
28
29
  require "mogli/video"
30
+ require "mogli/work"
29
31
  require "mogli/user"
30
32
  require "mogli/client"
data/lib/mogli/action.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Mogli
2
- class Action < Hashie::Mash
3
- include Model
2
+ class Action < Model
3
+ define_properties :name, :link
4
4
 
5
5
  end
6
6
  end
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Activity < Hashie::Mash
3
- include Model
4
-
2
+ class Activity < Model
3
+ define_properties :name, :category, :id
5
4
  end
6
5
  end
data/lib/mogli/address.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Address < Hashie::Dash
3
- include Model
2
+ class Address < Model
4
3
 
5
4
  define_properties :street, :city, :state, :zip, :country, :latitude, :longitude
6
5
  end
data/lib/mogli/album.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Album < Hashie::Dash
3
- include Model
2
+ class Album < Model
4
3
 
5
4
  define_properties :id, :name, :description, :link, :count, :created_time, :updated_time
6
5
 
@@ -9,8 +9,10 @@ module Mogli
9
9
  @callback_url = callback_url
10
10
  end
11
11
 
12
- def authorize_url
13
- "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}"
12
+ def authorize_url(*scopes)
13
+ scopes = scopes.flatten
14
+ scope_part = "&scope=#{scopes.join(",")}" unless scopes.blank?
15
+ "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}#{scope_part}"
14
16
  end
15
17
 
16
18
  def access_token_url(code)
data/lib/mogli/book.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Mogli
2
- class Book < Hashie::Mash
3
- include Model
2
+ class Book < Model
3
+ define_properties :name, :category, :id
4
+
4
5
 
5
6
  end
6
7
  end
data/lib/mogli/client.rb CHANGED
@@ -5,6 +5,7 @@ module Mogli
5
5
  class Client
6
6
  attr_reader :access_token
7
7
  attr_reader :default_params
8
+ attr_reader :expiration
8
9
 
9
10
  include HTTParty
10
11
  include Mogli::Client::User
@@ -14,23 +15,24 @@ module Mogli
14
15
  "http://graph.facebook.com/#{path}"
15
16
  end
16
17
 
17
- def initialize(access_token = nil)
18
+ def initialize(access_token = nil,expiration=nil)
18
19
  @access_token = access_token
20
+ @expiration = Time.now + expiration if expiration
19
21
  @default_params = @access_token ? {:access_token=>access_token} : {}
20
22
  end
21
23
 
22
- def self.get_access_token_for_code_and_authenticator(code,authenticator)
24
+ def expired?
25
+ expiration and expiration < Time.now
26
+ end
27
+
28
+ def self.create_from_code_and_authenticator(code,authenticator)
23
29
  post_data = get(authenticator.access_token_url(code))
24
30
  parts = post_data.split("&")
25
31
  hash = {}
26
32
  parts.each do |p| (k,v) = p.split("=")
27
33
  hash[k]=v
28
34
  end
29
- hash["access_token"]
30
- end
31
-
32
- def self.create_from_code_and_authenticator(code,authenticator)
33
- new(get_access_token_for_code_and_authenticator(code,authenticator))
35
+ new(hash["access_token"],hash["expires"].to_s.to_i)
34
36
  end
35
37
 
36
38
  def get_and_map(path,klass=nil)
@@ -92,7 +94,7 @@ module Mogli
92
94
 
93
95
  def determine_class(klass_or_klasses,data)
94
96
  klasses = Array(klass_or_klasses).map { |k| constantize_string(k)}
95
- klasses.detect {|klass| klass.recognize?(data)}
97
+ klasses.detect {|klass| klass.recognize?(data)} || klasses.first
96
98
  end
97
99
 
98
100
  def raise_error_if_necessary(data)
@@ -104,5 +106,31 @@ module Mogli
104
106
  end
105
107
  end
106
108
  end
109
+
110
+ def fields_to_serialize
111
+ [:access_token,:default_params,:expiration]
112
+ end
113
+
114
+ # Only serialize the bare minimum to recreate the session.
115
+ def marshal_load(variables)#:nodoc:
116
+ fields_to_serialize.each_with_index{|field, index| instance_variable_set("@#{field}", variables[index])}
117
+ end
118
+
119
+ # Only serialize the bare minimum to recreate the session.
120
+ def marshal_dump#:nodoc:
121
+ fields_to_serialize.map{|field| send(field)}
122
+ end
123
+
124
+ # Only serialize the bare minimum to recreate the session.
125
+ def to_yaml( opts = {} )#nodoc
126
+ YAML::quick_emit(self.object_id, opts) do |out|
127
+ out.map(taguri) do |map|
128
+ fields_to_serialize.each do |field|
129
+ map.add(field, send(field))
130
+ end
131
+ end
132
+ end
133
+ end
134
+
107
135
  end
108
136
  end
data/lib/mogli/comment.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Comment < Hashie::Dash
3
- include Model
2
+ class Comment < Model
4
3
 
5
4
  define_properties :id, :message, :created_time
6
5
 
@@ -0,0 +1,10 @@
1
+ module Mogli
2
+ class Education < Model
3
+
4
+ define_properties :start_date, :end_date, :degree
5
+
6
+ hash_populating_accessor :school, "Page"
7
+ hash_populating_accessor :year, "Page"
8
+ hash_populating_accessor :concentration, "Page"
9
+ end
10
+ end
data/lib/mogli/event.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Event < Hashie::Dash
3
- include Model
2
+ class Event < Model
4
3
  define_properties :id, :name, :description, :start_time, :end_time, :location, :privacy, :updated_time
5
4
 
6
5
  hash_populating_accessor :venue, "Address"
data/lib/mogli/group.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Group < Hashie::Dash
3
- include Model
2
+ class Group < Model
4
3
  define_properties :id, :name, :description, :link, :privacy, :updated_time
5
4
 
6
5
  hash_populating_accessor :owner, "User", "Page"
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Interest < Hashie::Mash
3
- include Model
4
-
2
+ class Interest < Model
3
+ define_properties :id, :name
5
4
  end
6
5
  end
data/lib/mogli/model.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Mogli
2
- module Model
2
+ class Model < Hashie::Dash
3
3
  def client=(val)
4
4
  @client=val
5
5
  end
@@ -10,49 +10,56 @@ module Mogli
10
10
 
11
11
  def initialize(hash={},client=nil)
12
12
  self.client=client
13
- super(hash)
13
+ super(hash||{})
14
14
  end
15
15
 
16
16
  def self.included(other)
17
17
  other.extend(ClassMethods)
18
18
  end
19
19
 
20
-
21
- module ClassMethods
22
-
23
-
24
- def define_properties(*args)
25
- args.each do |arg|
26
- property arg
27
- end
20
+ def method_missing(method, *args)
21
+ method_as_s = method.to_s
22
+ if method_as_s.to_s[-1].chr == "="
23
+ warn_about_invalid_property(method_as_s.chop)
24
+ else
25
+ super
28
26
  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
27
+ end
28
+ def warn_about_invalid_property(property)
29
+ puts "Warning: property #{property} doesn't exist for class #{self.class.name}"
30
+ end
31
+
32
+ def self.define_properties(*args)
33
+ args.each do |arg|
34
+ property arg
37
35
  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
36
+ end
37
+
38
+ def self.hash_populating_accessor(method_name,*klass)
39
+ define_method "#{method_name}=" do |hash|
40
+ instance_variable_set("@#{method_name}",client.map_data(hash,klass))
47
41
  end
48
-
49
- def recognize?(data)
50
- true
42
+ define_method "#{method_name}" do
43
+ instance_variable_get "@#{method_name}"
51
44
  end
52
-
53
- def find(id,client=nil)
54
- (client||Mogli::Client.new).get_and_map(id,self)
45
+ end
46
+
47
+ def self.has_association(name,klass)
48
+ define_method name do
49
+ if (ret=instance_variable_get("@#{name}")).nil?
50
+ ret = client.get_and_map("/#{id}/#{name}",klass)
51
+ instance_variable_set("@#{name}",ret)
52
+ end
53
+ return ret
55
54
  end
56
55
  end
56
+
57
+ def self.recognize?(data)
58
+ true
59
+ end
60
+
61
+ def self.find(id,client=nil)
62
+ (client||Mogli::Client.new).get_and_map(id,self)
63
+ end
57
64
  end
58
65
  end
data/lib/mogli/movie.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Mogli
2
- class Movie < Hashie::Mash
3
- include Model
2
+ class Movie < Model
3
+ define_properties :name, :category, :id
4
4
 
5
5
  end
6
6
  end
data/lib/mogli/music.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Mogli
2
- class Music < Hashie::Mash
3
- include Model
2
+ class Music < Model
3
+ define_properties :name, :category, :id
4
4
 
5
5
  end
6
6
  end
data/lib/mogli/note.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Note < Hashie::Dash
3
- include Model
2
+ class Note < Model
4
3
 
5
4
  define_properties :id, :subject, :message, :created_time, :updated_time, :icon
6
5
 
data/lib/mogli/page.rb CHANGED
@@ -1,15 +1,21 @@
1
1
  module Mogli
2
- class Page < Hashie::Dash
2
+ class Page < Model
3
3
 
4
- include Model
5
4
  define_properties :id, :name, :category
6
5
 
7
6
  hash_populating_accessor :albums, "Album"
8
7
  hash_populating_accessor :photos, "Photo"
8
+ hash_populating_accessor :feed, "Post"
9
+ hash_populating_accessor :posts, "Post"
10
+ hash_populating_accessor :events, "Event"
11
+ hash_populating_accessor :videos, "Video"
9
12
 
10
13
  def self.recognize?(hash)
11
14
  hash.has_key?("category")
12
15
  end
13
16
 
17
+ def to_s
18
+ name
19
+ end
14
20
  end
15
21
  end
data/lib/mogli/photo.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Photo < Hashie::Dash
3
- include Model
2
+ class Photo < Model
4
3
  define_properties :id, :name, :picture, :source, :height, :width, :link, :icon,
5
4
  :created_time, :updated_time
6
5
 
data/lib/mogli/post.rb CHANGED
@@ -1,10 +1,9 @@
1
1
  module Mogli
2
- class Post < Hashie::Dash
2
+ class Post < Model
3
3
 
4
- include Model
5
4
  define_properties :id, :to, :message, :picture, :link, :name, :caption,
6
5
  :description, :source, :icon, :attribution, :actions, :likes,
7
- :created_time, :updated_time
6
+ :created_time, :updated_time, :privacy
8
7
 
9
8
  hash_populating_accessor :actions, "Action"
10
9
  hash_populating_accessor :comments, "Comment"
@@ -1,6 +1,6 @@
1
1
  module Mogli
2
- class Television < Hashie::Mash
3
- include Model
2
+ class Television < Model
3
+ define_properties :name, :category, :id
4
4
 
5
5
  end
6
6
  end
data/lib/mogli/user.rb CHANGED
@@ -1,12 +1,17 @@
1
1
 
2
2
  module Mogli
3
- class User < Hashie::Mash
4
- include Model
3
+ class User < Model
4
+
5
+ define_properties :id, :first_name, :last_name, :name, :link, :about, :birthday,
6
+ :email, :website, :timezone, :updated_time, :verified
5
7
 
6
8
  def self.recognize?(hash)
7
9
  !hash.has_key?("category")
8
10
  end
9
11
 
12
+ hash_populating_accessor :work, "Work"
13
+ hash_populating_accessor :education, "Education"
14
+
10
15
  has_association :activities,"Activity"
11
16
  has_association :albums,"Album"
12
17
  has_association :friends, "User"
@@ -16,5 +21,6 @@ module Mogli
16
21
  has_association :movies, "Movie"
17
22
  has_association :television, "Television"
18
23
  has_association :posts, "Post"
24
+ has_association :feed, "Post"
19
25
  end
20
26
  end
data/lib/mogli/video.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Mogli
2
- class Video < Hashie::Dash
3
- include Model
2
+ class Video < Model
4
3
 
5
4
  define_properties :id, :message, :name, :description, :length, :created_time, :updated_time, :icon, :picture, :embed_html
6
5
 
data/lib/mogli/work.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Mogli
2
+ class Work < Model
3
+
4
+ define_properties :start_date, :end_date
5
+
6
+ hash_populating_accessor :employer, "Page"
7
+ hash_populating_accessor :location, "Page"
8
+ hash_populating_accessor :position, "Page"
9
+ end
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mogli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Mangino
@@ -50,6 +50,7 @@ files:
50
50
  - lib/mogli/client/user.rb
51
51
  - lib/mogli/client.rb
52
52
  - lib/mogli/comment.rb
53
+ - lib/mogli/education.rb
53
54
  - lib/mogli/event.rb
54
55
  - lib/mogli/fetching_array.rb
55
56
  - lib/mogli/group.rb
@@ -64,6 +65,7 @@ files:
64
65
  - lib/mogli/television.rb
65
66
  - lib/mogli/user.rb
66
67
  - lib/mogli/video.rb
68
+ - lib/mogli/work.rb
67
69
  - lib/mogli.rb
68
70
  has_rdoc: true
69
71
  homepage: http://developers.facebook.com/docs/api