palidanx-fb_graph 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +308 -0
  5. data/Rakefile +49 -0
  6. data/VERSION +1 -0
  7. data/assets/fb_graph.ai +1726 -6
  8. data/assets/fb_graph.png +0 -0
  9. data/fb_graph.gemspec +327 -0
  10. data/lib/fb_graph.rb +75 -0
  11. data/lib/fb_graph/album.rb +133 -0
  12. data/lib/fb_graph/application.rb +57 -0
  13. data/lib/fb_graph/auth.rb +52 -0
  14. data/lib/fb_graph/auth/cookie.rb +43 -0
  15. data/lib/fb_graph/checkin.rb +44 -0
  16. data/lib/fb_graph/collection.rb +44 -0
  17. data/lib/fb_graph/comment.rb +20 -0
  18. data/lib/fb_graph/comparison.rb +9 -0
  19. data/lib/fb_graph/connection.rb +30 -0
  20. data/lib/fb_graph/connections.rb +3 -0
  21. data/lib/fb_graph/connections/accounts.rb +58 -0
  22. data/lib/fb_graph/connections/activities.rb +14 -0
  23. data/lib/fb_graph/connections/albums.rb +96 -0
  24. data/lib/fb_graph/connections/attending.rb +18 -0
  25. data/lib/fb_graph/connections/books.rb +14 -0
  26. data/lib/fb_graph/connections/checkins.rb +22 -0
  27. data/lib/fb_graph/connections/comments.rb +39 -0
  28. data/lib/fb_graph/connections/declined.rb +18 -0
  29. data/lib/fb_graph/connections/events.rb +21 -0
  30. data/lib/fb_graph/connections/feed.rb +90 -0
  31. data/lib/fb_graph/connections/friends.rb +14 -0
  32. data/lib/fb_graph/connections/groups.rb +14 -0
  33. data/lib/fb_graph/connections/home.rb +14 -0
  34. data/lib/fb_graph/connections/insights.rb +13 -0
  35. data/lib/fb_graph/connections/interests.rb +14 -0
  36. data/lib/fb_graph/connections/invited.rb +14 -0
  37. data/lib/fb_graph/connections/likes.rb +16 -0
  38. data/lib/fb_graph/connections/links.rb +21 -0
  39. data/lib/fb_graph/connections/maybe.rb +18 -0
  40. data/lib/fb_graph/connections/members.rb +14 -0
  41. data/lib/fb_graph/connections/movies.rb +14 -0
  42. data/lib/fb_graph/connections/music.rb +14 -0
  43. data/lib/fb_graph/connections/noreply.rb +14 -0
  44. data/lib/fb_graph/connections/notes.rb +21 -0
  45. data/lib/fb_graph/connections/photos.rb +21 -0
  46. data/lib/fb_graph/connections/picture.rb +14 -0
  47. data/lib/fb_graph/connections/posts.rb +14 -0
  48. data/lib/fb_graph/connections/statuses.rb +14 -0
  49. data/lib/fb_graph/connections/subscriptions.rb +53 -0
  50. data/lib/fb_graph/connections/tagged.rb +14 -0
  51. data/lib/fb_graph/connections/television.rb +14 -0
  52. data/lib/fb_graph/connections/videos.rb +14 -0
  53. data/lib/fb_graph/education.rb +25 -0
  54. data/lib/fb_graph/event.rb +47 -0
  55. data/lib/fb_graph/group.rb +27 -0
  56. data/lib/fb_graph/insight.rb +14 -0
  57. data/lib/fb_graph/link.rb +28 -0
  58. data/lib/fb_graph/node.rb +121 -0
  59. data/lib/fb_graph/note.rb +28 -0
  60. data/lib/fb_graph/page.rb +41 -0
  61. data/lib/fb_graph/photo.rb +41 -0
  62. data/lib/fb_graph/post.rb +50 -0
  63. data/lib/fb_graph/searchable.rb +25 -0
  64. data/lib/fb_graph/searchable/result.rb +31 -0
  65. data/lib/fb_graph/status.rb +23 -0
  66. data/lib/fb_graph/subscription.rb +14 -0
  67. data/lib/fb_graph/tag.rb +16 -0
  68. data/lib/fb_graph/user.rb +89 -0
  69. data/lib/fb_graph/venue.rb +17 -0
  70. data/lib/fb_graph/video.rb +28 -0
  71. data/lib/fb_graph/work.rb +35 -0
  72. data/spec/fake_json/albums/photos/matake_private.json +97 -0
  73. data/spec/fake_json/albums/photos/post_with_valid_access_token.json +1 -0
  74. data/spec/fake_json/applications/subscriptions/fb_graph_private.json +12 -0
  75. data/spec/fake_json/checkins/search_private.json +54 -0
  76. data/spec/fake_json/checkins/search_public.json +6 -0
  77. data/spec/fake_json/events/attending/post_with_valid_access_token.json +1 -0
  78. data/spec/fake_json/events/attending/smartday_private.json +44 -0
  79. data/spec/fake_json/events/declined/post_with_valid_access_token.json +1 -0
  80. data/spec/fake_json/events/declined/smartday_private.json +720 -0
  81. data/spec/fake_json/events/invited/smartday_private.json +7092 -0
  82. data/spec/fake_json/events/maybe/post_with_valid_access_token.json +1 -0
  83. data/spec/fake_json/events/maybe/smartday_private.json +956 -0
  84. data/spec/fake_json/events/noreply/smartday_private.json +896 -0
  85. data/spec/fake_json/groups/members/emacs_private.json +1996 -0
  86. data/spec/fake_json/pages/checkins/gowalla_private.json +113 -0
  87. data/spec/fake_json/pages/checkins/gowalla_public.json +6 -0
  88. data/spec/fake_json/pages/notes/post_with_valid_access_token.json +1 -0
  89. data/spec/fake_json/pages/platform_private.json +13 -0
  90. data/spec/fake_json/pages/platform_public.json +13 -0
  91. data/spec/fake_json/pages/statuses/platform_private.json +258 -0
  92. data/spec/fake_json/pages/statuses/platform_public.json +6 -0
  93. data/spec/fake_json/posts/comments/post_with_invalid_access_token.json +1 -0
  94. data/spec/fake_json/posts/comments/post_with_valid_access_token.json +1 -0
  95. data/spec/fake_json/posts/comments/post_without_access_token.json +1 -0
  96. data/spec/fake_json/posts/likes/post_with_invalid_access_token.json +1 -0
  97. data/spec/fake_json/posts/likes/post_with_valid_access_token.json +1 -0
  98. data/spec/fake_json/posts/likes/post_without_access_token.json +1 -0
  99. data/spec/fake_json/posts/platform_private.json +97 -0
  100. data/spec/fake_json/posts/platform_public.json +52 -0
  101. data/spec/fake_json/users/accounts/matake_private.json +14 -0
  102. data/spec/fake_json/users/accounts/matake_private_with_manage_pages_permission.json +16 -0
  103. data/spec/fake_json/users/accounts/matake_public.json +6 -0
  104. data/spec/fake_json/users/activities/arjun_private.json +24 -0
  105. data/spec/fake_json/users/activities/arjun_public.json +6 -0
  106. data/spec/fake_json/users/albums/matake_private.json +36 -0
  107. data/spec/fake_json/users/albums/matake_public.json +6 -0
  108. data/spec/fake_json/users/albums/post_with_valid_access_token.json +1 -0
  109. data/spec/fake_json/users/arjun_private.json +109 -0
  110. data/spec/fake_json/users/arjun_public.json +12 -0
  111. data/spec/fake_json/users/books/matake_private.json +9 -0
  112. data/spec/fake_json/users/books/matake_public.json +6 -0
  113. data/spec/fake_json/users/checkins/mattt_private.json +389 -0
  114. data/spec/fake_json/users/checkins/mattt_public.json +6 -0
  115. data/spec/fake_json/users/events/matake_private.json +71 -0
  116. data/spec/fake_json/users/events/matake_public.json +6 -0
  117. data/spec/fake_json/users/events/post_with_valid_access_token.json +1 -0
  118. data/spec/fake_json/users/feed/arjun_private.json +520 -0
  119. data/spec/fake_json/users/feed/arjun_public.json +520 -0
  120. data/spec/fake_json/users/feed/post_with_invalid_access_token.json +1 -0
  121. data/spec/fake_json/users/feed/post_with_valid_access_token.json +1 -0
  122. data/spec/fake_json/users/feed/post_without_access_token.json +1 -0
  123. data/spec/fake_json/users/friends/arjun_private.json +6 -0
  124. data/spec/fake_json/users/friends/arjun_public.json +6 -0
  125. data/spec/fake_json/users/friends/me_private.json +524 -0
  126. data/spec/fake_json/users/friends/me_public.json +6 -0
  127. data/spec/fake_json/users/groups/matake_private.json +48 -0
  128. data/spec/fake_json/users/groups/matake_public.json +6 -0
  129. data/spec/fake_json/users/home/arjun_private.json +6 -0
  130. data/spec/fake_json/users/home/arjun_public.json +6 -0
  131. data/spec/fake_json/users/home/me_private.json +460 -0
  132. data/spec/fake_json/users/home/me_private_next.json +382 -0
  133. data/spec/fake_json/users/home/me_private_previous.json +36 -0
  134. data/spec/fake_json/users/home/me_public.json +6 -0
  135. data/spec/fake_json/users/interests/matake_private.json +14 -0
  136. data/spec/fake_json/users/likes/arjun_private.json +1394 -0
  137. data/spec/fake_json/users/likes/arjun_public.json +6 -0
  138. data/spec/fake_json/users/links/matake_private.json +221 -0
  139. data/spec/fake_json/users/links/post_with_valid_access_token.json +1 -0
  140. data/spec/fake_json/users/me_private.json +128 -0
  141. data/spec/fake_json/users/me_public.json +6 -0
  142. data/spec/fake_json/users/movies/matake_private.json +9 -0
  143. data/spec/fake_json/users/music/matake_private.json +34 -0
  144. data/spec/fake_json/users/notes/matake_private.json +308 -0
  145. data/spec/fake_json/users/posts/arjun_private.json +386 -0
  146. data/spec/fake_json/users/posts/arjun_public.json +386 -0
  147. data/spec/fake_json/users/statuses/arjun_private.json +233 -0
  148. data/spec/fake_json/users/statuses/arjun_public.json +6 -0
  149. data/spec/fake_json/users/tagged/arjun_private.json +308 -0
  150. data/spec/fake_json/users/tagged/arjun_public.json +308 -0
  151. data/spec/fake_json/users/television/matake_private.json +9 -0
  152. data/spec/fake_json/users/videos/kirk_private.json +41 -0
  153. data/spec/fb_graph/album_spec.rb +61 -0
  154. data/spec/fb_graph/application_spec.rb +23 -0
  155. data/spec/fb_graph/auth_spec.rb +35 -0
  156. data/spec/fb_graph/checkin_spec.rb +29 -0
  157. data/spec/fb_graph/collection_spec.rb +45 -0
  158. data/spec/fb_graph/comment_spec.rb +31 -0
  159. data/spec/fb_graph/connection_spec.rb +38 -0
  160. data/spec/fb_graph/connections/accounts_spec.rb +47 -0
  161. data/spec/fb_graph/connections/activities_spec.rb +34 -0
  162. data/spec/fb_graph/connections/albums_spec.rb +60 -0
  163. data/spec/fb_graph/connections/attending_spec.rb +24 -0
  164. data/spec/fb_graph/connections/books_spec.rb +33 -0
  165. data/spec/fb_graph/connections/checkins_spec.rb +109 -0
  166. data/spec/fb_graph/connections/comments_spec.rb +85 -0
  167. data/spec/fb_graph/connections/declined_spec.rb +24 -0
  168. data/spec/fb_graph/connections/events_spec.rb +53 -0
  169. data/spec/fb_graph/connections/feed_spec.rb +102 -0
  170. data/spec/fb_graph/connections/friends_spec.rb +50 -0
  171. data/spec/fb_graph/connections/groups_spec.rb +32 -0
  172. data/spec/fb_graph/connections/home_spec.rb +58 -0
  173. data/spec/fb_graph/connections/interests_spec.rb +14 -0
  174. data/spec/fb_graph/connections/invited_spec.rb +14 -0
  175. data/spec/fb_graph/connections/likes_spec.rb +33 -0
  176. data/spec/fb_graph/connections/links_spec.rb +33 -0
  177. data/spec/fb_graph/connections/maybe_spec.rb +24 -0
  178. data/spec/fb_graph/connections/members_spec.rb +14 -0
  179. data/spec/fb_graph/connections/movies_spec.rb +14 -0
  180. data/spec/fb_graph/connections/music_spec.rb +14 -0
  181. data/spec/fb_graph/connections/noreply_spec.rb +14 -0
  182. data/spec/fb_graph/connections/notes_spec.rb +32 -0
  183. data/spec/fb_graph/connections/photos_spec.rb +30 -0
  184. data/spec/fb_graph/connections/picture_spec.rb +29 -0
  185. data/spec/fb_graph/connections/posts_spec.rb +35 -0
  186. data/spec/fb_graph/connections/statuses_spec.rb +74 -0
  187. data/spec/fb_graph/connections/subscriptions_spec.rb +18 -0
  188. data/spec/fb_graph/connections/tagged_spec.rb +42 -0
  189. data/spec/fb_graph/connections/television_spec.rb +14 -0
  190. data/spec/fb_graph/connections/videos_spec.rb +14 -0
  191. data/spec/fb_graph/education_spec.rb +61 -0
  192. data/spec/fb_graph/event_spec.rb +50 -0
  193. data/spec/fb_graph/group_spec.rb +46 -0
  194. data/spec/fb_graph/insight_spec.rb +17 -0
  195. data/spec/fb_graph/link_spec.rb +43 -0
  196. data/spec/fb_graph/node_spec.rb +13 -0
  197. data/spec/fb_graph/note_spec.rb +37 -0
  198. data/spec/fb_graph/page_spec.rb +56 -0
  199. data/spec/fb_graph/photo_spec.rb +60 -0
  200. data/spec/fb_graph/post_spec.rb +71 -0
  201. data/spec/fb_graph/status_spec.rb +31 -0
  202. data/spec/fb_graph/subscription_spec.rb +5 -0
  203. data/spec/fb_graph/tag_spec.rb +20 -0
  204. data/spec/fb_graph/user_spec.rb +139 -0
  205. data/spec/fb_graph/venue_spec.rb +23 -0
  206. data/spec/fb_graph/video_spec.rb +37 -0
  207. data/spec/fb_graph/work_spec.rb +67 -0
  208. data/spec/helpers/fake_json_helper.rb +11 -0
  209. data/spec/spec.opts +3 -0
  210. data/spec/spec_helper.rb +18 -0
  211. metadata +414 -0
@@ -0,0 +1,21 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Notes
4
+ def notes(options = {})
5
+ notes = self.connection(:notes, options)
6
+ notes.map! do |note|
7
+ Note.new(note.delete(:id), note.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+
13
+ def note!(options = {})
14
+ note = post(options.merge(:connection => 'notes'))
15
+ Note.new(note.delete(:id), options.merge(note).merge(
16
+ :access_token => options[:access_token] || self.access_token
17
+ ))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Photos
4
+ def photos(options = {})
5
+ photos = self.connection(:photos, options)
6
+ photos.map! do |photo|
7
+ Photo.new(photo.delete(:id), photo.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+
13
+ def photo!(options = {})
14
+ photo = post(options.merge(:connection => 'photos'))
15
+ Photo.new(photo.delete(:id), options.merge(photo).merge(
16
+ :access_token => options[:access_token] || self.access_token
17
+ ))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Picture
4
+ def picture(size = nil)
5
+ _endpoint_ = "#{self.endpoint}/picture"
6
+ if size
7
+ "#{_endpoint_}?type=#{size}"
8
+ else
9
+ _endpoint_
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Posts
4
+ def posts(options = {})
5
+ posts = self.connection(:posts, options)
6
+ posts.map! do |post|
7
+ Post.new(post.delete(:id), post.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Statuses
4
+ def statuses(options = {})
5
+ statuses = self.connection(:statuses, options)
6
+ statuses.map! do |status|
7
+ Status.new(status.delete(:id), status.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,53 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Subscriptions
4
+ # == Fetch current subscriptions
5
+ #
6
+ # app = FbGraph::Application.new(APP_ID, :secret => APP_SECRET)
7
+ # app.subscriptions
8
+ # => Array of FbGraph::Subscriptions
9
+ def subscriptions(options = {})
10
+ options[:access_token] ||= self.access_token || get_access_token(options[:secret])
11
+ subscriptions = self.connection(:subscriptions, options)
12
+ subscriptions.map! do |subscription|
13
+ Subscription.new(subscription.merge(:access_token => options[:access_token]))
14
+ end
15
+ end
16
+
17
+ # == Subscribe
18
+ #
19
+ # Subscriber have to setup PubSubHubbub subscriber endpoint.
20
+ # See details at "Subscription Verification" in the Facebook API document.
21
+ # ref) http://developers.facebook.com/docs/api/realtime
22
+ #
23
+ # app = FbGraph::Application.new(APP_ID, :secret => APP_SECRET)
24
+ # app.subscribe!(
25
+ # :object => "user",
26
+ # :fields => "name,email",
27
+ # :callback_url => "http://fbgraphsample.heroku.com/subscription",
28
+ # :verify_token => "Define by yourself"
29
+ # )
30
+ # => Array of FbGraph::Subscriptions
31
+ def subscribe!(options = {})
32
+ options[:access_token] ||= self.access_token || get_access_token(options[:secret])
33
+ post(options.merge(:connection => 'subscriptions'))
34
+ end
35
+
36
+ # == Subscribe
37
+ #
38
+ # Delete all of your subscriptions.
39
+ # If you specify an object parameter, it will only delete the corresponding subscription.
40
+ # ref) http://developers.facebook.com/docs/api/realtime
41
+ #
42
+ # app = FbGraph::Application.new(APP_ID, :secret => APP_SECRET)
43
+ # app.unsubscribe!(
44
+ # :object => "user"
45
+ # )
46
+ # => Array of FbGraph::Subscriptions
47
+ def unsubscribe!(options = {})
48
+ options[:access_token] ||= self.access_token || get_access_token(options[:secret])
49
+ destroy(options.merge(:connection => 'subscriptions'))
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Tagged
4
+ def tagged(options = {})
5
+ posts = self.connection(:tagged, options)
6
+ posts.map! do |post|
7
+ Post.new(post.delete(:id), post.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Television
4
+ def television(options = {})
5
+ television = self.connection(:television, options)
6
+ television.map! do |_television_|
7
+ Page.new(_television_.delete(:id), _television_.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ module Connections
3
+ module Videos
4
+ def videos(options = {})
5
+ videos = self.connection(:videos, options)
6
+ videos.map! do |video|
7
+ Video.new(video.delete(:id), video.merge(
8
+ :access_token => options[:access_token] || self.access_token
9
+ ))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ module FbGraph
2
+ class Education
3
+ include Comparison
4
+
5
+ attr_accessor :school, :degree, :year, :concentration
6
+
7
+ def initialize(attributes = {})
8
+ if (school = attributes[:school])
9
+ @school = FbGraph::Page.new(school.delete(:id), school)
10
+ end
11
+ if (degree = attributes[:degree])
12
+ @degree = FbGraph::Page.new(degree.delete(:id), degree)
13
+ end
14
+ if (year = attributes[:year])
15
+ @year = FbGraph::Page.new(year.delete(:id), year)
16
+ end
17
+ @concentration = []
18
+ if attributes[:concentration]
19
+ attributes[:concentration].each do |concentration|
20
+ @concentration << FbGraph::Page.new(concentration.delete(:id), concentration)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ module FbGraph
2
+ class Event < Node
3
+ include Connections::Feed
4
+ include Connections::Noreply
5
+ include Connections::Maybe
6
+ include Connections::Invited
7
+ include Connections::Attending
8
+ include Connections::Declined
9
+ include Connections::Picture
10
+ extend Searchable
11
+
12
+ attr_accessor :owner, :name, :description, :start_time, :end_time, :location, :venue, :privacy, :updated_time
13
+
14
+ def initialize(identifier, attributes = {})
15
+ super
16
+ if (owner = attributes[:owner])
17
+ @owner = FbGraph::User.new(owner.delete(:id), owner)
18
+ end
19
+ @name = attributes[:name]
20
+ @description = attributes[:description]
21
+ @location = attributes[:location]
22
+ @privacy = attributes[:privacy]
23
+ if (start_time = attributes[:start_time])
24
+ @start_time = case start_time
25
+ when String
26
+ Time.parse(start_time).utc
27
+ when Fixnum
28
+ Time.at(start_time).utc
29
+ end
30
+ end
31
+ if (end_time = attributes[:end_time])
32
+ @end_time = case end_time
33
+ when String
34
+ Time.parse(end_time).utc
35
+ when Fixnum
36
+ Time.at(end_time).utc
37
+ end
38
+ end
39
+ if attributes[:venue]
40
+ @venue = FbGraph::Venue.new(attributes[:venue])
41
+ end
42
+ if attributes[:updated_time]
43
+ @updated_time = Time.parse(attributes[:updated_time]).utc
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ module FbGraph
2
+ class Group < Node
3
+ include Connections::Feed
4
+ include Connections::Members
5
+ include Connections::Picture
6
+ include Searchable
7
+
8
+ attr_accessor :owner, :name, :description, :link, :venue, :privacy, :updated_time
9
+
10
+ def initialize(identifier, attributes = {})
11
+ super
12
+ if (owner = attributes[:owner])
13
+ @owner = FbGraph::User.new(owner.delete(:id), owner)
14
+ end
15
+ @name = attributes[:name]
16
+ @description = attributes[:description]
17
+ @link = attributes[:link]
18
+ @privacy = attributes[:privacy]
19
+ if attributes[:venue]
20
+ @venue = FbGraph::Venue.new(attributes[:venue])
21
+ end
22
+ if attributes[:updated_time]
23
+ @updated_time = Time.parse(attributes[:updated_time]).utc
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ module FbGraph
2
+ class Insight
3
+ include FbGraph::Comparison
4
+
5
+ attr_accessor :name, :period, :values, :description
6
+
7
+ def initialize(attributes = {})
8
+ @name = attributes[:name]
9
+ @period = attributes[:period]
10
+ @values = attributes[:values].collect(&:with_indifferent_access)
11
+ @description = attributes[:description]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module FbGraph
2
+ class Link < Node
3
+ include Connections::Comments
4
+
5
+ attr_accessor :from, :link, :name, :caption, :description, :icon, :picture, :message, :created_time
6
+
7
+ def initialize(identifier, attributes = {})
8
+ super
9
+ if (from = attributes[:from])
10
+ @from = if from[:category]
11
+ FbGraph::Page.new(from.delete(:id), from)
12
+ else
13
+ FbGraph::User.new(from.delete(:id), from)
14
+ end
15
+ end
16
+ @name = attributes[:name]
17
+ @link = attributes[:link]
18
+ @caption = attributes[:caption]
19
+ @description = attributes[:description]
20
+ @icon = attributes[:icon]
21
+ @picture = attributes[:picture] # NOTE: this is external image, so isn't connection.
22
+ @message = attributes[:message]
23
+ if attributes[:created_time]
24
+ @created_time = Time.parse(attributes[:created_time]).utc
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,121 @@
1
+ module FbGraph
2
+ class Node
3
+ include Comparison
4
+
5
+ attr_accessor :identifier, :endpoint, :access_token
6
+
7
+ def initialize(identifier, options = {})
8
+ @identifier = identifier
9
+ @endpoint = File.join(FbGraph::ROOT_URL, identifier.to_s)
10
+ @access_token = options[:access_token]
11
+ end
12
+
13
+ def fetch(options = {})
14
+ options[:access_token] ||= self.access_token if self.access_token
15
+ _fetched_ = get(options)
16
+ self.class.new(_fetched_.delete(:id), _fetched_.merge(:access_token => options[:access_token]))
17
+ end
18
+
19
+ def self.fetch(identifier, options = {})
20
+ new(identifier).fetch(options)
21
+ end
22
+
23
+ def connection(connection, options = {})
24
+ collection = FbGraph::Collection.new(get(options.merge(:connection => connection)))
25
+ Connection.new(self, connection, options.merge(:collection => collection))
26
+ end
27
+
28
+ def destroy(options = {})
29
+ options[:access_token] ||= self.access_token if self.access_token
30
+ delete(options)
31
+ end
32
+
33
+ protected
34
+
35
+ def get(params = {})
36
+ _params_ = stringfy_access_token(params)
37
+ _endpoint_ = build_endpoint(_params_.merge!(:method => :get))
38
+ handle_response do
39
+ RestClient.get(_endpoint_)
40
+ end
41
+ end
42
+
43
+ def post(params = {})
44
+ _params_ = stringfy_access_token(params)
45
+ _endpoint_ = build_endpoint(_params_.merge!(:method => :post))
46
+ handle_response do
47
+ RestClient.post(_endpoint_, _params_)
48
+ end
49
+ end
50
+
51
+ def delete(params = {})
52
+ _params_ = stringfy_access_token(params)
53
+ _endpoint_ = build_endpoint(_params_.merge!(:method => :delete))
54
+ handle_response do
55
+ # NOTE:
56
+ # DELETE method didn't work for some reason.
57
+ # Use POST with "method=delete" for now.
58
+ RestClient.post(_endpoint_, _params_.merge!(:method => :delete))
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def build_endpoint(params = {})
65
+ _endpoint_ = if params[:connection]
66
+ File.join(self.endpoint, params.delete(:connection).to_s)
67
+ else
68
+ params[:endpoint] || self.endpoint
69
+ end
70
+
71
+ params.delete_if do |k, v|
72
+ v.blank?
73
+ end
74
+ if params.delete(:method) == :get && params.present?
75
+ _endpoint_ << "?#{params.to_query}"
76
+ end
77
+ _endpoint_
78
+ end
79
+
80
+ def stringfy_access_token(params)
81
+ _params_ = params.dup
82
+ _params_[:access_token] ||= self.access_token
83
+ if access_token.is_a?(OAuth2::AccessToken)
84
+ _params_[:access_token] = _params_[:access_token].token
85
+ end
86
+ _params_
87
+ end
88
+
89
+ def handle_response
90
+ response = yield
91
+ case response.body
92
+ when 'true'
93
+ true
94
+ when 'false'
95
+ # NOTE: When the object is not found, Graph API returns
96
+ # - error response (JSON) when the identifier contains alphabet (ex. graph.facebook.com/iamnotfound)
97
+ # - false when the identifier is only integer + underbar (ex. graph.facebook.com/1234567890, graph.facebook.com/12345_67890)
98
+ # This is an undocumented behaviour, so facebook might chaange it without any announcement.
99
+ # I've posted this issue on their forum, so hopefully I'll get a document about Graph API error responses.
100
+ # ref) http://forum.developers.facebook.com/viewtopic.php?pid=228256#p228256
101
+ raise FbGraph::NotFound.new('Graph API returned false, so probably it means your requested object is not found.')
102
+ when 'null'
103
+ nil
104
+ else
105
+ _response_ = JSON.parse(response.body).with_indifferent_access
106
+ if _response_[:error]
107
+ case _response_[:error][:type]
108
+ when 'OAuthAccessTokenException', 'QueryParseException', 'OAuthInvalidRequestException', 'OAuthInvalidTokenException'
109
+ raise FbGraph::Unauthorized.new(_response_[:error][:message])
110
+ else
111
+ raise FbGraph::BadRequest.new("#{_response_[:error][:type]} :: #{_response_[:error][:message]}")
112
+ end
113
+ else
114
+ _response_
115
+ end
116
+ end
117
+ rescue RestClient::Exception => e
118
+ raise FbGraph::Exception.new(e.http_code, e.message, e.http_body)
119
+ end
120
+ end
121
+ end