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.
Files changed (76) hide show
  1. data/.gitignore +4 -2
  2. data/.yardopts +6 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +5 -1
  5. data/README.md +200 -12
  6. data/Rakefile +3 -0
  7. data/lib/sendgrid/api/client.rb +16 -0
  8. data/lib/sendgrid/api/entities/category.rb +13 -0
  9. data/lib/sendgrid/api/entities/email.rb +13 -0
  10. data/lib/sendgrid/api/entities/entity.rb +2 -2
  11. data/lib/sendgrid/api/entities/list.rb +30 -0
  12. data/lib/sendgrid/api/entities/marketing_email.rb +20 -0
  13. data/lib/sendgrid/api/entities/response_insert.rb +23 -0
  14. data/lib/sendgrid/api/entities/response_remove.rb +23 -0
  15. data/lib/sendgrid/api/entities/schedule.rb +13 -0
  16. data/lib/sendgrid/api/entities/sender_address.rb +13 -0
  17. data/lib/sendgrid/api/newsletter/categories.rb +74 -0
  18. data/lib/sendgrid/api/newsletter/emails.rb +69 -0
  19. data/lib/sendgrid/api/newsletter/lists.rb +64 -0
  20. data/lib/sendgrid/api/newsletter/marketing_emails.rb +72 -0
  21. data/lib/sendgrid/api/newsletter/recipients.rb +55 -0
  22. data/lib/sendgrid/api/newsletter/schedule.rb +70 -0
  23. data/lib/sendgrid/api/newsletter/sender_addresses.rb +80 -0
  24. data/lib/sendgrid/api/newsletter/utils.rb +34 -0
  25. data/lib/sendgrid/api/rest/errors/error.rb +9 -3
  26. data/lib/sendgrid/api/rest/resource.rb +3 -1
  27. data/lib/sendgrid/api/version.rb +1 -1
  28. data/lib/sendgrid/api/web/mail.rb +44 -0
  29. data/lib/sendgrid/api/web/profile.rb +3 -3
  30. data/lib/sendgrid/api/web/stats.rb +3 -3
  31. data/spec/fixtures/categories.json +11 -0
  32. data/spec/fixtures/emails/email.json +6 -0
  33. data/spec/fixtures/emails/emails.json +10 -0
  34. data/spec/fixtures/errors/already_exists.json +3 -0
  35. data/spec/fixtures/errors/bad_request.json +6 -0
  36. data/spec/fixtures/errors/database_error.json +3 -0
  37. data/spec/fixtures/errors/does_not_exist.json +3 -0
  38. data/spec/fixtures/{forbidden.json → errors/forbidden.json} +0 -0
  39. data/spec/fixtures/errors/invalid_fields.json +3 -0
  40. data/spec/fixtures/errors/not_scheduled.json +3 -0
  41. data/spec/fixtures/errors/unauthorized.json +6 -0
  42. data/spec/fixtures/lists/list.json +5 -0
  43. data/spec/fixtures/lists/lists.json +11 -0
  44. data/spec/fixtures/marketing_emails/marketing_email.json +19 -0
  45. data/spec/fixtures/marketing_emails/marketing_emails.json +10 -0
  46. data/spec/fixtures/recipients.json +8 -0
  47. data/spec/fixtures/schedule.json +3 -0
  48. data/spec/fixtures/sender_addresses/sender_address.json +11 -0
  49. data/spec/fixtures/sender_addresses/sender_addresses.json +11 -0
  50. data/spec/sendgrid/api/client_spec.rb +16 -0
  51. data/spec/sendgrid/api/entities/category_spec.rb +14 -0
  52. data/spec/sendgrid/api/entities/email_spec.rb +15 -0
  53. data/spec/sendgrid/api/entities/list_spec.rb +34 -0
  54. data/spec/sendgrid/api/entities/marketing_email_spec.rb +31 -0
  55. data/spec/sendgrid/api/entities/response_insert_spec.rb +28 -0
  56. data/spec/sendgrid/api/entities/response_remove_spec.rb +28 -0
  57. data/spec/sendgrid/api/entities/schedule_spec.rb +14 -0
  58. data/spec/sendgrid/api/entities/sender_address_spec.rb +21 -0
  59. data/spec/sendgrid/api/newsletter/categories_spec.rb +247 -0
  60. data/spec/sendgrid/api/newsletter/emails_spec.rb +265 -0
  61. data/spec/sendgrid/api/newsletter/lists_spec.rb +307 -0
  62. data/spec/sendgrid/api/newsletter/marketing_emails_spec.rb +306 -0
  63. data/spec/sendgrid/api/newsletter/recipients_spec.rb +252 -0
  64. data/spec/sendgrid/api/newsletter/schedule_spec.rb +263 -0
  65. data/spec/sendgrid/api/newsletter/sender_addresses_spec.rb +300 -0
  66. data/spec/sendgrid/api/rest/errors/error_spec.rb +40 -16
  67. data/spec/sendgrid/api/rest/resource_spec.rb +2 -0
  68. data/spec/sendgrid/api/web/mail_spec.rb +111 -0
  69. data/spec/sendgrid/api/web/profile_spec.rb +13 -29
  70. data/spec/sendgrid/api/web/stats_spec.rb +9 -15
  71. data/spec/support/helpers.rb +8 -0
  72. data/spec/support/mock.rb +6 -2
  73. data/spec/support/online.rb +114 -0
  74. data/spec/support/shared_examples.rb +93 -0
  75. metadata +96 -10
  76. 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][:code]
31
- message = body[:error][:message]
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!(authentication_params)
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
@@ -1,5 +1,5 @@
1
1
  module Sendgrid
2
2
  module API
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -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 profile [Profile] An Entities::Profile object.
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 response [Response] An Entities::Response object.
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 stats [Stats] An array of Entities::Stats object.
27
- def advanced(params = {})
28
- perform_request(Entities::Stats, 'stats.getAdvanced.json', params)
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
@@ -0,0 +1,11 @@
1
+ [
2
+ {
3
+ "category": "sendgrid"
4
+ },
5
+ {
6
+ "category": "api"
7
+ },
8
+ {
9
+ "category": "my category"
10
+ }
11
+ ]
@@ -0,0 +1,6 @@
1
+ [
2
+ {
3
+ "email": "john@example.com",
4
+ "name": "John"
5
+ }
6
+ ]
@@ -0,0 +1,10 @@
1
+ [
2
+ {
3
+ "email": "john@example.com",
4
+ "name": "John"
5
+ },
6
+ {
7
+ "email": "brian@example.com",
8
+ "name": "Brian"
9
+ }
10
+ ]
@@ -0,0 +1,3 @@
1
+ {
2
+ "error": "object already exists"
3
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "message": "error",
3
+ "errors": [
4
+ "Bad username / password"
5
+ ]
6
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "error": "A database error has occured. Please try again"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "error": "object does not exist"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "error": "error in email: email is required"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "error": "'The newsletter is not scheduled'"
3
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "error": {
3
+ "code": 401,
4
+ "message": "Permission denied, wrong credentials"
5
+ }
6
+ }
@@ -0,0 +1,5 @@
1
+ [
2
+ {
3
+ "list": "list 1"
4
+ }
5
+ ]
@@ -0,0 +1,11 @@
1
+ [
2
+ {
3
+ "list": "list 1"
4
+ },
5
+ {
6
+ "list": "list 2"
7
+ },
8
+ {
9
+ "list": "list 3"
10
+ }
11
+ ]
@@ -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
+ }
@@ -0,0 +1,10 @@
1
+ [
2
+ {
3
+ "name": "Sendgrid Tutorial",
4
+ "newsletter_id": 123456
5
+ },
6
+ {
7
+ "name": "Email Deliverability Spooks and Scares",
8
+ "newsletter_id": 123457
9
+ }
10
+ ]
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "list": "list 1"
4
+ },
5
+ {
6
+ "list": "list 2"
7
+ }
8
+ ]
@@ -0,0 +1,3 @@
1
+ {
2
+ "date": "2013-10-30 18:01:40"
3
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "city": "Anaheim",
3
+ "name": "SendGrid",
4
+ "zip": "92806",
5
+ "replyto": "contact@sendgrid.com",
6
+ "country": "US",
7
+ "state": "CA",
8
+ "address": "1065 N Pacificenter Drive, Suite 425",
9
+ "email": "contact@sendgrid.com",
10
+ "identity": "sendgrid"
11
+ }
@@ -0,0 +1,11 @@
1
+ [
2
+ {
3
+ "identity": "sendgrid"
4
+ },
5
+ {
6
+ "identity": "sendgrid helpdesk"
7
+ },
8
+ {
9
+ "identity": "sendgrid developers"
10
+ }
11
+ ]
@@ -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