Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix require that is breaking the world.

  • Loading branch information...
commit bad70611b8786832b3a5ca767052b9cc389bd596 0 parents
mpd authored
23 .autotest
@@ -0,0 +1,23 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+# Autotest.add_hook :initialize do |at|
+# at.extra_files << "../some/external/dependency.rb"
+#
+# at.libs << ":../some/external"
+#
+# at.add_exception 'vendor'
+#
+# at.add_mapping(/dependency.rb/) do |f, _|
+# at.files_matching(/test_.*rb$/)
+# end
+#
+# %w(TestA TestB).each do |klass|
+# at.extra_class_map[klass] = "test/test_misc.rb"
+# end
+# end
+
+# Autotest.add_hook :run_command do |at|
+# system "rake build"
+# end
1  .gitignore
@@ -0,0 +1 @@
+pkg
75 History.txt
@@ -0,0 +1,75 @@
+=== 0.1.1 / 2010-05-11
+
+* 1 minor fix
+
+ * Added explicit tmail dependency in gemspec
+ * Added better README tutorial content
+
+=== 0.0.9 / 2010-04-17
+
+* 1 bugfix
+
+ * Fixed content-transfer-encoding when sending email
+
+=== 0.0.8 / 2009-12-23
+
+* 1 bugfix
+
+ * Fixed attaching a file to an empty message
+
+=== 0.0.7 / 2009-12-23
+
+* 1 bugfix
+
+ * Improved multipart message parsing reliability
+
+=== 0.0.6 / 2009-12-21
+
+* 1 bugfix
+
+ * Fixed multipart parsing for when the boundary is marked in quotes.
+
+=== 0.0.5 / 2009-12-16
+
+* 1 bugfix
+
+ * Fixed IMAP initializer to work with Ruby 1.9's net/imap
+
+* 4 minor enhancements
+
+ * Better logout depending on the IMAP connection itself
+ * Added MIME::Message#text and MIME::Message#html for easier access to an email body
+ * Improved the MIME-parsing API slightly
+ * Added some tests
+
+=== 0.0.4 / 2009-11-30
+
+* 4 minor enhancement
+
+ * Added label creation (thanks to Justin Perkins / http://github.com/justinperkins)
+ * Made the gem login automatically when first needed
+ * Added an optional block on the Gmail.new object that will login and logout for you
+ * Added several search options (thanks to Mikkel Malmberg / http://github.com/mikker)
+
+=== 0.0.3 / 2009-11-19
+
+* 1 bugfix
+
+ * Fixed MIME::Message#content= for messages without an encoding
+
+* 1 minor enhancement
+
+ * Added Gmail#new_message
+
+=== 0.0.2 / 2009-11-18
+
+* 1 minor enhancement
+
+ * Made all of the examples in the README possible
+
+=== 0.0.1 / 2009-11-18
+
+* 1 major enhancement
+
+ * Birthday!
+
14 Manifest.txt
@@ -0,0 +1,14 @@
+.autotest
+History.txt
+lib/gmail/mailbox.rb
+lib/gmail/message.rb
+lib/gmail.rb
+lib/ietf/rfc2045.rb
+lib/ietf/rfc822.rb
+lib/mime/entity.rb
+lib/mime/entity_tmail.rb
+lib/mime/message.rb
+lib/smtp_tls.rb
+Manifest.txt
+Rakefile
+README.markdown
172 README.markdown
@@ -0,0 +1,172 @@
+# ruby-gmail
+
+* Homepage: [http://dcparker.github.com/ruby-gmail/](http://dcparker.github.com/ruby-gmail/)
+* Code: [http://github.com/dcparker/ruby-gmail](http://github.com/dcparker/ruby-gmail)
+* Gem: [http://gemcutter.org/gems/ruby-gmail](http://gemcutter.org/gems/ruby-gmail)
+
+## Author(s)
+
+* Daniel Parker of BehindLogic.com
+
+Extra thanks for specific feature contributions from:
+
+ * [Justin Perkins](http://github.com/justinperkins)
+ * [Mikkel Malmberg](http://github.com/mikker)
+ * [Julien Blanchard](http://github.com/julienXX)
+ * [Federico Galassi](http://github.com/fgalassi)
+
+## Description
+
+A Rubyesque interface to Gmail, with all the tools you'll need. Search, read and send multipart emails; archive, mark as read/unread, delete emails; and manage labels.
+
+## Features
+
+* Search emails
+* Read emails (handles attachments)
+* Emails: Label, archive, delete, mark as read/unread/spam
+* Create and delete labels
+* Create and send multipart email messages in plaintext and/or html, with inline images and attachments
+* Utilizes Gmail's IMAP & SMTP, MIME-type detection and parses and generates MIME properly.
+
+## Problems:
+
+* May not correctly read malformed MIME messages. This could possibly be corrected by having IMAP parse the MIME structure.
+* Cannot grab the plain or html message without also grabbing attachments. It might be nice to lazy-[down]load attachments.
+
+## Example Code:
+
+### 1) Require gmail
+
+ require 'gmail'
+
+### 2) Start an authenticated gmail session
+
+ # If you pass a block, the session will be passed into the block,
+ # and the session will be logged out after the block is executed.
+ gmail = Gmail.new(username, password)
+ # ...do things...
+ gmail.logout
+
+ Gmail.new(username, password) do |gmail|
+ # ...do things...
+ end
+
+### 3) Count and gather emails!
+
+ # Get counts for messages in the inbox
+ gmail.inbox.count
+ gmail.inbox.count(:unread)
+ gmail.inbox.count(:read)
+
+ # Count with some criteria
+ gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
+ gmail.inbox.count(:on => Date.parse("2010-04-15"))
+ gmail.inbox.count(:from => "myfriend@gmail.com")
+ gmail.inbox.count(:to => "directlytome@gmail.com")
+
+ # Combine flags and options
+ gmail.inbox.count(:unread, :from => "myboss@gmail.com")
+
+ # Labels work the same way as inbox
+ gmail.mailbox('Urgent').count
+
+ # Getting messages works the same way as counting: optional flag, and optional arguments
+ # Remember that every message in a conversation/thread will come as a separate message.
+ gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "myboss@gmail.com")
+
+### 4) Work with emails!
+
+ # any news older than 4-20, mark as read and archive it...
+ gmail.inbox.emails(:before => Date.parse("2010-04-20"), :from => "news@nbcnews.com").each do |email|
+ email.mark(:read) # can also mark :unread or :spam
+ email.archive!
+ end
+
+ # delete emails from X...
+ gmail.inbox.emails(:from => "x-fiancé@gmail.com").each do |email|
+ email.delete!
+ end
+
+ # Save all attachments in the "Faxes" label to a folder
+ folder = "/where/ever"
+ gmail.mailbox("Faxes").emails.each do |email|
+ if !email.attachments.empty?
+ email.save_attachments_to(folder)
+ end
+ end
+
+ # Save just the first attachment from the newest unread email (assuming pdf)
+ # For #save_to_file:
+ # + provide a path - save to attachment filename in path
+ # + provide a filename - save to file specified
+ # + provide no arguments - save to attachment filename in current directory
+ email = gmail.inbox.emails(:unread).first
+ email.attachments[0].save_to_file("/path/to/location")
+
+ # Add a label to a message
+ email.label("Faxes")
+
+ # Or "move" the message to a label
+ email.move_to("Faxes")
+
+### 5) Create new emails!
+
+Creating emails now uses the amazing [Mail](http://rubygems.org/gems/mail) rubygem. See its [documentation here](http://github.com/mikel/mail). Ruby-gmail will automatically configure your Mail emails to be sent via your Gmail account's SMTP, so they will be in your Gmail's "Sent" folder. Also, no need to specify the "From" email either, because ruby-gmail will set it for you.
+
+ gmail.deliver do
+ to "email@example.com"
+ subject "Having fun in Puerto Rico!"
+ text_part do
+ body "Text of plaintext message."
+ end
+ html_part do
+ body "<p>Text of <em>html</em> message.</p>"
+ end
+ add_file "/path/to/some_image.jpg"
+ end
+ # Or, generate the message first and send it later
+ email = gmail.generate_message do
+ to "email@example.com"
+ subject "Having fun in Puerto Rico!"
+ body "Spent the day on the road..."
+ end
+ email.deliver!
+ # Or...
+ gmail.deliver(email)
+
+## Requirements
+
+* ruby
+* net/smtp
+* net/imap
+* tmail
+* shared-mime-info rubygem (for MIME-detection when attaching files)
+
+## Install
+
+ gem install ruby-gmail
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2009 BehindLogic
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Rakefile
@@ -0,0 +1,22 @@
+# -*- ruby -*-
+
+require 'rubygems'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "ruby-gmail"
+ gem.summary = %Q{A Rubyesque interface to Gmail, with all the tools you'll need.}
+ gem.description = %Q{A Rubyesque interface to Gmail, with all the tools you'll need. Search, read and send multipart emails; archive, mark as read/unread, delete emails; and manage labels.}
+ gem.email = "gems@behindlogic.com"
+ gem.homepage = "http://dcparker.github.com/ruby-gmail"
+ gem.authors = ["BehindLogic"]
+ gem.post_install_message = "\n\033[34mIf ruby-gmail saves you TWO hours of work, want to compensate me for, like, a half-hour?\nSupport me in making new and better gems:\033[0m \033[31;4mhttp://pledgie.com/campaigns/7087\033[0m\n\n"
+ gem.add_dependency('shared-mime-info', '>= 0')
+ gem.add_dependency('mail', '>= 2.2.1')
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
1  VERSION
@@ -0,0 +1 @@
+0.2.1
168 lib/gmail.rb
@@ -0,0 +1,168 @@
+require 'net/imap'
+
+class Gmail
+ VERSION = '0.0.9'
+
+ class NoLabel < RuntimeError; end
+
+ ##################################
+ # Gmail.new(username, password)
+ ##################################
+ def initialize(username, password)
+ # This is to hide the username and password, not like it REALLY needs hiding, but ... you know.
+ # Could be helpful when demoing the gem in irb, these bits won't show up that way.
+ class << self
+ class << self
+ attr_accessor :username, :password
+ end
+ end
+ meta.username = username =~ /@/ ? username : username + '@gmail.com'
+ meta.password = password
+ @imap = Net::IMAP.new('imap.gmail.com',993,true,nil,false)
+ if block_given?
+ login # This is here intentionally. Normally, we get auto logged-in when first needed.
+ yield self
+ logout
+ end
+ end
+
+ ###########################
+ # READING EMAILS
+ #
+ # gmail.inbox
+ # gmail.label('News')
+ #
+ ###########################
+
+ def inbox
+ in_label('inbox')
+ end
+
+ def create_label(name)
+ imap.create(name)
+ end
+
+ # List the available labels
+ def labels
+ (imap.list("", "%") + imap.list("[Gmail]/", "%")).inject([]) { |labels,label|
+ label[:name].each_line { |l| labels << l }; labels }
+ end
+
+ # gmail.label(name)
+ def label(name)
+ mailboxes[name] ||= Mailbox.new(self, mailbox)
+ end
+ alias :mailbox :label
+
+ ###########################
+ # MAKING EMAILS
+ #
+ # gmail.generate_message do
+ # ...inside Mail context...
+ # end
+ #
+ # gmail.deliver do ... end
+ #
+ # mail = Mail.new...
+ # gmail.deliver!(mail)
+ ###########################
+ def generate_message(&block)
+ require 'net/smtp'
+ require 'smtp_tls'
+ require 'mail'
+ mail = Mail.new(&block)
+ mail.delivery_method(*smtp_settings)
+ mail
+ end
+
+ def deliver(mail=nil, &block)
+ require 'net/smtp'
+ require 'smtp_tls'
+ require 'mail'
+ mail = Mail.new(&block) if block_given?
+ mail.delivery_method(*smtp_settings)
+ mail.from = meta.username unless mail.from
+ mail.deliver!
+ end
+
+ ###########################
+ # LOGIN
+ ###########################
+ def login
+ res = @imap.login(meta.username, meta.password)
+ @logged_in = true if res.name == 'OK'
+ end
+ def logged_in?
+ !!@logged_in
+ end
+ # Log out of gmail
+ def logout
+ if logged_in?
+ res = @imap.logout
+ @logged_in = false if res.name == 'OK'
+ end
+ end
+
+ def in_mailbox(mailbox, &block)
+ if block_given?
+ mailbox_stack << mailbox
+ unless @selected == mailbox.name
+ imap.select(mailbox.name)
+ @selected = mailbox.name
+ end
+ value = block.arity == 1 ? block.call(mailbox) : block.call
+ mailbox_stack.pop
+ # Select previously selected mailbox if there is one
+ if mailbox_stack.last
+ imap.select(mailbox_stack.last.name)
+ @selected = mailbox.name
+ end
+ return value
+ else
+ mailboxes[mailbox] ||= Mailbox.new(self, mailbox)
+ end
+ end
+ alias :in_label :in_mailbox
+
+ ###########################
+ # Other...
+ ###########################
+ def inspect
+ "#<Gmail:#{'0x%x' % (object_id << 1)} (#{meta.username}) #{'dis' if !logged_in?}connected>"
+ end
+
+ # Accessor for @imap, but ensures that it's logged in first.
+ def imap
+ unless logged_in?
+ login
+ at_exit { logout } # Set up auto-logout for later.
+ end
+ @imap
+ end
+
+ private
+ def mailboxes
+ @mailboxes ||= {}
+ end
+ def mailbox_stack
+ @mailbox_stack ||= []
+ end
+ def meta
+ class << self; self end
+ end
+ def domain
+ meta.username.split('@')[0]
+ end
+ def smtp_settings
+ [:smtp, {:address => "smtp.gmail.com",
+ :port => 587,
+ :domain => domain,
+ :user_name => meta.username,
+ :password => meta.password,
+ :authentication => 'plain',
+ :enable_starttls_auto => true}]
+ end
+end
+
+require 'gmail/mailbox'
+require 'gmail/message'
71 lib/gmail/mailbox.rb
@@ -0,0 +1,71 @@
+require 'date'
+require 'time'
+class Object
+ def to_imap_date
+ Date.parse(to_s).strftime("%d-%B-%Y")
+ end
+end
+
+class Gmail
+ class Mailbox
+ attr_reader :name
+
+ def initialize(gmail, name)
+ @gmail = gmail
+ @name = name
+ end
+
+ def inspect
+ "<#Mailbox name=#{@name}>"
+ end
+
+ def to_s
+ name
+ end
+
+ # Method: emails
+ # Args: [ :all | :unread | :read ]
+ # Opts: {:since => Date.new}
+ def emails(key_or_opts = :all, opts={})
+ if key_or_opts.is_a?(Hash) && opts.empty?
+ search = ['ALL']
+ opts = key_or_opts
+ elsif key_or_opts.is_a?(Symbol) && opts.is_a?(Hash)
+ aliases = {
+ :all => ['ALL'],
+ :unread => ['UNSEEN'],
+ :read => ['SEEN']
+ }
+ search = aliases[key_or_opts]
+ elsif key_or_opts.is_a?(Array) && opts.empty?
+ search = key_or_opts
+ else
+ raise ArgumentError, "Couldn't make sense of arguments to #emails - should be an optional hash of options preceded by an optional read-status bit; OR simply an array of parameters to pass directly to the IMAP uid_search call."
+ end
+ if !opts.empty?
+ # Support for several search macros
+ # :before => Date, :on => Date, :since => Date, :from => String, :to => String
+ search.concat ['SINCE', opts[:after].to_imap_date] if opts[:after]
+ search.concat ['BEFORE', opts[:before].to_imap_date] if opts[:before]
+ search.concat ['ON', opts[:on].to_imap_date] if opts[:on]
+ search.concat ['FROM', opts[:from]] if opts[:from]
+ search.concat ['TO', opts[:to]] if opts[:to]
+ end
+
+ # puts "Gathering #{(aliases[key] || key).inspect} messages for mailbox '#{name}'..."
+ @gmail.in_mailbox(self) do
+ @gmail.imap.uid_search(search).collect { |uid| messages[uid] ||= Message.new(@gmail, self, uid) }
+ end
+ end
+
+ # This is a convenience method that really probably shouldn't need to exist, but it does make code more readable
+ # if seriously all you want is the count of messages.
+ def count(*args)
+ emails(*args).length
+ end
+
+ def messages
+ @messages ||= {}
+ end
+ end
+end
107 lib/gmail/message.rb
@@ -0,0 +1,107 @@
+class Gmail
+ class Message
+ def initialize(gmail, mailbox, uid)
+ @gmail = gmail
+ @mailbox = mailbox
+ @uid = uid
+ end
+
+ def inspect
+ "<#Message:#{object_id} mailbox=#{@mailbox.name}#{' uid='+@uid.to_s if @uid}#{' message_id='+@message_id.to_s if @message_id}>"
+ end
+
+ # Auto IMAP info
+ def uid
+ @uid ||= @gmail.imap.uid_search(['HEADER', 'Message-ID', message_id])[0]
+ end
+
+ # IMAP Operations
+ def flag(flg)
+ @gmail.in_mailbox(@mailbox) do
+ @gmail.imap.uid_store(uid, "+FLAGS", [flg])
+ end ? true : false
+ end
+
+ def unflag(flg)
+ @gmail.in_mailbox(@mailbox) do
+ @gmail.imap.uid_store(uid, "-FLAGS", [flg])
+ end ? true : false
+ end
+
+ # Gmail Operations
+ def mark(flag)
+ case flag
+ when :read
+ flag(:Seen)
+ when :unread
+ unflag(:Seen)
+ when :deleted
+ flag(:Deleted)
+ when :spam
+ move_to('[Gmail]/Spam')
+ end ? true : false
+ end
+
+ def delete!
+ @mailbox.messages.delete(uid)
+ flag(:Deleted)
+ end
+
+ def label(name)
+ @gmail.in_mailbox(@mailbox) do
+ begin
+ @gmail.imap.uid_copy(uid, name)
+ rescue Net::IMAP::NoResponseError
+ raise Gmail::NoLabel, "No label `#{name}' exists!"
+ end
+ end
+ end
+
+ def label!(name)
+ @gmail.in_mailbox(@mailbox) do
+ begin
+ @gmail.imap.uid_copy(uid, name)
+ rescue Net::IMAP::NoResponseError
+ # need to create the label first
+ @gmail.create_label(name)
+ retry
+ end
+ end
+ end
+
+ # We're not sure of any 'labels' except the 'mailbox' we're in at the moment.
+ # Research whether we can find flags that tell which other labels this email is a part of.
+ # def remove_label(name)
+ # end
+
+ def move_to(name)
+ label(name) && delete!
+ end
+
+ def archive!
+ move_to('[Gmail]/All Mail')
+ end
+
+ def save_attachments_to(path=nil)
+ attachments.each {|a| a.save_to_file(path) }
+ end
+
+ private
+
+ # Parsed MIME message object
+ def message
+ require 'mail'
+ _body = @gmail.in_mailbox(@mailbox) { @gmail.imap.uid_fetch(uid, "RFC822")[0].attr["RFC822"] }
+ @message ||= Mail.new(_body)
+ end
+
+ # Delegate all other methods to the Mail message
+ def method_missing(*args, &block)
+ if block_given?
+ message.send(*args, &block)
+ else
+ message.send(*args)
+ end
+ end
+ end
+end
24 lib/mail/part.rb
@@ -0,0 +1,24 @@
+module Mail
+ class Message
+ def attachment?
+ !!find_attachment
+ end
+ end
+ class Part < Message
+ def save_to_file(path=nil)
+ return false unless attachment?
+ fname = path if path && !File.exists?(path) # If path doesn't exist, assume it's a filename
+ fname ||= path + '/' + filename if path && File.directory?(path) # If path does exist, and we're saving an attachment, use the attachment filename
+ fname ||= (path || filename) # Use the path, or the attachment filename
+ if File.directory?(fname)
+ i = 0
+ begin
+ i += 1
+ fname = fname + "/attachment-#{i}"
+ end until !File.exists(fname)
+ end
+ # After all that trouble to get a filename to save to...
+ File.open(fname, 'w') { |f| f << read }
+ end
+ end
+end
94 lib/smtp_tls.rb
@@ -0,0 +1,94 @@
+require "openssl"
+require "net/smtp"
+
+Net::SMTP.class_eval do
+
+ def self.start( address, port = nil,
+ helo = 'localhost.localdomain',
+ user = nil, secret = nil, authtype = nil, use_tls = false,
+ &block) # :yield: smtp
+ new(address, port).start(helo, user, secret, authtype, use_tls, &block)
+ end
+
+ def start( helo = 'localhost.localdomain',
+ user = nil, secret = nil, authtype = nil ) # :yield: smtp
+ start_method = starttls_auto? ? :do_tls_start : :do_start
+ if block_given?
+ begin
+ send(start_method, helo, user, secret, authtype)
+ return yield(self)
+ ensure
+ do_finish
+ end
+ else
+ send(start_method, helo, user, secret, authtype)
+ return self
+ end
+ end
+
+ private
+
+ def do_tls_start(helodomain, user, secret, authtype)
+ raise IOError, 'SMTP session already started' if @started
+ if RUBY_VERSION == '1.8.6'
+ check_auth_args(user, secret, authtype) if user or secret
+ else
+ check_auth_args(user, secret) if user or secret
+ end
+
+ sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
+ @socket = Net::InternetMessageIO.new(sock)
+ @socket.read_timeout = 60 #@read_timeout
+ @socket.debug_output = STDERR #@debug_output
+
+ check_response(critical { recv_response() })
+ do_helo(helodomain)
+
+ raise 'openssl library not installed' unless defined?(OpenSSL)
+ starttls
+ ssl = OpenSSL::SSL::SSLSocket.new(sock)
+ ssl.sync_close = true
+ ssl.connect
+ @socket = Net::InternetMessageIO.new(ssl)
+ @socket.read_timeout = 60 #@read_timeout
+ @socket.debug_output = STDERR #@debug_output
+ do_helo(helodomain)
+
+ authenticate user, secret, authtype if user
+ @started = true
+ ensure
+ unless @started
+ # authentication failed, cancel connection.
+ @socket.close if not @started and @socket and not @socket.closed?
+ @socket = nil
+ end
+ end
+
+ def do_helo(helodomain)
+ begin
+ if @esmtp
+ ehlo helodomain
+ else
+ helo helodomain
+ end
+ rescue Net::ProtocolError
+ if @esmtp
+ @esmtp = false
+ @error_occured = false
+ retry
+ end
+ raise
+ end
+ end
+
+ def starttls
+ getok('STARTTLS')
+ end
+
+ def quit
+ begin
+ getok('QUIT')
+ rescue EOFError, OpenSSL::SSL::SSLError
+ end
+ end
+end
66 ruby-gmail.gemspec
@@ -0,0 +1,66 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{ruby-gmail}
+ s.version = "0.2.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["BehindLogic"]
+ s.date = %q{2010-05-14}
+ s.description = %q{A Rubyesque interface to Gmail, with all the tools you'll need. Search, read and send multipart emails; archive, mark as read/unread, delete emails; and manage labels.}
+ s.email = %q{gems@behindlogic.com}
+ s.extra_rdoc_files = [
+ "README.markdown"
+ ]
+ s.files = [
+ ".autotest",
+ ".gitignore",
+ "History.txt",
+ "Manifest.txt",
+ "README.markdown",
+ "Rakefile",
+ "VERSION",
+ "lib/gmail.rb",
+ "lib/gmail/mailbox.rb",
+ "lib/gmail/message.rb",
+ "lib/mail/part.rb",
+ "lib/smtp_tls.rb",
+ "ruby-gmail.gemspec",
+ "test/test_gmail.rb",
+ "test/test_helper.rb"
+ ]
+ s.homepage = %q{http://dcparker.github.com/ruby-gmail}
+ s.post_install_message = %q{
+If ruby-gmail saves you TWO hours of work, want to compensate me for, like, a half-hour?
+Support me in making new and better gems: http://pledgie.com/campaigns/7087
+
+}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{A Rubyesque interface to Gmail, with all the tools you'll need.}
+ s.test_files = [
+ "test/test_gmail.rb",
+ "test/test_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<shared-mime-info>, [">= 0"])
+ s.add_runtime_dependency(%q<mail>, [">= 2.2.1"])
+ else
+ s.add_dependency(%q<shared-mime-info>, [">= 0"])
+ s.add_dependency(%q<mail>, [">= 2.2.1"])
+ end
+ else
+ s.add_dependency(%q<shared-mime-info>, [">= 0"])
+ s.add_dependency(%q<mail>, [">= 2.2.1"])
+ end
+end
+
72 test/test_gmail.rb
@@ -0,0 +1,72 @@
+require 'test_helper'
+
+class GmailTest < Test::Unit::TestCase
+ def test_initialize
+ imap = mock('imap')
+ Net::IMAP.expects(:new).with('imap.gmail.com', 993, true, nil, false).returns(imap)
+ gmail = Gmail.new('test', 'password')
+ end
+
+ def test_imap_does_login
+ setup_mocks(:at_exit => true)
+
+ @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ @imap.expects(:login).with('test@gmail.com', 'password')
+ @gmail.imap
+ end
+
+ def test_imap_does_login_only_once
+ setup_mocks(:at_exit => true)
+
+ @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ @imap.expects(:login).with('test@gmail.com', 'password')
+ @gmail.imap
+ @gmail.imap
+ @gmail.imap
+ end
+
+ def test_imap_does_login_without_appending_gmail_domain
+ setup_mocks(:at_exit => true)
+
+ @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ @imap.expects(:login).with('test@gmail.com', 'password')
+ @gmail.imap
+ end
+
+ def test_imap_logs_out
+ setup_mocks(:at_exit => true)
+
+ @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ @imap.expects(:login).with('test@gmail.com', 'password')
+ @gmail.imap
+ @imap.expects(:logout).returns(true)
+ @gmail.logout
+ end
+
+ def test_imap_logout_does_nothing_if_not_logged_in
+ setup_mocks
+
+ @imap.expects(:disconnected?).returns(true)
+ @imap.expects(:logout).never
+ @gmail.logout
+ end
+
+ def test_imap_calls_create_label
+ setup_mocks(:at_exit => true)
+ @imap.expects(:disconnected?).at_least_once.returns(true).then.returns(false)
+ @imap.expects(:login).with('test@gmail.com', 'password')
+ @imap.expects(:create).with('foo')
+ @gmail.create_label('foo')
+ end
+
+ private
+ def setup_mocks(options = {})
+ options = {:at_exit => false}.merge(options)
+ @imap = mock('imap')
+ Net::IMAP.expects(:new).with('imap.gmail.com', 993, true, nil, false).returns(@imap)
+ @gmail = Gmail.new('test@gmail.com', 'password')
+
+ # need this for the at_exit block that auto-exits after this test method completes
+ @imap.expects(:logout).at_least(0) if options[:at_exit]
+ end
+end
4 test/test_helper.rb
@@ -0,0 +1,4 @@
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+require 'gmail'
Please sign in to comment.
Something went wrong with that request. Please try again.