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.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +3 -0
  5. data/.yardopts +6 -0
  6. data/Gemfile +17 -0
  7. data/Gemfile.lock +67 -0
  8. data/LICENSE +22 -0
  9. data/README.md +261 -0
  10. data/Rakefile +12 -0
  11. data/lib/sendgrid/api.rb +7 -0
  12. data/lib/sendgrid/api/client.rb +43 -0
  13. data/lib/sendgrid/api/entities/category.rb +13 -0
  14. data/lib/sendgrid/api/entities/email.rb +13 -0
  15. data/lib/sendgrid/api/entities/entity.rb +83 -0
  16. data/lib/sendgrid/api/entities/list.rb +30 -0
  17. data/lib/sendgrid/api/entities/marketing_email.rb +20 -0
  18. data/lib/sendgrid/api/entities/profile.rb +14 -0
  19. data/lib/sendgrid/api/entities/response.rb +21 -0
  20. data/lib/sendgrid/api/entities/response_insert.rb +23 -0
  21. data/lib/sendgrid/api/entities/response_remove.rb +23 -0
  22. data/lib/sendgrid/api/entities/schedule.rb +13 -0
  23. data/lib/sendgrid/api/entities/sender_address.rb +13 -0
  24. data/lib/sendgrid/api/entities/stats.rb +14 -0
  25. data/lib/sendgrid/api/newsletter/categories.rb +74 -0
  26. data/lib/sendgrid/api/newsletter/emails.rb +69 -0
  27. data/lib/sendgrid/api/newsletter/lists.rb +64 -0
  28. data/lib/sendgrid/api/newsletter/marketing_emails.rb +72 -0
  29. data/lib/sendgrid/api/newsletter/recipients.rb +55 -0
  30. data/lib/sendgrid/api/newsletter/schedule.rb +70 -0
  31. data/lib/sendgrid/api/newsletter/sender_addresses.rb +80 -0
  32. data/lib/sendgrid/api/newsletter/utils.rb +34 -0
  33. data/lib/sendgrid/api/rest/errors/error.rb +66 -0
  34. data/lib/sendgrid/api/rest/resource.rb +58 -0
  35. data/lib/sendgrid/api/rest/response/parse_error.rb +19 -0
  36. data/lib/sendgrid/api/rest/response/parse_json.rb +24 -0
  37. data/lib/sendgrid/api/service.rb +23 -0
  38. data/lib/sendgrid/api/version.rb +5 -0
  39. data/lib/sendgrid/api/web/mail.rb +44 -0
  40. data/lib/sendgrid/api/web/profile.rb +38 -0
  41. data/lib/sendgrid/api/web/stats.rb +36 -0
  42. data/sendgrid-api.gemspec +23 -0
  43. data/spec/fixtures/categories.json +11 -0
  44. data/spec/fixtures/emails/email.json +6 -0
  45. data/spec/fixtures/emails/emails.json +10 -0
  46. data/spec/fixtures/errors/already_exists.json +3 -0
  47. data/spec/fixtures/errors/bad_request.json +6 -0
  48. data/spec/fixtures/errors/database_error.json +3 -0
  49. data/spec/fixtures/errors/does_not_exist.json +3 -0
  50. data/spec/fixtures/errors/forbidden.json +3 -0
  51. data/spec/fixtures/errors/invalid_fields.json +3 -0
  52. data/spec/fixtures/errors/not_scheduled.json +3 -0
  53. data/spec/fixtures/errors/unauthorized.json +6 -0
  54. data/spec/fixtures/lists/list.json +5 -0
  55. data/spec/fixtures/lists/lists.json +11 -0
  56. data/spec/fixtures/marketing_emails/marketing_email.json +19 -0
  57. data/spec/fixtures/marketing_emails/marketing_emails.json +10 -0
  58. data/spec/fixtures/profile.json +18 -0
  59. data/spec/fixtures/recipients.json +8 -0
  60. data/spec/fixtures/schedule.json +3 -0
  61. data/spec/fixtures/sender_addresses/sender_address.json +11 -0
  62. data/spec/fixtures/sender_addresses/sender_addresses.json +11 -0
  63. data/spec/fixtures/stats.json +50 -0
  64. data/spec/fixtures/success.json +3 -0
  65. data/spec/sendgrid/api/client_spec.rb +38 -0
  66. data/spec/sendgrid/api/entities/category_spec.rb +14 -0
  67. data/spec/sendgrid/api/entities/email_spec.rb +15 -0
  68. data/spec/sendgrid/api/entities/entity_spec.rb +279 -0
  69. data/spec/sendgrid/api/entities/list_spec.rb +34 -0
  70. data/spec/sendgrid/api/entities/marketing_email_spec.rb +31 -0
  71. data/spec/sendgrid/api/entities/profile_spec.rb +26 -0
  72. data/spec/sendgrid/api/entities/response_insert_spec.rb +28 -0
  73. data/spec/sendgrid/api/entities/response_remove_spec.rb +28 -0
  74. data/spec/sendgrid/api/entities/response_spec.rb +28 -0
  75. data/spec/sendgrid/api/entities/schedule_spec.rb +14 -0
  76. data/spec/sendgrid/api/entities/sender_address_spec.rb +21 -0
  77. data/spec/sendgrid/api/entities/stats_spec.rb +25 -0
  78. data/spec/sendgrid/api/newsletter/categories_spec.rb +247 -0
  79. data/spec/sendgrid/api/newsletter/emails_spec.rb +265 -0
  80. data/spec/sendgrid/api/newsletter/lists_spec.rb +307 -0
  81. data/spec/sendgrid/api/newsletter/marketing_emails_spec.rb +306 -0
  82. data/spec/sendgrid/api/newsletter/recipients_spec.rb +252 -0
  83. data/spec/sendgrid/api/newsletter/schedule_spec.rb +263 -0
  84. data/spec/sendgrid/api/newsletter/sender_addresses_spec.rb +300 -0
  85. data/spec/sendgrid/api/rest/errors/error_spec.rb +121 -0
  86. data/spec/sendgrid/api/rest/resource_spec.rb +145 -0
  87. data/spec/sendgrid/api/rest/response/parse_error_spec.rb +39 -0
  88. data/spec/sendgrid/api/rest/response/parse_json_spec.rb +45 -0
  89. data/spec/sendgrid/api/service_spec.rb +44 -0
  90. data/spec/sendgrid/api/version_spec.rb +11 -0
  91. data/spec/sendgrid/api/web/mail_spec.rb +111 -0
  92. data/spec/sendgrid/api/web/profile_spec.rb +110 -0
  93. data/spec/sendgrid/api/web/stats_spec.rb +94 -0
  94. data/spec/spec_helper.rb +23 -0
  95. data/spec/support/helpers.rb +23 -0
  96. data/spec/support/mock.rb +30 -0
  97. data/spec/support/online.rb +114 -0
  98. data/spec/support/shared_examples.rb +104 -0
  99. 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,5 @@
1
+ module Sendgrid
2
+ module API
3
+ VERSION = "0.0.4"
4
+ end
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
@@ -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,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":"Bad username / password"
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,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
+ ]
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "list": "list 1"
4
+ },
5
+ {
6
+ "list": "list 2"
7
+ }
8
+ ]