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
@@ -1,102 +0,0 @@
1
- class ApiBaseTests < Test::Unit::TestCase
2
-
3
- describe "Koala API base class" do
4
- before(:each) do
5
- @service = Koala::Facebook::API.new
6
- end
7
-
8
- it "should not include an access token if none was given" do
9
- Koala.should_receive(:make_request).with(
10
- anything,
11
- hash_not_including('access_token' => 1),
12
- anything,
13
- anything
14
- ).and_return(Koala::Response.new(200, "", ""))
15
-
16
- @service.api('anything')
17
- end
18
-
19
- it "should include an access token if given" do
20
- token = 'adfadf'
21
- service = Koala::Facebook::API.new token
22
-
23
- Koala.should_receive(:make_request).with(
24
- anything,
25
- hash_including('access_token' => token),
26
- anything,
27
- anything
28
- ).and_return(Koala::Response.new(200, "", ""))
29
-
30
- service.api('anything')
31
- end
32
-
33
- it "should have an attr_reader for access token" do
34
- token = 'adfadf'
35
- service = Koala::Facebook::API.new token
36
- service.access_token.should == token
37
- end
38
-
39
- it "should get the attribute of a Koala::Response given by the http_component parameter" do
40
- http_component = :method_name
41
-
42
- response = mock('Mock KoalaResponse', :body => '', :status => 200)
43
- response.should_receive(http_component).and_return('')
44
-
45
- Koala.stub(:make_request).and_return(response)
46
-
47
- @service.api('anything', 'get', {}, :http_component => http_component)
48
- end
49
-
50
- it "should return the body of the request as JSON if no http_component is given" do
51
- response = stub('response', :body => 'body', :status => 200)
52
- Koala.stub(:make_request).and_return(response)
53
-
54
- json_body = mock('JSON body')
55
- JSON.stub(:parse).and_return([json_body])
56
-
57
- @service.api('anything').should == json_body
58
- end
59
-
60
- it "should execute a block with the response body if passed one" do
61
- body = '{}'
62
- Koala.stub(:make_request).and_return(Koala::Response.new(200, body, {}))
63
-
64
- yield_test = mock('Yield Tester')
65
- yield_test.should_receive(:pass)
66
-
67
- @service.api('anything') do |arg|
68
- yield_test.pass
69
- arg.should == JSON.parse(body)
70
- end
71
- end
72
-
73
- it "should raise an API error if the HTTP response code is greater than or equal to 500" do
74
- Koala.stub(:make_request).and_return(Koala::Response.new(500, 'response body', {}))
75
-
76
- lambda { @service.api('anything') }.should raise_exception(Koala::Facebook::APIError)
77
- end
78
-
79
- it "should handle rogue true/false as responses" do
80
- Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'true', {}))
81
- @service.api('anything').should be_true
82
-
83
- Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'false', {}))
84
- @service.api('anything').should be_false
85
- end
86
-
87
- describe "with regard to leading slashes" do
88
- it "should add a leading / to the path if not present" do
89
- path = "anything"
90
- Koala.should_receive(:make_request).with("/#{path}", anything, anything, anything).and_return(Koala::Response.new(200, 'true', {}))
91
- @service.api(path)
92
- end
93
-
94
- it "shouldn't change the path if a leading / is present" do
95
- path = "/anything"
96
- Koala.should_receive(:make_request).with(path, anything, anything, anything).and_return(Koala::Response.new(200, 'true', {}))
97
- @service.api(path)
98
- end
99
- end
100
-
101
- end
102
- end
@@ -1,14 +0,0 @@
1
- class GraphAndRestAPINoTokenTests < Test::Unit::TestCase
2
- describe "Koala GraphAndRestAPI without an access token" do
3
- before(:each) do
4
- @api = Koala::Facebook::GraphAndRestAPI.new
5
- end
6
-
7
- it_should_behave_like "Koala RestAPI"
8
- it_should_behave_like "Koala RestAPI without an access token"
9
-
10
- it_should_behave_like "Koala GraphAPI"
11
- it_should_behave_like "Koala GraphAPI without an access token"
12
- it_should_behave_like "Koala GraphAPI with GraphCollection"
13
- end
14
- end
@@ -1,16 +0,0 @@
1
- class GraphAndRestAPIWithTokenTests < Test::Unit::TestCase
2
- describe "Koala GraphAndRestAPI without an access token" do
3
- include LiveTestingDataHelper
4
-
5
- before(:each) do
6
- @api = Koala::Facebook::GraphAndRestAPI.new(@token)
7
- end
8
-
9
- it_should_behave_like "Koala RestAPI"
10
- it_should_behave_like "Koala RestAPI with an access token"
11
-
12
- it_should_behave_like "Koala GraphAPI"
13
- it_should_behave_like "Koala GraphAPI with an access token"
14
- it_should_behave_like "Koala GraphAPI with GraphCollection"
15
- end
16
- end
@@ -1,63 +0,0 @@
1
- shared_examples_for "Koala GraphAPI without an access token" do
2
-
3
- it "should not get private data about a user" do
4
- result = @api.get_object("koppel")
5
- # updated_time should be a pretty fixed test case
6
- result["updated_time"].should be_nil
7
- end
8
-
9
- it "should not be able to get data about 'me'" do
10
- lambda { @api.get_object("me") }.should raise_error(Koala::Facebook::APIError)
11
- end
12
-
13
- it "shouldn't be able to access connections from users" do
14
- lambda { @api.get_connections("lukeshepard", "likes") }.should raise_error(Koala::Facebook::APIError)
15
- end
16
-
17
- it "should not be able to put an object" do
18
- lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
19
- puts "Error! Object #{@result.inspect} somehow put onto Luke Shepard's wall!" if @result
20
- end
21
-
22
- # these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
23
- it "should not be able to post to a feed" do
24
- (lambda do
25
- attachment = {:name => "Context Optional", :link => "http://www.contextoptional.com/"}
26
- @result = @api.put_wall_post("Hello, world", attachment, "contextoptional")
27
- end).should raise_error(Koala::Facebook::APIError)
28
- puts "Error! Object #{@result.inspect} somehow put onto Context Optional's wall!" if @result
29
- end
30
-
31
- it "should not be able to comment on an object" do
32
- # random public post on the ContextOptional wall
33
- lambda { @result = @api.put_comment("7204941866_119776748033392", "The hackathon was great!") }.should raise_error(Koala::Facebook::APIError)
34
- puts "Error! Object #{@result.inspect} somehow commented on post 7204941866_119776748033392!" if @result
35
- end
36
-
37
- it "should not be able to like an object" do
38
- lambda { @api.put_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
39
- end
40
-
41
- # DELETE
42
- it "should not be able to delete posts" do
43
- # test post on the Ruby SDK Test application
44
- lambda { @result = @api.delete_object("115349521819193_113815981982767") }.should raise_error(Koala::Facebook::APIError)
45
- end
46
-
47
- it "should not be able to delete a like" do
48
- lambda { @api.delete_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
49
- end
50
- end
51
-
52
- class FacebookNoAccessTokenTests < Test::Unit::TestCase
53
- describe "Koala GraphAPI without an access token" do
54
- before :each do
55
- @api = Koala::Facebook::GraphAPI.new
56
- end
57
-
58
- it_should_behave_like "Koala GraphAPI"
59
- it_should_behave_like "Koala GraphAPI without an access token"
60
- it_should_behave_like "Koala GraphAPI with GraphCollection"
61
-
62
- end
63
- end
@@ -1,86 +0,0 @@
1
- shared_examples_for "Koala GraphAPI" do
2
- # all Graph API instances should pass these tests, regardless of configuration
3
-
4
- # API
5
- it "should never use the rest api server" do
6
- Koala.should_receive(:make_request).with(
7
- anything,
8
- anything,
9
- anything,
10
- hash_not_including(:rest_api => true)
11
- ).and_return(Koala::Response.new(200, "", {}))
12
-
13
- @api.api("anything")
14
- end
15
-
16
- # GRAPH CALL
17
- describe "graph_call" do
18
- it "should pass all arguments to the api method" do
19
- args = ["koppel", {}, "get", {:a => :b}]
20
-
21
- @api.should_receive(:api).with(*args)
22
-
23
- @api.graph_call(*args)
24
- end
25
-
26
- it "should throw an APIError if the result hash has an error key" do
27
- Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error" => "An error occurred!"}, {}))
28
- lambda { @api.graph_call("koppel", {}) }.should raise_exception(Koala::Facebook::APIError)
29
- end
30
- end
31
-
32
- # SEARCH
33
- it "should be able to search" do
34
- result = @api.search("facebook")
35
- result.length.should be_an(Integer)
36
- end
37
-
38
- # DATA
39
- # access public info
40
-
41
- it "should get public data about a user" do
42
- result = @api.get_object("koppel")
43
- # the results should have an ID and a name, among other things
44
- (result["id"] && result["name"]).should_not be_nil
45
- end
46
-
47
- it "should get public data about a Page" do
48
- result = @api.get_object("contextoptional")
49
- # the results should have an ID and a name, among other things
50
- (result["id"] && result["name"]).should
51
- end
52
-
53
- it "should be able to get multiple objects" do
54
- results = @api.get_objects(["contextoptional", "naitik"])
55
- results.length.should == 2
56
- end
57
-
58
- it "should be able to access a user's picture" do
59
- @api.get_picture("chris.baclig").should =~ /http\:\/\//
60
- end
61
-
62
- it "should be able to access a user's picture, given a picture type" do
63
- @api.get_picture("chris.baclig", {:type => 'large'}).should =~ /^http\:\/\//
64
- end
65
-
66
- it "should be able to access connections from public Pages" do
67
- result = @api.get_connections("contextoptional", "photos")
68
- result.should be_a(Array)
69
- end
70
-
71
- # SEARCH
72
- it "should be able to search" do
73
- result = @api.search("facebook")
74
- result.length.should be_an(Integer)
75
- end
76
-
77
- # PAGING THROUGH COLLECTIONS
78
- # see also graph_collection_tests
79
- it "should make a request for a page when provided a specific set of page params" do
80
- query = [1, 2]
81
- @api.should_receive(:graph_call).with(*query)
82
- @api.get_page(query)
83
- end
84
-
85
-
86
- end
@@ -1,154 +0,0 @@
1
- shared_examples_for "Koala GraphAPI with an access token" do
2
-
3
- it "should get private data about a user" do
4
- result = @api.get_object("koppel")
5
- # updated_time should be a pretty fixed test case
6
- result["updated_time"].should_not be_nil
7
- end
8
-
9
- it "should get data about 'me'" do
10
- result = @api.get_object("me")
11
- result["updated_time"].should
12
- end
13
-
14
- it "should be able to get multiple objects" do
15
- result = @api.get_objects(["contextoptional", "naitik"])
16
- result.length.should == 2
17
- end
18
- it "should be able to access connections from users" do
19
- result = @api.get_connections("lukeshepard", "likes")
20
- result.length.should > 0
21
- end
22
-
23
- # PUT
24
- it "should be able to write an object to the graph" do
25
- result = @api.put_wall_post("Hello, world, from the test suite!")
26
- @temporary_object_id = result["id"]
27
- @temporary_object_id.should_not be_nil
28
- end
29
-
30
- # DELETE
31
- it "should be able to delete posts" do
32
- result = @api.put_wall_post("Hello, world, from the test suite delete method!")
33
- object_id_to_delete = result["id"]
34
- delete_result = @api.delete_object(object_id_to_delete)
35
- delete_result.should == true
36
- end
37
-
38
- it "should be able to delete likes" do
39
- result = @api.put_wall_post("Hello, world, from the test suite delete method!")
40
- @temporary_object_id = result["id"]
41
- @api.put_like(@temporary_object_id)
42
- delete_like_result = @api.delete_like(@temporary_object_id)
43
- delete_like_result.should == true
44
- end
45
-
46
- # additional put tests
47
- it "should be able to verify messages posted to a wall" do
48
- message = "the cats are asleep"
49
- put_result = @api.put_wall_post(message)
50
- @temporary_object_id = put_result["id"]
51
- get_result = @api.get_object(@temporary_object_id)
52
-
53
- # make sure the message we sent is the message that got posted
54
- get_result["message"].should == message
55
- end
56
-
57
- it "should be able to post a message with an attachment to a feed" do
58
- result = @api.put_wall_post("Hello, world, from the test suite again!", {:name => "Context Optional", :link => "http://www.contextoptional.com/"})
59
- @temporary_object_id = result["id"]
60
- @temporary_object_id.should_not be_nil
61
- end
62
-
63
- it "should be able to post photos to the user's wall with an open file object" do
64
- file_hash = {
65
- "content_type" => "image/jpg",
66
- "path" => File.join(File.dirname(__FILE__), "..", "assets", "beach.jpg"),
67
- "file" => File.open(File.join(File.dirname(__FILE__), "..", "assets", "beach.jpg"))
68
- }
69
- result = @api.put_picture(file_hash)
70
- @temporary_object_id = result["id"]
71
- @temporary_object_id.should_not be_nil
72
- end
73
-
74
- it "should be able to post photos to the user's wall without an open file object" do
75
- file_hash = {
76
- "content_type" => "image/jpg",
77
- "path" => File.join(File.dirname(__FILE__), "..", "assets", "beach.jpg")
78
- }
79
- result = @api.put_picture(file_hash)
80
- @temporary_object_id = result["id"]
81
- @temporary_object_id.should_not be_nil
82
- end
83
-
84
- it "should be able to verify a photo posted to a user's wall" do
85
- file_hash = {
86
- "content_type" => "image/jpg",
87
- "path" => File.join(File.dirname(__FILE__), "..", "assets", "beach.jpg")
88
- }
89
- expected_message = "This is the test message"
90
-
91
- result = @api.put_picture(file_hash, :message => expected_message)
92
- @temporary_object_id = result["id"]
93
- @temporary_object_id.should_not be_nil
94
-
95
- get_result = @api.get_object(@temporary_object_id)
96
- get_result["name"].should == expected_message
97
- end
98
-
99
- it "should be able to verify a message with an attachment posted to a feed" do
100
- attachment = {"name" => "Context Optional", "link" => "http://www.contextoptional.com/"}
101
- result = @api.put_wall_post("Hello, world, from the test suite again!", attachment)
102
- @temporary_object_id = result["id"]
103
- get_result = @api.get_object(@temporary_object_id)
104
-
105
- # make sure the result we fetch includes all the parameters we sent
106
- it_matches = attachment.inject(true) {|valid, param| valid && (get_result[param[0]] == attachment[param[0]])}
107
- it_matches.should == true
108
- end
109
-
110
- it "should be able to comment on an object" do
111
- result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
112
- @temporary_object_id = result["id"]
113
-
114
- # this will be deleted when the post gets deleted
115
- comment_result = @api.put_comment(@temporary_object_id, "it's my comment!")
116
- comment_result.should_not be_nil
117
- end
118
-
119
- it "should be able to verify a comment posted about an object" do
120
- message_text = "Hello, world, from the test suite, testing comments!"
121
- result = @api.put_wall_post(message_text)
122
- @temporary_object_id = result["id"]
123
-
124
- # this will be deleted when the post gets deleted
125
- comment_text = "it's my comment!"
126
- comment_result = @api.put_comment(@temporary_object_id, comment_text)
127
- get_result = @api.get_object(comment_result["id"])
128
-
129
- # make sure the text of the comment matches what we sent
130
- get_result["message"].should == comment_text
131
- end
132
-
133
- it "should be able to like an object" do
134
- result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
135
- @temporary_object_id = result["id"]
136
- like_result = @api.put_like(@temporary_object_id)
137
- like_result.should be_true
138
- end
139
- end
140
-
141
- class FacebookWithAccessTokenTests < Test::Unit::TestCase
142
- describe "Koala GraphAPI with an access token" do
143
- include LiveTestingDataHelper
144
-
145
- before :each do
146
- @api = Koala::Facebook::GraphAPI.new(@token)
147
- end
148
-
149
- it_should_behave_like "Koala GraphAPI"
150
- it_should_behave_like "Koala GraphAPI with an access token"
151
- it_should_behave_like "Koala GraphAPI with GraphCollection"
152
-
153
- end
154
- end
@@ -1,104 +0,0 @@
1
- # GraphCollection
2
- shared_examples_for "Koala GraphAPI with GraphCollection" do
3
-
4
- it "should create an array-like object" do
5
- call = @api.graph_call("contextoptional/photos")
6
- Koala::Facebook::GraphCollection.new(call, @api).should be_an(Array)
7
- end
8
-
9
- describe "when getting a collection" do
10
- # GraphCollection methods
11
- it "should get a GraphCollection when getting connections" do
12
- @result = @api.get_connections("contextoptional", "photos")
13
- @result.should be_a(Koala::Facebook::GraphCollection)
14
- end
15
-
16
- it "should return nil if the get_collections call fails with nil" do
17
- # this happens sometimes
18
- @api.should_receive(:graph_call).and_return(nil)
19
- @api.get_connections("contextoptional", "photos").should be_nil
20
- end
21
-
22
- it "should get a GraphCollection when searching" do
23
- result = @api.search("facebook")
24
- result.should be_a(Koala::Facebook::GraphCollection)
25
- end
26
-
27
- it "should return nil if the search call fails with nil" do
28
- # this happens sometimes
29
- @api.should_receive(:graph_call).and_return(nil)
30
- @api.search("facebook").should be_nil
31
- end
32
-
33
- it "should get a GraphCollection when paging through results" do
34
- @results = @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=>"2010-09-23T21:17:33+0000"}])
35
- @results.should be_a(Koala::Facebook::GraphCollection)
36
- end
37
-
38
- it "should return nil if the page call fails with nil" do
39
- # this happens sometimes
40
- @api.should_receive(:graph_call).and_return(nil)
41
- @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=>"2010-09-23T21:17:33+0000"}]).should be_nil
42
- end
43
-
44
- # GraphCollection attributes
45
- describe "the GraphCollection" do
46
- before(:each) do
47
- @result = @api.get_connections("contextoptional", "photos")
48
- end
49
-
50
- it "should have a read-only paging attribute" do
51
- lambda { @result.paging }.should_not raise_error
52
- lambda { @result.paging = "paging" }.should raise_error(NoMethodError)
53
- end
54
-
55
- describe "when getting a whole page" do
56
- before(:each) do
57
- @second_page = stub("page of Fb graph results")
58
- @base = stub("base")
59
- @args = stub("args")
60
- @page_of_results = stub("page of results")
61
- end
62
-
63
- it "should return the previous page of results" do
64
- @result.should_receive(:previous_page_params).and_return([@base, @args])
65
- @api.should_receive(:graph_call).with(@base, @args).and_return(@second_page)
66
- Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
67
-
68
- @result.previous_page.should == @page_of_results
69
- end
70
-
71
- it "should return the next page of results" do
72
- @result.should_receive(:next_page_params).and_return([@base, @args])
73
- @api.should_receive(:graph_call).with(@base, @args).and_return(@second_page)
74
- Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
75
-
76
- @result.next_page.should == @page_of_results
77
- end
78
-
79
- it "should return nil it there are no other pages" do
80
- %w{next previous}.each do |this|
81
- @result.should_receive("#{this}_page_params".to_sym).and_return(nil)
82
- @result.send("#{this}_page").should == nil
83
- end
84
- end
85
- end
86
-
87
- describe "when parsing page paramters" do
88
- before(:each) do
89
- @graph_collection = Koala::Facebook::GraphCollection.new({"data" => []}, Koala::Facebook::GraphAPI.new)
90
- end
91
-
92
- it "should return the base as the first array entry" do
93
- base = "url_path"
94
- @graph_collection.parse_page_url("anything.com/#{base}?anything").first.should == base
95
- end
96
-
97
- it "should return the arguments as a hash as the last array entry" do
98
- args_hash = {"one" => "val_one", "two" => "val_two"}
99
- @graph_collection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
100
- end
101
- end
102
- end
103
- end
104
- end