gmailish 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.
@@ -0,0 +1,3 @@
1
+ module Gmailish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+ require_relative 'support/data'
3
+
4
+ module Gmailish
5
+ describe Gmailish do
6
+ let(:instance) { Gmailish::Account.new(username, password) }
7
+ let(:username) { 'venkmanapp' }
8
+ let(:password) { 'ABC123456' }
9
+
10
+ let(:account) {
11
+ fire_double(
12
+ 'Net::IMAP'
13
+ )
14
+ }
15
+
16
+ let(:tagged_response) {
17
+ fire_double(
18
+ 'Net::IMAP::TaggedResponse',
19
+ :name => 'OK'
20
+ )
21
+ }
22
+
23
+ let(:fetch_data) {
24
+ fire_double(
25
+ 'Net::IMAP::FetchData',
26
+ :attr => Gmailish::SupportData.fake_fetch_data_response
27
+ )
28
+ }
29
+
30
+ context 'invalid' do
31
+ subject { instance.process }
32
+
33
+ it 'raises error' do
34
+ expect { subject }.to raise_error(Net::IMAP::NoResponseError)
35
+ end
36
+ end
37
+
38
+ context 'valid' do
39
+ subject { instance.process }
40
+
41
+ let(:address) { 'imap.gmail.com' }
42
+ let(:port) { 993 }
43
+
44
+ let(:mailbox) { 'inbox' }
45
+ let(:read_label) { :Seen }
46
+ let(:unread_label) { 'UNSEEN' }
47
+ let(:delete_label) { :Deleted }
48
+ let(:transfered_label) { 'Transfered' }
49
+ let(:archive_label) { "[Gmail]/All Mail" }
50
+ let(:uid) { 110 }
51
+ let(:rfc) { 'RFC822' }
52
+
53
+ let(:mail_subject) {
54
+ "Fwd: Raising and Rescuing Custom Errors in Rails | We Are Stac"
55
+ }
56
+
57
+ let(:mail_to) {
58
+ ["venkmanapp+40f7f061c2f7254b1b50f699d9eac16a@gmail.com"]
59
+ }
60
+
61
+ let(:mail_from) {
62
+ ["chuckjhardy@gmail.com"]
63
+ }
64
+
65
+ it 'returns messages' do
66
+ # Create connection
67
+ Net::IMAP.should_receive(:new).
68
+ with(address, port, true, nil, false).
69
+ and_return(account)
70
+
71
+ # Login to account
72
+ account.should_receive(:login).
73
+ with(username, password).
74
+ and_return(tagged_response)
75
+
76
+ # Select Inbox
77
+ account.should_receive(:select).
78
+ with(mailbox).
79
+ and_return(tagged_response)
80
+
81
+ # Find unread messages
82
+ account.should_receive(:uid_search).
83
+ with([unread_label]).
84
+ and_return([uid])
85
+
86
+ # Get unread messages
87
+ account.should_receive(:uid_fetch).
88
+ with(uid, rfc).
89
+ and_return(fetch_data)
90
+
91
+ # Add 'Transfered' label to messages
92
+ account.should_receive(:uid_copy).
93
+ with(uid, transfered_label).
94
+ and_return(tagged_response)
95
+
96
+ # Make messages as read
97
+ account.should_receive(:uid_store).
98
+ with(uid, "+FLAGS", [read_label]).
99
+ and_return(tagged_response)
100
+
101
+ # Archive messages
102
+ account.should_receive(:uid_copy).
103
+ with(uid, archive_label).
104
+ and_return(tagged_response)
105
+
106
+ # Delete messages from inbox
107
+ account.should_receive(:uid_store).
108
+ with(uid, "+FLAGS", [delete_label]).
109
+ and_return(tagged_response)
110
+
111
+ # Logout of account
112
+ account.should_receive(:logout).
113
+ and_return(tagged_response)
114
+
115
+ # Always returns an array
116
+ expect(subject.messages).to be_an_instance_of(Array)
117
+
118
+ # Array contains Mail objects
119
+ expect(subject.messages.first).to be_an_instance_of(Mail::Message)
120
+
121
+ # Message has a subject
122
+ expect(subject.messages.first.subject).to eq(mail_subject)
123
+
124
+ # Message has a to field
125
+ expect(subject.messages.first.to).to eq(mail_to)
126
+
127
+ # Message has a from field
128
+ expect(subject.messages.first.from).to eq(mail_from)
129
+
130
+ # Message has a body field
131
+ expect(subject.messages.first.body).to be_an_instance_of(Mail::Body)
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ module Gmailish
4
+ describe Account do
5
+ let(:instance) { Gmailish::Account.new(username, password) }
6
+ let(:username) { 'venkmanapp' }
7
+ let(:password) { 'ABC123456' }
8
+
9
+ let(:account) {
10
+ fire_double(
11
+ 'Net::IMAP'
12
+ )
13
+ }
14
+
15
+ describe '#initialize' do
16
+ it 'take arguments' do
17
+ instance
18
+ end
19
+ end
20
+
21
+ describe '.process' do
22
+ subject { described_class.process(username, password) }
23
+
24
+ it 'calls off to process' do
25
+ described_class.any_instance.should_receive(:process)
26
+
27
+ subject
28
+ end
29
+ end
30
+
31
+ describe '#process' do
32
+ subject { instance.process }
33
+
34
+ let(:actions) {
35
+ fire_double(
36
+ 'Gmailish::Actions'
37
+ )
38
+ }
39
+
40
+ let(:messages) { lambda{} }
41
+
42
+ let(:address) { 'imap.gmail.com' }
43
+ let(:port) { 993 }
44
+
45
+ before { instance.stub(:messages => messages) }
46
+
47
+ it 'calls off to actions and return self' do
48
+ Net::IMAP.should_receive(:new).
49
+ with(address, port, true, nil, false).
50
+ and_return(account)
51
+
52
+ Actions.should_receive(:new).
53
+ with(account, username, password).
54
+ and_return(actions)
55
+
56
+ actions.should_receive(:process)
57
+
58
+ expect(subject).to be_an_instance_of(Gmailish::Account)
59
+ end
60
+ end
61
+
62
+ describe '#messages' do
63
+ subject { instance.messages }
64
+
65
+ let(:label) { 'UNSEEN' }
66
+ let(:uid) { 37 }
67
+ let(:uids) { [ uid ] }
68
+ let(:message) {
69
+ fire_double(
70
+ 'Gmailish::Message'
71
+ )
72
+ }
73
+
74
+ before { instance.stub(:account => account) }
75
+
76
+ it 'returns array of message objects' do
77
+ account.should_receive(:uid_search).
78
+ with([label]).
79
+ and_return(uids)
80
+
81
+ Message.should_receive(:process).
82
+ with(account, uid).
83
+ and_return(message)
84
+
85
+ expect(subject).to eq([message])
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ module Gmailish
4
+ describe Actions do
5
+ let(:instance) { Gmailish::Actions.new(account, username, password) }
6
+ let(:username) { 'venkmanapp' }
7
+ let(:password) { 'ABC123456' }
8
+ let(:account) {
9
+ fire_double(
10
+ 'Net::IMAP'
11
+ )
12
+ }
13
+
14
+ let(:message) {
15
+ fire_double(
16
+ 'Gmailish::Message'
17
+ )
18
+ }
19
+
20
+ let(:response) {
21
+ fire_double(
22
+ 'Net::IMAP::TaggedResponse',
23
+ :name => 'OK'
24
+ )
25
+ }
26
+
27
+ describe '#initialize' do
28
+ it 'take arguments' do
29
+ instance
30
+ end
31
+ end
32
+
33
+ describe '#process' do
34
+ context 'called with message' do
35
+ subject { instance.process { message } }
36
+
37
+ it 'calls actions in order' do
38
+ instance.should_receive(:login)
39
+ instance.should_receive(:inbox)
40
+ instance.should_receive(:logout)
41
+
42
+ subject
43
+ end
44
+
45
+ context 'login' do
46
+ before do
47
+ instance.stub(:inbox)
48
+ instance.stub(:logout)
49
+ end
50
+
51
+ it 'sets logged_in to true' do
52
+ account.should_receive(:login).
53
+ with(username, password).
54
+ and_return(response)
55
+
56
+ subject
57
+
58
+ expect(instance.logged_in).to be_true
59
+ end
60
+ end
61
+
62
+ context 'inbox' do
63
+ before do
64
+ instance.stub(:login)
65
+ instance.stub(:logout)
66
+ end
67
+
68
+ let(:inbox) { 'inbox' }
69
+
70
+ it 'selects inbox' do
71
+ account.should_receive(:select).
72
+ with(inbox)
73
+
74
+ subject
75
+ end
76
+ end
77
+
78
+ context 'logout' do
79
+ let(:response) { nil }
80
+
81
+ before do
82
+ instance.stub(:login)
83
+ instance.stub(:inbox)
84
+ end
85
+
86
+ it 'sets logged_in to false' do
87
+ account.should_receive(:logout).
88
+ and_return(response)
89
+
90
+ subject
91
+
92
+ expect(instance.logged_in).to be_false
93
+ end
94
+ end
95
+ end
96
+
97
+ context 'called without message' do
98
+ subject { instance.process }
99
+
100
+ let(:msg) { "Messages must be passed within a block." }
101
+
102
+ it 'raises error' do
103
+ expect { subject }.to raise_error(Error::NoMessageError, msg)
104
+ end
105
+ end
106
+ end
107
+
108
+ describe '#logged_in?' do
109
+ subject { instance.logged_in? }
110
+
111
+ it 'returns false' do
112
+ expect(subject).to be_false
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ module Gmailish
4
+ describe Error do
5
+ let(:dummy_class) { Class.new { include Gmailish::Error }.new }
6
+
7
+ describe Error::NoMessageError do
8
+ it 'raises error' do
9
+ expect { raise Error::NoMessageError }.to raise_error
10
+ end
11
+ end
12
+
13
+ describe Error::NoLabelError do
14
+ it 'raises error' do
15
+ expect { raise Error::NoLabelError }.to raise_error
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ module Gmailish
4
+ describe Flagger do
5
+ let(:instance) { Gmailish::Flagger.new(account, uid) }
6
+ let(:uid) { 37 }
7
+ let(:account) {
8
+ fire_double(
9
+ 'Net::IMAP'
10
+ )
11
+ }
12
+
13
+ describe '#initialize' do
14
+ it 'take arguments' do
15
+ instance
16
+ end
17
+ end
18
+
19
+ describe '#delete' do
20
+ subject { instance.delete }
21
+
22
+ let(:label) { :Deleted }
23
+
24
+ it 'calls uid_store with expected arguments' do
25
+ account.should_receive(:uid_store).
26
+ with(uid, "+FLAGS", [label])
27
+
28
+ subject
29
+ end
30
+ end
31
+
32
+ describe '#unread' do
33
+ subject { instance.unread }
34
+
35
+ let(:label) { :Seen }
36
+
37
+ it 'calls uid_store with expected arguments' do
38
+ account.should_receive(:uid_store).
39
+ with(uid, "+FLAGS", [label])
40
+
41
+ subject
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ module Gmailish
4
+ describe Labeler do
5
+ let(:instance) { Gmailish::Labeler.new(account, uid) }
6
+ let(:uid) { 37 }
7
+ let(:account) {
8
+ fire_double(
9
+ 'Net::IMAP'
10
+ )
11
+ }
12
+
13
+ let(:uid_copy) { stub }
14
+
15
+ describe '#initialize' do
16
+ it 'takes arguments' do
17
+ instance
18
+ end
19
+ end
20
+
21
+ describe '#transfered' do
22
+ subject { instance.transfered }
23
+
24
+ let(:label) { 'Transfered' }
25
+
26
+ context 'label exists' do
27
+ it 'returns response' do
28
+ account.should_receive(:uid_copy).
29
+ with(uid, label)
30
+
31
+ subject
32
+ end
33
+ end
34
+
35
+ context 'label does not exist' do
36
+ let(:msg) { "Manually create `#{label}' label." }
37
+
38
+ it 'raise error' do
39
+ account.should_receive(:uid_copy).
40
+ with(uid, label).
41
+ and_raise
42
+
43
+ expect { subject }.to raise_error(Error::NoLabelError, msg)
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#all_mail' do
49
+ subject { instance.all_mail }
50
+
51
+ let(:label) { '[Gmail]/All Mail' }
52
+
53
+ context 'label exists' do
54
+ it 'returns response' do
55
+ account.should_receive(:uid_copy).
56
+ with(uid, label)
57
+
58
+ subject
59
+ end
60
+ end
61
+
62
+ context 'label does not exist' do
63
+ let(:msg) { "Manually create `#{label}' label." }
64
+
65
+ it 'raise error' do
66
+ account.should_receive(:uid_copy).
67
+ with(uid, label).
68
+ and_raise
69
+
70
+ expect { subject }.to raise_error(Error::NoLabelError, msg)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end