koala 1.0.0.beta → 1.0.0

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 (52) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +17 -3
  3. data/Gemfile +3 -0
  4. data/LICENSE +1 -1
  5. data/Manifest +5 -2
  6. data/Rakefile +13 -14
  7. data/koala.gemspec +35 -20
  8. data/lib/koala/graph_api.rb +183 -131
  9. data/lib/koala/http_services.rb +46 -55
  10. data/lib/koala/test_users.rb +21 -8
  11. data/lib/koala/uploadable_io.rb +115 -0
  12. data/lib/koala.rb +52 -84
  13. data/readme.md +19 -6
  14. data/spec/cases/api_base_spec.rb +101 -0
  15. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  16. data/spec/cases/graph_api_spec.rb +25 -0
  17. data/spec/cases/http_services/http_service_spec.rb +54 -0
  18. data/spec/cases/http_services/net_http_service_spec.rb +350 -0
  19. data/spec/cases/http_services/typhoeus_service_spec.rb +144 -0
  20. data/spec/cases/oauth_spec.rb +409 -0
  21. data/spec/cases/realtime_updates_spec.rb +184 -0
  22. data/spec/cases/rest_api_spec.rb +25 -0
  23. data/spec/{koala/test_users/test_users_tests.rb → cases/test_users_spec.rb} +46 -33
  24. data/spec/cases/uploadable_io_spec.rb +151 -0
  25. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +12 -14
  26. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +313 -306
  27. data/spec/spec_helper.rb +18 -0
  28. data/spec/support/graph_api_shared_examples.rb +424 -0
  29. data/spec/{koala → support}/live_testing_data_helper.rb +39 -42
  30. data/spec/{mock_http_service.rb → support/mock_http_service.rb} +94 -95
  31. data/spec/{koala/rest_api/rest_api_tests.rb → support/rest_api_shared_examples.rb} +43 -0
  32. data/spec/support/setup_mocks_or_live.rb +52 -0
  33. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  34. metadata +107 -48
  35. data/init.rb +0 -2
  36. data/spec/koala/api_base_tests.rb +0 -102
  37. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -14
  38. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -16
  39. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -63
  40. data/spec/koala/graph_api/graph_api_tests.rb +0 -86
  41. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -154
  42. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  43. data/spec/koala/net_http_service_tests.rb +0 -430
  44. data/spec/koala/oauth/oauth_tests.rb +0 -409
  45. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  46. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -25
  47. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -38
  48. data/spec/koala/typhoeus_service_tests.rb +0 -156
  49. data/spec/koala_spec.rb +0 -18
  50. data/spec/koala_spec_helper.rb +0 -70
  51. data/spec/koala_spec_without_mocks.rb +0 -19
  52. /data/spec/{koala/assets → fixtures}/beach.jpg +0 -0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ .project
3
+ Gemfile.lock
data/CHANGELOG CHANGED
@@ -1,15 +1,29 @@
1
1
  v1.0
2
2
  New methods:
3
- -- Photo and file upload now supported through #put_picture and general support for file hashes (see documentation)
4
- -- Added a delete_like method (thanks to wassem)
3
+ -- Photo and file upload now supported through #put_picture
4
+ -- Added UploadableIO class to manage file uploads
5
+ -- Added a delete_like method (thanks to waseem)
6
+ -- Added put_connection and delete_connection convenience methods
7
+ Updated methods:
8
+ -- Search can now search places, checkins, etc. (thanks, rickyc!)
9
+ -- You can now pass :beta => true in the http options to use Facebook's beta tier.
10
+ -- TestUser#befriend now requires user info hashes (id and access token) due to Facebook API changes (thanks, pulsd and kbighorse!)
11
+ -- All methods now accept an http_options hash as their optional last parameter (thanks, spiegela!)
12
+ -- url_for_oauth_code can now take a :display option (thanks, netbe!)
13
+ -- Net::HTTP can now accept :timeout and :proxy options (thanks, gilles!)
14
+ -- Test users now supports using test accounts across multiple apps
5
15
  Internal improvements:
6
16
  -- For public requests, Koala now uses http by default (instead of https) to improve speed
7
17
  -- This can be overridden through Koala.always_use_ssl= or by passing :use_ssl => true in the options hash for an api call
8
18
  -- Read-only REST API requests now go through the faster api-read server
9
19
  -- Replaced parse_signed_request with a version from Facebook that also supports the new signed params proposal
10
20
  -- Note: invalid requests will now raise exceptions rather than return nil, in keeping with other SDKs
11
- -- delete_object will now raise an error if there's no access token (like put_object and delete_like)
21
+ -- Delete methods will now raise an error if there's no access token (like put_object and delete_like)
22
+ -- Updated parse_signed_request to match Facebook's current implementation (thanks, imajes!)
23
+ -- APIError is now < StandardError, not Exception
24
+ -- Added KoalaError for non-API errors
12
25
  Test improvements:
26
+ -- Incorporated joshk's awesome rewrite of the entire Koala test suite (thanks, joshk!)
13
27
  -- Expanded HTTP service tests (added Typhoeus test suite and additional Net::HTTP test cases)
14
28
  -- Live tests now verify that the access token has the necessary permissions before starting
15
29
  -- Replaced the 50-person network test, which often took 15+ minutes to run live, with a 5-person test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2010 Alex Koppel
3
+ Copyright (c) 2010-2011 Alex Koppel
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/Manifest CHANGED
@@ -10,6 +10,7 @@ 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
@@ -20,15 +21,17 @@ spec/koala/graph_api/graph_api_no_access_token_tests.rb
20
21
  spec/koala/graph_api/graph_api_tests.rb
21
22
  spec/koala/graph_api/graph_api_with_access_token_tests.rb
22
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
23
27
  spec/koala/live_testing_data_helper.rb
24
- spec/koala/net_http_service_tests.rb
25
28
  spec/koala/oauth/oauth_tests.rb
26
29
  spec/koala/realtime_updates/realtime_updates_tests.rb
27
30
  spec/koala/rest_api/rest_api_no_access_token_tests.rb
28
31
  spec/koala/rest_api/rest_api_tests.rb
29
32
  spec/koala/rest_api/rest_api_with_access_token_tests.rb
30
33
  spec/koala/test_users/test_users_tests.rb
31
- spec/koala/typhoeus_service_tests.rb
34
+ spec/koala/uploadable_io/uploadable_io_tests.rb
32
35
  spec/koala_spec.rb
33
36
  spec/koala_spec_helper.rb
34
37
  spec/koala_spec_without_mocks.rb
data/Rakefile CHANGED
@@ -1,17 +1,16 @@
1
- # Rakefile
2
- require 'rubygems'
3
1
  require 'rake'
4
- require 'echoe'
5
2
 
6
- # gem management
7
- Echoe.new('koala', '1.0.0.beta') 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
- p.url = "http://github.com/arsduo/koala"
11
- p.author = ["Alex Koppel", "Chris Baclig", "Rafi Jacoby", "Context Optional"]
12
- p.email = "alex@alexkoppel.com"
13
- p.ignore_pattern = ["tmp/*", "script/*", "pkg/*"]
14
- p.runtime_dependencies = ["json >=1.0", "multipart-post >=1.0"]
15
- p.development_dependencies = []
16
- p.retain_gemspec = true
3
+ begin
4
+ require 'bundler/setup'
5
+ Bundler::GemHelper.install_tasks
6
+ rescue LoadError
7
+ puts 'although not required, bundler is recommened for running the tests'
8
+ end
9
+
10
+
11
+ task :default => :spec
12
+
13
+ require 'rspec/core/rake_task'
14
+ RSpec::Core::RakeTask.new do |t|
15
+ t.rspec_opts = ["--color", '--format doc']
17
16
  end
data/koala.gemspec CHANGED
@@ -1,35 +1,50 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.name = %q{koala}
5
- s.version = "1.0.0.beta"
4
+ s.name = %q{koala}
5
+ s.version = "1.0.0"
6
+ s.date = %q{2011-05-01}
7
+
8
+ s.summary = %q{A lightweight, flexible library for Facebook with support for the Graph API, the REST API, realtime updates, and OAuth authentication.}
9
+ s.description = %q{Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph and REST APIs, 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
+ s.homepage = %q{http://github.com/arsduo/koala}
6
11
 
7
- s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
12
  s.authors = ["Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional"]
9
- s.date = %q{2011-01-26}
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
- 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/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/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_tests.rb", "spec/koala/rest_api/rest_api_with_access_token_tests.rb", "spec/koala/test_users/test_users_tests.rb", "spec/koala/typhoeus_service_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
- s.homepage = %q{http://github.com/arsduo/koala}
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala"]
16
- s.require_paths = ["lib"]
17
- s.rubyforge_project = %q{koala}
18
- s.rubygems_version = %q{1.4.2}
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.}
13
+ s.email = %q{alex@alexkoppel.com}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+
18
+ s.extra_rdoc_files = ["readme.md", "CHANGELOG"]
19
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala"]
20
+
21
+ s.require_paths = ["lib"]
22
+
23
+ s.rubygems_version = %q{1.4.2}
24
+
25
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
20
26
 
21
27
  if s.respond_to? :specification_version then
22
28
  s.specification_version = 3
23
29
 
24
30
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- s.add_runtime_dependency(%q<json>, [">= 1.0"])
26
- s.add_runtime_dependency(%q<multipart-post>, [">= 1.0"])
31
+ s.add_runtime_dependency(%q<json>, ["~> 1.0"])
32
+ s.add_runtime_dependency(%q<multipart-post>, ["~> 1.0"])
33
+ s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
34
+ s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
35
+ s.add_development_dependency(%q<typhoeus>, ["~> 0.2.4"])
27
36
  else
28
- s.add_dependency(%q<json>, [">= 1.0"])
29
- s.add_dependency(%q<multipart-post>, [">= 1.0"])
37
+ s.add_dependency(%q<json>, ["~> 1.0"])
38
+ s.add_dependency(%q<multipart-post>, ["~> 1.0"])
39
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
40
+ s.add_dependency(%q<rake>, ["~> 0.8.7"])
41
+ s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
30
42
  end
31
43
  else
32
- s.add_dependency(%q<json>, [">= 1.0"])
33
- s.add_dependency(%q<multipart-post>, [">= 1.0"])
44
+ s.add_dependency(%q<json>, ["~> 1.0"])
45
+ s.add_dependency(%q<multipart-post>, ["~> 1.0"])
46
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
47
+ s.add_dependency(%q<rake>, ["~> 0.8.7"])
48
+ s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
34
49
  end
35
50
  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,121 +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
147
 
159
- def put_picture(file_hash, args = {}, target_id = "me")
160
- raise APIError.new("type" => "KoalaInvaliFileFormat", "message" => "put_picture requires a hash describing the File to upload (see HTTPService::is_valid_file_hash?") unless file_hash.kind_of?(Hash)
161
-
162
- args["source"] = file_hash
163
- self.put_object(target_id, "photos", args)
164
- end
148
+ # Comments
149
+ # to delete comments, use delete_object(comment_id)
150
+ # to get comments, use get_connections(object, "likes")
165
151
 
166
- def put_comment(object_id, message)
152
+ def put_comment(object_id, message, options = {})
167
153
  # Writes the given comment on the given post.
168
- self.put_object(object_id, "comments", {:message => message})
154
+ self.put_object(object_id, "comments", {:message => message}, options)
169
155
  end
170
-
171
- 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 = {})
172
161
  # Likes the given post.
173
- self.put_object(object_id, "likes")
162
+ self.put_object(object_id, "likes", {}, options)
174
163
  end
175
164
 
176
- def delete_like(object_id)
177
- raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Unliking requires an access token"}) unless @access_token
178
- graph_call("#{object_id}/likes", {}, "delete")
179
- end
180
-
181
- def delete_object(id)
182
- # Deletes the object with the given ID from the graph.
165
+ def delete_like(object_id, options = {})
166
+ # Unlikes a given object for the logged-in user
183
167
  raise APIError.new({"type" => "KoalaMissingAccessToken", "message" => "Unliking requires an access token"}) unless @access_token
184
- graph_call(id, {}, "delete")
168
+ graph_call("#{object_id}/likes", {}, "delete", options)
185
169
  end
186
-
187
- def search(search_terms, args = {})
188
- # Searches for a given term
189
- result = graph_call("search", args.merge({:q => search_terms}))
170
+
171
+ # Search
172
+
173
+ def search(search_terms, args = {}, options = {})
174
+ args.merge!({:q => search_terms}) unless search_terms.nil?
175
+ result = graph_call("search", args, "get", options)
190
176
  result ? GraphCollection.new(result, self) : nil # when facebook is down nil can be returned
191
- end
177
+ end
178
+
179
+ # API access
192
180
 
193
181
  def graph_call(*args)
182
+ # Direct access to the Facebook API
183
+ # see any of the above methods for example invocations
194
184
  response = api(*args) do |response|
195
185
  # check for Graph API-specific errors
196
186
  if response.is_a?(Hash) && error_details = response["error"]
@@ -199,7 +189,69 @@ module Koala
199
189
  end
200
190
 
201
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
202
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
+
203
255
  end
204
256
  end
205
257
  end