vkontakte_api 0.2.1 → 1.0.rc

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 (42) hide show
  1. data/.rspec +1 -0
  2. data/.travis.yml +1 -0
  3. data/.yardopts +1 -1
  4. data/CHANGELOG.md +23 -0
  5. data/README.md +136 -61
  6. data/lib/generators/vkontakte_api/install/USAGE +2 -0
  7. data/lib/generators/vkontakte_api/install/install_generator.rb +9 -0
  8. data/lib/generators/vkontakte_api/install/templates/initializer.rb +22 -0
  9. data/lib/vkontakte_api/api.rb +27 -39
  10. data/lib/vkontakte_api/authorization.rb +66 -0
  11. data/lib/vkontakte_api/client.rb +14 -12
  12. data/lib/vkontakte_api/configuration.rb +22 -4
  13. data/lib/vkontakte_api/error.rb +15 -7
  14. data/lib/vkontakte_api/logger.rb +35 -0
  15. data/lib/vkontakte_api/method.rb +40 -0
  16. data/lib/vkontakte_api/namespace.rb +7 -0
  17. data/lib/vkontakte_api/resolvable.rb +20 -0
  18. data/lib/vkontakte_api/resolver.rb +18 -103
  19. data/lib/vkontakte_api/result.rb +48 -0
  20. data/lib/vkontakte_api/uploading.rb +29 -0
  21. data/lib/vkontakte_api/utils.rb +28 -0
  22. data/lib/vkontakte_api/version.rb +2 -1
  23. data/lib/vkontakte_api.rb +14 -3
  24. data/spec/integration_spec.rb +84 -0
  25. data/spec/spec_helper.rb +18 -0
  26. data/spec/vkontakte_api/api_spec.rb +39 -58
  27. data/spec/vkontakte_api/authorization_spec.rb +111 -0
  28. data/spec/vkontakte_api/client_spec.rb +17 -24
  29. data/spec/vkontakte_api/configuration_spec.rb +5 -0
  30. data/spec/vkontakte_api/error_spec.rb +30 -10
  31. data/spec/vkontakte_api/logger_spec.rb +88 -0
  32. data/spec/vkontakte_api/method_spec.rb +59 -0
  33. data/spec/vkontakte_api/namespace_spec.rb +5 -0
  34. data/spec/vkontakte_api/resolvable_spec.rb +21 -0
  35. data/spec/vkontakte_api/resolver_spec.rb +58 -141
  36. data/spec/vkontakte_api/result_spec.rb +115 -0
  37. data/spec/vkontakte_api/uploading_spec.rb +46 -0
  38. data/spec/vkontakte_api/utils_spec.rb +47 -0
  39. data/spec/vkontakte_api_spec.rb +4 -0
  40. data/vkontakte_api.gemspec +6 -5
  41. metadata +119 -38
  42. data/README.ru.md +0 -115
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe VkontakteApi::Authorization do
4
+ before(:each) do
5
+ @app_id = stub("App id")
6
+ VkontakteApi.stub(:app_id).and_return(@app_id)
7
+ @app_secret = stub("App secret")
8
+ VkontakteApi.stub(:app_secret).and_return(@app_secret)
9
+ @redirect_uri = stub("Redirect uri")
10
+ VkontakteApi.stub(:redirect_uri).and_return(@redirect_uri)
11
+
12
+ @url = stub("Authorization url")
13
+ @token = stub("Token")
14
+
15
+ @auth_code = stub("Authorization code strategy", :get_token => @token, :authorize_url => @url)
16
+ @implicit = stub("Implicit strategy", :authorize_url => @url)
17
+ @client_credentials = stub("Client credentials strategy", :get_token => @token)
18
+
19
+ @client = stub("OAuth2::Client instance", :auth_code => @auth_code, :implicit => @implicit, :client_credentials => @client_credentials)
20
+ OAuth2::Client.stub(:new).and_return(@client)
21
+
22
+ @auth = Object.new
23
+ @auth.extend VkontakteApi::Authorization
24
+ end
25
+
26
+ describe "#authorization_url" do
27
+ before(:each) do
28
+ @auth.stub(:client).and_return(@client)
29
+ end
30
+
31
+ context "with a site type" do
32
+ it "returns a valid authorization url" do
33
+ @auth_code.should_receive(:authorize_url).with(:redirect_uri => @redirect_uri)
34
+ @auth.authorization_url(:type => :site).should == @url
35
+ end
36
+ end
37
+
38
+ context "with a client type" do
39
+ it "returns a valid authorization url" do
40
+ @implicit.should_receive(:authorize_url).with(:redirect_uri => @redirect_uri)
41
+ @auth.authorization_url(:type => :client).should == @url
42
+ end
43
+ end
44
+
45
+ context "given a redirect_uri" do
46
+ it "prefers the given uri over VkontakteApi.redirect_uri" do
47
+ redirect_uri = 'http://example.com/oauth/callback'
48
+ @auth_code.should_receive(:authorize_url).with(:redirect_uri => redirect_uri)
49
+ @auth.authorization_url(:redirect_uri => redirect_uri)
50
+ end
51
+ end
52
+
53
+ context "given a scope" do
54
+ it "sends it to VkontakteApi::Utils.flatten_argument" do
55
+ scope = stub("Scope")
56
+ flat_scope = stub("Flat scope")
57
+
58
+ VkontakteApi::Utils.should_receive(:flatten_argument).with(scope).and_return(flat_scope)
59
+ @auth_code.should_receive(:authorize_url).with(:redirect_uri => @redirect_uri, :scope => flat_scope)
60
+ @auth.authorization_url(:scope => scope)
61
+ end
62
+ end
63
+ end
64
+
65
+ describe "#authorize" do
66
+ context "with a site type" do
67
+ before(:each) do
68
+ @code = stub("Authorization code")
69
+ @auth_code.stub(:get_token).and_return(@token)
70
+ end
71
+
72
+ it "gets the token" do
73
+ @auth_code.should_receive(:get_token).with(@code)
74
+ @auth.authorize(:type => :site, :code => @code)
75
+ end
76
+ end
77
+
78
+ context "with an app_server type" do
79
+ it "gets the token" do
80
+ @client_credentials.should_receive(:get_token).with({}, subject::OPTIONS[:client_credentials])
81
+ @auth.authorize(:type => :app_server)
82
+ end
83
+ end
84
+
85
+ context "with an unknown type" do
86
+ it "raises an ArgumentError" do
87
+ expect {
88
+ @auth.authorize(:type => :unknown)
89
+ }.to raise_error(ArgumentError)
90
+ end
91
+ end
92
+
93
+ it "builds a VkontakteApi::Client instance with the received token" do
94
+ client = stub("VkontakteApi::Client instance")
95
+ VkontakteApi::Client.should_receive(:new).with(@token).and_return(client)
96
+ @auth.authorize.should == client
97
+ end
98
+ end
99
+
100
+ describe "#client" do
101
+ it "creates and returns an OAuth2::Client instance" do
102
+ OAuth2::Client.should_receive(:new).with(@app_id, @app_secret, subject::OPTIONS[:client])
103
+ @auth.send(:client).should == @client
104
+ end
105
+
106
+ it "caches the result" do
107
+ OAuth2::Client.should_receive(:new).once
108
+ 5.times { @auth.send(:client) }
109
+ end
110
+ end
111
+ end
@@ -2,20 +2,31 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Client do
4
4
  before(:each) do
5
- @token = stub("Access token")
5
+ @string_token = stub("Access token as a String")
6
+ @oauth2_token = stub("Access token as an OAuth2::AccessToken", :token => @string_token)
6
7
  end
7
8
 
8
9
  describe "#initialize" do
9
10
  context "without arguments" do
10
11
  it "creates a client with a nil access_token" do
11
- VkontakteApi::Client.new.access_token.should be_nil
12
+ client = VkontakteApi::Client.new
13
+ client.token.should be_nil
12
14
  end
13
15
  end
14
16
 
15
17
  context "with a token argument" do
16
- it "creates a client with a given access_token" do
17
- client = VkontakteApi::Client.new(@token)
18
- client.access_token.should == @token
18
+ context "with a string value" do
19
+ it "creates a client with a given access_token" do
20
+ client = VkontakteApi::Client.new(@string_token)
21
+ client.token.should == @string_token
22
+ end
23
+ end
24
+
25
+ context "with an OAuth2::AccessToken value" do
26
+ it "extracts the string token and uses it" do
27
+ client = VkontakteApi::Client.new(@oauth2_token)
28
+ client.token.should == @string_token
29
+ end
19
30
  end
20
31
  end
21
32
  end
@@ -29,26 +40,8 @@ describe VkontakteApi::Client do
29
40
 
30
41
  context "with an authorized client" do
31
42
  it "returns true" do
32
- VkontakteApi::Client.new(@token).should be_authorized
43
+ VkontakteApi::Client.new(@string_token).should be_authorized
33
44
  end
34
45
  end
35
46
  end
36
-
37
- describe "#method_missing" do
38
- before(:each) do
39
- @resolver = stub("Resolver").as_null_object
40
- VkontakteApi::Resolver.stub(:new).and_return(@resolver)
41
- @args = {:field => 'value'}
42
- end
43
-
44
- it "creates a resolver, passing it the access_token" do
45
- VkontakteApi::Resolver.should_receive(:new).with(:access_token => @token)
46
- VkontakteApi::Client.new(@token).api_method(@args)
47
- end
48
-
49
- it "delegates to VkontakteApi::Resolver" do
50
- @resolver.should_receive(:api_method).with(@args)
51
- VkontakteApi::Client.new(@token).api_method(@args)
52
- end
53
- end
54
47
  end
@@ -24,6 +24,11 @@ describe VkontakteApi::Configuration do
24
24
  Configurable.app_id.should be_nil
25
25
  Configurable.app_secret.should be_nil
26
26
  Configurable.adapter.should == VkontakteApi::Configuration::DEFAULT_ADAPTER
27
+
28
+ Configurable.logger.should be_a(Logger)
29
+ Configurable.should log_requests
30
+ Configurable.should log_errors
31
+ Configurable.should_not log_responses
27
32
  end
28
33
  end
29
34
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Error do
4
4
  before(:each) do
5
- @e = VkontakteApi::Error.new(
5
+ @error_data = Hashie::Mash.new(
6
6
  :error_code => 5,
7
7
  :error_msg => 'User authorization failed: invalid access_token.',
8
8
  :request_params => [
@@ -14,10 +14,6 @@ describe VkontakteApi::Error do
14
14
  :key => 'method',
15
15
  :value => 'unknownMethod'
16
16
  },
17
- {
18
- :key => 'some',
19
- :value => 'params'
20
- },
21
17
  {
22
18
  :key => 'access_token',
23
19
  :value => '123'
@@ -27,11 +23,35 @@ describe VkontakteApi::Error do
27
23
  end
28
24
 
29
25
  describe "#message" do
30
- it "returns all needed data about an error" do
31
- message = 'VKontakte returned an error 5: \'User authorization failed: invalid access_token.\' after calling method \'unknownMethod\' with parameters {"some"=>"params"}.'
32
- expect {
33
- raise @e
34
- }.to raise_error(@e.class, message)
26
+ context "without parameters" do
27
+ before(:each) do
28
+ @e = VkontakteApi::Error.new(@error_data)
29
+ end
30
+
31
+ it "returns all needed data about an error" do
32
+ message = 'VKontakte returned an error 5: \'User authorization failed: invalid access_token.\''
33
+ message << ' after calling method \'unknownMethod\' without parameters.'
34
+
35
+ expect {
36
+ raise @e
37
+ }.to raise_error(@e.class, message)
38
+ end
39
+ end
40
+
41
+ context "with parameters" do
42
+ before(:each) do
43
+ @error_data[:request_params] << Hashie::Mash.new(:key => 'some', :value => 'params')
44
+ @e = VkontakteApi::Error.new(@error_data)
45
+ end
46
+
47
+ it "returns all needed data about an error" do
48
+ message = 'VKontakte returned an error 5: \'User authorization failed: invalid access_token.\''
49
+ message << ' after calling method \'unknownMethod\' with parameters {"some"=>"params"}.'
50
+
51
+ expect {
52
+ raise @e
53
+ }.to raise_error(@e.class, message)
54
+ end
35
55
  end
36
56
  end
37
57
  end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe VkontakteApi::Logger do
4
+ before(:each) do
5
+ @success_response = Oj.dump('a' => 1, 'b' => 2)
6
+ @fail_response = Oj.dump('error' => 404)
7
+
8
+ @connection = Faraday.new(:url => 'http://example.com') do |builder|
9
+ builder.response :vk_logger
10
+ builder.response :mashify
11
+ builder.response :oj, :preserve_raw => true
12
+
13
+ builder.adapter :test do |stub|
14
+ stub.get('/success') do
15
+ [200, {}, @success_response]
16
+ end
17
+ stub.get('/fail') do
18
+ [200, {}, @fail_response]
19
+ end
20
+ end
21
+ end
22
+
23
+ @logger = stub("Logger").as_null_object
24
+ VkontakteApi.logger = @logger
25
+
26
+ VkontakteApi.log_requests = false
27
+ VkontakteApi.log_responses = false
28
+ VkontakteApi.log_errors = false
29
+ end
30
+
31
+ context "with VkontakteApi.log_requests?" do
32
+ before(:each) do
33
+ VkontakteApi.log_requests = true
34
+ end
35
+
36
+ it "logs the request" do
37
+ @logger.should_receive(:debug).with('GET http://example.com/success')
38
+ @connection.get('/success')
39
+ end
40
+ end
41
+
42
+ context "without VkontakteApi.log_requests?" do
43
+ it "doesn't log the request" do
44
+ @logger.should_not_receive(:debug)
45
+ @connection.get('/success')
46
+ end
47
+ end
48
+
49
+ context "with a successful response" do
50
+ context "with VkontakteApi.log_responses?" do
51
+ before(:each) do
52
+ VkontakteApi.log_responses = true
53
+ end
54
+
55
+ it "logs the response body" do
56
+ @logger.should_receive(:debug).with(@success_response)
57
+ @connection.get('/success')
58
+ end
59
+ end
60
+
61
+ context "without VkontakteApi.log_responses?" do
62
+ it "doesn't log the response body" do
63
+ @logger.should_not_receive(:debug)
64
+ @connection.get('/success')
65
+ end
66
+ end
67
+ end
68
+
69
+ context "with an error response" do
70
+ context "with VkontakteApi.log_errors?" do
71
+ before(:each) do
72
+ VkontakteApi.log_errors = true
73
+ end
74
+
75
+ it "logs the response body" do
76
+ @logger.should_receive(:warn).with(@fail_response)
77
+ @connection.get('/fail')
78
+ end
79
+ end
80
+
81
+ context "without VkontakteApi.log_errors?" do
82
+ it "doesn't log the response body" do
83
+ @logger.should_not_receive(:warn)
84
+ @connection.get('/fail')
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe VkontakteApi::Method do
4
+ describe "#call" do
5
+ before(:each) do
6
+ @full_name = stub("Full method name")
7
+ @args = stub("Method arguments")
8
+ @token = stub("Access token")
9
+
10
+ @method = VkontakteApi::Method.new('some_name')
11
+ @method.stub(:full_name).and_return(@full_name)
12
+ @method.stub(:token).and_return(@token)
13
+ VkontakteApi::Result.stub(:process)
14
+ end
15
+
16
+ it "calls API.call with full name, args and token" do
17
+ VkontakteApi::API.should_receive(:call).with(@full_name, @args, @token)
18
+ @method.call(@args)
19
+ end
20
+
21
+ it "sends the response to Result.process" do
22
+ response = stub("VK response")
23
+ VkontakteApi::API.stub(:call).and_return(response)
24
+ type = stub("Type")
25
+ @method.stub(:type).and_return(type)
26
+
27
+ VkontakteApi::Result.should_receive(:process).with(response, type, nil)
28
+ @method.call(@args)
29
+ end
30
+ end
31
+
32
+ describe "#full_name" do
33
+ before(:each) do
34
+ resolver = Hashie::Mash.new(:name => 'name_space')
35
+ @name = 'name'
36
+ @method = VkontakteApi::Method.new(@name, :resolver => resolver)
37
+ end
38
+
39
+ it "sends each part to #camelize" do
40
+ @method.send(:full_name).should == 'nameSpace.name'
41
+ end
42
+ end
43
+
44
+ describe "#type" do
45
+ context "with a usual name" do
46
+ it "returns :anything" do
47
+ method = VkontakteApi::Method.new('get')
48
+ method.send(:type).should == :anything
49
+ end
50
+ end
51
+
52
+ context "with a predicate name" do
53
+ it "returns :boolean" do
54
+ method = VkontakteApi::Method.new('is_app_user?')
55
+ method.send(:type).should == :boolean
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe VkontakteApi::Namespace do
4
+ # nothing to spec here yet
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe VkontakteApi::Resolvable do
4
+ before(:each) do
5
+ @class = Class.new do
6
+ include VkontakteApi::Resolvable
7
+ end
8
+ end
9
+
10
+ describe "#initialize" do
11
+ it "should save name and resolver" do
12
+ name = stub("Name")
13
+ token = stub("Token")
14
+ resolver = Hashie::Mash.new(:token => token)
15
+
16
+ resolvable = @class.new(name, :resolver => resolver)
17
+ resolvable.name.should == name
18
+ resolvable.token.should == token
19
+ end
20
+ end
21
+ end
@@ -1,177 +1,94 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Resolver do
4
- describe "#method_missing" do
5
- before(:each) do
6
- @result = stub("API result")
7
- @result.stub(:respond_to?).and_return(false)
8
-
9
- VkontakteApi::API.stub(:call).and_return(@result)
10
- @args = {:arg_name => 'arg_value'}
11
- @token = stub("Access token")
12
- end
13
-
14
- context "with a nil @namespace" do
15
- before(:each) do
16
- VkontakteApi::Resolver.stub(:vk_method_name).and_return(['apiMethod', :anything])
17
- @resolver = VkontakteApi::Resolver.new(:access_token => @token)
18
- end
19
-
20
- context "with method_name not from namespaces" do
21
- it "calls #vk_method_name with method name and nil namespace" do
22
- VkontakteApi::Resolver.should_receive(:vk_method_name).with('api_method', nil)
23
- @resolver.api_method
24
- end
25
-
26
- it "calls #api_call with full VK method name and access_token" do
27
- @args.should_receive(:[]=).with(:access_token, @token)
28
- VkontakteApi::API.should_receive(:call).with('apiMethod', @args)
29
- @resolver.api_method(@args).should == @result
30
- end
31
- end
32
-
33
- context "with method_name from namespaces" do
34
- it "return a new resolver with the corresponding @namespace" do
35
- new_resolver = @resolver.friends
36
- new_resolver.should be_a(VkontakteApi::Resolver)
37
- new_resolver.namespace.should == 'friends'
38
- end
39
- end
4
+ before(:each) do
5
+ @class = Class.new do
6
+ include VkontakteApi::Resolver
40
7
 
41
- context "without a token" do
42
- before(:each) do
43
- @resolver = VkontakteApi::Resolver.new
44
- end
45
-
46
- it "calls #api_call with full VK method name but without a token" do
47
- @args.should_not_receive(:[]=).with(:access_token, anything)
48
- VkontakteApi::API.should_receive(:call).with('apiMethod', @args)
49
- @resolver.api_method(@args).should == @result
50
- end
8
+ def initialize(name)
9
+ @name = name
51
10
  end
52
11
  end
53
-
54
- context "with a non-nil @namespace" do
55
- before(:each) do
56
- @resolver = VkontakteApi::Resolver.new(:namespace => 'friends')
57
- VkontakteApi::Resolver.stub(:vk_method_name).and_return('friends.apiMethod')
58
- end
59
-
60
- it "calls #vk_method_name with method name and @namespace" do
61
- VkontakteApi::Resolver.should_receive(:vk_method_name).with('api_method', 'friends')
62
- @resolver.api_method
63
- end
12
+ end
13
+
14
+ describe "#method_missing" do
15
+ before(:each) do
16
+ @resolver = @class.new(:trololo)
17
+ @token = stub("Token")
18
+ @resolver.stub(:token).and_return(@token)
64
19
  end
65
20
 
66
- context "with a non-enumerable result" do
67
- before(:each) do
68
- @resolver = VkontakteApi::Resolver.new
69
-
70
- @type = stub("Type")
71
- VkontakteApi::Resolver.stub(:vk_method_name).and_return([:method_name, @type])
72
-
73
- @typecasted_value = stub("Typecasted value")
74
- @resolver.stub(:typecast).and_return(@typecasted_value)
75
- end
76
-
77
- it "returns #typecast-ed value" do
78
- @resolver.should_receive(:typecast).with(@result, @type).and_return(@typecasted_value)
79
- @resolver.method_name.should == @typecasted_value
80
- end
81
-
82
- context "when block_given?" do
83
- it "yields the #typecast-ed value and returns the result of the block" do
84
- block_result = stub("Block result")
85
- @typecasted_value.should_receive(:result_method).and_return(block_result)
86
-
87
- @resolver.api_method do |result|
88
- result.result_method
89
- end.should == block_result
90
- end
21
+ context "called with a namespace" do
22
+ it "returns a Namespace instance" do
23
+ namespace = @resolver.users
24
+ namespace.should be_a(VkontakteApi::Namespace)
91
25
  end
92
26
  end
93
27
 
94
- context "with an enumerable result" do
28
+ context "called with a method" do
95
29
  before(:each) do
96
- @resolver = VkontakteApi::Resolver.new
97
-
98
- @element1 = stub("First element")
99
- @element2 = stub("Second element")
100
- @enumerable_result = [@element1, @element2]
101
- VkontakteApi::API.stub(:call).and_return(@enumerable_result)
30
+ @result = stub("Result")
31
+ @method = stub("Method", :call => @result)
32
+ VkontakteApi::Method.stub(:new).and_return(@method)
102
33
  end
103
34
 
104
- it "returns the untouched value" do
105
- @resolver.method_name.should == @enumerable_result
35
+ it "creates a Method instance" do
36
+ VkontakteApi::Method.should_receive(:new).with('get', :resolver => @resolver.resolver)
37
+ @resolver.get(:id => 1)
106
38
  end
107
39
 
108
- context "when block_given?" do
109
- it "yields each element untouched to the block" do
110
- result1 = stub("First element after result_method")
111
- result2 = stub("Second element after result_method")
112
- @element1.should_receive(:result_method).and_return(result1)
113
- @element2.should_receive(:result_method).and_return(result2)
114
-
115
- @resolver.api_method do |result|
116
- result.result_method
117
- end.should == [result1, result2]
118
- end
40
+ it "calls Method#call and returns the result" do
41
+ @method.should_receive(:call).with(:id => 1)
42
+ @resolver.get(:id => 1).should == @result
119
43
  end
120
44
  end
121
45
  end
122
46
 
123
- describe ".vk_method_name" do
124
- context "with a usual method name" do
125
- context "without a namespace" do
126
- it "returns 'methodName'" do
127
- VkontakteApi::Resolver.vk_method_name(:api_method).first.should == 'apiMethod'
128
- end
129
- end
130
-
131
- context "with a namespace" do
132
- it "returns 'namespace.methodName'" do
133
- VkontakteApi::Resolver.vk_method_name(:api_method, 'namespace').first.should == 'namespace.apiMethod'
134
- end
135
- end
136
-
137
- it "returns :anything as a type" do
138
- VkontakteApi::Resolver.vk_method_name(:api_method).last.should == :anything
139
- end
47
+ describe "#resolver" do
48
+ before(:each) do
49
+ @name = stub("Name")
50
+ @resolver = @class.new(@name)
51
+ @token = stub("Token")
52
+ @resolver.stub(:token).and_return(@token)
140
53
  end
141
54
 
142
- context "with a predicate method name" do
143
- it "returns method name without '?'" do
144
- VkontakteApi::Resolver.vk_method_name(:is_it_working?).first.should == 'isItWorking'
145
- end
146
-
147
- it "returns :boolean as a type" do
148
- VkontakteApi::Resolver.vk_method_name(:is_it_working?).last.should == :boolean
149
- end
55
+ it "returns a Hashie::Mash with a name and a token" do
56
+ r = @resolver.resolver
57
+ r.name.should == @name
58
+ r.token.should == @token
59
+ end
60
+
61
+ it "caches the result" do
62
+ @mash = stub("Mash", :name => @name, :token => @token)
63
+ Hashie::Mash.should_receive(:new).once.and_return(@mash)
64
+ 5.times { @resolver.resolver }
150
65
  end
151
66
  end
152
67
 
153
- describe "#typecast" do
68
+ describe ".namespaces" do
154
69
  before(:each) do
155
- @resolver = VkontakteApi::Resolver.new
70
+ VkontakteApi::Resolver.instance_variable_set(:@namespaces, nil)
156
71
  end
157
72
 
158
- context "with an :anything type" do
159
- it "returns it's argument untouched" do
160
- @resolver.send(:typecast, :some_arg, :anything).should == :some_arg
73
+ context "on first call" do
74
+ it "loads namespaces from a file" do
75
+ filename = stub("Filename")
76
+ File.should_receive(:expand_path).and_return(filename)
77
+ namespaces = stub("Namespaces list")
78
+ YAML.should_receive(:load_file).with(filename).and_return(namespaces)
79
+
80
+ VkontakteApi::Resolver.namespaces
161
81
  end
162
82
  end
163
83
 
164
- context "with a :boolean type" do
165
- context "and zero result" do
166
- it "returns false" do
167
- @resolver.send(:typecast, '0', :boolean).should == false
168
- end
84
+ context "on subsequent calls" do
85
+ before(:each) do
86
+ VkontakteApi::Resolver.namespaces
169
87
  end
170
88
 
171
- context "and non-zero parameter" do
172
- it "returns true" do
173
- @resolver.send(:typecast, '1', :boolean).should == true
174
- end
89
+ it "returns the cached list" do
90
+ YAML.should_not_receive(:load_file)
91
+ VkontakteApi::Resolver.namespaces
175
92
  end
176
93
  end
177
94
  end