Skip to content

Commit

Permalink
don't allow API requests when api_version is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
7even committed Jun 3, 2018
1 parent 623ecdd commit fe4381a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/vkontakte_api/api.rb
Expand Up @@ -12,9 +12,14 @@ class << self
# @param [Hash] args Method arguments.
# @param [String] token The access token.
# @return [Hashie::Mash] Mashed server response.
# @raise [ArgumentError] raised when the API version is not set.
def call(method_name, args = {}, token = nil)
if VkontakteApi.api_version.nil?
raise ArgumentError, 'You must specify the API version in VkontakteApi configuration'
end

flat_arguments = Utils.flatten_arguments(args)
flat_arguments[:v] ||= VkontakteApi.api_version unless VkontakteApi.api_version.nil?
flat_arguments[:v] ||= VkontakteApi.api_version
connection(url: URL_PREFIX, token: token).send(VkontakteApi.http_verb, method_name, flat_arguments).body
end

Expand Down
32 changes: 17 additions & 15 deletions spec/vkontakte_api/api_spec.rb
Expand Up @@ -19,13 +19,18 @@ def create_connection
describe ".call" do
before(:each) do
create_connection

VkontakteApi.configure do |config|
config.api_version = api_version
end
end

context "called with a token parameter" do
it "sends it to .connection" do
expect(subject).to receive(:connection).with(url: VkontakteApi::API::URL_PREFIX, token: 'token')
subject.call('apiMethod', { some: :params }, 'token')
end
let(:api_version) { double("API version") }
let(:response) { double("API response", body: @result) }

it "sends the token to .connection" do
expect(subject).to receive(:connection).with(url: VkontakteApi::API::URL_PREFIX, token: 'token')
subject.call('apiMethod', { some: :params }, 'token')
end

it "returns the response body" do
Expand All @@ -36,24 +41,21 @@ def create_connection
http_verb = double("HTTP verb")
VkontakteApi.http_verb = http_verb

response = double("Response", body: double)
expect(@connection).to receive(:send).with(http_verb, 'apiMethod', {}).and_return(response)
expect(@connection).to receive(:send).with(http_verb, 'apiMethod', v: api_version).and_return(response)
subject.call('apiMethod')
end

context "when the api_version is set" do
let(:api_version) { double("API version") }
let(:response) { double("API response", body: @result) }

context "when the api_version is not set" do
before(:each) do
VkontakteApi.configure do |config|
config.api_version = api_version
config.api_version = nil
end
end

it "adds it to request params" do
expect(@connection).to receive(:post).with('apiMethod', v: api_version).and_return(response)
subject.call('apiMethod')
it "raises an ArgumentError" do
expect {
subject.call('apiMethod')
}.to raise_error(ArgumentError)
end
end

Expand Down

0 comments on commit fe4381a

Please sign in to comment.