sendgrid-web 0.0.1 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0c66be35eba75ec8a6cfdb0bdf918e2bbe63d3b
4
- data.tar.gz: d4713a455c5db0915922186a916f271914940092
3
+ metadata.gz: c25529f42cb3b382076bbf57b48be5ae98991bee
4
+ data.tar.gz: 91ceedecf727a38be051799fe81932900357d661
5
5
  SHA512:
6
- metadata.gz: f8aaf092ef851396f5c829f77fa6d7016d94659674ccba3990a31bb29a468f0d05efb6002d8e03cfddd339f732efc988d335a64f72e6def1a65fc741f1af221c
7
- data.tar.gz: 51d40f45c36b90c05a3a151a879d96fa07aa649d57692e5de93ce9501dcc98f116bc245dd9fae048a66542403bbe81aefe9b94212d40fba756599393b98f8015
6
+ metadata.gz: 1a26f04093ebc8c51137300f121a3afa0eab6d5edb51dc99f5fc921d855e0c41136a1b6b692911be75e577243a6875cd56530b292d03ba02e9e16a5bf2e149f0
7
+ data.tar.gz: 8a56c673cd34b2d4e7cbc535a28097198080af2efb61047f46bec2e332704426df89ccf2661ea60620c5554755f7ce04b19945fb609c655c5238582863497e9b
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Sendgrid::Web
2
2
 
3
- TODO: Write a gem description
3
+ Developed as a means to communicate effectively with the [Sendgrid Web API]
4
+ whilst utilising new Ruby 2.0.0 features. The aim is to become the go-to
5
+ library for interacting with Sendgrid via their web API.
6
+
7
+ [Sendgrid Web API]: http://sendgrid.com/docs/API_Reference/Web_API/
4
8
 
5
9
  ## Installation
6
10
 
@@ -10,20 +14,60 @@ Add this line to your application's Gemfile:
10
14
 
11
15
  And then execute:
12
16
 
13
- $ bundle
17
+ bundle
14
18
 
15
19
  Or install it yourself as:
16
20
 
17
- $ gem install sendgrid-web
21
+ gem install sendgrid-web
18
22
 
19
23
  ## Usage
20
24
 
21
- TODO: Write usage instructions here
25
+ First you will need to configure the client:
26
+
27
+ Sendgrid::Web::Client.configure do |config|
28
+ config.username = 'your sendgrid username'
29
+ config.password = 'your sendgrid password'
30
+ end
31
+
32
+ Now you can create client instances based on the api module you
33
+ want to access:
34
+
35
+ client = Sendgrid::Web::Bounces.new
36
+ client.get # get list of bounces
37
+ client.delete(email: 'foobar@example.com') # delete all bounces for foobar
22
38
 
23
39
  ## Contributing
24
40
 
25
41
  1. Fork it
26
42
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
43
+ 3. Provide working specs for new features
44
+ 4. Document changes (if changing public interface)
45
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 6. Push to the branch (`git push origin my-new-feature`)
47
+ 7. Create new Pull Request
48
+
49
+ ## License
50
+
51
+ ```
52
+ The MIT License (MIT)
53
+
54
+ Copyright (c) 2013 Darren Coxall
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining a copy
57
+ of this software and associated documentation files (the "Software"), to deal
58
+ in the Software without restriction, including without limitation the rights
59
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
60
+ copies of the Software, and to permit persons to whom the Software is
61
+ furnished to do so, subject to the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be included in
64
+ all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
72
+ THE SOFTWARE.
73
+ ```
@@ -0,0 +1 @@
1
+ require 'sendgrid/web'
data/lib/sendgrid/web.rb CHANGED
@@ -1,13 +1,17 @@
1
- require 'faraday'
2
- require 'typhoeus'
3
- require 'typhoeus/adapters/faraday'
4
- require 'sendgrid/web/version'
1
+ require 'oj'
2
+ require 'httparty'
5
3
 
6
4
  module Sendgrid
7
5
  module Web
8
- autoload :Configurator, 'sendgrid/web/configurator'
9
- autoload :Client, 'sendgrid/web/client'
10
- autoload :Blocks, 'sendgrid/web/blocks'
11
- autoload :Bounces, 'sendgrid/web/bounces'
6
+ autoload :VERSION, 'sendgrid/web/version'
7
+ autoload :Configurator, 'sendgrid/web/configurator'
8
+ autoload :Client, 'sendgrid/web/client'
9
+ autoload :Blocks, 'sendgrid/web/blocks'
10
+ autoload :Bounces, 'sendgrid/web/bounces'
11
+ autoload :FilterCommands, 'sendgrid/web/filter_commands'
12
+ autoload :InvalidEmails, 'sendgrid/web/invalid_emails'
13
+ autoload :Mail, 'sendgrid/web/mail'
14
+ autoload :Credentials, 'sendgrid/web/credentials'
15
+ autoload :Response, 'sendgrid/web/response'
12
16
  end
13
17
  end
@@ -1,12 +1,48 @@
1
1
  class Sendgrid::Web::Blocks < Sendgrid::Web::Client
2
2
 
3
- def get
4
- connection.post('blocks.get.json', default_params)
3
+ # Retrieves a list of all blocked email addresses along with the
4
+ # reason why they are blocked.
5
+ #
6
+ # @param date [Integer] Retrieve the timestamp of the Block records.
7
+ # @param days [Integer] Number of days in the past for which to
8
+ # retrieve blocks (includes today)
9
+ # @param start_date [DateTime] The start of the date range for which
10
+ # to retrieve blocks.
11
+ # @param end_date [DateTime] The end of the date range for which to
12
+ # retrieve blocks.
13
+ # @param limit [Integer] Optional field to limit the number of
14
+ # results returned.
15
+ # @param offset [Integer] Optional beginning point in the list to
16
+ # retrieve from.
17
+ # @return [Sendgrid::Web::Response] The sendgrid response
18
+ # @note All parameters are optional
19
+ def get(
20
+ date: nil, days: nil, start_date: nil,
21
+ end_date: nil, limit: nil, offset: nil)
22
+ res = connection.post(
23
+ '/api/blocks.get.json',
24
+ default_params(
25
+ date: date,
26
+ days: days,
27
+ start_date: start_date,
28
+ end_date: end_date,
29
+ limit: limit,
30
+ offset: offset))
31
+ craft_response(res)
5
32
  end
6
33
 
34
+ # Deletes all blocks associated with the provided email
35
+ #
36
+ # @param email [String] Email block address to remove
37
+ # @return [Sendgrid::Web::Response] The sendgrid response
7
38
  def delete(email: nil)
8
- raise ArgumentError.new('Missing required `email` option') if email.nil?
9
- connection.post('blocks.delete.json', default_params.merge(email: email))
39
+ if email.nil?
40
+ raise ArgumentError.new('Missing required `email` option')
41
+ end
42
+ res = connection.post(
43
+ '/api/blocks.delete.json',
44
+ default_params(email: email))
45
+ craft_response(res)
10
46
  end
11
47
 
12
48
  end
@@ -1,16 +1,64 @@
1
1
  class Sendgrid::Web::Bounces < Sendgrid::Web::Client
2
2
 
3
- def get
4
- connection.post('bounces.get.json', default_params)
3
+ # Retrieve a list of bounces with addresses and response codes,
4
+ # optionally with dates.
5
+ #
6
+ # @param date [Integer] Retrieve the timestamp of the bounce
7
+ # records.
8
+ # @param days [Integer] Number of days in the past for which to
9
+ # retrieve bounces (includes today)
10
+ # @param start_date [DateTime] The start of the date range for which to
11
+ # retrieve bounces.
12
+ # @param end_date [DateTime] The end of the date range for which to
13
+ # retrieve bounces.
14
+ # @param limit [Integer] Optional field to limit the number of
15
+ # results returned.
16
+ # @param offset [Integer] Optional beginning point in the list to
17
+ # retrieve from.
18
+ # @param type [String] Choose the type of bounce to search for. Can
19
+ # be either +hard+ or +soft+.
20
+ # @param email [String] Optional email addresses to search for.
21
+ # @return [Sendgrid::Web::Response] The sendgrid response
22
+ # @note All parameters are optional
23
+ def get(
24
+ date: nil, days: nil, start_date: nil,
25
+ end_date: nil, limit: nil, offset: nil,
26
+ type: nil, email: nil)
27
+ res = connection.post(
28
+ '/api/bounces.get.json',
29
+ default_params(
30
+ date: date,
31
+ days: days,
32
+ start_date: start_date,
33
+ end_date: end_date,
34
+ limit: limit,
35
+ offset: offset,
36
+ type: type,
37
+ email: email))
38
+ craft_response(res)
5
39
  end
6
40
 
7
- def delete(start_date: nil, end_date: nil, type: nil, email: nil)
8
- connection.post('bounces.delete.json',
9
- default_params.merge(
41
+ # Delete an address from the Bounce list.
42
+ #
43
+ # @param start_date [DateTime] Optional date to start deleting from.
44
+ # @param end_date [DateTime] Optional date to end deleting from.
45
+ # @param type [String] Choose the type of bounce to be removed. Can
46
+ # be either +hard+ or +soft+.
47
+ # @param email [String] Email bounce address to remove.
48
+ # @return [Sendgrid::Web::Response] The sendgrid response
49
+ # @note All parameters are optional
50
+ # @note If no parameters are specified the ENTIRE list will be
51
+ # deleted.
52
+ def delete(
53
+ start_date: nil, end_date: nil, type: nil,
54
+ email: nil)
55
+ res = connection.post('/api/bounces.delete.json',
56
+ default_params(
10
57
  start_date: start_date,
11
58
  end_date: end_date,
12
59
  type: type,
13
60
  email: email))
61
+ craft_response(res)
14
62
  end
15
63
 
16
64
  end
@@ -1,33 +1,67 @@
1
1
  class Sendgrid::Web::Client
2
+
3
+ # Sets the global configuration object shared between all clients.
4
+ # You can use it like so:
5
+ #
6
+ # Sendgrid::Web::Client.configure do |config|
7
+ # config.username = 'foo'
8
+ # config.password = 'bar'
9
+ # end
10
+ #
11
+ # @return [Sendgrid::Web::Configurator]
2
12
  def self.configure(&block)
3
13
  @@config = Sendgrid::Web::Configurator.new(&block)
4
14
  end
5
15
 
16
+ # Retrieve the current global configuration object and if none
17
+ # exists, then create an empty one.
18
+ #
19
+ # @see #config
6
20
  def self.config
7
- @@config
21
+ @@config ||= Sendgrid::Web::Configurator.new
8
22
  end
9
23
 
24
+ # Returns the configured +root_url+.
25
+ def self.base_uri
26
+ config.root_url
27
+ end
28
+
29
+ # Retrieve the current global configuration object and if none
30
+ # exists, then create an empty one.
31
+ #
32
+ # @see .config
10
33
  def config
11
34
  @@config ||= Sendgrid::Web::Configurator.new
12
35
  end
13
36
 
14
37
  private
15
38
 
39
+ class API
40
+ include ::HTTParty
41
+ base_uri Sendgrid::Web::Client.config.root_url
42
+ end
43
+
16
44
  def config=(configurator)
17
45
  @@config = configurator
18
46
  end
19
47
 
20
48
  def connection
21
- @connection ||= Faraday.new(:url => config.root_url) do |faraday|
22
- faraday.request :url_encoded
23
- faraday.adapter :typhoeus
24
- end
49
+ @connection = API
25
50
  end
26
51
 
27
- def default_params
28
- {
29
- api_user: config.username,
30
- api_key: config.password
52
+ def default_params(additions = {})
53
+ defaults = {
54
+ query: {
55
+ api_user: config.username,
56
+ api_key: config.password
57
+ }
31
58
  }
59
+ defaults[:query].merge!(additions)
60
+ defaults
61
+ end
62
+
63
+ def craft_response(response)
64
+ Sendgrid::Web::Response.new(response.code, response.body)
32
65
  end
66
+
33
67
  end
@@ -2,7 +2,7 @@ class Sendgrid::Web::Configurator
2
2
  attr_accessor :username, :password, :root_url
3
3
 
4
4
  def initialize(&block)
5
- self.root_url = 'https://sendgrid.com/api/'
5
+ self.root_url = 'https://sendgrid.com'
6
6
  yield self if block_given?
7
7
  end
8
8
  end
@@ -0,0 +1,80 @@
1
+ class Sendgrid::Web::Credentials < Sendgrid::Web::Client
2
+
3
+ # Retrieve a list of all credentials, or permissions for a specific
4
+ # credential.
5
+ #
6
+ # @param username [String] If a username is supplied, the API
7
+ # returns the JSON permissions for that user.
8
+ # @return [Sendgrid::Web::Response] the Sendgrid response.
9
+ # @note All parameters are optional
10
+ def get(username: nil)
11
+ res = connection.post(
12
+ '/api/credentials/get.json',
13
+ default_params(username: username))
14
+ craft_response(res)
15
+ end
16
+
17
+ # This API call allows user to add a new set of credentials to their
18
+ # account.
19
+ #
20
+ # @param username [String] Enter a username for the new account.
21
+ # @param password [String] Enter a password for the new account.
22
+ # @param permissions [Hash] There are three key names: +email+ for
23
+ # access to SMTP, +api+ for programmatic access, and +web+ for
24
+ # administration. The values for each are a bit, +0+ for off or
25
+ # +1+ for on.
26
+ #
27
+ # The following example allows the specified username/password to
28
+ # log into the dashboard, but not send email or have access to any
29
+ # of the APIs:
30
+ #
31
+ # { email: 0, web: 1, api: 0 }
32
+ #
33
+ # @return [Sendgrid::Web::Response] the Sendgrid response.
34
+ # @note Requires +username+ and +password+.
35
+ def add(username: nil, password: nil, permissions: nil)
36
+ raise ArgumentError.new('Missing required `username` option') if username.nil?
37
+ raise ArgumentError.new('Missing required `password` option') if password.nil?
38
+ res = connection.post(
39
+ '/api/credentials/add.json',
40
+ default_params(
41
+ username: username,
42
+ password: password,
43
+ permissions: permissions))
44
+ craft_response(res)
45
+ end
46
+
47
+ # Update/edit a users credentials and permissions.
48
+ #
49
+ # @param username [String] The existing users username.
50
+ # @param password [String] Optionally update the password.
51
+ # @param permissions [Hash] Optionally update the permissions.
52
+ # @return [Sendgrid::Web::Response] the Sendgrid response.
53
+ # @note Only +username+ is required.
54
+ # @see #add
55
+ def edit(username: nil, password: nil, permissions: nil)
56
+ raise ArgumentError.new('Missing required `username` option') if username.nil?
57
+ res = connection.post(
58
+ '/api/credentials/edit.json',
59
+ default_params(
60
+ username: username,
61
+ password: password,
62
+ permissions: permissions))
63
+ craft_response(res)
64
+ end
65
+
66
+ # Remove an individuals credentials.
67
+ #
68
+ # @param username [String] The credential to remove.
69
+ # @return [Sendgrid::Web::Response] the Sendgrid response.
70
+ # @note +username+ is required.
71
+ def delete(username: nil)
72
+ raise ArgumentError.new('Missing required `username` option') if username.nil?
73
+ res = connection.post(
74
+ '/api/credentials/delete.json',
75
+ default_params(
76
+ username: username))
77
+ craft_response(res)
78
+ end
79
+
80
+ end
@@ -0,0 +1,67 @@
1
+ class Sendgrid::Web::FilterCommands < Sendgrid::Web::Client
2
+
3
+ # List all of the available apps.
4
+ #
5
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
6
+ # @note The name entry is used in all the other API calls to
7
+ # identify the app.
8
+ def get_available
9
+ res = connection.post(
10
+ '/api/filter.getavailable.json',
11
+ default_params)
12
+ craft_response(res)
13
+ end
14
+
15
+ # Activate an app.
16
+ #
17
+ # @param name [String] The app in which to activate.
18
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
19
+ # @note +name+ is required.
20
+ def activate_app(name: nil)
21
+ raise ArgumentError.new('Missing required `name` option') if name.nil?
22
+ res = connection.post(
23
+ '/api/filter.activate.json',
24
+ default_params(name: name))
25
+ craft_response(res)
26
+ end
27
+
28
+ # Deactivate an app.
29
+ #
30
+ # @param name [String] The app in which to deactivate.
31
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
32
+ # @note +name+ is required.
33
+ def deactivate_app(name: nil)
34
+ raise ArgumentError.new('Missing required `name` option') if name.nil?
35
+ res = connection.post(
36
+ '/api/filter.deactivate.json',
37
+ default_params(name: name))
38
+ craft_response(res)
39
+ end
40
+
41
+ # Update an apps settings.
42
+ #
43
+ # @param name [String] The app in which to update.
44
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
45
+ # @note Other options are dependant on the app in question.
46
+ def setup_app(options = {})
47
+ raise ArgumentError.new('Missing required `name` option') if options[:name].nil?
48
+ res = connection.post(
49
+ '/api/filter.setup.json',
50
+ default_params(options))
51
+ craft_response(res)
52
+ end
53
+
54
+ # Retrieve the settings of an app.
55
+ #
56
+ # @param name [String] The app in which to retrieve.
57
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
58
+ # @note +name+ is required.
59
+ def get_settings(name: nil)
60
+ raise ArgumentError.new('Missing required `name` option') if name.nil?
61
+ res = connection.post(
62
+ '/api/filter.getsettings.json',
63
+ default_params(name: name))
64
+ craft_response(res)
65
+ end
66
+
67
+ end
@@ -0,0 +1,51 @@
1
+ class Sendgrid::Web::InvalidEmails < Sendgrid::Web::Client
2
+
3
+ # Retrieve a list of invalid emails with addresses and response
4
+ # codes, optionally with dates.
5
+ #
6
+ # @param date [Integer] Retrieve the timestamp of the invalid email
7
+ # records.
8
+ # @param days [Integer] Number of days in the past for which to
9
+ # retrieve invalid emails (includes today).
10
+ # @param start_date [DateTime] The start of the date range for which
11
+ # to retrieve invalid emails.
12
+ # @param end_date [DateTime] The end of the date range for which to
13
+ # retrieve invalid emails.
14
+ # @param limit [Integer] Optional field to limit the number of
15
+ # results returned.
16
+ # @param offset [Integer] Optional beginning point in the list to
17
+ # retrieve from.
18
+ # @param email [String] Optional email addresses to search for.
19
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
20
+ # @note All parameters are optional.
21
+ def get(
22
+ date: nil, days: nil, start_date: nil,
23
+ end_date: nil, limit: nil, offset: nil,
24
+ email: nil)
25
+ res = connection.post(
26
+ '/api/invalidemails.get.json',
27
+ default_params(
28
+ date: date,
29
+ days: days,
30
+ start_date: start_date,
31
+ end_date: end_date,
32
+ limit: limit,
33
+ offset: offset,
34
+ email: email))
35
+ craft_response(res)
36
+ end
37
+
38
+ # Delete an address from the Invalid Email list.
39
+ #
40
+ # @param email [String] Email address to remove.
41
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
42
+ # @note +email+ parameter is required.
43
+ def delete(email: nil)
44
+ raise ArgumentError.new('Missing required `email` option') if email.nil?
45
+ res = connection.post(
46
+ '/api/invalidemails.delete.json',
47
+ default_params(email: email))
48
+ craft_response(res)
49
+ end
50
+
51
+ end
@@ -0,0 +1,63 @@
1
+ class Sendgrid::Web::Mail < Sendgrid::Web::Client
2
+
3
+ # Send an email.
4
+ #
5
+ # @param to [String|Array<String>] This can also be passed in as an
6
+ # array, to send to multiple locations. Note that recipients
7
+ # passed in this parameter will be visible as part of the message.
8
+ # @param to_name [String|Array<String>] Give a name to the
9
+ # recipient.
10
+ # @param x_smtpapi [Hash] see
11
+ # {http://sendgrid.com/docs/API_Reference/SMTP_API/index.html X-SMTP API}
12
+ # for details.
13
+ # @param subject [String] The subject of your email.
14
+ # @param text [String] The actual content of your email message.
15
+ # @param html [String] The actual content of your email message.
16
+ # @param from [String] This is where the email will appear to
17
+ # originate from for your recipient.
18
+ # @param bcc [String|Array<String>] This can also be passed in as an
19
+ # array of email addresses for multiple recipients.
20
+ # @param from_name [String] This is name appended to the from email
21
+ # field.
22
+ # @param reply_to [String] Append a reply-to field to your email
23
+ # message.
24
+ # @param date [DateTime] Specify the date header of your email.
25
+ # @param files [Array] Files to be attached. The file contents must
26
+ # be part of the multipart HTTP POST.
27
+ # @param content [Hash] Content IDs of the files to be used as
28
+ # inline images. Content IDs should match the cid’s used in the
29
+ # HTML markup.
30
+ # @param headers [Hash] Each key represents a header name and the
31
+ # value the header value.
32
+ # @return [Sendgrid::Web::Response] The Sendgrid response.
33
+ # @note The required parameters are: +from+, +text+/+html+,
34
+ # +subject+ and +to+.
35
+ def send(
36
+ to: nil, to_name: nil, x_smtpapi: nil,
37
+ subject: nil, text: nil, html: nil,
38
+ from: nil, bcc: nil, from_name: nil,
39
+ reply_to: nil, date: nil, files: nil,
40
+ content: nil, headers: nil)
41
+ options = {
42
+ 'to' => to, 'toname' => to_name, 'x-smtpapi' => x_smtpapi,
43
+ 'subject' => subject, 'text' => text, 'html' => html,
44
+ 'from' => from, 'bcc' => bcc, 'fromname' => from_name,
45
+ 'replyto' => reply_to, 'date' => date, 'files' => files,
46
+ 'content' => content, 'headers' => headers }
47
+ %w{ subject to }.each do |required_option|
48
+ if options[required_option].nil?
49
+ raise ArgumentError.new(
50
+ "Missing required `#{required_option}` option")
51
+ end
52
+ end
53
+ if options['text'].nil? && options['html'].nil?
54
+ raise ArgumentError.new(
55
+ 'Missing required `text` or `html` option')
56
+ end
57
+ res = connection.post(
58
+ '/api/mail.send.json',
59
+ default_params(options))
60
+ craft_response(res)
61
+ end
62
+
63
+ end
@@ -0,0 +1,27 @@
1
+ class Sendgrid::Web::Response
2
+
3
+ attr_reader :status_code, :raw_body, :parsed_body
4
+
5
+ def initialize(status_code, body)
6
+ @status_code = status_code.to_i
7
+ @raw_body = body.to_s
8
+ @parsed_body = ::Oj.safe_load(raw_body)
9
+ end
10
+
11
+ # Checks if the Sengrid response contained errors.
12
+ #
13
+ # @return [bool] True if there were errors found.
14
+ def errors?
15
+ !parsed_body.nil? &&
16
+ parsed_body.is_a?(Hash) &&
17
+ parsed_body.has_key?('errors')
18
+ end
19
+
20
+ # Fetches an array of error messages from the response.
21
+ #
22
+ # @return [Array<String>] A list of any error messages.
23
+ def error_messages
24
+ errors? ? parsed_body['errors'] : []
25
+ end
26
+
27
+ end
@@ -1,5 +1,5 @@
1
1
  module Sendgrid
2
2
  module Web
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/sendgrid-web.gemspec CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "typhoeus", ">= 0.6.3"
22
- spec.add_runtime_dependency "faraday"
21
+ spec.add_runtime_dependency "httparty", ">= 0.11.0"
22
+ spec.add_runtime_dependency "oj", "~> 2.1.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
25
  spec.add_development_dependency "rake"
@@ -9,42 +9,29 @@ describe Sendgrid::Web::Blocks do
9
9
  end
10
10
 
11
11
  describe '#get' do
12
- let(:test_connection) do
13
- Faraday::Adapter::Test::Stubs.new do |stub|
14
- stub.post('blocks.get.json') { [200, {}, 'sent blocks.get'] }
12
+ it_behaves_like('a sendgrid response', '/api/blocks.get.json') do
13
+ let(:action) { subject.get }
14
+ let(:response) do
15
+ '[
16
+ {
17
+ "status": "4.0.0",
18
+ "created": "2012-09-02 00:12:12",
19
+ "reason": "550 Access denied...04d52d35b93501d500a9bca895ddad5cddd1a9f8486c89ace8c5e959398198cd49cd58288c9d11313975284d852811... (throttled)",
20
+ "email": "example@juno.com"
21
+ }
22
+ ]'
15
23
  end
16
24
  end
17
-
18
- before do
19
- subject.stub(:connection).and_return(test_connection)
20
- end
21
-
22
- it 'makes a json request to /blocks.get.json' do
23
- test_connection.should_receive(:post).
24
- with('blocks.get.json',
25
- hash_including(api_user: 'foo', api_key: 'bar')).
26
- and_call_original
27
- subject.get
28
- end
29
25
  end
30
26
 
31
27
  describe '#delete' do
32
- let(:test_connection) do
33
- Faraday::Adapter::Test::Stubs.new do |stub|
34
- stub.post('blocks.delete.json') { [200, {}, 'sent blocks.delete'] }
28
+ it_behaves_like('a sendgrid response', '/api/blocks.delete.json') do
29
+ let(:action) { subject.delete(email: 'foobar@example.com') }
30
+ let(:response) do
31
+ '{
32
+ "message": "success"
33
+ }'
35
34
  end
36
35
  end
37
-
38
- before do
39
- subject.stub(:connection).and_return(test_connection)
40
- end
41
-
42
- it 'makes a json request to /blocks.delete.json' do
43
- test_connection.should_receive(:post).
44
- with('blocks.delete.json',
45
- hash_including(api_user: 'foo', api_key: 'bar', email: 'foobar@example.com')).
46
- and_call_original
47
- subject.delete(email: 'foobar@example.com')
48
- end
49
36
  end
50
37
  end
@@ -9,42 +9,29 @@ describe Sendgrid::Web::Bounces do
9
9
  end
10
10
 
11
11
  describe '#get' do
12
- let(:test_connection) do
13
- Faraday::Adapter::Test::Stubs.new do |stub|
14
- stub.post('bounces.get.json') { [200, {}, 'sent bounces.get'] }
12
+ it_behaves_like('a sendgrid response', '/api/bounces.get.json') do
13
+ let(:action) { subject.get }
14
+ let(:response) do
15
+ '[
16
+ {
17
+ "status": "4.0.0",
18
+ "created": "2011-09-16 22:02:19",
19
+ "reason": "Unable to resolve MX host sendgrid.ne",
20
+ "email": "esting@sendgrid.ne"
21
+ }
22
+ ]'
15
23
  end
16
24
  end
17
-
18
- before do
19
- subject.stub(:connection).and_return(test_connection)
20
- end
21
-
22
- it 'makes a json request to /bounces.get.json' do
23
- test_connection.should_receive(:post).
24
- with('bounces.get.json',
25
- hash_including(api_user: 'foo', api_key: 'bar')).
26
- and_call_original
27
- subject.get
28
- end
29
25
  end
30
26
 
31
27
  describe '#delete' do
32
- let(:test_connection) do
33
- Faraday::Adapter::Test::Stubs.new do |stub|
34
- stub.post('bounces.delete.json') { [200, {}, 'sent bounces.delete'] }
28
+ it_behaves_like('a sendgrid response', '/api/bounces.delete.json') do
29
+ let(:action) { subject.delete }
30
+ let(:response) do
31
+ '{
32
+ "message": "success"
33
+ }'
35
34
  end
36
35
  end
37
-
38
- before do
39
- subject.stub(:connection).and_return(test_connection)
40
- end
41
-
42
- it 'makes a json request to /bounces.delete.json' do
43
- test_connection.should_receive(:post).
44
- with('bounces.delete.json',
45
- hash_including(api_user: 'foo', api_key: 'bar', email: 'foobar@example.com')).
46
- and_call_original
47
- subject.delete(email: 'foobar@example.com')
48
- end
49
36
  end
50
37
  end
@@ -14,7 +14,7 @@ describe Sendgrid::Web::Configurator do
14
14
  end
15
15
 
16
16
  it 'sets a default root_url for sendgrid' do
17
- subject.root_url.should eql('https://sendgrid.com/api/')
17
+ subject.root_url.should eql('https://sendgrid.com')
18
18
  end
19
19
 
20
20
  it 'can overwrite the api root_url' do
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendgrid::Web::Credentials do
4
+ before(:all) do
5
+ Sendgrid::Web::Client.configure do |config|
6
+ config.username = 'foo'
7
+ config.password = 'bar'
8
+ end
9
+ end
10
+
11
+ describe '#get' do
12
+ it_behaves_like('a sendgrid response', '/api/credentials/get.json') do
13
+ let(:action) { subject.get }
14
+ let(:response) do
15
+ '[
16
+ {
17
+ "id": 4,
18
+ "name": "johnsmith",
19
+ "permissions": {
20
+ "email": 0,
21
+ "api": 1
22
+ }
23
+ }
24
+ ]'
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#add' do
30
+ it_behaves_like('a sendgrid response', '/api/credentials/add.json') do
31
+ let(:action) { subject.add(username: 'foobar', password: 'foobar') }
32
+ let(:response) do
33
+ '{
34
+ "message": "success"
35
+ }'
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#edit' do
41
+ it_behaves_like('a sendgrid response', '/api/credentials/edit.json') do
42
+ let(:action) { subject.edit(username: 'foobar') }
43
+ let(:response) do
44
+ '{
45
+ "message": "success"
46
+ }'
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#delete' do
52
+ it_behaves_like('a sendgrid response', '/api/credentials/delete.json') do
53
+ let(:action) { subject.delete(username: 'foobar') }
54
+ let(:response) do
55
+ '{
56
+ "message": "success"
57
+ }'
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendgrid::Web::FilterCommands do
4
+ before(:all) do
5
+ Sendgrid::Web::Client.configure do |config|
6
+ config.username = 'foo'
7
+ config.password = 'bar'
8
+ end
9
+ end
10
+
11
+ describe '#get_available' do
12
+ it_behaves_like('a sendgrid response', '/api/filter.getavailable.json') do
13
+ let(:action) { subject.get_available }
14
+ let(:response) do
15
+ '[
16
+ {
17
+ "name": "twitter",
18
+ "title": "Twitter",
19
+ "description": "This plugin allows you to send an email message to twitter",
20
+ "activated": false
21
+ }
22
+ ]'
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#activate_app' do
28
+ it_behaves_like('a sendgrid response', '/api/filter.activate.json') do
29
+ let(:action) { subject.activate_app(name: 'twitter') }
30
+ let(:response) do
31
+ '{
32
+ "message": "success"
33
+ }'
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#deactivate_app' do
39
+ it_behaves_like('a sendgrid response', '/api/filter.deactivate.json') do
40
+ let(:action) { subject.deactivate_app(name: 'twitter') }
41
+ let(:response) do
42
+ '{
43
+ "message": "success"
44
+ }'
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#setup_app' do
50
+ it_behaves_like('a sendgrid response', '/api/filter.setup.json') do
51
+ let(:action) { subject.setup_app(name: 'twitter', username: 'foo', password: 'bar') }
52
+ let(:response) do
53
+ '{
54
+ "message": "success"
55
+ }'
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#get_settings' do
61
+ it_behaves_like('a sendgrid response', '/api/filter.getsettings.json') do
62
+ let(:action) { subject.get_settings(name: 'twitter') }
63
+ let(:response) do
64
+ '{
65
+ "message": "success",
66
+ "settings": [
67
+ {
68
+ "field_name": "field_value"
69
+ }
70
+ ]
71
+ }'
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendgrid::Web::InvalidEmails do
4
+ before(:all) do
5
+ Sendgrid::Web::Client.configure do |config|
6
+ config.username = 'foo'
7
+ config.password = 'bar'
8
+ end
9
+ end
10
+
11
+ describe '#get' do
12
+ it_behaves_like('a sendgrid response', '/api/invalidemails.get.json') do
13
+ let(:action) { subject.get }
14
+ let(:response) do
15
+ '[
16
+ {
17
+ "reason": "Known bad domain",
18
+ "created": "2011-06-06 16:11:57",
19
+ "email": "test@example.com"
20
+ }
21
+ ]'
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '#delete' do
27
+ it_behaves_like('a sendgrid response', '/api/invalidemails.delete.json') do
28
+ let(:action) { subject.delete(email: 'foobar@example.com') }
29
+ let(:response) do
30
+ '{
31
+ "message": "success"
32
+ }'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendgrid::Web::Mail do
4
+ before(:all) do
5
+ Sendgrid::Web::Client.configure do |config|
6
+ config.username = 'foo'
7
+ config.password = 'bar'
8
+ end
9
+ end
10
+
11
+ describe '#send' do
12
+ it_behaves_like('a sendgrid response', '/api/mail.send.json') do
13
+ let(:action) { subject.send(
14
+ to: 'foobar@example.com',
15
+ subject: 'Test Email',
16
+ text: 'Sent using `sendgrid-web`',
17
+ from: 'test@example.com') }
18
+ let(:response) do
19
+ '{
20
+ "message": "success"
21
+ }'
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sendgrid::Web::Response do
4
+
5
+ let(:valid_body) do
6
+ '[{
7
+ "username": "sampleuser@example.com",
8
+ "email": "sampleemail@example.com",
9
+ "active": "true",
10
+ "first_name": "john",
11
+ "last_name": "doe",
12
+ "address": "555 any street",
13
+ "city": "any city",
14
+ "state": "CA",
15
+ "zip": "91234",
16
+ "country": "US",
17
+ "phone": "555-555-5555",
18
+ "website": "example.com"
19
+ }]'
20
+ end
21
+
22
+ let(:error_body) do
23
+ '{
24
+ "message": "error",
25
+ "errors": [
26
+ "error message one",
27
+ "error message two"
28
+ ]
29
+ }'
30
+ end
31
+
32
+ let(:body) { valid_body }
33
+ let(:status_code) { '200' }
34
+ let(:response) { Sendgrid::Web::Response.new(status_code, body) }
35
+
36
+ subject { response }
37
+
38
+ it 'stores the response body' do
39
+ subject.raw_body.should eql(body)
40
+ end
41
+
42
+ it 'stores the response status' do
43
+ subject.status_code.should eql(status_code.to_i)
44
+ end
45
+
46
+ describe '#errors?' do
47
+ subject { response.errors? }
48
+
49
+ context 'when sendgrid returns a json error' do
50
+ let(:body) { error_body }
51
+ it { should be_true }
52
+ end
53
+
54
+ context 'when sendgrid returns a valid result' do
55
+ let(:body) { valid_body }
56
+ it { should be_false }
57
+ end
58
+ end
59
+
60
+ describe '#error_messages' do
61
+ subject { response.error_messages }
62
+
63
+ context 'when sendgrid returns a json error' do
64
+ let(:body) { error_body }
65
+ it { should eql(['error message one', 'error message two']) }
66
+ end
67
+
68
+ context 'when sendgrid returns a valid result' do
69
+ let(:body) { valid_body }
70
+ it { should be_empty }
71
+ end
72
+ end
73
+
74
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,11 @@ require 'sendgrid/web'
7
7
  # loaded once.
8
8
  #
9
9
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+
10
11
  RSpec.configure do |config|
12
+
13
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
14
+
11
15
  config.treat_symbols_as_metadata_keys_with_true_values = true
12
16
  config.run_all_when_everything_filtered = true
13
17
  config.filter_run :focus
@@ -0,0 +1,25 @@
1
+ shared_examples "a sendgrid response" do |path|
2
+ let(:test_connection) { double(:connection) }
3
+
4
+ before do
5
+ test_connection.stub(:post).
6
+ with(path, anything).
7
+ and_return(double(:response, code: 200, body: response))
8
+ subject.stub(:connection).and_return(test_connection)
9
+ end
10
+
11
+ it "makes a json request to #{path}" do
12
+ params = double(:parameters)
13
+ subject.stub(:default_params) { params }
14
+ test_connection.should_receive(:post).
15
+ with(path, params)
16
+ action
17
+ end
18
+
19
+ it 'returns a Sendgrid::Web::Response' do
20
+ result = action
21
+ result.should be_instance_of(Sendgrid::Web::Response)
22
+ result.status_code.should eql(200)
23
+ result.raw_body.should eql(response)
24
+ end
25
+ end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendgrid-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darren Coxall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-11 00:00:00.000000000 Z
11
+ date: 2013-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: typhoeus
14
+ name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.6.3
19
+ version: 0.11.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.6.3
26
+ version: 0.11.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: faraday
28
+ name: oj
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 2.1.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 2.1.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -95,18 +95,30 @@ files:
95
95
  - LICENSE.txt
96
96
  - README.md
97
97
  - Rakefile
98
+ - lib/sendgrid-web.rb
98
99
  - lib/sendgrid/web.rb
99
100
  - lib/sendgrid/web/blocks.rb
100
101
  - lib/sendgrid/web/bounces.rb
101
102
  - lib/sendgrid/web/client.rb
102
103
  - lib/sendgrid/web/configurator.rb
104
+ - lib/sendgrid/web/credentials.rb
105
+ - lib/sendgrid/web/filter_commands.rb
106
+ - lib/sendgrid/web/invalid_emails.rb
107
+ - lib/sendgrid/web/mail.rb
108
+ - lib/sendgrid/web/response.rb
103
109
  - lib/sendgrid/web/version.rb
104
110
  - sendgrid-web.gemspec
105
111
  - spec/sendgrid/web/blocks_spec.rb
106
112
  - spec/sendgrid/web/bounces_spec.rb
107
113
  - spec/sendgrid/web/client_spec.rb
108
114
  - spec/sendgrid/web/configurator_spec.rb
115
+ - spec/sendgrid/web/credentials_spec.rb
116
+ - spec/sendgrid/web/filter_commands_spec.rb
117
+ - spec/sendgrid/web/invalid_emails_spec.rb
118
+ - spec/sendgrid/web/mail_spec.rb
119
+ - spec/sendgrid/web/response_spec.rb
109
120
  - spec/spec_helper.rb
121
+ - spec/support/shared_sendgrid_response.rb
110
122
  homepage: https://github.com/FreakyDazio/sendgrid-web
111
123
  licenses:
112
124
  - MIT
@@ -136,4 +148,10 @@ test_files:
136
148
  - spec/sendgrid/web/bounces_spec.rb
137
149
  - spec/sendgrid/web/client_spec.rb
138
150
  - spec/sendgrid/web/configurator_spec.rb
151
+ - spec/sendgrid/web/credentials_spec.rb
152
+ - spec/sendgrid/web/filter_commands_spec.rb
153
+ - spec/sendgrid/web/invalid_emails_spec.rb
154
+ - spec/sendgrid/web/mail_spec.rb
155
+ - spec/sendgrid/web/response_spec.rb
139
156
  - spec/spec_helper.rb
157
+ - spec/support/shared_sendgrid_response.rb