chatty_crow 1.2.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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +0 -0
  3. data/Gemfile +3 -0
  4. data/Guardfile +5 -0
  5. data/INSTALL +0 -0
  6. data/LICENSE +201 -0
  7. data/README.textile +183 -0
  8. data/Rakefile +14 -0
  9. data/chatty_crow.gemspec +37 -0
  10. data/lib/chatty-crow.rb +1 -0
  11. data/lib/chatty_crow.rb +119 -0
  12. data/lib/chatty_crow/action_mailer_extension.rb +31 -0
  13. data/lib/chatty_crow/config.rb +64 -0
  14. data/lib/chatty_crow/contacts_request.rb +79 -0
  15. data/lib/chatty_crow/error.rb +26 -0
  16. data/lib/chatty_crow/error/channel_not_found.rb +6 -0
  17. data/lib/chatty_crow/error/invalid_attributes.rb +21 -0
  18. data/lib/chatty_crow/error/invalid_return.rb +7 -0
  19. data/lib/chatty_crow/error/unauthorized_request.rb +6 -0
  20. data/lib/chatty_crow/notification_request.rb +54 -0
  21. data/lib/chatty_crow/railtie.rb +10 -0
  22. data/lib/chatty_crow/request.rb +53 -0
  23. data/lib/chatty_crow/request/android.rb +23 -0
  24. data/lib/chatty_crow/request/ios.rb +16 -0
  25. data/lib/chatty_crow/request/jabber.rb +17 -0
  26. data/lib/chatty_crow/request/mail.rb +264 -0
  27. data/lib/chatty_crow/request/skype.rb +17 -0
  28. data/lib/chatty_crow/request/sms.rb +17 -0
  29. data/lib/chatty_crow/response.rb +34 -0
  30. data/lib/chatty_crow/response/contacts_add.rb +43 -0
  31. data/lib/chatty_crow/response/contacts_remove.rb +35 -0
  32. data/lib/chatty_crow/response/notification.rb +19 -0
  33. data/lib/chatty_crow/version.rb +4 -0
  34. data/lib/chattycrow.rb +1 -0
  35. data/test/android_test.rb +57 -0
  36. data/test/attachment_test.rb +13 -0
  37. data/test/base_parser_test.rb +125 -0
  38. data/test/configuration_test.rb +37 -0
  39. data/test/contacts_test.rb +90 -0
  40. data/test/factories/stewie.jpeg +0 -0
  41. data/test/helper.rb +63 -0
  42. data/test/mail_test.rb +62 -0
  43. data/test/response_test.rb +33 -0
  44. metadata +251 -0
@@ -0,0 +1,19 @@
1
+ module ChattyCrow
2
+ module Response
3
+ # Notification response
4
+ class Notification < Base
5
+ attr_accessor :success, :total, :failed_contacts
6
+
7
+ # Initialize notification response
8
+ # @param response [RestClient::Response] Response from server
9
+ def initialize(response)
10
+ super response
11
+
12
+ # Parse response
13
+ @success = @body.delete('success')
14
+ @total = @body.delete('total')
15
+ @failed_contacts = @body.delete('contacts')
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ # Version
2
+ module ChattyCrow
3
+ VERSION = '1.2.1'.freeze
4
+ end
data/lib/chattycrow.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/chatty_crow.rb'
@@ -0,0 +1,57 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ # Test to send android push notification
4
+ class AndroidTest < MiniTest::Should::TestCase
5
+ should 'Create notification' do
6
+ request = { status: 'OK', msg: 'Success', sucess: 15, total: 15, contacts: [] }
7
+ mock_notification body: request.to_json
8
+
9
+ # Create request
10
+ response = ChattyCrow.send_android payload: { data: { key1: 'Data1', data_test: 'Data Test' } }
11
+
12
+ # Expect response
13
+ expect(response).to_be_kind_of ChattyCrow::Response::Notification
14
+ expect(response.status).to_equal request[:status]
15
+ expect(response.msg).to_equal request[:msg]
16
+ expect(response.success).to_equal request[:success]
17
+ expect(response.total).to_equal request[:total]
18
+
19
+ # Clear mock
20
+ clear_mock_url
21
+ end
22
+
23
+ should 'Raise error when payload is empty' do
24
+ expect { ChattyCrow::Request::Android.new(contacts: %w(test1 test2)) }.to_raise ::ArgumentError
25
+ end
26
+
27
+ should 'Raise error when payload data is nil' do
28
+ expect { ChattyCrow::Request::Android.new(payload: { time_to_live: 5 }, contacts: %w(test1 test2)) }.to_raise ::ArgumentError
29
+ end
30
+
31
+ should 'Raise error when payload data is empty' do
32
+ expect { ChattyCrow::Request::Android.new(payload: { data: {}, time_to_live: 5 }, contacts: %w(test1 test2)) }.to_raise ::ArgumentError
33
+ end
34
+
35
+ should 'Raise error when payload data is string' do
36
+ expect { ChattyCrow::Request::Android.new(payload: { data: 'string', time_to_live: 5 }, contacts: %w(test1 test2)) }.to_raise ::ArgumentError
37
+ end
38
+
39
+ should 'Create partial notification' do
40
+ request = { status: 'PERROR', msg: 'Success partial', sucess: 12, total: 15, contacts: %w(test1 test2) }
41
+ mock_notification body: request.to_json, status: %(201 Created)
42
+
43
+ # Create request
44
+ response = ChattyCrow.send_android payload: { data: { key1: 'Data1', data_test: 'Data Test' } }
45
+
46
+ # Expect response
47
+ expect(response).to_be_instance_of ChattyCrow::Response::Notification
48
+ expect(response.status).to_equal request[:status]
49
+ expect(response.msg).to_equal request[:msg]
50
+ expect(response.success).to_equal request[:success]
51
+ expect(response.total).to_equal request[:total]
52
+ expect(response.failed_contacts).to_equal request[:contacts]
53
+
54
+ # Clear mock
55
+ clear_mock_url
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ # Test for attachments
4
+ class AttachmentTest < MiniTest::Should::TestCase
5
+ let(:file) { File.open('test/factories/stewie.jpeg', 'rb') }
6
+ let(:attachment) { ChattyCrow::Request::Attachment.new(file: file, filename: 'stewie.jpeg', mime_type: 'image/jpeg', inline: false) }
7
+
8
+ should 'valid attachment' do
9
+ expect(attachment.filename).to_equal 'stewie.jpeg'
10
+ expect(attachment.mime_type).to_equal 'image/jpeg'
11
+ expect(attachment.inline).to_equal false
12
+ end
13
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ # Base parser test
4
+ class BaseParserTest < MiniTest::Should::TestCase
5
+ should 'Raise UnauthorizedRequest when invalid channel or token was send' do
6
+ # Fake URL for contacts
7
+ mock_contacts status: %w(401 Unauthorized)
8
+
9
+ # Call for Contacts!
10
+ expect { ChattyCrow.add_contacts 'test1', 'test2' }.to_raise ChattyCrow::Error::UnauthorizedRequest
11
+
12
+ # Remove mock
13
+ clear_mock_url
14
+ end
15
+
16
+ should 'Raise ChannelNotFound when invalid channel or token was send' do
17
+ # Fake URL for contacts
18
+ mock_contacts status: ['404', 'Not Found']
19
+
20
+ # Call for Contacts!
21
+ expect { ChattyCrow.add_contacts 'test1', 'test2' }.to_raise ChattyCrow::Error::ChannelNotFound
22
+
23
+ # Remove mock
24
+ clear_mock_url
25
+ end
26
+
27
+ should 'Raise InvalidReturn when invalid response accepted' do
28
+ # Fake URL for contacts
29
+ mock_contacts status: ['500', 'Internal Server Error']
30
+
31
+ # Call for Contacts!
32
+ expect { ChattyCrow.add_contacts 'test1', 'test2' }.to_raise ChattyCrow::Error::InvalidReturn
33
+
34
+ # Remove mock
35
+ clear_mock_url
36
+ end
37
+
38
+ should 'Raise InvalidAttributes when attributes missing' do
39
+ body = {
40
+ status: 'ERROR',
41
+ parameters: %w(subject text_body),
42
+ msg: 'Missing parameters'
43
+ }
44
+
45
+ # Fake URL for notification
46
+ mock_notification status: ['400', 'Bad Request'], body: body.to_json
47
+
48
+ # Get error
49
+ error = nil
50
+ begin
51
+ ChattyCrow.send_ios payload: 'Welcome users'
52
+ rescue => e
53
+ error = e
54
+ end
55
+
56
+ expect(error).to_be_instance_of ChattyCrow::Error::InvalidAttributes
57
+ expect(error.attributes).to_include 'subject'
58
+ expect(error.attributes).to_include 'text_body'
59
+
60
+ # Remove mock
61
+ clear_mock_url
62
+ end
63
+
64
+ should 'Use token from configuration' do
65
+ # Fake URL for notification
66
+ mock_notification status: ['400', 'Bad Request'], body: {}.to_json
67
+
68
+ # Send !
69
+ ChattyCrow.send_ios payload: 'Welcome users' rescue nil
70
+
71
+ # Get last request
72
+ expect(last_headers['token']).to_equal ChattyCrow.configuration.token
73
+
74
+ # Clear
75
+ clear_mock_url
76
+ end
77
+
78
+ should 'Use token from parameters' do
79
+ # Fake URL for notification
80
+ mock_notification status: ['400', 'Bad Request'], body: {}.to_json
81
+
82
+ # New token
83
+ token = 'test_token'
84
+
85
+ # Send !
86
+ ChattyCrow.send_ios payload: 'Welcome users', token: token rescue nil
87
+
88
+ # Get last request
89
+ expect(last_headers['token']).to_equal token
90
+
91
+ # Clear
92
+ clear_mock_url
93
+ end
94
+
95
+ should 'Use contact configuration token' do
96
+ # Fake URL for contacts
97
+ mock_contacts status: %w(401 Unauthorized)
98
+
99
+ # Call for Contacts!
100
+ ChattyCrow.add_contacts 'test1' rescue nil
101
+
102
+ # Get last request
103
+ expect(last_headers['token']).to_equal ChattyCrow.configuration.token
104
+
105
+ # Remove mock
106
+ clear_mock_url
107
+ end
108
+
109
+ should 'Use contact token from parameters' do
110
+ # Fake URL for contacts
111
+ mock_contacts status: %w(401 Unauthorized)
112
+
113
+ # New token
114
+ token = 'test_token'
115
+
116
+ # Call for Contacts!
117
+ ChattyCrow.add_contacts('test1', token: token) rescue nil
118
+
119
+ # Get last request
120
+ expect(last_headers['token']).to_equal token
121
+
122
+ # Remove mock
123
+ clear_mock_url
124
+ end
125
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ # Test for proper gem configuration
4
+ class ConfigurationTest < MiniTest::Should::TestCase
5
+ should 'provide defaults' do
6
+ assert_config_default :host, 'https://chatty-crow.com/api/v1/'
7
+ assert_config_default :notification_url, 'https://chatty-crow.com/api/v1/notification'
8
+ assert_config_default :contacts_url, 'https://chatty-crow.com/api/v1/contacts'
9
+ assert_config_default :token, nil
10
+ assert_config_default :default_channel, nil
11
+ assert_config_default :http_open_timeout, 2
12
+ assert_config_default :http_read_timeout, 5
13
+ end
14
+
15
+ should 'use correct url wihtout backslash' do
16
+ config = ChattyCrow::Configuration.new
17
+ url = 'http://test.com/api/v1'
18
+ config.host = url
19
+ assert_equal url, config.host
20
+ assert_equal "#{url}/notification", config.notification_url
21
+ assert_equal "#{url}/contacts", config.contacts_url
22
+ end
23
+
24
+ should 'use correct url with backslash' do
25
+ config = ChattyCrow::Configuration.new
26
+ url = 'http://test.com/api/v1/'
27
+ config.host = url
28
+ assert_equal url, config.host
29
+ assert_equal "#{url}notification", config.notification_url
30
+ assert_equal "#{url}contacts", config.contacts_url
31
+ end
32
+
33
+ def assert_config_default(option, default_value, config = nil)
34
+ config ||= ChattyCrow::Configuration.new
35
+ assert_equal default_value, config.send(option)
36
+ end
37
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ # Unit test for contact methods
4
+ class ContactsTest < MiniTest::Should::TestCase
5
+ should 'Raise invalid argument when no contact list is present' do
6
+ expect { ChattyCrow.add_contacts }.to_raise ArgumentError
7
+ expect { ChattyCrow.remove_contacts }.to_raise ArgumentError
8
+ expect { ChattyCrow.add_contacts(test_option: 'option') }.to_raise ArgumentError
9
+ expect { ChattyCrow.remove_contacts(test_option: 'option') }.to_raise ArgumentError
10
+ end
11
+
12
+ should 'Return contact add list' do
13
+ # Prepare response
14
+ request = {
15
+ status: 'OK',
16
+ msg: 'Status message',
17
+ stats: {
18
+ success: 15,
19
+ exists: 28,
20
+ failed: 12
21
+ },
22
+ contacts: {
23
+ exists: %w(franta1 franta5),
24
+ failed: %w(franta2 franta3)
25
+ }
26
+ }
27
+
28
+ # Fake URL contacts
29
+ mock_contacts method: :post, body: request.to_json
30
+
31
+ # Get contacts
32
+ response = ChattyCrow.add_contacts('test1', 'test2')
33
+
34
+ # Validate
35
+ expect(response).to_be_kind_of ChattyCrow::Response::ContactsAdd
36
+ expect(response.status).to_equal request[:status]
37
+ expect(response.msg).to_equal request[:msg]
38
+
39
+ # Statistics
40
+ expect(response.success_count).to_equal request[:stats][:success]
41
+ expect(response.exists_count).to_equal request[:stats][:exists]
42
+ expect(response.failed_count).to_equal request[:stats][:failed]
43
+
44
+ # Failed & Exists contacts
45
+ expect(response.exists).to_include 'franta1'
46
+ expect(response.exists).to_include 'franta5'
47
+ expect(response.failed).to_include 'franta2'
48
+ expect(response.failed).to_include 'franta3'
49
+
50
+ # Remove mock
51
+ clear_mock_url
52
+ end
53
+
54
+ should 'Return contact remove list' do
55
+ # Prepare response
56
+ request = {
57
+ status: 'OK',
58
+ msg: 'Status message',
59
+ stats: {
60
+ success: 15,
61
+ failed: 12
62
+ },
63
+ contacts: {
64
+ failed: %w(franta2 franta3)
65
+ }
66
+ }
67
+
68
+ # Fake URL contacts
69
+ mock_contacts method: :delete, status: %w(201 Created), body: request.to_json
70
+
71
+ # Get contacts
72
+ response = ChattyCrow.remove_contacts('test12', 'test15')
73
+
74
+ # Validate
75
+ expect(response).to_be_kind_of ChattyCrow::Response::ContactsRemove
76
+ expect(response.status).to_equal request[:status]
77
+ expect(response.msg).to_equal request[:msg]
78
+
79
+ # Statistics
80
+ expect(response.success_count).to_equal request[:stats][:success]
81
+ expect(response.failed_count).to_equal request[:stats][:failed]
82
+
83
+ # Failed & Exists contacts
84
+ expect(response.failed).to_include 'franta2'
85
+ expect(response.failed).to_include 'franta3'
86
+
87
+ # Remove mock
88
+ clear_mock_url
89
+ end
90
+ end
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'fakeweb'
3
+ require 'minitest/autorun'
4
+ require 'minitest/should'
5
+ require 'minitest/spec/expect'
6
+ require 'simplecov'
7
+ require 'coveralls'
8
+
9
+ # Start Coveralls
10
+ SimpleCov.start do
11
+ add_filter '/test/'
12
+ end
13
+ Coveralls.wear!
14
+
15
+ # Require Gem
16
+ require 'chatty_crow'
17
+
18
+ # Set chatty_crow by block
19
+ ChattyCrow.configure do |config|
20
+ config.token = 'temporary_token'
21
+ config.default_channel = 'default_channel'
22
+ end
23
+
24
+ # Usefull test helpers
25
+ module TestHelpers
26
+ def configuration
27
+ ChattyCrow.configuration
28
+ end
29
+
30
+ # Mock default notification url
31
+ def mock_notification(options)
32
+ method = options.delete(:method) || :post
33
+ options[:status] ||= %w(200 OK)
34
+
35
+ FakeWeb.register_uri(method, configuration.notification_url, options)
36
+ end
37
+
38
+ # Mock default URL for contacts
39
+ def mock_contacts(options)
40
+ method = options.delete(:method) || :post
41
+ options[:status] ||= %w(200 OK)
42
+
43
+ FakeWeb.register_uri(method, configuration.contacts_url, options)
44
+ end
45
+
46
+ # Get last headers
47
+ def last_headers
48
+ ret = {}
49
+ FakeWeb.last_request.each_header do |key, value|
50
+ ret[key] = value
51
+ end
52
+ ret
53
+ end
54
+
55
+ def clear_mock_url
56
+ FakeWeb.clean_registry
57
+ end
58
+ end
59
+
60
+ # Include test helper methods to minitest!
61
+ class MiniTest::Should::TestCase
62
+ include ::TestHelpers
63
+ end
data/test/mail_test.rb ADDED
@@ -0,0 +1,62 @@
1
+ require File.expand_path '../helper', __FILE__
2
+ require 'base64'
3
+
4
+ # Mail test
5
+ class MailTest < MiniTest::Should::TestCase
6
+ let(:valid_mail) { ChattyCrow.create_mail subject: 'test', text_body: 'text body', contacts: 'test@netbrick.eu' }
7
+ let(:invalid_mail) { ChattyCrow.create_mail subject: 'test' }
8
+ let(:mail) { ChattyCrow.create_mail subject: 'test', text_body: 'text_body', html_body: 'html_body', contacts: 'test@netbrick.eu' }
9
+
10
+ should 'create mail' do
11
+ expect(valid_mail).to_be_instance_of ChattyCrow::Request::Mail
12
+ expect(valid_mail.subject).to_equal 'test'
13
+ expect(valid_mail.text_body).to_equal 'text body'
14
+ expect(valid_mail.contacts).to_include 'test@netbrick.eu'
15
+ end
16
+
17
+ should 'raise error when trying deliver without body' do
18
+ expect { invalid_mail.deliver! }.to_raise ::ArgumentError
19
+ end
20
+
21
+ should 'return false when trying deliver without body' do
22
+ expect(invalid_mail.deliver).to_equal false
23
+ end
24
+
25
+ should 'serializer payload test' do
26
+ json = mail.payload
27
+ expect(json[:subject]).to_equal 'test'
28
+ expect(json[:text_body]).to_equal Base64.encode64('text_body')
29
+ expect(json[:html_body]).to_equal Base64.encode64('html_body')
30
+ end
31
+
32
+ should 'raise error unless file is present' do
33
+ expect { mail.add_file {} }.to_raise ::ArgumentError
34
+ end
35
+
36
+ should 'raise error if file is string but not filename provided' do
37
+ expect { mail.add_file file: 'base64data', mime_type: 'image/jpeg' }.to_raise ::ArgumentError
38
+ end
39
+
40
+ should 'raise error if file is string but not mime-type provided' do
41
+ expect { mail.add_file file: 'base64data', filename: 'test.jpeg' }.to_raise ::ArgumentError
42
+ end
43
+
44
+ should 'decide mime type and filename from factory' do
45
+ file = File.open('test/factories/stewie.jpeg')
46
+ attachment = mail.add_file file: file
47
+ expect(attachment).to_be_instance_of ChattyCrow::Request::Attachment
48
+ expect(attachment.filename).to_equal 'stewie.jpeg'
49
+ expect(attachment.mime_type).to_equal 'image/jpeg'
50
+ end
51
+
52
+ should 'test attachment serializer' do
53
+ file = File.open('test/factories/stewie.jpeg')
54
+ attachment = mail.add_file file: file
55
+ json = attachment.to_json
56
+ file.seek(0)
57
+ expect(json[:name]).to_equal 'stewie.jpeg'
58
+ expect(json[:mime_type]).to_equal 'image/jpeg'
59
+ expect(json[:inline]).to_equal false
60
+ expect(json[:base64data]).to_equal Base64.encode64(file.read)
61
+ end
62
+ end