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,58 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'sendgrid/api/rest/response/parse_json'
|
3
|
+
require 'sendgrid/api/rest/response/parse_error'
|
4
|
+
|
5
|
+
module Sendgrid
|
6
|
+
module API
|
7
|
+
module REST
|
8
|
+
class Resource
|
9
|
+
|
10
|
+
attr_reader :user, :key
|
11
|
+
|
12
|
+
ENDPOINT = 'https://sendgrid.com/api'.freeze
|
13
|
+
|
14
|
+
def initialize(user, key)
|
15
|
+
@user = user
|
16
|
+
@key = key
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(url, params = {})
|
20
|
+
request(:post, url, params)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def request(method, url, params = {})
|
26
|
+
params = params.merge(authentication_params)
|
27
|
+
connection.send(method, url, params)
|
28
|
+
rescue Faraday::Error::ClientError, JSON::ParserError
|
29
|
+
raise Errors::Unknown
|
30
|
+
end
|
31
|
+
|
32
|
+
def middleware
|
33
|
+
@middleware ||= Faraday::RackBuilder.new do |builder|
|
34
|
+
# checks for files in the payload, otherwise leaves everything untouched
|
35
|
+
builder.request :multipart
|
36
|
+
# form-encode POST params
|
37
|
+
builder.request :url_encoded
|
38
|
+
# Parse response errors
|
39
|
+
builder.use Response::ParseError
|
40
|
+
# Parse JSON response bodies
|
41
|
+
builder.use Response::ParseJson
|
42
|
+
# Set Faraday's HTTP adapter
|
43
|
+
builder.adapter Faraday.default_adapter
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def connection
|
48
|
+
@connection ||= Faraday.new(ENDPOINT, :builder => middleware)
|
49
|
+
end
|
50
|
+
|
51
|
+
def authentication_params
|
52
|
+
{ :api_user => @user, :api_key => @key }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'sendgrid/api/rest/errors/error'
|
3
|
+
|
4
|
+
module Sendgrid
|
5
|
+
module API
|
6
|
+
module REST
|
7
|
+
module Response
|
8
|
+
class ParseError < Faraday::Response::Middleware
|
9
|
+
|
10
|
+
def on_complete(env)
|
11
|
+
error = REST::Errors::Error.from_response(env)
|
12
|
+
raise error unless error.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Sendgrid
|
5
|
+
module API
|
6
|
+
module REST
|
7
|
+
module Response
|
8
|
+
class ParseJson < Faraday::Response::Middleware
|
9
|
+
|
10
|
+
def parse(body)
|
11
|
+
JSON.parse(body, :symbolize_names => true) unless blank?(body)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def blank?(string)
|
17
|
+
string.respond_to?(:empty?) ? !!string.empty? : !string
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Sendgrid
|
2
|
+
module API
|
3
|
+
class Service
|
4
|
+
|
5
|
+
attr_reader :resource
|
6
|
+
|
7
|
+
def initialize(resource)
|
8
|
+
@resource = resource
|
9
|
+
end
|
10
|
+
|
11
|
+
def perform_request(entity, url, params = {})
|
12
|
+
entity.from_response(request(url, params))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def request(url, params = {})
|
18
|
+
resource.post(url, params)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
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
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'sendgrid/api/service'
|
2
|
+
require 'sendgrid/api/entities/profile'
|
3
|
+
require 'sendgrid/api/entities/response'
|
4
|
+
|
5
|
+
module Sendgrid
|
6
|
+
module API
|
7
|
+
module Web
|
8
|
+
module Profile
|
9
|
+
|
10
|
+
def profile
|
11
|
+
Services.new(resource)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Services < Sendgrid::API::Service
|
15
|
+
|
16
|
+
# View your SendGrid profile
|
17
|
+
#
|
18
|
+
# @see http://sendgrid.com/docs/API_Reference/Web_API/profile.html
|
19
|
+
# @return [Entities::Profile] An Entities::Profile object.
|
20
|
+
def get
|
21
|
+
perform_request(Entities::Profile, 'profile.get.json').first
|
22
|
+
end
|
23
|
+
|
24
|
+
# Update your SendGrid profile
|
25
|
+
#
|
26
|
+
# @see http://sendgrid.com/docs/API_Reference/Web_API/profile.html#-set
|
27
|
+
# @param profile [Entities::Profile] An Entities::Profile object.
|
28
|
+
# @return [Entities::Response] An Entities::Response object.
|
29
|
+
def set(profile)
|
30
|
+
perform_request(Entities::Response, 'profile.set.json', profile.as_json)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'sendgrid/api/service'
|
2
|
+
require 'sendgrid/api/entities/stats'
|
3
|
+
|
4
|
+
module Sendgrid
|
5
|
+
module API
|
6
|
+
module Web
|
7
|
+
module Stats
|
8
|
+
|
9
|
+
def stats
|
10
|
+
Services.new(resource)
|
11
|
+
end
|
12
|
+
|
13
|
+
class Services < Sendgrid::API::Service
|
14
|
+
|
15
|
+
# Get Advanced Statistics
|
16
|
+
#
|
17
|
+
# @see http://sendgrid.com/docs/API_Reference/Web_API/Statistics/statistics_advanced.html
|
18
|
+
# @param options [Hash] A customizable set of options.
|
19
|
+
# @option options [String] :data_type One of the following: browsers, clients, devices, geo, global, isps. Required.
|
20
|
+
# @option options [Date] :start_date Date format is based on aggregated_by value (default is yyyy-mm-dd). Required.
|
21
|
+
# @option options [Date] :end_date Date format is based on aggregated_by value (default is yyyy-mm-dd).
|
22
|
+
# @option options [String] :metric One of the following (default is all): open, click, unique_open, unique_click, processed, delivered, drop, bounce, deferred, spamreport, blocked, all.
|
23
|
+
# @option options [String] :category Return stats for the given category.
|
24
|
+
# @option options [String] :aggregated_by Aggregate the data by the given period (default is day): day, week or month.
|
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 [Array<Entities::Stats>] An array of Entities::Stats object.
|
27
|
+
def advanced(options = {})
|
28
|
+
perform_request(Entities::Stats, 'stats.getAdvanced.json', options)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sendgrid/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "parity-sendgrid-api"
|
8
|
+
spec.version = Sendgrid::API::VERSION
|
9
|
+
spec.authors = ["Jimish Jobanputra, Hardik Gondaliya"]
|
10
|
+
spec.email = ["jimish@desidime.com"]
|
11
|
+
spec.description = %q{A Ruby interface to the SendGrid API}
|
12
|
+
spec.summary = %q{A Ruby interface to the SendGrid API}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'faraday', '~> 0.9.0'
|
22
|
+
spec.add_dependency 'json', '~> 1.8.1'
|
23
|
+
end
|
@@ -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,18 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"username":"sendgrid",
|
4
|
+
"email":"contact@sendgrid.com",
|
5
|
+
"active":"true",
|
6
|
+
"first_name":"Jim",
|
7
|
+
"last_name":"Franklin",
|
8
|
+
"address":"1065 N Pacificenter Drive, Suite 425",
|
9
|
+
"address2":"",
|
10
|
+
"city":"Anaheim",
|
11
|
+
"state":"CA",
|
12
|
+
"zip":"92806",
|
13
|
+
"country":"US",
|
14
|
+
"phone":"123456789",
|
15
|
+
"website":"http:\/\/www.sendgrid.com",
|
16
|
+
"website_access":"true"
|
17
|
+
}
|
18
|
+
]
|