sendgrid-api 0.0.2 → 0.0.3
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/.gitignore +4 -2
- data/.yardopts +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +5 -1
- data/README.md +200 -12
- data/Rakefile +3 -0
- data/lib/sendgrid/api/client.rb +16 -0
- data/lib/sendgrid/api/entities/category.rb +13 -0
- data/lib/sendgrid/api/entities/email.rb +13 -0
- data/lib/sendgrid/api/entities/entity.rb +2 -2
- data/lib/sendgrid/api/entities/list.rb +30 -0
- data/lib/sendgrid/api/entities/marketing_email.rb +20 -0
- data/lib/sendgrid/api/entities/response_insert.rb +23 -0
- data/lib/sendgrid/api/entities/response_remove.rb +23 -0
- data/lib/sendgrid/api/entities/schedule.rb +13 -0
- data/lib/sendgrid/api/entities/sender_address.rb +13 -0
- data/lib/sendgrid/api/newsletter/categories.rb +74 -0
- data/lib/sendgrid/api/newsletter/emails.rb +69 -0
- data/lib/sendgrid/api/newsletter/lists.rb +64 -0
- data/lib/sendgrid/api/newsletter/marketing_emails.rb +72 -0
- data/lib/sendgrid/api/newsletter/recipients.rb +55 -0
- data/lib/sendgrid/api/newsletter/schedule.rb +70 -0
- data/lib/sendgrid/api/newsletter/sender_addresses.rb +80 -0
- data/lib/sendgrid/api/newsletter/utils.rb +34 -0
- data/lib/sendgrid/api/rest/errors/error.rb +9 -3
- data/lib/sendgrid/api/rest/resource.rb +3 -1
- data/lib/sendgrid/api/version.rb +1 -1
- data/lib/sendgrid/api/web/mail.rb +44 -0
- data/lib/sendgrid/api/web/profile.rb +3 -3
- data/lib/sendgrid/api/web/stats.rb +3 -3
- data/spec/fixtures/categories.json +11 -0
- data/spec/fixtures/emails/email.json +6 -0
- data/spec/fixtures/emails/emails.json +10 -0
- data/spec/fixtures/errors/already_exists.json +3 -0
- data/spec/fixtures/errors/bad_request.json +6 -0
- data/spec/fixtures/errors/database_error.json +3 -0
- data/spec/fixtures/errors/does_not_exist.json +3 -0
- data/spec/fixtures/{forbidden.json → errors/forbidden.json} +0 -0
- data/spec/fixtures/errors/invalid_fields.json +3 -0
- data/spec/fixtures/errors/not_scheduled.json +3 -0
- data/spec/fixtures/errors/unauthorized.json +6 -0
- data/spec/fixtures/lists/list.json +5 -0
- data/spec/fixtures/lists/lists.json +11 -0
- data/spec/fixtures/marketing_emails/marketing_email.json +19 -0
- data/spec/fixtures/marketing_emails/marketing_emails.json +10 -0
- data/spec/fixtures/recipients.json +8 -0
- data/spec/fixtures/schedule.json +3 -0
- data/spec/fixtures/sender_addresses/sender_address.json +11 -0
- data/spec/fixtures/sender_addresses/sender_addresses.json +11 -0
- data/spec/sendgrid/api/client_spec.rb +16 -0
- data/spec/sendgrid/api/entities/category_spec.rb +14 -0
- data/spec/sendgrid/api/entities/email_spec.rb +15 -0
- data/spec/sendgrid/api/entities/list_spec.rb +34 -0
- data/spec/sendgrid/api/entities/marketing_email_spec.rb +31 -0
- data/spec/sendgrid/api/entities/response_insert_spec.rb +28 -0
- data/spec/sendgrid/api/entities/response_remove_spec.rb +28 -0
- data/spec/sendgrid/api/entities/schedule_spec.rb +14 -0
- data/spec/sendgrid/api/entities/sender_address_spec.rb +21 -0
- data/spec/sendgrid/api/newsletter/categories_spec.rb +247 -0
- data/spec/sendgrid/api/newsletter/emails_spec.rb +265 -0
- data/spec/sendgrid/api/newsletter/lists_spec.rb +307 -0
- data/spec/sendgrid/api/newsletter/marketing_emails_spec.rb +306 -0
- data/spec/sendgrid/api/newsletter/recipients_spec.rb +252 -0
- data/spec/sendgrid/api/newsletter/schedule_spec.rb +263 -0
- data/spec/sendgrid/api/newsletter/sender_addresses_spec.rb +300 -0
- data/spec/sendgrid/api/rest/errors/error_spec.rb +40 -16
- data/spec/sendgrid/api/rest/resource_spec.rb +2 -0
- data/spec/sendgrid/api/web/mail_spec.rb +111 -0
- data/spec/sendgrid/api/web/profile_spec.rb +13 -29
- data/spec/sendgrid/api/web/stats_spec.rb +9 -15
- data/spec/support/helpers.rb +8 -0
- data/spec/support/mock.rb +6 -2
- data/spec/support/online.rb +114 -0
- data/spec/support/shared_examples.rb +93 -0
- metadata +96 -10
- data/spec/fixtures/unauthorized.json +0 -6
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'sendgrid/api/service'
|
2
|
+
require 'sendgrid/api/entities/sender_address'
|
3
|
+
require 'sendgrid/api/entities/response'
|
4
|
+
|
5
|
+
module Sendgrid
|
6
|
+
module API
|
7
|
+
module Newsletter
|
8
|
+
module SenderAddresses
|
9
|
+
|
10
|
+
def sender_addresses
|
11
|
+
Services.new(resource)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Services < Sendgrid::API::Service
|
15
|
+
# Create a new Sender Address.
|
16
|
+
#
|
17
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/sender_address.html#-add
|
18
|
+
# @param sender_address [Entities::SenderAddress] An Entities::SenderAddress object.
|
19
|
+
# @return [Entities::Response] An Entities::Response object.
|
20
|
+
def add(sender_address)
|
21
|
+
perform_request(Entities::Response, 'newsletter/identity/add.json', sender_address.as_json)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Edit an existing Sender Address.
|
25
|
+
#
|
26
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/sender_address.html#-edit
|
27
|
+
# @param sender_address [Entities::SenderAddress] An existing Entities::SenderAddress object.
|
28
|
+
# @param new_identity [String] A new identity for the existing sender address. Optional.
|
29
|
+
# @return [Entities::Response] An Entities::Response object.
|
30
|
+
def edit(sender_address, new_identity = nil)
|
31
|
+
params = sender_address.as_json
|
32
|
+
params[:newidentity] = new_identity if new_identity
|
33
|
+
perform_request(Entities::Response, 'newsletter/identity/edit.json', params)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Retrieve information associated with a particular Sender Address.
|
37
|
+
#
|
38
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/sender_address.html#-get
|
39
|
+
# @param sender_address [String, Entities::SenderAddress] An existing sender address identity or Entities::SenderAddress object.
|
40
|
+
# @return [Entities::SenderAddress] An Entities::SenderAddress object.
|
41
|
+
def get(sender_address)
|
42
|
+
params = { :identity => extract_identity(sender_address) }
|
43
|
+
perform_request(Entities::SenderAddress, 'newsletter/identity/get.json', params)
|
44
|
+
end
|
45
|
+
|
46
|
+
# List all Sender Addresses on your account.
|
47
|
+
#
|
48
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/sender_address.html#-list
|
49
|
+
# @return [Array<Entities::SenderAddress>] An array of Entities::SenderAddress objects.
|
50
|
+
def list
|
51
|
+
perform_request(Entities::SenderAddress, 'newsletter/identity/list.json')
|
52
|
+
end
|
53
|
+
|
54
|
+
# Remove a Sender Address from your account.
|
55
|
+
#
|
56
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/sender_address.html#-delete
|
57
|
+
# @param sender_address [String, Entities::SenderAddress] An existing sender address identity or Entities::SenderAddress object.
|
58
|
+
# @return [Entities::Response] An Entities::Response object.
|
59
|
+
def delete(sender_address)
|
60
|
+
params = { :identity => extract_identity(sender_address) }
|
61
|
+
perform_request(Entities::Response, 'newsletter/identity/delete.json', params)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def extract_identity(sender_address)
|
67
|
+
case sender_address
|
68
|
+
when ::String
|
69
|
+
sender_address
|
70
|
+
when Entities::SenderAddress
|
71
|
+
sender_address.identity
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'sendgrid/api/entities/list'
|
2
|
+
require 'sendgrid/api/entities/marketing_email'
|
3
|
+
|
4
|
+
module Sendgrid
|
5
|
+
module API
|
6
|
+
module Newsletter
|
7
|
+
module Utils
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
# Convert the object to an Entities::List.
|
12
|
+
def extract_list(object)
|
13
|
+
Entities::List.from_object(object)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Retrieve the list name.
|
17
|
+
def extract_listname(object)
|
18
|
+
extract_list(object).list
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieve the marketing email name
|
22
|
+
def extract_marketing_email(marketing_email)
|
23
|
+
case marketing_email
|
24
|
+
when ::String
|
25
|
+
marketing_email
|
26
|
+
when Entities::MarketingEmail
|
27
|
+
marketing_email.name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -17,7 +17,7 @@ module Sendgrid
|
|
17
17
|
body = env[:body]
|
18
18
|
status = env[:status].to_i
|
19
19
|
if status != 200
|
20
|
-
message = body[:error] if body.is_a?(Hash)
|
20
|
+
message = body[:error] || body[:errors].join(', ') if body.is_a?(Hash)
|
21
21
|
error_class(status).new(message)
|
22
22
|
else
|
23
23
|
nil
|
@@ -27,8 +27,12 @@ module Sendgrid
|
|
27
27
|
def body_error(env)
|
28
28
|
body = env[:body]
|
29
29
|
if body.is_a?(Hash) && body.has_key?(:error)
|
30
|
-
status = body[:error]
|
31
|
-
|
30
|
+
status, message = case body[:error]
|
31
|
+
when ::Hash
|
32
|
+
[ body[:error][:code], body[:error][:message] ]
|
33
|
+
when ::String
|
34
|
+
[ 422, body[:error] ]
|
35
|
+
end
|
32
36
|
error_class(status).new(message)
|
33
37
|
else
|
34
38
|
nil
|
@@ -47,11 +51,13 @@ module Sendgrid
|
|
47
51
|
class Unauthorized < Error; end
|
48
52
|
class Forbidden < Error; end
|
49
53
|
class Unknown < Error; end
|
54
|
+
class UnprocessableEntity < Error; end
|
50
55
|
|
51
56
|
CODES = {
|
52
57
|
400 => BadRequest,
|
53
58
|
401 => Unauthorized,
|
54
59
|
403 => Forbidden,
|
60
|
+
422 => UnprocessableEntity
|
55
61
|
}.freeze
|
56
62
|
|
57
63
|
end
|
@@ -23,7 +23,7 @@ module Sendgrid
|
|
23
23
|
private
|
24
24
|
|
25
25
|
def request(method, url, params = {})
|
26
|
-
params.merge
|
26
|
+
params = params.merge(authentication_params)
|
27
27
|
connection.send(method, url, params)
|
28
28
|
rescue Faraday::Error::ClientError, JSON::ParserError
|
29
29
|
raise Errors::Unknown
|
@@ -31,6 +31,8 @@ module Sendgrid
|
|
31
31
|
|
32
32
|
def middleware
|
33
33
|
@middleware ||= Faraday::Builder.new do |builder|
|
34
|
+
# checks for files in the payload, otherwise leaves everything untouched
|
35
|
+
builder.request :multipart
|
34
36
|
# form-encode POST params
|
35
37
|
builder.request :url_encoded
|
36
38
|
# Parse response errors
|
data/lib/sendgrid/api/version.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'sendgrid/api/service'
|
2
|
+
require 'sendgrid/api/entities/response'
|
3
|
+
|
4
|
+
module Sendgrid
|
5
|
+
module API
|
6
|
+
module Web
|
7
|
+
module Mail
|
8
|
+
|
9
|
+
def mail
|
10
|
+
Services.new(resource)
|
11
|
+
end
|
12
|
+
|
13
|
+
class Services < Sendgrid::API::Service
|
14
|
+
|
15
|
+
# Send email.
|
16
|
+
#
|
17
|
+
# @see http://sendgrid.com/docs/API_Reference/Web_API/mail.html#-send
|
18
|
+
# @param options [Hash] A customizable set of options.
|
19
|
+
# @option options [String] :to Must be a valid email address. This can also be passed in as an array, to send to multiple locations.
|
20
|
+
# @option options [String] :toname Give a name to the recipient. This can also be passed as an array if the to above is an array.
|
21
|
+
# @option options [String] :x_smtpapi Must be in valid JSON format. See http://sendgrid.com/docs/API_Reference/SMTP_API/index.html.
|
22
|
+
# @option options [String] :subject The subject of your email.
|
23
|
+
# @option options [String] :text The actual content of your email message. It can be sent as either plain text or HTML for the user to display.
|
24
|
+
# @option options [String] :html The actual content of your email message. It can be sent as either plain text or HTML for the user to display.
|
25
|
+
# @option options [String] :from This is where the email will appear to originate from for your recipient.
|
26
|
+
# @option options [String] :bcc This can also be passed in as an array of email addresses for multiple recipients.
|
27
|
+
# @option options [String] :fromname This is name appended to the from email field.
|
28
|
+
# @option options [String] :replyto Append a reply-to field to your email message.
|
29
|
+
# @option options [String] :date Specify the date header of your email.
|
30
|
+
# @option options [String] :files Files to be attached.
|
31
|
+
# @option options [String] :content Content IDs of the files to be used as inline images.
|
32
|
+
# @option options [String] :headers A collection of key/value pairs in JSON format.
|
33
|
+
# @return [Entities::Response] An Entities::Response object.
|
34
|
+
def send(options = {})
|
35
|
+
options['x-smtpapi'] = options.delete(:x_smtpapi) if options.member?(:x_smtpapi)
|
36
|
+
perform_request(Entities::Response, 'mail.send.json', options)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -16,7 +16,7 @@ module Sendgrid
|
|
16
16
|
# View your SendGrid profile
|
17
17
|
#
|
18
18
|
# @see http://sendgrid.com/docs/API_Reference/Web_API/profile.html
|
19
|
-
# @return
|
19
|
+
# @return [Entities::Profile] An Entities::Profile object.
|
20
20
|
def get
|
21
21
|
perform_request(Entities::Profile, 'profile.get.json').first
|
22
22
|
end
|
@@ -24,8 +24,8 @@ module Sendgrid
|
|
24
24
|
# Update your SendGrid profile
|
25
25
|
#
|
26
26
|
# @see http://sendgrid.com/docs/API_Reference/Web_API/profile.html#-set
|
27
|
-
# @param profile [Profile] An Entities::Profile object.
|
28
|
-
# @return
|
27
|
+
# @param profile [Entities::Profile] An Entities::Profile object.
|
28
|
+
# @return [Entities::Response] An Entities::Response object.
|
29
29
|
def set(profile)
|
30
30
|
perform_request(Entities::Response, 'profile.set.json', profile.as_json)
|
31
31
|
end
|
@@ -23,9 +23,9 @@ module Sendgrid
|
|
23
23
|
# @option options [String] :category Return stats for the given category.
|
24
24
|
# @option options [String] :aggregated_by Aggregate the data by the given period (default is day): day, week or month.
|
25
25
|
# @option options [String] :country Get stats for each region/state for the given country. Only US (United States) and CA (Canada) is supported at this time.
|
26
|
-
# @return
|
27
|
-
def advanced(
|
28
|
-
perform_request(Entities::Stats, 'stats.getAdvanced.json',
|
26
|
+
# @return [Array<Entities::Stats>] An array of Entities::Stats object.
|
27
|
+
def advanced(options = {})
|
28
|
+
perform_request(Entities::Stats, 'stats.getAdvanced.json', options)
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"content_preview": 1,
|
3
|
+
"subject": "SendGrid Email Marketing Service Tutorial",
|
4
|
+
"html": "<html><body>SendGrid Email Marketing Service Tutorial: Managing Your Marketing Email Unsubscribe List</body></html>",
|
5
|
+
"text": "SendGrid Email Marketing Service Tutorial: Managing Your Marketing Email Unsubscribe List",
|
6
|
+
"winner_sending_time": null,
|
7
|
+
"type": "html",
|
8
|
+
"date_schedule": null,
|
9
|
+
"is_winner": 0,
|
10
|
+
"nl_type": 0,
|
11
|
+
"identity": "comercial",
|
12
|
+
"can_edit": true,
|
13
|
+
"is_deleted": 0,
|
14
|
+
"name": "sendgrid tutorial",
|
15
|
+
"timezone_id": null,
|
16
|
+
"newsletter_id": 123456,
|
17
|
+
"total_recipients": 4459,
|
18
|
+
"is_split": 0
|
19
|
+
}
|
@@ -13,9 +13,25 @@ module Sendgrid
|
|
13
13
|
|
14
14
|
it { should respond_to(:profile) }
|
15
15
|
it { should respond_to(:stats) }
|
16
|
+
it { should respond_to(:mail) }
|
17
|
+
it { should respond_to(:lists) }
|
18
|
+
it { should respond_to(:emails) }
|
19
|
+
it { should respond_to(:sender_addresses) }
|
20
|
+
it { should respond_to(:categories) }
|
21
|
+
it { should respond_to(:marketing_emails) }
|
22
|
+
it { should respond_to(:recipients) }
|
23
|
+
it { should respond_to(:schedule) }
|
16
24
|
|
17
25
|
its(:profile) { should_not be_nil }
|
18
26
|
its(:stats) { should_not be_nil }
|
27
|
+
its(:mail) { should_not be_nil }
|
28
|
+
its(:lists) { should_not be_nil }
|
29
|
+
its(:emails) { should_not be_nil }
|
30
|
+
its(:sender_addresses) { should_not be_nil }
|
31
|
+
its(:categories) { should_not be_nil }
|
32
|
+
its(:marketing_emails) { should_not be_nil }
|
33
|
+
its(:recipients) { should_not be_nil }
|
34
|
+
its(:schedule) { should_not be_nil }
|
19
35
|
|
20
36
|
end
|
21
37
|
end
|