Skip to content

Commit

Permalink
Fix small error in client.rb, clients log in by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuan Duc Nguyen committed Feb 20, 2011
1 parent 1ce8261 commit fe02fce
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 46 deletions.
48 changes: 27 additions & 21 deletions lib/gmail.rb
Expand Up @@ -24,8 +24,7 @@ module Gmail

class << self

# Create new Gmail client using given authorization information.
# A client created with <tt>Gmail#new</tt> does not connect and login by default.
# Create new Gmail client and login using given authorization information.
#
# ==== Examples
#
Expand All @@ -41,26 +40,33 @@ class << self
# # ...
# end
#
def new(*args, &block)
client = Client.new(*args)
perform_block(client, &block)
end

# Create new Gmail client and login using given authorization infomation.
def connect(*args, &block)
client = Client.new(*args)
client.connect()
client.login()
perform_block(client, &block)
end

# This version of connect will raise error on failure...
def connect!(*args, &block)
client = Client.new(*args)
client.connect!()
client.login!()
perform_block(client, &block)
%w[new new! connect connect! login login!].each do |method|
define_method(method) do |*args, &block|
client = Client.new(*args)
client.send(method)
perform_block(client, &block)
end
end
# def new(*args, &block)
# client = Client.new(*args)
# perform_block(client, &block)
# end
#
# # Create new Gmail client and login using given authorization infomation.
# def connect(*args, &block)
# client = Client.new(*args)
# client.connect()
# client.login()
# perform_block(client, &block)
# end
#
# # This version of connect will raise error on failure...
# def connect!(*args, &block)
# client = Client.new(*args)
# client.connect!()
# client.login!()
# perform_block(client, &block)
# end

protected

Expand Down
18 changes: 15 additions & 3 deletions lib/gmail/client.rb
Expand Up @@ -20,7 +20,7 @@ def initialize(response, message)

attr_reader :connection
def initialize(*args)
raise AgumentError, 'wrong number of arguments' if args.length < 2
raise ArgumentError, 'wrong number of arguments' if args.length < 2

username = args[0]
password = args[1].is_a?(String) ? args[1] : ''
Expand All @@ -34,10 +34,22 @@ def initialize(*args)
self
end

def connect
connection.connect and connection.login
end
alias :new :connect
alias :login :connect

def connect!
connection.connect! and connection.login!
end
alias :new! :connect!
alias :login! :connect!

# Make access to methods in connection object.
%w[connect connect! login login! logout logged_in? imap username password smtp_settings authentication].each do |method|
%w[logout logged_in? imap username password smtp_settings authentication].each do |method|
define_method(method) do |*args|
@connection.send(method, *args)
connection.send(method, *args)
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/gmail/connection/plain_connection.rb
Expand Up @@ -11,7 +11,8 @@ def login(raise_errors = false)

@logged_in = @imap.login(username, password).name == 'OK'
rescue Net::IMAP::NoResponseError => e
raise_errors and raise Gmail::Client::AuthorizationError.new(e.response, "Couldn't login to given Gmail account: #{username}")
raise_errors and raise Gmail::Client::AuthorizationError.new(e.response,
"Couldn't login to given Gmail account: #{username}")
end
end # PlainConnection
end # Gmail
1 change: 1 addition & 0 deletions spec/client_message_spec.rb
Expand Up @@ -23,6 +23,7 @@

it "should deliver inline composed email" do
mock_client do |client|
client.expects(:deliver).returns(true)
client.deliver do
to TEST_ACCOUNT[0]
subject "Hello world!"
Expand Down
1 change: 0 additions & 1 deletion spec/client_spec.rb
Expand Up @@ -59,7 +59,6 @@
it "should raise error when given Gmail account is invalid and errors enabled" do
lambda {
client = Gmail::Client.new("foo", "bar")
client.connect.should be_true
client.login!.should_not be_true
}.should raise_error(Gmail::Client::AuthorizationError)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/gmail_spec.rb
Expand Up @@ -19,9 +19,9 @@
end
end

it "should not login after a #new method" do
it "should always login after a #new method" do
gmail = Gmail.new(*TEST_ACCOUNT)
gmail.should_not be_logged_in
gmail.should be_logged_in
end
end

Expand Down
23 changes: 16 additions & 7 deletions spec/mailbox_controller_spec.rb
@@ -1,7 +1,16 @@
require 'spec_helper'

describe Gmail::MailboxController, "instance" do
subject { Gmail.connect!(*TEST_ACCOUNT).mailbox_controller }
before(:all) do
@controller = Gmail.connect!(*TEST_ACCOUNT).mailbox_controller
end

after(:all) do
@controller.client.logout
@controller = nil
end

subject { @controller }

it "should get list of all available mailboxes" do
subject.labels.should include("INBOX")
Expand All @@ -14,23 +23,23 @@

it "should be able to create given label" do
subject.create("MYLABEL")
subject.exists?("MYLABEL").should be_true
subject.exist?("MYLABEL").should be_true
subject.create("MYLABEL").should be_false
subject.delete("MYLABEL")
end

it "should be able to remove existing label" do
subject.create("MYLABEL")
subject.delete("MYLABEL").should be_true
subject.exists?("MYLABEL").should be_false
subject.delete("MYLABEL").should be_false
subject.create("MYLABEL 2")
subject.delete("MYLABEL 2").should be_true
subject.exist?("MYLABEL 2").should be_false
subject.delete("MYLABEL 2").should be_false
end

it "should be able to create label with non-ascii characters" do
name = Net::IMAP.decode_utf7("TEST &APYA5AD8-") # TEST äöü
subject.create(name)
subject.delete(name).should be_true
subject.exists?(name).should be_false
subject.exist?(name).should be_false
subject.delete(name).should be_false
end

Expand Down
25 changes: 15 additions & 10 deletions spec/mailbox_spec.rb
@@ -1,27 +1,32 @@
require 'spec_helper'

describe "A Gmail mailbox" do
before(:all) do
@client = Gmail.connect!(*TEST_ACCOUNT)
end

after(:all) do
@client.logout
@client = nil
end

context "on initialize" do
subject { Gmail::Mailbox }

it "should set controller and name" do
mock_client do |client|
mailbox = subject.new(client.mailbox_controller, "TEST")
mailbox.instance_variable_get("@controller").should == client.mailbox_controller
mailbox.name.should == "TEST"
end
mailbox = subject.new(@client.mailbox_controller, "TEST")
mailbox.instance_variable_get("@controller").should == @client.mailbox_controller
mailbox.name.should == "TEST"
end

it "should work in INBOX by default" do
mock_client do |client|
mailbox = subject.new(client.mailbox_controller)
mailbox.name.should == "INBOX"
end
mailbox = subject.new(@client.mailbox_controller)
mailbox.name.should == "INBOX"
end
end

context "instance" do
subject { Gmail.connect!(*TEST_ACCOUNT).all_mail }
subject { @client.all_mail }

it "should be able to count all emails" do
subject.count.should > 0
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -3,11 +3,12 @@

require 'rubygems'
require 'rspec'
require 'mocha'
require 'yaml'
require 'gmail'

RSpec.configure do |config|
config.mock_framework = :rspec
config.mock_framework = :mocha
end

def mock_client(&block)
Expand Down

0 comments on commit fe02fce

Please sign in to comment.