gmail-api-ruby 0.0.1 → 0.0.2

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.
data/lib/gmail/draft.rb CHANGED
@@ -10,7 +10,12 @@ module Gmail
10
10
  if @values.message.is_a?(Gmail::Message)
11
11
  @values.message
12
12
  else
13
- @values.message = Util.convert_to_gmail_object(to_hash["message"], key="message").detailed
13
+ @values.message = Util.convert_to_gmail_object(to_hash[:message], key="message")
14
+ if @values.message.payload.nil?
15
+ self.detailed!
16
+ message
17
+ end
18
+ @values.message
14
19
  end
15
20
  end
16
21
 
@@ -40,16 +40,25 @@ module Gmail
40
40
  end
41
41
 
42
42
  def detailed
43
+ if self.class == Gmail::GmailObject
44
+ raise "Can't detail a generic GmailObject. It needs to be a Thread, Message, Draft or Label"
45
+ end
46
+
43
47
  self.class.get(id)
44
48
  end
45
49
 
46
- def detailed!
50
+ def refresh
51
+ if self.class == Gmail::GmailObject
52
+ raise "Can't refresh a generic GmailObject. It needs to be a Thread, Message, Draft or Label"
53
+ end
47
54
  @values = self.class.get(id).values
48
55
  self
49
56
  end
57
+
58
+ alias_method :detailed!, :refresh
50
59
  #
51
60
  def to_hash
52
- @values.to_hash
61
+ Util.symbolize_names(@values.to_hash)
53
62
  end
54
63
 
55
64
  def values
data/lib/gmail/label.rb CHANGED
@@ -7,11 +7,11 @@ module Gmail
7
7
  include Gmail::Base::Update
8
8
 
9
9
  def save
10
- update(@values.to_hash)
10
+ update(to_hash)
11
11
  end
12
12
 
13
13
  def save!
14
- update!(@values.to_hash)
14
+ update!(to_hash)
15
15
  end
16
16
 
17
17
  def self.boxes
data/lib/gmail/message.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Gmail
2
3
  class Message < APIResource
3
4
  include Gmail::Base::List
@@ -93,10 +94,11 @@ module Gmail
93
94
  msg.to = to
94
95
  msg.cc = cc
95
96
  msg.header['X-Bcc'] = bcc unless bcc.nil?#because Mail gem doesn't allow bcc headers...
96
- msg.header['In-Reply-To'] = in_reply_to unless in_reply_to.nil?
97
- msg.header['References'] = references unless references.nil?
97
+ msg.in_reply_to = in_reply_to unless in_reply_to.nil?
98
+ msg.references = references unless references.nil?
98
99
  if text
99
100
  msg.text_part = Mail::Part.new do |p|
101
+ content_type 'text/plain; charset=UTF-8'
100
102
  p.body s.text
101
103
  end
102
104
  end
@@ -106,7 +108,6 @@ module Gmail
106
108
  p.body s.html
107
109
  end
108
110
  end
109
-
110
111
  Base64.urlsafe_encode64 msg.to_s.sub("X-Bcc", "Bcc") #because Mail gem doesn't allow bcc headers...
111
112
  end
112
113
  end
@@ -171,11 +172,11 @@ module Gmail
171
172
  end
172
173
 
173
174
  if payload.parts
174
- text_part=@values.payload.parts.select{|h| h.mimeType=="text/plain"}.first
175
+ text_part=@values.payload.find_all_object_containing("mimeType", "text/plain").first
175
176
  if text_part
176
177
  @values.text = urlsafe_decode64(text_part.body.data)
177
178
  end
178
- html_part=@values.payload.parts.select{|h| h.mimeType=="text/html"}.first
179
+ html_part=@values.payload.find_all_object_containing("mimeType", "text/html").first
179
180
  if html_part
180
181
  @values.html = urlsafe_decode64(html_part.body.data)
181
182
  end
@@ -185,5 +186,22 @@ module Gmail
185
186
  end
186
187
  end
187
188
  end
189
+
190
+ class Hashie::Mash
191
+ def find_all_object_containing(key, value )
192
+ result=[]
193
+ if self.send(key) == value
194
+ result << self
195
+ end
196
+ self.values.each do |vs|
197
+ vs = [vs] unless vs.is_a? Array
198
+ vs.each do |v|
199
+ result += v.find_all_object_containing(key,value) if v.is_a? Hashie::Mash
200
+ end
201
+ end
202
+ result
203
+ end
204
+ end
205
+
188
206
  end
189
207
  end
data/lib/gmail/thread.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  module Gmail
2
2
  class Thread < APIResource
3
3
  include Gmail::Base::List
4
- include Gmail::Base::Create
5
4
  include Gmail::Base::Delete
6
5
  include Gmail::Base::Get
7
6
  include Gmail::Base::Modify
@@ -9,9 +8,16 @@ module Gmail
9
8
 
10
9
  def messages
11
10
 
12
- msgs = to_hash["messages"] || detailed.messages
13
-
14
- Util.convert_to_gmail_object(msgs, key="message")
11
+ if @values.messages.is_a? Array
12
+ if @values.messages.first.is_a? Gmail::Message
13
+ @values.messages
14
+ else
15
+ @values.messages = Util.convert_to_gmail_object(to_hash[:messages], key="message")
16
+ end
17
+ else
18
+ self.detailed!
19
+ messages
20
+ end
15
21
 
16
22
  end
17
23
 
data/lib/gmail/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gmail
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end # Gmail
@@ -0,0 +1,47 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Gmail
5
+ class ApiResourceTest < Test::Unit::TestCase
6
+
7
+ should "creating a new APIResource should not fetch over the network" do
8
+ @mock.expects(:execute).never
9
+ Gmail::Label.new({
10
+ name: "test"
11
+ })
12
+ end
13
+
14
+ should "setting an attribute should not cause a network request" do
15
+ @mock.expects(:execute).never
16
+ m = Gmail::Message.new({subject: "test"})
17
+ m.body = "this is a test body"
18
+ end
19
+
20
+ should "accessing id should not issue a fetch" do
21
+ @mock.expects(:execute).never
22
+ c = Gmail::Message.new({subject: "test"})
23
+ c.id
24
+ end
25
+
26
+ should "construct URL properly with base query parameters" do
27
+ response = test_response(test_thread_list)
28
+ @mock.expects(:execute).with(api_method: Gmail.service.users.threads.list, parameters: {userId: "me"}, headers: {'Content-Type' => 'application/json'}).returns(response)
29
+ Gmail::Thread.all
30
+
31
+ @mock.expects(:execute).with(api_method: Gmail.service.users.threads.list, parameters: {maxResults: 150, userId: "test@test.com"}, headers: {'Content-Type' => 'application/json'}).returns(response)
32
+ Gmail::Thread.all(maxResults: 150, userId: "test@test.com")
33
+ end
34
+
35
+
36
+ should "deleting should return true" do
37
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.delete, parameters: {userId: "me", id: test_draft[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(""))
38
+
39
+ d = Gmail::Draft.new test_draft
40
+
41
+ assert_equal true, d.delete
42
+
43
+ end
44
+
45
+
46
+ end
47
+ end
@@ -0,0 +1,104 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Gmail
5
+ class DraftTest < Test::Unit::TestCase
6
+
7
+ should "Draft should be retrievable by id" do
8
+
9
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.get, parameters: {userId: "me", id: test_draft[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_draft))
10
+ d = Gmail::Draft.get(test_draft[:id])
11
+ assert d.kind_of?Gmail::Draft
12
+ assert_equal test_draft[:id], d.id
13
+ end
14
+
15
+ should "drafts should be listable" do
16
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.list, parameters: {userId: "me"}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_draft_list))
17
+ list = Gmail::Draft.all
18
+ assert list.kind_of? Array
19
+ assert list[0].kind_of? Gmail::Draft
20
+ end
21
+
22
+ context "Message Object in draft" do
23
+ should "retrieved Draft should not generate call to get Message Object" do
24
+ draft = Gmail::Draft.new(test_draft)
25
+ @mock.expects(:execute).never
26
+ assert draft.message.kind_of?Gmail::Message
27
+ end
28
+
29
+ should "Draft get from a draft list should generate call to get Message Object" do
30
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.list, parameters: {userId: "me"}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_draft_list))
31
+ list = Gmail::Draft.all
32
+ draft = list.first
33
+
34
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.get, parameters: {userId: "me", id: test_draft[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_draft))
35
+
36
+ assert draft.message.kind_of?Gmail::Message
37
+ assert_not_nil draft.message.payload
38
+ end
39
+ end
40
+
41
+
42
+ should "drafts should be deletable" do
43
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.delete, parameters: {userId: "me", id: test_draft[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(""))
44
+ d = Gmail::Draft.new(test_draft)
45
+ r = d.delete
46
+ assert r
47
+ end
48
+
49
+ should "drafts should be updateable" do
50
+ draft_hash = test_draft
51
+ draft_hash[:message].merge!({labelIds: ["COOL LABEL"]})
52
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.get, parameters: {id: test_draft[:id], userId: "me"} , headers: {'Content-Type' => 'application/json'}).once.returns(test_response(draft_hash))
53
+
54
+ d = Gmail::Draft.new(id: test_draft[:id]).detailed
55
+ # those two lines are required because raw generation change between two calls...
56
+ raw = d.message.raw
57
+ d.message.raw = raw
58
+ ###
59
+ assert_equal ["COOL LABEL"], d.message.labelIds
60
+
61
+ draft_hash[:message].merge!({labelIds: ["INBOX"]})
62
+
63
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.update, parameters: {id: test_draft[:id], userId: "me"}, body_object:{message: {raw: d.message.raw, threadId: test_draft[:message][:threadId], labelIds: ["INBOX"]}} , headers: {'Content-Type' => 'application/json'}).twice.returns(test_response(draft_hash))
64
+
65
+
66
+ d.message.labelIds = ["INBOX"]
67
+ new_d = d.save
68
+ assert_equal ["INBOX"], new_d.message.labelIds
69
+ assert_not_equal d.object_id, new_d.object_id
70
+ new_d = d.save!
71
+ assert_equal d.object_id, new_d.object_id
72
+ end
73
+
74
+ should "create should return a new Draft" do
75
+ draft_hash = test_draft
76
+ draft_hash.delete(:id)
77
+ d = Gmail::Draft.new draft_hash
78
+ # those two lines are required because raw generation change between two calls...
79
+ raw = d.message.raw
80
+ d.message.raw = raw
81
+ ###
82
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.create, parameters: {userId: "me"}, body_object:{message: {raw: d.message.raw, threadId: draft_hash[:message][:threadId], labelIds: draft_hash[:message][:labelIds]}} , headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_draft))
83
+ created_d = d.save!
84
+ assert_equal Gmail::Draft, created_d.class
85
+ assert_equal test_draft[:id], d.id
86
+ end
87
+
88
+
89
+ should "Draft should be sendable and return a Message" do
90
+ @mock.expects(:execute).with(api_method: Gmail.service.users.drafts.to_h['gmail.users.drafts.send'], parameters: {userId: "me"}, body_object:{id: test_draft[:id]} , headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_message))
91
+ @mock.expects(:execute).with(api_method: Gmail.service.users.messages.get, parameters: {userId: "me", id: test_message[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_message))
92
+
93
+ d = Gmail::Draft.new test_draft
94
+ m = d.deliver
95
+
96
+ assert m.kind_of?Gmail::Message
97
+
98
+ end
99
+
100
+
101
+
102
+
103
+ end
104
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Gmail
4
+ class GmailObjectTest < Test::Unit::TestCase
5
+ should "implement #respond_to correctly" do
6
+ obj = Gmail::GmailObject.new({ :id => 1, :foo => 'bar' })
7
+ assert_not_nil obj.id
8
+ assert_not_nil obj.foo
9
+ assert_nil obj.other
10
+ end
11
+
12
+ should "detail and refresh a Gmail object correctly" do
13
+ obj = Gmail::GmailObject.new test_message
14
+ exception = assert_raise do obj.refresh end
15
+ assert_equal "Can't refresh a generic GmailObject. It needs to be a Thread, Message, Draft or Label", exception.message
16
+ exception = assert_raise do obj.detailed end
17
+ assert_equal "Can't detail a generic GmailObject. It needs to be a Thread, Message, Draft or Label", exception.message
18
+
19
+ not_generic_object = Gmail::Message.new test_message
20
+ @mock.expects(:execute).with(api_method: Gmail.service.users.messages.get, parameters: {userId: "me", id: test_message[:id]}, headers: {'Content-Type' => 'application/json'}).twice.returns(test_response(test_message))
21
+
22
+ new_o = not_generic_object.detailed
23
+ assert_not_equal new_o.object_id, not_generic_object.object_id
24
+
25
+ new_o = not_generic_object.refresh
26
+ assert_equal new_o.object_id, not_generic_object.object_id
27
+
28
+ end
29
+
30
+ should "not recursively call to_hash on GmailObject" do
31
+ nested = Gmail::GmailObject.new({ :id => 7, :foo => 'bar' })
32
+ obj = Gmail::GmailObject.new({ :id => 1, :nested => nested })
33
+ expected_hash = { :id => 1, :nested => nested }
34
+ assert_equal expected_hash, obj.to_hash
35
+ end
36
+
37
+ should "recursively call to_hash on its value" do
38
+ nested = { :id => 7, :foo => 'bar' }
39
+ obj = Gmail::GmailObject.new({ :id => 1, :nested => nested })
40
+ expected_hash = { :id => 1, :nested => nested }
41
+ assert_equal 7, obj.nested.id
42
+ assert_equal expected_hash, obj.to_hash
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,129 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Gmail
5
+ class LabelTest < Test::Unit::TestCase
6
+
7
+ should "Labels should be listable" do
8
+ @mock.expects(:execute).with(api_method: Gmail.service.users.labels.list, parameters: {userId: "me"}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_label_list))
9
+ list = Gmail::Label.all
10
+ assert list.kind_of? Array
11
+ assert list[0].kind_of? Gmail::Label
12
+ end
13
+
14
+
15
+ context "Retrieve a Label" do
16
+ should "Label should be retrievable by id" do
17
+
18
+ @mock.expects(:execute).with(api_method: Gmail.service.users.labels.get, parameters: {userId: "me", id: test_label[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_label))
19
+ l = Gmail::Label.get(test_label[:id])
20
+ assert l.kind_of?Gmail::Label
21
+ assert_equal test_label[:id], l.id
22
+ end
23
+
24
+
25
+ [:inbox, :sent, :trash, :important, :starred, :draft, :spam, :unread, :category_updates, :category_promotions, :category_social, :category_personal, :category_forums ].each do |label_id|
26
+ should "System Label should be retrievable by calling #{label_id.to_s}" do
27
+ @mock.expects(:execute).with(api_method: Gmail.service.users.labels.get, parameters: {userId: "me", id: label_id.to_s.upcase}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_label_list[:labels].select{|l| l[:id] == label_id.to_s.upcase}.first))
28
+ l = Gmail::Label.send(label_id.to_s)
29
+ assert l.kind_of?Gmail::Label
30
+ assert_equal label_id.to_s.upcase, l.id
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+
37
+ context "Access list of Messages from Label" do
38
+ should "Access list of Messages" do
39
+ label = Gmail::Label.new test_label
40
+ @mock.expects(:execute).with(api_method: Gmail.service.users.messages.list, parameters: {userId: "me", labelIds: [test_label[:id]]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_message_list))
41
+ list = label.messages
42
+ assert list.kind_of? Array
43
+ assert list[0].kind_of? Gmail::Message
44
+ end
45
+
46
+ should 'Access list of unread Messages' do
47
+ label = Gmail::Label.new test_label
48
+ @mock.expects(:execute).with(api_method: Gmail.service.users.messages.list, parameters: {userId: "me", labelIds: [test_label[:id], "UNREAD"]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_message_list))
49
+ list = label.unread_messages
50
+ assert list.kind_of? Array
51
+ assert list[0].kind_of? Gmail::Message
52
+ end
53
+
54
+ should 'Access filtered Messages' do
55
+ label = Gmail::Label.new test_label
56
+ @mock.expects(:execute).with(api_method: Gmail.service.users.messages.list, parameters: {userId: "me", labelIds: ["IMPORTANT", "COOL", test_label[:id]], threadId: "1"}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_message_list))
57
+ list = label.messages(threadId: "1", labelIds: ["IMPORTANT", "COOL"])
58
+ assert list.kind_of? Array
59
+ assert list[0].kind_of? Gmail::Message
60
+ end
61
+
62
+ should "Access list of Threads" do
63
+ label = Gmail::Label.new test_label
64
+ @mock.expects(:execute).with(api_method: Gmail.service.users.threads.list, parameters: {userId: "me", labelIds: [test_label[:id]]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_thread_list))
65
+ list = label.threads
66
+ assert list.kind_of? Array
67
+ assert list[0].kind_of? Gmail::Thread
68
+ end
69
+
70
+ should 'Access list of unread Threads' do
71
+ label = Gmail::Label.new test_label
72
+ @mock.expects(:execute).with(api_method: Gmail.service.users.threads.list, parameters: {userId: "me", labelIds: [test_label[:id], "UNREAD"]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_thread_list))
73
+ list = label.unread_threads
74
+ assert list.kind_of? Array
75
+ assert list[0].kind_of? Gmail::Thread
76
+ end
77
+
78
+ should 'Access filtered Threads' do
79
+ label = Gmail::Label.new test_label
80
+ @mock.expects(:execute).with(api_method: Gmail.service.users.threads.list, parameters: {userId: "me", labelIds: ["IMPORTANT", "COOL", test_label[:id]], threadId: "1"}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_thread_list))
81
+ list = label.threads(threadId: "1", labelIds: ["IMPORTANT", "COOL"])
82
+ assert list.kind_of? Array
83
+ assert list[0].kind_of? Gmail::Thread
84
+ end
85
+
86
+
87
+
88
+ end
89
+
90
+
91
+ should "Label should be deletable" do
92
+ @mock.expects(:execute).with(api_method: Gmail.service.users.labels.delete, parameters: {userId: "me", id: test_label[:id]}, headers: {'Content-Type' => 'application/json'}).once.returns(test_response(""))
93
+ d = Gmail::Label.new(test_label)
94
+ r = d.delete
95
+ assert r
96
+ end
97
+
98
+ should "Label should be updateable" do
99
+
100
+ label = Gmail::Label.new test_label(:messageListVisibility=>"show")
101
+
102
+ assert_equal "show", label.messageListVisibility
103
+
104
+
105
+ @mock.expects(:execute).with(api_method: Gmail.service.users.labels.update, parameters: {id: test_label[:id], userId: "me"}, body_object:test_label(:messageListVisibility=>"hide") , headers: {'Content-Type' => 'application/json'}).twice.returns(test_response(test_label(:messageListVisibility=>"hide")))
106
+
107
+ label.messageListVisibility = "hide"
108
+ new_l = label.save
109
+ assert_equal "hide", new_l.messageListVisibility
110
+ assert_not_equal label.object_id, new_l.object_id
111
+ new_l = label.save!
112
+ assert_equal label.object_id, new_l.object_id
113
+ end
114
+
115
+ should "create should return a new Label" do
116
+ label_hash = test_label
117
+ label_hash.delete(:id)
118
+ label = Gmail::Label.new label_hash
119
+ @mock.expects(:execute).with(api_method: Gmail.service.users.labels.create, parameters: {userId: "me"}, body_object:label_hash , headers: {'Content-Type' => 'application/json'}).once.returns(test_response(test_label))
120
+ created_l = label.save!
121
+ assert_equal Gmail::Label, created_l.class
122
+ assert_equal test_label[:id], label.id
123
+ end
124
+
125
+
126
+
127
+
128
+ end
129
+ end