gmail-api-ruby 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24b38375594da2fabd46ccde5cb098b9d5cfebc9
4
+ data.tar.gz: d67652090a269c623f7930ae74660f3b56813d7a
5
+ SHA512:
6
+ metadata.gz: 286c48ccfce4c9551296e2d24fd44e13654fbad378295502e3ddae54ab18b5afae1224bdd34adc2772fe72c3810b94493f4e9f446b0b7dae531c502e4ba48198
7
+ data.tar.gz: 573ca19b9f5f4ffcc8bdd272a6a86d5d0ab21d7414f0bc36ff4f070a343b1ef5fc8a412d1509d4fc582bc7cad65fd0dfa75e8ae83a4791b520a48c65d98c957c
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## rubymine
17
+ .idea
18
+
19
+ ## PROJECT::GENERAL
20
+ coverage
21
+ rdoc
22
+ pkg
23
+ Gemfile.lock
24
+
25
+ ## Gem build
26
+ .gem
27
+
28
+ ## PROJECT::SPECIFIC
29
+ *.rdb
30
+
31
+ ## Test Account details
32
+ spec/account.yml
33
+ env.yml
34
+ spec/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --backtrace
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Gmail gem changelog
2
+ ## 0.0.1 / 2015-01
3
+
4
+ * Birthday!
5
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyrignt (c) 2015 Julien Hobeika
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # GMail for Ruby
2
+
3
+ A Rubyesque interface to Google's GMail, with all the tools you'll need. Search,
4
+ read and send multipart emails, archive, mark as read/unread, delete emails,
5
+ and manage labels. Everything goes through Gmail API (and not throught IMAP or SMTP protocols)
6
+
7
+ # Notice
8
+
9
+ If for your usecase, the gem is too limited there are two solutions for you:
10
+
11
+ 1- Contribute ;)
12
+ 2- Look at Gmail Api doc (https://developers.google.com/gmail/api/v1/reference) to:
13
+ *Do custom query by adding parameters I didn't talk about in my doc
14
+ *If for some reason I forgot to build some methods, note that you can call fully custom request via Gmail.client (Standard Google Api Client)
15
+
16
+ # To Do
17
+
18
+ Write test
19
+ write proper error handling
20
+
21
+ ## Installation
22
+
23
+ gem install gmail-ruby-api
24
+
25
+ ## initialisation
26
+
27
+ Gmail.client_id = "...Your app client id..."
28
+ Gmail.client_secret = "...Your app Secret..."
29
+ Gmail.refresh_token = "...the refresh token of the Gmail account you want to use..."
30
+
31
+ ## Usage
32
+
33
+ require "Gmail"
34
+
35
+ ### Common methods
36
+
37
+ this works for Messages, Labels, Drafts and Threads (everything)
38
+ Gmail::Message.all
39
+ Gmail::Message.all(maxResults: -1) #will parse everything. Use with care. Can through request timeout if used in web apps
40
+ Gmail::Message.get(message_id)
41
+ some_gmail_message.delete
42
+ this will work for Messages and Threads
43
+ some_message.archive
44
+ some_message.un_archive
45
+ some_message.trash
46
+ some_message.un_trash
47
+ some_message.star
48
+ some_message.un_star
49
+ some_message.mark_as_read
50
+ some_message.mark_as_unread
51
+
52
+ Gmail::Message.search(in: "labelId" from: "me@gmail.com", to: "you@gmail.com, theothers@gmail.com", subject: "some subject", after: "date", before: "date", has_words: "some words", has_not_words: "some text")
53
+ Gmail::Message.search("some words") #search as you would do in gmail interface
54
+
55
+
56
+
57
+ ### Message
58
+
59
+ Create a Message object
60
+
61
+ m = Gmail::Message.new(
62
+ from: "test@test.com",
63
+ to: "hello@world.com",
64
+ subject: "this is the subject",
65
+ body: "this is a text body")
66
+
67
+ If you want a multipart message
68
+
69
+ m = Gmail::Message.new(
70
+ from: "test@test.com",
71
+ to: "hello@world.com",
72
+ subject: "this is the subject",
73
+ text: "this is a text body",
74
+ html: "<div>this is a html body</div>",
75
+ labelIds: ["cool label", "Newsletter"] #labelIds is always array of labels
76
+ )
77
+
78
+ From this you can create a Draft in Gmail
79
+
80
+ m.create_draft
81
+
82
+ or just send the Message
83
+
84
+ m.deliver
85
+
86
+ Notice that the Message object can use from, to, cc, bcc, threadId, labelIds, text, html, body
87
+
88
+ If you need to send Message or create draft with custom headers or set other headers.
89
+ Or if you need to create a different multipart email, you need to build the "raw" email yourself
90
+
91
+ example:
92
+
93
+ mail = Mail.new #this is the native ruby Mail object
94
+ #
95
+ # construct your Mail object
96
+ #
97
+ raw = Base64.urlsafe_encode64 mail.to_s
98
+ m = Gmail::Message.new(raw: raw)
99
+
100
+ In that scenario, you still assign, if needed, ThreadId and labelIds as previously
101
+
102
+ m.threadId = "somethreadId"
103
+ m.labelIds = ["cool label"]
104
+
105
+ If you want to quickly generate a reply or a forward to message
106
+
107
+ # this will handle everything for you, keeping the subject the thread etc as it would happen in Gmail
108
+ # reply only to the sender of Message m.
109
+ m.reply_sender_with Gmail::Message.new(body: "some reply text")
110
+ # reply to all (sender, to (without yourself) and cc)
111
+ m.reply_all_with Gmail::Message.new(body: "some reply text")
112
+ # forward
113
+ m.forward_with Gmail::Message.new(body: "some forward text", to: "address@toforward.com")
114
+
115
+ Other stuffs that you can do with Message object
116
+
117
+ m.detailed #if for any reason Google did send us the full detail of on email object
118
+ m.thread #to get the associated Thread
119
+ m.inbox? #to know if email is in inbox
120
+ m.sent? #to know if email is a sent email
121
+ m.unread? #to know if email is unread
122
+
123
+ Those are basics helpers feel free to add more if you want (ex: starred?, important?, etc)
124
+
125
+
126
+ ## Thread
127
+
128
+ Get a thread
129
+ one_thread = Gmail::Thread.get(thread_id)
130
+ or
131
+ one_thread = one_message.thread
132
+
133
+ Filter messages in Thread
134
+
135
+ one_thread.unread_messages
136
+ one_thread.sent_messages
137
+
138
+ Expand messages in Thread
139
+
140
+ one_thread.detailed #this will return a thread object but with all messages detailed
141
+ one_thread.messages #this will return an array of message objects
142
+
143
+ ## Draft
144
+
145
+ As we explained in Message, you create a draft with
146
+
147
+ d = some_message.create_draft
148
+
149
+ Modify it with
150
+
151
+ d.message.subject = "some different subject than before"
152
+ d.save
153
+
154
+ send it with
155
+
156
+ d.deliver
157
+
158
+ ## Label
159
+
160
+ You can create a label like this:
161
+
162
+ l = Gmail::Label.new
163
+ l.name = "different Name"
164
+ l.messageListVisibility = "hide" #can be 'hide' or 'show'
165
+ l.labelListVisibility = "labelShowIfUnread" #can be "labelShowIfUnread", "labelShow", "labelHide"
166
+ l.save
167
+
168
+ You can modify a label like this:
169
+
170
+ l = Gmail::Label.get("labelId")
171
+ l.name = "different Name"
172
+ l.messageListVisibility = "hide"
173
+ l.labelListVisibility = "labelShowIfUnread"
174
+ l.save
175
+
176
+ You can use Labels to directly access box information
177
+
178
+ l.detailed #will display number of messages and number of threads if you don't see it
179
+
180
+ For system labels like inbox, sent, trash, important, starred, draft, spam, unread, category_updates, category_promotions, category_social, category_personal, category_forums
181
+
182
+ l = Gmail::Label.inbox
183
+
184
+ Access threads and messages from labels
185
+
186
+ l.threads
187
+ l.messages
188
+
189
+ l.unread_threads
190
+ l.unread_messages
191
+
192
+ Access threads, drafts and messages with labels
193
+
194
+ Gmail::Message.all(labelIds: ["UNREAD", "INBOX"])
195
+
196
+
197
+ ## License
198
+
199
+ Copyrignt (c) 2015 Julien Hobeika
200
+
201
+ Permission is hereby granted, free of charge, to any person obtaining
202
+ a copy of this software and associated documentation files (the
203
+ "Software"), to deal in the Software without restriction, including
204
+ without limitation the rights to use, copy, modify, merge, publish,
205
+ distribute, sublicense, and/or sell copies of the Software, and to
206
+ permit persons to whom the Software is furnished to do so, subject to
207
+ the following conditions:
208
+
209
+ The above copyright notice and this permission notice shall be
210
+ included in all copies or substantial portions of the Software.
211
+
212
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
213
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
214
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
215
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
216
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
217
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
218
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ begin
10
+ require 'rdoc/task'
11
+ rescue LoadError
12
+ require 'rdoc/rdoc'
13
+ require 'rake/rdoctask'
14
+ RDoc::Task = Rake::RDocTask
15
+ end
16
+
17
+ RDoc::Task.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = "Gmail API for Ruby #{Gmail::VERSION}"
20
+ rdoc.rdoc_files.include('README*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ Bundler::GemHelper.install_tasks
25
+
26
+ begin
27
+ require 'rspec/core/rake_task'
28
+ RSpec::Core::RakeTask.new(:spec)
29
+ rescue LoadError
30
+ task :spec do
31
+ abort 'Run `gem install rspec` to install RSpec'
32
+ end
33
+ end
34
+
35
+ task :default => :spec
data/TODO.md ADDED
@@ -0,0 +1,4 @@
1
+ # TODO
2
+
3
+ *proper errors handling
4
+ *write test
data/gmail.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path('../lib', __FILE__)
4
+ require 'gmail/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gmail-api-ruby"
8
+ s.summary = "A Rubyesque interface to Gmail API (NO IMAP, NO SMTP), with all the tools you will need."
9
+ s.description = "A Rubyesque interface to Gmail, with all the tools you will need.
10
+ Search, read and send multipart emails; archive, mark as read/unread,
11
+ delete emails; and manage labels.
12
+ "
13
+ s.version = Gmail::VERSION
14
+ s.platform = Gem::Platform::RUBY
15
+ s.authors = ["Julien Hobeika"]
16
+ s.homepage = "http://github.com/jhk753/gmail-ruby-api"
17
+
18
+ # runtime dependencies
19
+ s.add_dependency "mime", ">= 0.1"
20
+ s.add_dependency "mail", ">= 2.2.1"
21
+ s.add_dependency 'google-api-client'
22
+ s.add_dependency "hooks"
23
+ s.add_dependency "hashie"
24
+
25
+ # development dependencies
26
+ s.add_development_dependency "rake"
27
+ s.add_development_dependency "rspec", "~> 2.0"
28
+ s.add_development_dependency "mocha", ">= 0.9"
29
+ s.add_development_dependency "gem-release"
30
+
31
+ s.files = `git ls-files`.split("\n")
32
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
33
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
34
+ s.require_paths = ["lib"]
35
+ end
data/lib/gmail.rb ADDED
@@ -0,0 +1,112 @@
1
+ require 'mail'
2
+ require 'date'
3
+ require 'time'
4
+ require 'hashie'
5
+ require 'hooks'
6
+ require 'google/api_client'
7
+ require 'gmail/gmail_object'
8
+ require 'gmail/api_resource'
9
+ #base
10
+ require 'gmail/base/create'
11
+ require 'gmail/base/delete'
12
+ require 'gmail/base/get'
13
+ require 'gmail/base/list'
14
+ require 'gmail/base/update'
15
+ require 'gmail/base/modify'
16
+ require 'gmail/base/trash'
17
+
18
+ #object
19
+ require 'gmail/util'
20
+ require 'gmail/message'
21
+ require 'gmail/draft'
22
+ require 'gmail/thread'
23
+ require 'gmail/label'
24
+
25
+
26
+
27
+ module Gmail
28
+ begin
29
+ @client_id ||= YAML.load_file("env.yml")["GOOGLE_CLIENT_ID"]
30
+ @client_secret ||= YAML.load_file("env.yml")["GOOGLE_CLIENT_SECRET"]
31
+ @refresh_token ||= YAML.load_file("env.yml")["GOOGLE_REFRESH_TOKEN"]
32
+ rescue
33
+
34
+ end
35
+
36
+ class << self
37
+ attr_accessor :client_id, :client_secret, :refresh_token, :client, :service
38
+ end
39
+
40
+ def self.request(method, params={}, body={})
41
+ params[:userId] ||= "me"
42
+ if @client.nil?
43
+ self.connect
44
+ end
45
+ if body.empty?
46
+ response = @client.execute(
47
+ :api_method => method,
48
+ :parameters => params,
49
+
50
+ :headers => {'Content-Type' => 'application/json'})
51
+ else
52
+
53
+ response = @client.execute(
54
+ :api_method => method,
55
+ :parameters => params,
56
+ :body_object => body,
57
+ :headers => {'Content-Type' => 'application/json'})
58
+ end
59
+ parse(response)
60
+
61
+ end
62
+
63
+ def self.connect(client_id=@client_id, client_secret=@client_secret, refresh_token=@refresh_token)
64
+ unless client_id
65
+ raise 'No client_id specified'
66
+ end
67
+
68
+ unless client_secret
69
+ raise 'No client_secret specified'
70
+ end
71
+
72
+ unless refresh_token
73
+ raise 'No refresh_token specified'
74
+ end
75
+
76
+ @client = Google::APIClient.new(
77
+ application_name: 'Juliedesk',
78
+ application_version: '1.0.0'
79
+ )
80
+ @client.authorization.client_id = client_id
81
+ @client.authorization.client_secret = client_secret
82
+ @client.authorization.refresh_token = refresh_token
83
+ @client.authorization.grant_type = 'refresh_token'
84
+ @client.authorization.fetch_access_token!
85
+
86
+ @service = @client.discovered_api('gmail', 'v1')
87
+
88
+ end
89
+
90
+ def self.parse(response)
91
+ begin
92
+ # Would use :symbolize_names => true, but apparently there is
93
+ # some library out there that makes symbolize_names not work.
94
+ if response.body.empty?
95
+ return response.body
96
+ else
97
+ response = JSON.parse(response.body)
98
+ end
99
+
100
+ rescue JSON::ParserError
101
+ raise "error code: #{response.error},body: #{response.body})"
102
+ end
103
+
104
+ r = Util.symbolize_names(response)
105
+ if r[:error]
106
+ raise "#{r[:error]}"
107
+ end
108
+ r
109
+ end
110
+
111
+
112
+ end # Gmail