mailosaur 6.0.6 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93384f72178b70f3cbf990d30be7d7110ede5b6e81338486d019b041d10b829c
4
- data.tar.gz: 5b7342050cd44184dab1ffdf453c7eac88c12ccfae420dccab9f19b29aca66f7
3
+ metadata.gz: 7ded354d904892b5edb18803695bda97a368a55bcf8bee8f1b3f7cc3d54223a8
4
+ data.tar.gz: 5e7e2e660129a7c4067d1ef74075f3f1be5e2ca8a78411199acb77b3c6b2352d
5
5
  SHA512:
6
- metadata.gz: 57d70f504d3b930d4d017a82c6de18cb89d6b9518219a9c5fb3b3cde1ab386f082977aba014ef698afb0052d07a79305738c9b531afcb8c0ad37acb711945ac5
7
- data.tar.gz: ce5fc8101e3eff75a329c775fafe54b9615f81a928fde391e8932129ade0f90023c8352551f616e612c06d4b7f661a48e4ec940cc3de6b570ce97ce075ceac77
6
+ metadata.gz: c2217d5763f88bc5c4d8a5f769ab327c0a31e1e781300a8cff5fb264297568c05e664c4d431359b49cc401e1756b9a52e070d71a87489822da0069aaf3bf8d9c
7
+ data.tar.gz: a489db754706165b9c1a5637d73bd5fd5ab236c3fdfa81f6fe25e36d534b789425290e1c63d33107c0a79bc177e1eb20ee93101b3ab0dc8a18d7b99a6814b465
@@ -4,8 +4,9 @@ module Mailosaur
4
4
  # Creates and initializes a new instance of the Analysis class.
5
5
  # @param conn client connection.
6
6
  #
7
- def initialize(conn)
7
+ def initialize(conn, handle_http_error)
8
8
  @conn = conn
9
+ @handle_http_error = handle_http_error
9
10
  end
10
11
 
11
12
  # @return [Connection] the client connection.
@@ -22,13 +23,7 @@ module Mailosaur
22
23
  #
23
24
  def spam(email)
24
25
  response = conn.get 'api/analysis/spam/' + email
25
-
26
- unless response.status == 200
27
- error_model = JSON.load(response.body)
28
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
29
- raise mailosaur_error
30
- end
31
-
26
+ @handle_http_error.call(response) unless response.status == 200
32
27
  model = JSON.load(response.body)
33
28
  Mailosaur::Models::SpamAnalysisResult.new(model)
34
29
  end
@@ -4,8 +4,9 @@ module Mailosaur
4
4
  # Creates and initializes a new instance of the Files class.
5
5
  # @param client connection.
6
6
  #
7
- def initialize(conn)
7
+ def initialize(conn, handle_http_error)
8
8
  @conn = conn
9
+ @handle_http_error = handle_http_error
9
10
  end
10
11
 
11
12
  # @return [Connection] the client connection.
@@ -23,13 +24,7 @@ module Mailosaur
23
24
  #
24
25
  def get_attachment(id)
25
26
  response = conn.get 'api/files/attachments/' + id
26
-
27
- unless response.status == 200
28
- error_model = JSON.load(response.body)
29
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
30
- raise mailosaur_error
31
- end
32
-
27
+ @handle_http_error.call(response) unless response.status == 200
33
28
  response.body
34
29
  end
35
30
 
@@ -45,13 +40,7 @@ module Mailosaur
45
40
  #
46
41
  def get_email(id)
47
42
  response = conn.get 'api/files/email/' + id
48
-
49
- unless response.status == 200
50
- error_model = JSON.load(response.body)
51
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
52
- raise mailosaur_error
53
- end
54
-
43
+ @handle_http_error.call(response) unless response.status == 200
55
44
  response.body
56
45
  end
57
46
  end
@@ -1,23 +1,15 @@
1
- require 'json'
2
-
3
1
  module Mailosaur
4
2
  class MailosaurError < StandardError
5
- attr_reader :type
6
- attr_reader :messages
7
- attr_reader :model
3
+ attr_reader :error_type
4
+ attr_reader :http_status_code
5
+ attr_reader :http_response_body
8
6
 
9
- def initialize(message, error_model)
7
+ def initialize(message = '', error_type = '', http_status_code = nil, http_response_body = nil)
10
8
  super(message)
11
9
 
12
- @type = nil
13
- @messages = nil
14
- @model = nil
15
-
16
- unless error_model.nil?
17
- @type = error_model['type']
18
- @messages = error_model['messages']
19
- @model = error_model['model']
20
- end
10
+ @error_type = error_type
11
+ @http_status_code = http_status_code
12
+ @http_response_body = http_response_body
21
13
  end
22
14
  end
23
15
  end
@@ -6,8 +6,9 @@ module Mailosaur
6
6
  # Creates and initializes a new instance of the Messages class.
7
7
  # @param client connection.
8
8
  #
9
- def initialize(conn)
9
+ def initialize(conn, handle_http_error)
10
10
  @conn = conn
11
+ @handle_http_error = handle_http_error
11
12
  end
12
13
 
13
14
  # @return [Connection] the client connection.
@@ -31,7 +32,7 @@ module Mailosaur
31
32
  #
32
33
  def get(server, criteria, timeout: 10_000, received_after: DateTime.now - (1.0 / 24))
33
34
  # Defaults timeout to 10s, receivedAfter to 1h
34
- raise Mailosaur::MailosaurError.new('Use get_by_id to retrieve a message using its identifier', nil) if server.length > 8
35
+ raise Mailosaur::MailosaurError.new('Must provide a valid Server ID.', 'invalid_request') if server.length != 8
35
36
 
36
37
  result = search(server, criteria, timeout: timeout, received_after: received_after)
37
38
  get_by_id(result.items[0].id)
@@ -49,13 +50,7 @@ module Mailosaur
49
50
  #
50
51
  def get_by_id(id)
51
52
  response = conn.get 'api/messages/' + id
52
-
53
- unless response.status == 200
54
- error_model = JSON.load(response.body)
55
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
56
- raise mailosaur_error
57
- end
58
-
53
+ @handle_http_error.call(response) unless response.status == 200
59
54
  model = JSON.load(response.body)
60
55
  Mailosaur::Models::Message.new(model)
61
56
  end
@@ -70,13 +65,7 @@ module Mailosaur
70
65
  #
71
66
  def delete(id)
72
67
  response = conn.delete 'api/messages/' + id
73
-
74
- unless response.status == 204
75
- error_model = JSON.load(response.body)
76
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
77
- raise mailosaur_error
78
- end
79
-
68
+ @handle_http_error.call(response) unless response.status == 204
80
69
  nil
81
70
  end
82
71
 
@@ -99,17 +88,13 @@ module Mailosaur
99
88
  #
100
89
  def list(server, page: nil, items_per_page: nil, received_after: nil)
101
90
  url = 'api/messages?server=' + server
102
- url += page ? '&page=' + page : ''
103
- url += items_per_page ? '&itemsPerPage=' + items_per_page : ''
91
+ url += page ? '&page=' + page.to_s : ''
92
+ url += items_per_page ? '&itemsPerPage=' + items_per_page.to_s : ''
104
93
  url += received_after ? '&receivedAfter=' + CGI.escape(received_after.iso8601) : ''
105
94
 
106
95
  response = conn.get url
107
96
 
108
- unless response.status == 200
109
- error_model = JSON.load(response.body)
110
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
111
- raise mailosaur_error
112
- end
97
+ @handle_http_error.call(response) unless response.status == 200
113
98
 
114
99
  model = JSON.load(response.body)
115
100
  Mailosaur::Models::MessageListResult.new(model)
@@ -125,13 +110,7 @@ module Mailosaur
125
110
  #
126
111
  def delete_all(server)
127
112
  response = conn.delete 'api/messages?server=' + server
128
-
129
- unless response.status == 204
130
- error_model = JSON.load(response.body)
131
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
132
- raise mailosaur_error
133
- end
134
-
113
+ @handle_http_error.call(response) unless response.status == 204
135
114
  nil
136
115
  end
137
116
 
@@ -168,11 +147,7 @@ module Mailosaur
168
147
  loop do
169
148
  response = conn.post url, criteria.to_json
170
149
 
171
- unless response.status == 200
172
- error_model = JSON.load(response.body)
173
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model) # rubocop:disable Metrics/LineLength
174
- raise mailosaur_error
175
- end
150
+ @handle_http_error.call(response) unless response.status == 200
176
151
 
177
152
  model = JSON.load(response.body)
178
153
  return Mailosaur::Models::MessageListResult.new(model) if timeout.to_i.zero? || !model['items'].empty?
@@ -185,7 +160,7 @@ module Mailosaur
185
160
 
186
161
  ## Stop if timeout will be exceeded
187
162
  if ((1000 * (Time.now.to_f - start_time).to_i) + delay) > timeout
188
- raise Mailosaur::MailosaurError.new('No matching messages were found in time', nil)
163
+ raise Mailosaur::MailosaurError.new('No matching messages found in time. By default, only messages received in the last hour are checked (use receivedAfter to override this).', 'search_timeout')
189
164
  end
190
165
 
191
166
  sleep(delay / 1000)
@@ -4,8 +4,9 @@ module Mailosaur
4
4
  # Creates and initializes a new instance of the Servers class.
5
5
  # @param client connection.
6
6
  #
7
- def initialize(conn)
7
+ def initialize(conn, handle_http_error)
8
8
  @conn = conn
9
+ @handle_http_error = handle_http_error
9
10
  end
10
11
 
11
12
  # @return [Connection] the client connection.
@@ -21,13 +22,7 @@ module Mailosaur
21
22
  #
22
23
  def list
23
24
  response = conn.get 'api/servers'
24
-
25
- unless response.status == 200
26
- error_model = JSON.load(response.body)
27
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
28
- raise mailosaur_error
29
- end
30
-
25
+ @handle_http_error.call(response) unless response.status == 200
31
26
  model = JSON.load(response.body)
32
27
  Mailosaur::Models::ServerListResult.new(model)
33
28
  end
@@ -43,13 +38,7 @@ module Mailosaur
43
38
  #
44
39
  def create(server_create_options)
45
40
  response = conn.post 'api/servers', server_create_options.to_json
46
-
47
- unless response.status == 200
48
- error_model = JSON.load(response.body)
49
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
50
- raise mailosaur_error
51
- end
52
-
41
+ @handle_http_error.call(response) unless response.status == 200
53
42
  model = JSON.load(response.body)
54
43
  Mailosaur::Models::Server.new(model)
55
44
  end
@@ -66,13 +55,7 @@ module Mailosaur
66
55
  #
67
56
  def get(id)
68
57
  response = conn.get 'api/servers/' + id
69
-
70
- unless response.status == 200
71
- error_model = JSON.load(response.body)
72
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
73
- raise mailosaur_error
74
- end
75
-
58
+ @handle_http_error.call(response) unless response.status == 200
76
59
  model = JSON.load(response.body)
77
60
  Mailosaur::Models::Server.new(model)
78
61
  end
@@ -89,13 +72,7 @@ module Mailosaur
89
72
  #
90
73
  def update(id, server)
91
74
  response = conn.put 'api/servers/' + id, server.to_json
92
-
93
- unless response.status == 200
94
- error_model = JSON.load(response.body)
95
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
96
- raise mailosaur_error
97
- end
98
-
75
+ @handle_http_error.call(response) unless response.status == 200
99
76
  model = JSON.load(response.body)
100
77
  Mailosaur::Models::Server.new(model)
101
78
  end
@@ -110,13 +87,7 @@ module Mailosaur
110
87
  #
111
88
  def delete(id)
112
89
  response = conn.delete 'api/servers/' + id
113
-
114
- unless response.status == 204
115
- error_model = JSON.load(response.body)
116
- mailosaur_error = Mailosaur::MailosaurError.new('Operation returned an invalid status code \'' + response.status.to_s + '\'', error_model)
117
- raise mailosaur_error
118
- end
119
-
90
+ @handle_http_error.call(response) unless response.status == 204
120
91
  nil
121
92
  end
122
93
 
@@ -1,3 +1,3 @@
1
1
  module Mailosaur
2
- VERSION = '6.0.6'.freeze
2
+ VERSION = '7.0.0'.freeze
3
3
  end
@@ -50,22 +50,22 @@ module Mailosaur
50
50
 
51
51
  # @return [Analysis] analysis
52
52
  def analysis
53
- @analysis ||= Analysis.new(connection)
53
+ @analysis ||= Analysis.new(connection, method(:handle_http_error))
54
54
  end
55
55
 
56
56
  # @return [Files] files
57
57
  def files
58
- @files ||= Files.new(connection)
58
+ @files ||= Files.new(connection, method(:handle_http_error))
59
59
  end
60
60
 
61
61
  # @return [Messages] messages
62
62
  def messages
63
- @messages ||= Messages.new(connection)
63
+ @messages ||= Messages.new(connection, method(:handle_http_error))
64
64
  end
65
65
 
66
66
  # @return [Servers] servers
67
67
  def servers
68
- @servers ||= Servers.new(connection)
68
+ @servers ||= Servers.new(connection, method(:handle_http_error))
69
69
  end
70
70
 
71
71
  private
@@ -78,5 +78,20 @@ module Mailosaur
78
78
  }
79
79
  }).tap { |conn| conn.basic_auth(@api_key, '') }
80
80
  end
81
+
82
+ def handle_http_error(response)
83
+ case response.status
84
+ when 400
85
+ raise Mailosaur::MailosaurError.new('Request had one or more invalid parameters.', 'invalid_request', response.status, response.body)
86
+ when 401
87
+ raise Mailosaur::MailosaurError.new('Authentication failed, check your API key.', 'authentication_error', response.status, response.body)
88
+ when 403
89
+ raise Mailosaur::MailosaurError.new('Insufficient permission to perform that task.', 'permission_error', response.status, response.body)
90
+ when 404
91
+ raise Mailosaur::MailosaurError.new('Request did not find any matching resources.', 'invalid_request', response.status, response.body)
92
+ else
93
+ raise Mailosaur::MailosaurError.new('An API error occurred, see httpResponse for further information.', 'api_error', response.status, response.body)
94
+ end
95
+ end
81
96
  end
82
97
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailosaur
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.6
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Mailosaur Ltd
7
+ - Mailosaur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 0.9.0
20
17
  - - "<="
21
18
  - !ruby/object:Gem::Version
22
19
  version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 0.9.0
30
27
  - - "<="
31
28
  - !ruby/object:Gem::Version
32
29
  version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: json
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: 1.7.5
40
37
  - - "<="
41
38
  - !ruby/object:Gem::Version
42
39
  version: '3.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.7.5
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: 1.7.5
50
47
  - - "<="
51
48
  - !ruby/object:Gem::Version
52
49
  version: '3.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.7.5
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: mail
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -74,22 +74,22 @@ dependencies:
74
74
  name: rake
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- version: 12.3.0
80
77
  - - "~>"
81
78
  - !ruby/object:Gem::Version
82
79
  version: '12.3'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 12.3.0
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: 12.3.0
90
87
  - - "~>"
91
88
  - !ruby/object:Gem::Version
92
89
  version: '12.3'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 12.3.0
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: rubocop
95
95
  requirement: !ruby/object:Gem::Requirement
@@ -144,7 +144,8 @@ dependencies:
144
144
  - - ">="
145
145
  - !ruby/object:Gem::Version
146
146
  version: 3.2.7
147
- description: Ruby client library for Mailosaur.
147
+ description: The Mailosaur Ruby library lets you integrate email and SMS testing into
148
+ your continuous integration process.
148
149
  email: code@mailosaur.com
149
150
  executables: []
150
151
  extensions: []
@@ -179,10 +180,15 @@ files:
179
180
  - lib/Mailosaur/servers.rb
180
181
  - lib/Mailosaur/version.rb
181
182
  - lib/mailosaur.rb
182
- homepage: https://mailosaur.com
183
+ homepage: https://mailosaur.com/
183
184
  licenses:
184
185
  - MIT
185
- metadata: {}
186
+ metadata:
187
+ bug_tracker_uri: https://github.com/mailosaur/mailosaur-ruby/issues
188
+ documentation_uri: https://mailosaur.com/docs/email-testing/ruby/
189
+ github_repo: ssh://github.com/mailosaur/mailosaur-ruby
190
+ homepage_uri: https://mailosaur.com/
191
+ source_code_uri: https://github.com/mailosaur/mailosaur-ruby
186
192
  post_install_message:
187
193
  rdoc_options: []
188
194
  require_paths:
@@ -198,8 +204,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
204
  - !ruby/object:Gem::Version
199
205
  version: '0'
200
206
  requirements: []
201
- rubygems_version: 3.0.3
207
+ rubygems_version: 3.1.2
202
208
  signing_key:
203
209
  specification_version: 4
204
- summary: Ruby client library for Mailosaur
210
+ summary: The Mailosaur Ruby library
205
211
  test_files: []