koala 0.10.0 → 1.0.0.beta2

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 (34) hide show
  1. data/CHANGELOG +33 -7
  2. data/Manifest +8 -1
  3. data/Rakefile +4 -4
  4. data/koala.gemspec +10 -8
  5. data/lib/koala/graph_api.rb +188 -123
  6. data/lib/koala/http_services.rb +93 -17
  7. data/lib/koala/rest_api.rb +73 -6
  8. data/lib/koala/test_users.rb +18 -5
  9. data/lib/koala/uploadable_io.rb +115 -0
  10. data/lib/koala.rb +95 -72
  11. data/readme.md +18 -12
  12. data/spec/facebook_data.yml +9 -3
  13. data/spec/koala/assets/beach.jpg +0 -0
  14. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +5 -1
  15. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +8 -3
  16. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +12 -61
  17. data/spec/koala/graph_api/graph_api_tests.rb +85 -0
  18. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +167 -123
  19. data/spec/koala/http_services/http_service_tests.rb +51 -0
  20. data/spec/koala/http_services/net_http_service_tests.rb +339 -0
  21. data/spec/koala/http_services/typhoeus_service_tests.rb +162 -0
  22. data/spec/koala/live_testing_data_helper.rb +1 -1
  23. data/spec/koala/oauth/oauth_tests.rb +35 -64
  24. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +5 -74
  25. data/spec/koala/rest_api/rest_api_tests.rb +118 -0
  26. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +5 -3
  27. data/spec/koala/test_users/test_users_tests.rb +49 -48
  28. data/spec/koala/uploadable_io/uploadable_io_tests.rb +246 -0
  29. data/spec/koala_spec_helper.rb +30 -4
  30. data/spec/koala_spec_without_mocks.rb +3 -3
  31. data/spec/mock_facebook_responses.yml +41 -18
  32. data/spec/mock_http_service.rb +16 -3
  33. metadata +38 -8
  34. data/spec/koala/net_http_service_tests.rb +0 -186
data/CHANGELOG CHANGED
@@ -1,3 +1,29 @@
1
+ v1.0
2
+ New methods:
3
+ -- Photo and file upload now supported through #put_picture
4
+ -- Beta 2: support for Rails 3, Sinatra, File objects, and paths (see documentation)
5
+ -- Beta 2: Added UploadableIO class to manage file uploads
6
+ -- Added a delete_like method (thanks to wassem)
7
+ -- Beta 2: Added put_connection and delete_connection convenience methods
8
+ Updated methods:
9
+ -- Beta 2: TestUser#befriend now requires user info hashes (id and access token) due to Facebook API changes (thanks to pulsd/kbighorse)
10
+ -- Beta 2: all methods now accept an http_options hash as their optional last parameter
11
+ -- Beta 2: You can now pass :beta => true in the http options to use Facebook's beta tier.
12
+ Internal improvements:
13
+ -- For public requests, Koala now uses http by default (instead of https) to improve speed
14
+ -- This can be overridden through Koala.always_use_ssl= or by passing :use_ssl => true in the options hash for an api call
15
+ -- Read-only REST API requests now go through the faster api-read server
16
+ -- Replaced parse_signed_request with a version from Facebook that also supports the new signed params proposal
17
+ -- Note: invalid requests will now raise exceptions rather than return nil, in keeping with other SDKs
18
+ -- delete_object will now raise an error if there's no access token (like put_object and delete_like)
19
+ -- Beta 2: photo and file uploads now accept symbols as well as strings overall
20
+ -- Beta 2: APIError is now < StandardError, not Exception
21
+ -- Beta 2: Added KoalaError for non-API errors
22
+ Test improvements:
23
+ -- Expanded HTTP service tests (added Typhoeus test suite and additional Net::HTTP test cases)
24
+ -- Live tests now verify that the access token has the necessary permissions before starting
25
+ -- Replaced the 50-person network test, which often took 15+ minutes to run live, with a 5-person test
26
+
1
27
  v0.10.0
2
28
  -- Added test user module
3
29
  -- Fixed bug when raising APIError after Facebook fails to exchange session keys
@@ -11,10 +37,10 @@ v0.9.1
11
37
  v0.9.0
12
38
  -- Added parse_signed_request to handle Facebook's new authentication scheme
13
39
  -- note: creates dependency on OpenSSL (OpenSSL::HMAC) for decryption
14
- -- Added GraphCollection class to provide paging support for GraphAPI get_connections and search methods (thanks to jagthedrummer)
40
+ -- Added GraphCollection class to provide paging support for GraphAPI get_connections and search methods (thanks to jagthedrummer)
15
41
  -- Added get_page method to easily fetch pages of results from GraphCollections
16
42
  -- Exchanging sessions for tokens now works properly when provided invalid/expired session keys
17
- -- You can now include a :typhoeus_options key in TyphoeusService#make_request's options hash to control the Typhoeus call (for example, to set :disable_ssl_peer_verification => true)
43
+ -- You can now include a :typhoeus_options key in TyphoeusService#make_request's options hash to control the Typhoeus call (for example, to set :disable_ssl_peer_verification => true)
18
44
  -- All paths provided to HTTP services start with leading / to improve compatibility with stubbing libraries
19
45
  -- If Facebook returns nil for search or get_connections requests, Koala now returns nil rather than throwing an exception
20
46
 
@@ -23,7 +49,7 @@ v0.8.0
23
49
  -- Removed string overloading for the methods, per 0.7.3, which caused Marshaling issues
24
50
  -- Removed ability to provide a string as the second argument to url_for_access_token, per 0.5.0
25
51
 
26
- v0.7.4
52
+ v0.7.4
27
53
  -- Fixed bug with get_user_from_cookies
28
54
 
29
55
  v0.7.3
@@ -34,18 +60,18 @@ v0.7.3
34
60
  -- Using those methods triggers a deprecation warning
35
61
  -- This will be removed by 1.0
36
62
  -- There are new info methods (get_access_token_info, get_app_access_token_info, get_token_info_from_session_keys, and get_user_info_from_cookies) that natively return hashes, for when you want the expiration date
37
- -- Responses with HTTP status 500+ now properly throw errors under Net::HTTP
63
+ -- Responses with HTTP status 500+ now properly throw errors under Net::HTTP
38
64
  -- Updated changelog
39
65
  -- Added license
40
66
 
41
67
  v0.7.2
42
68
  -- Added support for exchanging session keys for OAuth access tokens (get_token_from_session_key for single keys, get_tokens_from_session_keys for multiple)
43
69
  -- Moved Koala files into a koala/ subdirectory to minimize risk of name collisions
44
- -- Added OAuth Playground git submodule as an example
70
+ -- Added OAuth Playground git submodule as an example
45
71
  -- Updated tests, readme, and changelog
46
72
 
47
73
  v0.7.1
48
- -- Updated RealtimeUpdates#list_subscriptions and GraphAPI#get_connections to now return an
74
+ -- Updated RealtimeUpdates#list_subscriptions and GraphAPI#get_connections to now return an
49
75
  array of results directly (rather than a hash with one key)
50
76
  -- Fixed a bug with Net::HTTP-based HTTP service in which the headers hash was improperly formatted
51
77
  -- Updated readme
@@ -92,7 +118,7 @@ v0.3.1
92
118
  v0.3
93
119
  -- Renamed Graph API class from Facebook::GraphAPI to FacebookGraph::API
94
120
  -- Created FacebookGraph::OAuth class for tokens and OAuth URLs
95
- -- Updated method for including HTTP service (think we've got it this time)
121
+ -- Updated method for including HTTP service (think we've got it this time)
96
122
  -- Updated tests
97
123
  -- Added CHANGELOG and gemspec
98
124
 
data/Manifest CHANGED
@@ -10,21 +10,28 @@ lib/koala/http_services.rb
10
10
  lib/koala/realtime_updates.rb
11
11
  lib/koala/rest_api.rb
12
12
  lib/koala/test_users.rb
13
+ lib/koala/uploadable_io.rb
13
14
  readme.md
14
15
  spec/facebook_data.yml
15
16
  spec/koala/api_base_tests.rb
17
+ spec/koala/assets/beach.jpg
16
18
  spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb
17
19
  spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb
18
20
  spec/koala/graph_api/graph_api_no_access_token_tests.rb
21
+ spec/koala/graph_api/graph_api_tests.rb
19
22
  spec/koala/graph_api/graph_api_with_access_token_tests.rb
20
23
  spec/koala/graph_api/graph_collection_tests.rb
24
+ spec/koala/http_services/http_service_tests.rb
25
+ spec/koala/http_services/net_http_service_tests.rb
26
+ spec/koala/http_services/typhoeus_service_tests.rb
21
27
  spec/koala/live_testing_data_helper.rb
22
- spec/koala/net_http_service_tests.rb
23
28
  spec/koala/oauth/oauth_tests.rb
24
29
  spec/koala/realtime_updates/realtime_updates_tests.rb
25
30
  spec/koala/rest_api/rest_api_no_access_token_tests.rb
31
+ spec/koala/rest_api/rest_api_tests.rb
26
32
  spec/koala/rest_api/rest_api_with_access_token_tests.rb
27
33
  spec/koala/test_users/test_users_tests.rb
34
+ spec/koala/uploadable_io/uploadable_io_tests.rb
28
35
  spec/koala_spec.rb
29
36
  spec/koala_spec_helper.rb
30
37
  spec/koala_spec_without_mocks.rb
data/Rakefile CHANGED
@@ -4,14 +4,14 @@ require 'rake'
4
4
  require 'echoe'
5
5
 
6
6
  # gem management
7
- Echoe.new('koala', '0.10.0') do |p|
8
- p.summary = "A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation."
9
- p.description = "Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services."
7
+ Echoe.new('koala', '1.0.0.beta2') do |p|
8
+ p.summary = "A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation."
9
+ p.description = "Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services."
10
10
  p.url = "http://github.com/arsduo/koala"
11
11
  p.author = ["Alex Koppel", "Chris Baclig", "Rafi Jacoby", "Context Optional"]
12
12
  p.email = "alex@alexkoppel.com"
13
13
  p.ignore_pattern = ["tmp/*", "script/*", "pkg/*"]
14
- p.runtime_dependencies = ["json >=1.0"]
14
+ p.runtime_dependencies = ["json >=1.0", "multipart-post >=1.0"]
15
15
  p.development_dependencies = []
16
16
  p.retain_gemspec = true
17
17
  end
data/koala.gemspec CHANGED
@@ -2,32 +2,34 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{koala}
5
- s.version = "0.10.0"
5
+ s.version = "1.0.0.beta2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional"]
9
- s.date = %q{2010-12-15}
9
+ s.date = %q{2011-03-10}
10
10
  s.description = %q{Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services.}
11
11
  s.email = %q{alex@alexkoppel.com}
12
- s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "lib/koala/test_users.rb"]
13
- s.files = ["CHANGELOG", "LICENSE", "Manifest", "Rakefile", "init.rb", "koala.gemspec", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "lib/koala/test_users.rb", "readme.md", "spec/facebook_data.yml", "spec/koala/api_base_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb", "spec/koala/graph_api/graph_api_no_access_token_tests.rb", "spec/koala/graph_api/graph_api_with_access_token_tests.rb", "spec/koala/graph_api/graph_collection_tests.rb", "spec/koala/live_testing_data_helper.rb", "spec/koala/net_http_service_tests.rb", "spec/koala/oauth/oauth_tests.rb", "spec/koala/realtime_updates/realtime_updates_tests.rb", "spec/koala/rest_api/rest_api_no_access_token_tests.rb", "spec/koala/rest_api/rest_api_with_access_token_tests.rb", "spec/koala/test_users/test_users_tests.rb", "spec/koala_spec.rb", "spec/koala_spec_helper.rb", "spec/koala_spec_without_mocks.rb", "spec/mock_facebook_responses.yml", "spec/mock_http_service.rb"]
12
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "lib/koala/test_users.rb", "lib/koala/uploadable_io.rb"]
13
+ s.files = ["CHANGELOG", "LICENSE", "Manifest", "Rakefile", "init.rb", "koala.gemspec", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "lib/koala/test_users.rb", "lib/koala/uploadable_io.rb", "readme.md", "spec/facebook_data.yml", "spec/koala/api_base_tests.rb", "spec/koala/assets/beach.jpg", "spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb", "spec/koala/graph_api/graph_api_no_access_token_tests.rb", "spec/koala/graph_api/graph_api_tests.rb", "spec/koala/graph_api/graph_api_with_access_token_tests.rb", "spec/koala/graph_api/graph_collection_tests.rb", "spec/koala/http_services/http_service_tests.rb", "spec/koala/http_services/net_http_service_tests.rb", "spec/koala/http_services/typhoeus_service_tests.rb", "spec/koala/live_testing_data_helper.rb", "spec/koala/oauth/oauth_tests.rb", "spec/koala/realtime_updates/realtime_updates_tests.rb", "spec/koala/rest_api/rest_api_no_access_token_tests.rb", "spec/koala/rest_api/rest_api_tests.rb", "spec/koala/rest_api/rest_api_with_access_token_tests.rb", "spec/koala/test_users/test_users_tests.rb", "spec/koala/uploadable_io/uploadable_io_tests.rb", "spec/koala_spec.rb", "spec/koala_spec_helper.rb", "spec/koala_spec_without_mocks.rb", "spec/mock_facebook_responses.yml", "spec/mock_http_service.rb"]
14
14
  s.homepage = %q{http://github.com/arsduo/koala}
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala", "--main", "readme.md"]
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{koala}
18
- s.rubygems_version = %q{1.3.6}
18
+ s.rubygems_version = %q{1.4.2}
19
19
  s.summary = %q{A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation.}
20
20
 
21
21
  if s.respond_to? :specification_version then
22
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
22
  s.specification_version = 3
24
23
 
25
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
25
  s.add_runtime_dependency(%q<json>, [">= 1.0"])
26
+ s.add_runtime_dependency(%q<multipart-post>, [">= 1.0"])
27
27
  else
28
28
  s.add_dependency(%q<json>, [">= 1.0"])
29
+ s.add_dependency(%q<multipart-post>, [">= 1.0"])
29
30
  end
30
31
  else
31
32
  s.add_dependency(%q<json>, [">= 1.0"])
33
+ s.add_dependency(%q<multipart-post>, [">= 1.0"])
32
34
  end
33
35
  end
@@ -1,65 +1,12 @@
1
1
  module Koala
2
2
  module Facebook
3
- GRAPH_SERVER = "graph.facebook.com"
4
-
5
- class GraphCollection < Array
6
- #This class is a light wrapper for collections returned
7
- #from the Graph API.
8
- #
9
- #It extends Array to allow direct access to the data colleciton
10
- #which should allow it to drop in seamlessly.
11
- #
12
- #It also allows access to paging information and the
13
- #ability to get the next/previous page in the collection
14
- #by calling next_page or previous_page.
15
- attr_reader :paging
16
- attr_reader :api
17
-
18
- def initialize(response, api)
19
- super response["data"]
20
- @paging = response["paging"]
21
- @api = api
22
- end
23
-
24
- # defines methods for NEXT and PREVIOUS pages
25
- %w{next previous}.each do |this|
26
-
27
- # def next_page
28
- # def previous_page
29
- define_method "#{this.to_sym}_page" do
30
- base, args = send("#{this}_page_params")
31
- base ? @api.get_page([base, args]) : nil
32
- end
33
-
34
- # def next_page_params
35
- # def previous_page_params
36
- define_method "#{this.to_sym}_page_params" do
37
- return nil unless @paging and @paging[this]
38
- parse_page_url(@paging[this])
39
- end
40
- end
41
-
42
- def parse_page_url(url)
43
- match = url.match(/.com\/(.*)\?(.*)/)
44
- base = match[1]
45
- args = match[2]
46
- params = CGI.parse(args)
47
- new_params = {}
48
- params.each_pair do |key,value|
49
- new_params[key] = value.join ","
50
- end
51
- [base,new_params]
52
- end
53
-
54
- end
55
-
56
-
3
+ GRAPH_SERVER = "graph.facebook.com"
57
4
 
58
5
  module GraphAPIMethods
59
6
  # A client for the Facebook Graph API.
60
7
  #
61
- # See http://developers.facebook.com/docs/api for complete documentation
62
- # for the API.
8
+ # See http://github.com/arsduo/koala for Ruby/Koala documentation
9
+ # and http://developers.facebook.com/docs/api for Facebook API documentation
63
10
  #
64
11
  # The Graph API is made up of the objects in Facebook (e.g., people, pages,
65
12
  # events, photos) and the connections between them (e.g., friends,
@@ -76,108 +23,164 @@ module Koala
76
23
  # by the API at http://developers.facebook.com/docs/reference/api/.
77
24
  #
78
25
  # You can obtain an access token via OAuth or by using the Facebook
79
- # JavaScript SDK. See http://developers.facebook.com/docs/authentication/
80
- # for details.
26
+ # JavaScript SDK. See the Koala and Facebook documentation for more information.
81
27
  #
82
28
  # If you are using the JavaScript SDK, you can use the
83
29
  # Koala::Facebook::OAuth.get_user_from_cookie() method below to get the OAuth access token
84
30
  # for the active user from the cookie saved by the SDK.
85
-
86
- def get_object(id, args = {})
31
+
32
+ # Objects
33
+
34
+ def get_object(id, args = {}, options = {})
87
35
  # Fetchs the given object from the graph.
88
- graph_call(id, args)
36
+ graph_call(id, args, "get", options)
89
37
  end
90
38
 
91
- def get_objects(ids, args = {})
39
+ def get_objects(ids, args = {}, options = {})
92
40
  # Fetchs all of the given object from the graph.
93
41
  # We return a map from ID to object. If any of the IDs are invalid,
94
42
  # we raise an exception.
95
- graph_call("", args.merge("ids" => ids.join(",")))
43
+ graph_call("", args.merge("ids" => ids.join(",")), "get", options)
96
44
  end
97
45
 
98
- def get_page(params)
99
- result = graph_call(*params)
100
- result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
101
- end
102
-
103
- def get_connections(id, connection_name, args = {})
104
- # Fetchs the connections for given object.
105
- result = graph_call("#{id}/#{connection_name}", args)
106
- result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
107
- end
108
-
109
-
110
- def get_picture(object, args = {})
111
- result = graph_call("#{object}/picture", args, "get", :http_component => :headers)
112
- result["Location"]
113
- end
114
-
115
- def put_object(parent_object, connection_name, args = {})
46
+ def put_object(parent_object, connection_name, args = {}, options = {})
116
47
  # Writes the given object to the graph, connected to the given parent.
117
- #
118
- # For example,
119
- #
120
- # graph.put_object("me", "feed", :message => "Hello, world")
121
- #
122
- # writes "Hello, world" to the active user's wall. Likewise, this
123
- # will comment on a the first post of the active user's feed:
124
- #
125
- # feed = graph.get_connections("me", "feed")
126
- # post = feed["data"][0]
127
- # graph.put_object(post["id"], "comments", :message => "First!")
128
- #
129
48
  # See http://developers.facebook.com/docs/api#publishing for all of
130
49
  # the supported writeable objects.
131
50
  #
51
+ # For example,
52
+ # graph.put_object("me", "feed", :message => "Hello, world")
53
+ # writes "Hello, world" to the active user's wall.
54
+ #
132
55
  # Most write operations require extended permissions. For example,
133
56
  # publishing wall posts requires the "publish_stream" permission. See
134
57
  # http://developers.facebook.com/docs/authentication/ for details about
135
58
  # extended permissions.
59
+
60
+ raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Write operations require an access token"}) unless @access_token
61
+ graph_call("#{parent_object}/#{connection_name}", args, "post", options)
62
+ end
63
+
64
+ def delete_object(id, options = {})
65
+ # Deletes the object with the given ID from the graph.
66
+ raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Delete requires an access token"}) unless @access_token
67
+ graph_call(id, {}, "delete", options)
68
+ end
69
+
70
+ # Connections
71
+
72
+ def get_connections(id, connection_name, args = {}, options = {})
73
+ # Fetchs the connections for given object.
74
+ result = graph_call("#{id}/#{connection_name}", args, "get", options)
75
+ result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
76
+ end
136
77
 
78
+ def put_connections(id, connection_name, args = {}, options = {})
79
+ # Posts a certain connection
137
80
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Write operations require an access token"}) unless @access_token
138
- graph_call("#{parent_object}/#{connection_name}", args, "post")
81
+ graph_call("#{id}/#{connection_name}", args, "post", options)
139
82
  end
83
+
84
+ def delete_connections(id, connection_name, args = {}, options = {})
85
+ # Deletes a given connection
86
+ raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Delete requires an access token"}) unless @access_token
87
+ graph_call("#{id}/#{connection_name}", args, "delete", options)
88
+ end
89
+
90
+ # Pictures
91
+ # to delete pictures, use delete_object(photo_id)
92
+ # note: you'll need the user_photos permission to actually access photos after uploading them
140
93
 
141
- def put_wall_post(message, attachment = {}, profile_id = "me")
142
- # Writes a wall post to the given profile's wall.
143
- #
144
- # We default to writing to the authenticated user's wall if no
145
- # profile_id is specified.
94
+ def get_picture(object, args = {}, options = {})
95
+ # Gets a picture object, returning the URL (which Facebook sends as a header)
96
+ result = graph_call("#{object}/picture", args, "get", options.merge(:http_component => :headers))
97
+ result["Location"]
98
+ end
99
+
100
+ def put_picture(*picture_args)
101
+ # Can be called in multiple ways:
102
+ #
103
+ # put_picture(file, [content_type], ...)
104
+ # put_picture(path_to_file, [content_type], ...)
105
+ #
106
+ # You can pass in uploaded files directly from Rails or Sinatra.
107
+ # (See lib/koala/uploadable_io.rb for supported frameworks)
108
+ #
109
+ # Optional parameters can be added to the end of the argument list:
110
+ # - args: a hash of request parameters (default: {})
111
+ # - target_id: ID of the target where to post the picture (default: "me")
112
+ # - options: a hash of http options passed to the HTTPService module
146
113
  #
147
- # attachment adds a structured attachment to the status message being
148
- # posted to the Wall. It should be a dictionary of the form:
114
+ # put_picture(file, content_type, {:message => "Message"}, 01234560)
115
+ # put_picture(params[:file], {:message => "Message"})
116
+
117
+ raise KoalaError.new("Wrong number of arguments for put_picture") unless picture_args.size.between?(1, 5)
118
+
119
+ args_offset = picture_args[1].kind_of?(Hash) || picture_args.size == 1 ? 0 : 1
120
+
121
+ args = picture_args[1 + args_offset] || {}
122
+ target_id = picture_args[2 + args_offset] || "me"
123
+ options = picture_args[3 + args_offset] || {}
124
+
125
+ args["source"] = Koala::UploadableIO.new(*picture_args.slice(0, 1 + args_offset))
126
+
127
+ self.put_object(target_id, "photos", args, options)
128
+ end
129
+
130
+ # Wall posts
131
+ # To get wall posts, use get_connections(user, "feed")
132
+ # To delete a wall post, just use delete_object(post_id)
133
+
134
+ def put_wall_post(message, attachment = {}, profile_id = "me", options = {})
135
+ # attachment is a hash describing the wall post
136
+ # (see X for more details)
137
+ # For instance,
149
138
  #
150
- # {"name": "Link name"
151
- # "link": "http://www.example.com/",
152
- # "caption": "{*actor*} posted a new review",
153
- # "description": "This is a longer description of the attachment",
154
- # "picture": "http://www.example.com/thumbnail.jpg"}
139
+ # {"name" => "Link name"
140
+ # "link" => "http://www.example.com/",
141
+ # "caption" => "{*actor*} posted a new review",
142
+ # "description" => "This is a longer description of the attachment",
143
+ # "picture" => "http://www.example.com/thumbnail.jpg"}
155
144
 
156
- self.put_object(profile_id, "feed", attachment.merge({:message => message}))
145
+ self.put_object(profile_id, "feed", attachment.merge({:message => message}), options)
157
146
  end
158
-
159
- def put_comment(object_id, message)
147
+
148
+ # Comments
149
+ # to delete comments, use delete_object(comment_id)
150
+ # to get comments, use get_connections(object, "likes")
151
+
152
+ def put_comment(object_id, message, options = {})
160
153
  # Writes the given comment on the given post.
161
- self.put_object(object_id, "comments", {:message => message})
154
+ self.put_object(object_id, "comments", {:message => message}, options)
162
155
  end
163
-
164
- def put_like(object_id)
156
+
157
+ # Likes
158
+ # to get likes, use get_connections(user, "likes")
159
+
160
+ def put_like(object_id, options = {})
165
161
  # Likes the given post.
166
- self.put_object(object_id, "likes")
162
+ self.put_object(object_id, "likes", {}, options)
167
163
  end
168
-
169
- def delete_object(id)
170
- # Deletes the object with the given ID from the graph.
171
- graph_call(id, {}, "delete")
164
+
165
+ def delete_like(object_id, options = {})
166
+ # Unlikes a given object for the logged-in user
167
+ raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Unliking requires an access token"}) unless @access_token
168
+ graph_call("#{object_id}/likes", {}, "delete", options)
172
169
  end
173
-
174
- def search(search_terms, args = {})
175
- # Searches for a given term
176
- result = graph_call("search", args.merge({:q => search_terms}))
170
+
171
+ # Search
172
+
173
+ def search(search_terms, args = {}, options = {})
174
+ # Searches for a given term among posts visible to the current user (or public posts if no token)
175
+ result = graph_call("search", args.merge(:q => search_terms), "get", options)
177
176
  result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
178
- end
177
+ end
178
+
179
+ # API access
179
180
 
180
181
  def graph_call(*args)
182
+ # Direct access to the Facebook API
183
+ # see any of the above methods for example invocations
181
184
  response = api(*args) do |response|
182
185
  # check for Graph API-specific errors
183
186
  if response.is_a?(Hash) && error_details = response["error"]
@@ -186,7 +189,69 @@ module Koala
186
189
  end
187
190
 
188
191
  response
192
+ end
193
+
194
+ # GraphCollection support
195
+
196
+ def get_page(params)
197
+ # Pages through a set of results stored in a GraphCollection
198
+ # Used for connections and search results
199
+ result = graph_call(*params)
200
+ result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
189
201
  end
202
+
203
+ end
204
+
205
+
206
+ class GraphCollection < Array
207
+ #This class is a light wrapper for collections returned
208
+ #from the Graph API.
209
+ #
210
+ #It extends Array to allow direct access to the data colleciton
211
+ #which should allow it to drop in seamlessly.
212
+ #
213
+ #It also allows access to paging information and the
214
+ #ability to get the next/previous page in the collection
215
+ #by calling next_page or previous_page.
216
+ attr_reader :paging
217
+ attr_reader :api
218
+
219
+ def initialize(response, api)
220
+ super response["data"]
221
+ @paging = response["paging"]
222
+ @api = api
223
+ end
224
+
225
+ # defines methods for NEXT and PREVIOUS pages
226
+ %w{next previous}.each do |this|
227
+
228
+ # def next_page
229
+ # def previous_page
230
+ define_method "#{this.to_sym}_page" do
231
+ base, args = send("#{this}_page_params")
232
+ base ? @api.get_page([base, args]) : nil
233
+ end
234
+
235
+ # def next_page_params
236
+ # def previous_page_params
237
+ define_method "#{this.to_sym}_page_params" do
238
+ return nil unless @paging and @paging[this]
239
+ parse_page_url(@paging[this])
240
+ end
241
+ end
242
+
243
+ def parse_page_url(url)
244
+ match = url.match(/.com\/(.*)\?(.*)/)
245
+ base = match[1]
246
+ args = match[2]
247
+ params = CGI.parse(args)
248
+ new_params = {}
249
+ params.each_pair do |key,value|
250
+ new_params[key] = value.join ","
251
+ end
252
+ [base,new_params]
253
+ end
254
+
190
255
  end
191
256
  end
192
- end
257
+ end