Skip to content

Commit

Permalink
VK::Client#offline?, some fixes and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
7even committed Dec 12, 2012
1 parent 40a2ac0 commit 7273d4c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
12 changes: 10 additions & 2 deletions lib/vkontakte_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize(token = nil)
# token is an OAuth2::AccessToken
@token = token.token
@user_id = token.params['user_id']
@expires_at = Time.at(token.expires_at)
@expires_at = Time.at(token.expires_at) unless token.expires_at.nil?
else
# token is a String or nil
@token = token
Expand All @@ -56,10 +56,18 @@ def authorized?
!@token.nil?
end

# Did the token already expire.
def expired?
@expires_at < Time.now
@expires_at && @expires_at < Time.now
end

# Is the token permanent.
def offline?
@expires_at.nil?
end

# Access rights of this token.
# @return [Array] An array of symbols representing the access rights.
def scope
SCOPE.reject do |access_scope, mask|
(settings & mask).zero?
Expand Down
44 changes: 39 additions & 5 deletions spec/vkontakte_api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@
end
end

describe "#authorized?" do
context "with an unauthorized client" do
it "returns false" do
VkontakteApi::Client.new.should_not be_authorized
end
end

context "with an authorized client" do
it "returns true" do
VkontakteApi::Client.new(@string_token).should be_authorized
end
end
end

describe "#expired?" do
context "with an expired token" do
before(:each) do
Expand All @@ -62,18 +76,38 @@
@client.should_not be_expired
end
end

context "with a String token" do
before(:each) do
@client = VkontakteApi::Client.new(@string_token)
end

it "returns false" do
@client.should_not be_expired
end
end
end

describe "#authorized?" do
context "with an unauthorized client" do
describe "#offline?" do
context "with a usual token" do
it "returns false" do
VkontakteApi::Client.new.should_not be_authorized
VkontakteApi::Client.new(@oauth2_token).should_not be_offline
end
end

context "with an authorized client" do
context "with an offline token" do
before(:each) do
@oauth2_token.stub(:expires_at).and_return(nil)
end

it "returns true" do
VkontakteApi::Client.new(@string_token).should be_authorized
VkontakteApi::Client.new(@oauth2_token).should be_offline
end
end

context "with a String token" do
it "returns true" do
VkontakteApi::Client.new(@string_token).should be_offline
end
end
end
Expand Down

0 comments on commit 7273d4c

Please sign in to comment.