mailup 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +79 -0
  6. data/Rakefile +7 -0
  7. data/lib/mailup.rb +206 -0
  8. data/lib/mailup/console/base.rb +115 -0
  9. data/lib/mailup/console/email.rb +35 -0
  10. data/lib/mailup/console/group.rb +111 -0
  11. data/lib/mailup/console/images.rb +69 -0
  12. data/lib/mailup/console/import.rb +38 -0
  13. data/lib/mailup/console/list.rb +863 -0
  14. data/lib/mailup/console/recipient.rb +69 -0
  15. data/lib/mailup/console/user.rb +77 -0
  16. data/lib/mailup/errors.rb +18 -0
  17. data/lib/mailup/public/base.rb +18 -0
  18. data/lib/mailup/public/console.rb +75 -0
  19. data/lib/mailup/stats/base.rb +41 -0
  20. data/lib/mailup/stats/message.rb +284 -0
  21. data/lib/mailup/stats/recipient.rb +303 -0
  22. data/lib/mailup/version.rb +4 -0
  23. data/mailup.gemspec +25 -0
  24. data/rails/init.rb +1 -0
  25. data/spec/mailup/console/base_spec.rb +33 -0
  26. data/spec/mailup/console/email_spec.rb +19 -0
  27. data/spec/mailup/console/group_spec.rb +42 -0
  28. data/spec/mailup/console/images_spec.rb +29 -0
  29. data/spec/mailup/console/import_spec.rb +17 -0
  30. data/spec/mailup/console/list_spec.rb +164 -0
  31. data/spec/mailup/console/recipient_spec.rb +11 -0
  32. data/spec/mailup/console/user_spec.rb +16 -0
  33. data/spec/mailup/mailup_spec.rb +36 -0
  34. data/spec/mailup/public/base_spec.rb +22 -0
  35. data/spec/mailup/public/console_spec.rb +24 -0
  36. data/spec/mailup/stats/base_spec.rb +22 -0
  37. data/spec/mailup/stats/message_spec.rb +35 -0
  38. data/spec/mailup/stats/recipient_spec.rb +40 -0
  39. data/spec/spec_helper.rb +37 -0
  40. metadata +138 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2954259506053f8acefde3cf2d6d9bfff76ffdc
4
+ data.tar.gz: b506fb4efd45263932edd677e6f155c761000401
5
+ SHA512:
6
+ metadata.gz: b9463e8d8a5e7de85161f34ca75280b60ea46e3b1b72a81b994224147be3d7f0c20de7b4c563e7fc04659dd25979c4adb93f7dcd804ef9c54b51fb227e37560b
7
+ data.tar.gz: 5d59ab00556ffbc4a98da5da3fc57b4b8e9333f1113266e078f0f52b6a33bba782a8aea12292ecf1a5af29eab918c534ceef02a4f14a8c70b7f43334a66cc25e
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .rbenv-gemsets
20
+ .rbenv-vars
21
+ .rspec
22
+ credentials.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mailup-rest.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Getting
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Mailup Ruby
2
+
3
+ A Ruby gem for interacting with the MailUp REST API.
4
+
5
+ ## Installation
6
+
7
+ Install the gem directly:
8
+
9
+ ```ruby
10
+ gem install mailup
11
+ ```
12
+
13
+ Or, add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'mailup'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Be sure that you have OAuth2 tokens for the MailUp REST API before using this gem. If you are using [Omniauth](https://github.com/intridea/omniauth), there is a [MailUp strategy](https://github.com/tatemae-consultancy/omniauth-mailup) available.
22
+
23
+ ### Credentials
24
+
25
+ Once you've gone through the OAuth process, and you have obtained your tokens, you need to provide your `client_id`, `client_secret` and OAuth2 tokens in a `credentials` hash to get started.
26
+
27
+ ```ruby
28
+ require 'mailup'
29
+
30
+ credentials = {
31
+ client_id: ENV['MAILUP_CLIENT_ID'],
32
+ client_secret: ENV['MAILUP_CLIENT_SECRET'],
33
+ oauth: {
34
+ token: "...",
35
+ refresh_token: "...",
36
+ expires_at: 1234567890
37
+ }
38
+ }
39
+
40
+ mailup = MailUp::Api.new(credentials)
41
+ ```
42
+
43
+ ### Console Methods
44
+
45
+ Check out the [console methods API documentation](http://help.mailup.com/display/mailupapi/Console+methods+v1.1) for more information.
46
+
47
+ ```ruby
48
+ lists = mailup.console.user.lists
49
+ lists['Items'].first['Name']
50
+ # => "Test List"
51
+ list_id = lists['Items'].first['idList'].to_i
52
+ # => 1
53
+ list = mailup.console.list(list_id)
54
+ groups = list.groups(pageNumber: 1, pageSize: 25)
55
+ groups['IsPaginated']
56
+ # => true
57
+ groups['PageNumber']
58
+ # => 1
59
+ groups['PageSize']
60
+ # => 25
61
+ groups['Skipped']
62
+ # => 25
63
+ groups['TotalElementsCount']
64
+ # => 35
65
+ groups['Items'].size
66
+ # => 10
67
+ ```
68
+
69
+ ### Email Statistics Methods
70
+
71
+ Check out the [email statistics API documentation](http://help.mailup.com/display/mailupapi/Email+statistics+methods+v1.1) for more information.
72
+
73
+ ```ruby
74
+ views_count = mailup.stats.message(17).views_count
75
+ # => 250
76
+
77
+ fields = mailup.stats.recipient(5).fields
78
+ # => {"IsPaginated"=>true, "Items"=>[{"Description"=>"AllOrderedProductIDs", "Id"=>26}, ...], ...}
79
+ ```
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ # If you want to make this the default task
7
+ #task default: :spec
@@ -0,0 +1,206 @@
1
+ require 'oauth2'
2
+ require 'multi_json'
3
+
4
+ require 'mailup/version'
5
+ require 'mailup/errors'
6
+ require 'mailup/console/base'
7
+ require 'mailup/console/email'
8
+ require 'mailup/console/group'
9
+ require 'mailup/console/images'
10
+ require 'mailup/console/import'
11
+ require 'mailup/console/list'
12
+ require 'mailup/console/recipient'
13
+ require 'mailup/console/user'
14
+ require 'mailup/public/base'
15
+ require 'mailup/public/console'
16
+ require 'mailup/stats/base'
17
+ require 'mailup/stats/message'
18
+ require 'mailup/stats/recipient'
19
+
20
+ module MailUp
21
+ class API
22
+ attr_accessor :access_token, :debug, :host, :path
23
+
24
+ # Initialize a new (thread-safe) API instance.
25
+ #
26
+ # @param [Hash] credentials for connecting to the MailUp API.
27
+ # * client_id [String]
28
+ # * client_secret [String]
29
+ # * oauth [Hash]
30
+ # * token [String]
31
+ # * refresh_token [String]
32
+ # * expires_at [Integer]
33
+ # @param [Boolean] debug whether or not to raise errors.
34
+ #
35
+ # @example
36
+ #
37
+ # credentials = {
38
+ # client_id: "1324567890",
39
+ # client_secret: "123abc456def",
40
+ # oauth: {
41
+ # token: "1324567890",
42
+ # refresh_token: "1324567890",
43
+ # expires_at: 123456789,
44
+ # }
45
+ # }
46
+ # mailup = MailUp::API.new(credentials)
47
+ #
48
+ def initialize(credentials=nil, debug=false)
49
+ @debug = debug
50
+ @host = 'https://services.mailup.com'
51
+ @path = ''
52
+
53
+ # Validate the credentials
54
+ raise Error.new, 'MailUp credentials missing' if credentials.nil? or !credentials.is_a?(Hash)
55
+ [:client_id, :client_secret, :oauth].each do |key|
56
+ raise Error.new, "MailUp credentials must include a #{key.to_s} key" unless credentials.has_key?(key)
57
+ end
58
+ raise Error.new, 'MailUp credentials :oauth must be a hash' unless credentials[:oauth].is_a?(Hash)
59
+ [:token, :refresh_token, :expires_at].each do |key|
60
+ raise Error.new, "MailUp credentials :oauth hash must include a #{key.to_s} key" unless credentials[:oauth].has_key?(key)
61
+ end
62
+
63
+ # Create a OAuth2 client instance
64
+ client = OAuth2::Client.new(
65
+ credentials[:client_id],
66
+ credentials[:client_secret],
67
+ site: @host,
68
+ authorize_url: "/Authorization/OAuth/LogOn",
69
+ token_url: "/Authorization/OAuth/Token",
70
+ raise_errors: @debug
71
+ )
72
+
73
+ # Create an access_token instance
74
+ @access_token = OAuth2::AccessToken.new(
75
+ client,
76
+ credentials[:oauth][:token],
77
+ {
78
+ refresh_token: credentials[:oauth][:refresh_token],
79
+ expires_at: credentials[:oauth][:expires_at]
80
+ }
81
+ )
82
+ end
83
+
84
+ # Make a request with the Access Token.
85
+ #
86
+ # @param [Symbol] verb the HTTP request method
87
+ # @param [String] path the HTTP URL path of the request
88
+ # @param [Hash] opts the options to make the request with
89
+ #
90
+ def request(method, path, opts={}, &block) # :nodoc:
91
+ unless @access_token == nil
92
+ # Refresh token if needed
93
+ @access_token = @access_token.refresh! if @access_token.expired?
94
+ # Ensure the body is JSON
95
+ opts[:body] = MultiJson.dump(opts[:body]) if opts[:body]
96
+ # Set the headers
97
+ opts[:headers] ||= {}
98
+ opts[:headers].merge!(headers)
99
+ # Make the request
100
+ req = @access_token.send(method, path, opts)
101
+ # Handle the response
102
+ handle_response(req)
103
+ end
104
+ end
105
+
106
+ # Make a GET request with the Access Token
107
+ # @see #request
108
+ #
109
+ def get(path, opts={}, &block) # :nodoc:
110
+ request(:get, path, opts, &block)
111
+ end
112
+
113
+ # Make a POST request with the Access Token
114
+ # @see #request
115
+ #
116
+ def post(path, opts={}, &block) # :nodoc:
117
+ request(:post, path, opts, &block)
118
+ end
119
+
120
+ # Make a PUT request with the Access Token
121
+ # @see #request
122
+ #
123
+ def put(path, opts={}, &block) # :nodoc:
124
+ request(:put, path, opts, &block)
125
+ end
126
+
127
+ # Make a PATCH request with the Access Token
128
+ # @see #request
129
+ #
130
+ def patch(path, opts={}, &block) # :nodoc:
131
+ request(:patch, path, opts, &block)
132
+ end
133
+
134
+ # Make a DELETE request with the Access Token
135
+ # @see #request
136
+ #
137
+ def delete(path, opts={}, &block) # :nodoc:
138
+ request(:delete, path, opts, &block)
139
+ end
140
+
141
+ # Handle the response of a request
142
+ def handle_response(response) # :nodoc:
143
+ case response.status
144
+ when 400
145
+ raise BadRequest.new response.parsed
146
+ when 401
147
+ raise Unauthorized.new
148
+ when 404
149
+ raise NotFound.new
150
+ when 400...500
151
+ raise ClientError.new response.parsed
152
+ when 500...600
153
+ raise ServerError.new
154
+ else
155
+ case response.body
156
+ when ''
157
+ true
158
+ when is_a?(Integer)
159
+ response.body
160
+ else
161
+ response.parsed
162
+ end
163
+ end
164
+ end
165
+
166
+ # Set the request headers
167
+ def headers # :nodoc:
168
+ {
169
+ 'User-Agent' => "mailup-ruby-#{VERSION}",
170
+ 'Content-Type' => 'application/json; charset=utf-8',
171
+ 'Accept' => 'application/json'
172
+ }
173
+ end
174
+
175
+ # Access the console API methods
176
+ #
177
+ # @example
178
+ #
179
+ # lists = mailup.console.user.lists
180
+ #
181
+ def console
182
+ Console::Base.new self
183
+ end
184
+
185
+ # Access the public API methods
186
+ #
187
+ # @example
188
+ #
189
+ # activation = mailup.public.activation.new(...)
190
+ #
191
+ def public
192
+ Public::Base.new self
193
+ end
194
+
195
+ # Access the email statistics API methods
196
+ #
197
+ # @example
198
+ #
199
+ # views = mailup.stats.message.views_count(1)
200
+ #
201
+ def stats
202
+ Stats::Base.new self
203
+ end
204
+
205
+ end
206
+ end
@@ -0,0 +1,115 @@
1
+ module MailUp
2
+ module Console
3
+ class Base
4
+ attr_accessor :api
5
+
6
+ def initialize(api) # :nodoc:
7
+ @api = api
8
+ @api.path = "/API/v#{MailUp::API_VERSION}/Rest/ConsoleService.svc/Console"
9
+ end
10
+
11
+ # Create an email object
12
+ #
13
+ # @return [MailUp::Console::Email]
14
+ #
15
+ # @example
16
+ #
17
+ # email = mailup.console.email
18
+ #
19
+ def email
20
+ Email.new @api
21
+ end
22
+
23
+ # Create a group object
24
+ #
25
+ # @param [Integer] id The group_id of the group to access.
26
+ #
27
+ # @return [MailUp::Console::Group]
28
+ #
29
+ # @example
30
+ #
31
+ # group = mailup.console.group(1)
32
+ #
33
+ def group(id)
34
+ Group.new id, @api
35
+ end
36
+
37
+ # Create an images object
38
+ #
39
+ # @return [MailUp::Console::Images]
40
+ #
41
+ # @example
42
+ #
43
+ # images = mailup.console.images
44
+ #
45
+ def images
46
+ Images.new @api
47
+ end
48
+
49
+ # Create an import object
50
+ #
51
+ # @param [Integer] idImport The ID of the import process.
52
+ #
53
+ # @return [MailUp::Console::Import]
54
+ #
55
+ # @example
56
+ #
57
+ # import = mailup.console.import(9)
58
+ #
59
+ def import(id)
60
+ Import.new id, @api
61
+ end
62
+
63
+ # Create a list object
64
+ #
65
+ # @param [Integer] id The list_id of the list to access.
66
+ #
67
+ # @return [MailUp::Console::List]
68
+ #
69
+ # @example
70
+ #
71
+ # list = mailup.console.list(2)
72
+ #
73
+ def list(id)
74
+ List.new id, @api
75
+ end
76
+
77
+ # Create a public methods object
78
+ #
79
+ # @return [MailUp::Console::List]
80
+ #
81
+ # @example
82
+ #
83
+ # list = mailup.console.list(2)
84
+ #
85
+ def public
86
+ Public.new @api
87
+ end
88
+
89
+ # Create a recipient object
90
+ #
91
+ # @return [MailUp::Console::Recipient]
92
+ #
93
+ # @example
94
+ #
95
+ # recipient = mailup.console.recipient
96
+ #
97
+ def recipient
98
+ Recipient.new @api
99
+ end
100
+
101
+ # Create a user object
102
+ #
103
+ # @return [MailUp::Console::User]
104
+ #
105
+ # @example
106
+ #
107
+ # user = mailup.console.user
108
+ #
109
+ def user
110
+ User.new @api
111
+ end
112
+
113
+ end
114
+ end
115
+ end