parity-sendgrid-api 0.0.4
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.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.rspec +3 -0
- data/.travis.yml +3 -0
- data/.yardopts +6 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +67 -0
- data/LICENSE +22 -0
- data/README.md +261 -0
- data/Rakefile +12 -0
- data/lib/sendgrid/api.rb +7 -0
- data/lib/sendgrid/api/client.rb +43 -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 +83 -0
- data/lib/sendgrid/api/entities/list.rb +30 -0
- data/lib/sendgrid/api/entities/marketing_email.rb +20 -0
- data/lib/sendgrid/api/entities/profile.rb +14 -0
- data/lib/sendgrid/api/entities/response.rb +21 -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/entities/stats.rb +14 -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 +66 -0
- data/lib/sendgrid/api/rest/resource.rb +58 -0
- data/lib/sendgrid/api/rest/response/parse_error.rb +19 -0
- data/lib/sendgrid/api/rest/response/parse_json.rb +24 -0
- data/lib/sendgrid/api/service.rb +23 -0
- data/lib/sendgrid/api/version.rb +5 -0
- data/lib/sendgrid/api/web/mail.rb +44 -0
- data/lib/sendgrid/api/web/profile.rb +38 -0
- data/lib/sendgrid/api/web/stats.rb +36 -0
- data/sendgrid-api.gemspec +23 -0
- 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/errors/forbidden.json +3 -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/profile.json +18 -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/fixtures/stats.json +50 -0
- data/spec/fixtures/success.json +3 -0
- data/spec/sendgrid/api/client_spec.rb +38 -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/entity_spec.rb +279 -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/profile_spec.rb +26 -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/response_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/entities/stats_spec.rb +25 -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 +121 -0
- data/spec/sendgrid/api/rest/resource_spec.rb +145 -0
- data/spec/sendgrid/api/rest/response/parse_error_spec.rb +39 -0
- data/spec/sendgrid/api/rest/response/parse_json_spec.rb +45 -0
- data/spec/sendgrid/api/service_spec.rb +44 -0
- data/spec/sendgrid/api/version_spec.rb +11 -0
- data/spec/sendgrid/api/web/mail_spec.rb +111 -0
- data/spec/sendgrid/api/web/profile_spec.rb +110 -0
- data/spec/sendgrid/api/web/stats_spec.rb +94 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/helpers.rb +23 -0
- data/spec/support/mock.rb +30 -0
- data/spec/support/online.rb +114 -0
- data/spec/support/shared_examples.rb +104 -0
- metadata +225 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class Entity
|
7
|
+
|
8
|
+
attr_reader :attributes
|
9
|
+
|
10
|
+
def initialize(attributes = {})
|
11
|
+
@attributes = sanitize_attributes(attributes)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_json(*a)
|
15
|
+
as_json.to_json(*a)
|
16
|
+
end
|
17
|
+
|
18
|
+
def as_json
|
19
|
+
attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(method, *args, &block)
|
23
|
+
setter = method.to_s.gsub(/=$/, '').to_sym
|
24
|
+
if has_attribute?(method)
|
25
|
+
attributes[method]
|
26
|
+
elsif has_attribute?(setter)
|
27
|
+
attributes[setter] = args.first
|
28
|
+
else
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def respond_to?(method, include_private = false)
|
34
|
+
super || has_attribute?(method)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def has_attribute?(attribute)
|
40
|
+
self.class.attributes.include?(attribute)
|
41
|
+
end
|
42
|
+
|
43
|
+
def sanitize_attributes(attributes)
|
44
|
+
attributes.reject { |key, value| !has_attribute?(key) }
|
45
|
+
end
|
46
|
+
|
47
|
+
class << self
|
48
|
+
|
49
|
+
# Instantiate the entity from API response body.
|
50
|
+
# Can generate multiple entities if response is an Array.
|
51
|
+
def from_response(response)
|
52
|
+
body = response.body
|
53
|
+
if body.is_a?(Array)
|
54
|
+
body.map { |item| new(item) }
|
55
|
+
elsif body.is_a?(Hash)
|
56
|
+
new(body)
|
57
|
+
else
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Add attributes to the entity
|
63
|
+
def attribute(*args)
|
64
|
+
@attributes = attributes
|
65
|
+
@attributes += args
|
66
|
+
@attributes.uniq!
|
67
|
+
end
|
68
|
+
|
69
|
+
# Get the entity attributes
|
70
|
+
def attributes
|
71
|
+
@attributes ||= []
|
72
|
+
end
|
73
|
+
|
74
|
+
def clear_attributes
|
75
|
+
@attributes = []
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class List < Entity
|
7
|
+
|
8
|
+
attribute :list
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
# Convert the object to an Entities::List.
|
13
|
+
#
|
14
|
+
# @param object [String, Entities::List] A list name or Entities::List object
|
15
|
+
# @return [Entities::List] An Entities::List object
|
16
|
+
def from_object(object)
|
17
|
+
case object
|
18
|
+
when ::String
|
19
|
+
new(:list => object)
|
20
|
+
when self
|
21
|
+
object
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class MarketingEmail < Entity
|
7
|
+
|
8
|
+
# required attributes to create
|
9
|
+
attribute :identity, :name, :subject, :text, :html
|
10
|
+
|
11
|
+
# other attributes
|
12
|
+
attribute :can_edit, :content_preview, :date_schedule,
|
13
|
+
:is_deleted, :is_split, :is_winner, :newsletter_id,
|
14
|
+
:nl_type, :timezone_id, :total_recipients,
|
15
|
+
:type, :winner_sending_time
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class Profile < Entity
|
7
|
+
|
8
|
+
attribute :username, :email, :active, :first_name, :last_name, :address,
|
9
|
+
:address2, :city, :state, :zip, :country, :phone, :website, :website_access
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class Response < Entity
|
7
|
+
|
8
|
+
attribute :message, :errors
|
9
|
+
|
10
|
+
def success?
|
11
|
+
message == 'success'
|
12
|
+
end
|
13
|
+
|
14
|
+
def error?
|
15
|
+
message == 'error'
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class ResponseInsert < Entity
|
7
|
+
|
8
|
+
attribute :inserted
|
9
|
+
|
10
|
+
# Return true if one or more inserts were made
|
11
|
+
def any?
|
12
|
+
inserted > 0
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return true if no inserts were made
|
16
|
+
def none?
|
17
|
+
!any?
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class ResponseRemove < Entity
|
7
|
+
|
8
|
+
attribute :removed
|
9
|
+
|
10
|
+
# Return true if one or more removals were made
|
11
|
+
def any?
|
12
|
+
removed > 0
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return true if no removals were made
|
16
|
+
def none?
|
17
|
+
!any?
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class Stats < Entity
|
7
|
+
|
8
|
+
attribute :delivered, :request, :unique_open, :unique_click, :processed, :date, :open, :click,
|
9
|
+
:blocked, :spamreport, :drop, :bounce, :deferred
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'sendgrid/api/service'
|
2
|
+
require 'sendgrid/api/entities/category'
|
3
|
+
require 'sendgrid/api/entities/response'
|
4
|
+
require 'sendgrid/api/newsletter/utils'
|
5
|
+
|
6
|
+
module Sendgrid
|
7
|
+
module API
|
8
|
+
module Newsletter
|
9
|
+
module Categories
|
10
|
+
|
11
|
+
def categories
|
12
|
+
Services.new(resource)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Services < Sendgrid::API::Service
|
16
|
+
include Newsletter::Utils
|
17
|
+
|
18
|
+
# Create a new Category.
|
19
|
+
#
|
20
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/categories.html#-create
|
21
|
+
# @param category [String, Entities::Category] A category name or Entities::Category object.
|
22
|
+
# @return [Entities::Response] An Entities::Response object.
|
23
|
+
def create(category)
|
24
|
+
params = { :category => extract_category(category) }
|
25
|
+
perform_request(Entities::Response, 'newsletter/category/create.json', params)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Assign a Category to an existing Marketing Email.
|
29
|
+
#
|
30
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/categories.html#-add
|
31
|
+
# @param marketing_email [String, Entities::MarketingEmail] An existing marketing email name or Entities::MarketingEmail object.
|
32
|
+
# @param category [String, Entities::Category] A category name or Entities::Category object.
|
33
|
+
# @return [Entities::Response] An Entities::Response object.
|
34
|
+
def add(marketing_email, category)
|
35
|
+
params = { :name => extract_marketing_email(marketing_email), :category => extract_category(category) }
|
36
|
+
perform_request(Entities::Response, 'newsletter/category/add.json', params)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Remove specific categories, or all categories from a Marketing Email.
|
40
|
+
#
|
41
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/categories.html#-remove
|
42
|
+
# @param marketing_email [String, Entities::MarketingEmail] An existing marketing email name or Entities::MarketingEmail object.
|
43
|
+
# @param category [String, Entities::Category] A category name or Entities::Category object.
|
44
|
+
# @return [Entities::Response] An Entities::Response object.
|
45
|
+
def remove(marketing_email, category)
|
46
|
+
params = { :name => extract_marketing_email(marketing_email), :category => extract_category(category) }
|
47
|
+
perform_request(Entities::Response, 'newsletter/category/remove.json', params)
|
48
|
+
end
|
49
|
+
|
50
|
+
# List all categories.
|
51
|
+
#
|
52
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/categories.html#-list
|
53
|
+
# @return [Array<Entities::Category>] An array of Entities::Category objects.
|
54
|
+
def list
|
55
|
+
perform_request(Entities::Category, 'newsletter/category/list.json')
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def extract_category(category)
|
61
|
+
case category
|
62
|
+
when ::String
|
63
|
+
category
|
64
|
+
when Entities::Category
|
65
|
+
category.category
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'sendgrid/api/service'
|
2
|
+
require 'sendgrid/api/entities/email'
|
3
|
+
require 'sendgrid/api/entities/response_insert'
|
4
|
+
require 'sendgrid/api/entities/response_remove'
|
5
|
+
require 'sendgrid/api/newsletter/utils'
|
6
|
+
|
7
|
+
module Sendgrid
|
8
|
+
module API
|
9
|
+
module Newsletter
|
10
|
+
module Emails
|
11
|
+
|
12
|
+
def emails
|
13
|
+
Services.new(resource)
|
14
|
+
end
|
15
|
+
|
16
|
+
class Services < Sendgrid::API::Service
|
17
|
+
include Newsletter::Utils
|
18
|
+
|
19
|
+
# Add one or more emails to a Recipient List.
|
20
|
+
#
|
21
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html#-add
|
22
|
+
# @param list [String, Entities::List] A list name or Entities::List object.
|
23
|
+
# @param emails [Array<Entities::Email>] A list of emails to be added. Limited to a 1000 entries maximum.
|
24
|
+
# @return [Entities::ResponseInsert] An Entities::ResponseInsert object.
|
25
|
+
def add(list, emails)
|
26
|
+
params = { :list => extract_listname(list), :data => map_emails(emails, :to_json) }
|
27
|
+
perform_request(Entities::ResponseInsert, 'newsletter/lists/email/add.json', params)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get the email addresses and associated fields for a Recipient List.
|
31
|
+
#
|
32
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html#-get
|
33
|
+
# @param list [String, Entities::List] A list name or Entities::List object.
|
34
|
+
# @param emails [Entities::Email, Array<Entities::Email>] An email or list of emails to be searched and retrieved. Optional.
|
35
|
+
# @return [Array<Entities::Email>] An array of Entities::Email object.
|
36
|
+
def get(list, emails = nil)
|
37
|
+
params = { :list => extract_listname(list) }
|
38
|
+
params[:email] = map_emails(emails, :email) if emails
|
39
|
+
perform_request(Entities::Email, 'newsletter/lists/email/get.json', params)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Remove one or more emails from a Recipient List.
|
43
|
+
#
|
44
|
+
# @see http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html#-delete
|
45
|
+
# @param list [String, Entities::List] A list name or Entities::List object.
|
46
|
+
# @param emails [Entities::Email, Array<Entities::Email>] An email or list of emails to be removed.
|
47
|
+
# @return [Entities::ResponseRemove] An Entities::ResponseRemove object.
|
48
|
+
def delete(list, emails)
|
49
|
+
params = { :list => extract_listname(list), :email => map_emails(emails, :email) }
|
50
|
+
perform_request(Entities::ResponseRemove, 'newsletter/lists/email/delete.json', params)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
# Return a mapping from an email method name.
|
56
|
+
#
|
57
|
+
# @param emails [Entities::Email, Array<Entities::Email>] An email or list of emails.
|
58
|
+
# @param to [Symbol] The email method name to be mapped.
|
59
|
+
# @return [Array] An array of the mapping.
|
60
|
+
def map_emails(emails, to)
|
61
|
+
emails = [emails] unless emails.is_a?(Array)
|
62
|
+
emails.map(&to)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|