sendgrid-ruby 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9dedc1735da8ee2a09e97d3555e92b63fd631526
4
+ data.tar.gz: 29028865ccff1abe7dec82a570942229b7489c9a
5
+ SHA512:
6
+ metadata.gz: 7ea857ca795888f80c853713d7a5ceb145d11d387f0a03e389b9e762ca089b576c68c7c5ed48e79d9c154d60296108e88df746dc2db81e778ea328c2228fe754
7
+ data.tar.gz: 77f7f2b3f6db200b02b3faff9e3434f7a219de75f326ec03838436589008be37c13f8e8f646c2217cf1343f9fa3f93437dbc647421084b48bcccc87db4f8c66f
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+
15
+ tmp
16
+ *.bundle
17
+ *.so
18
+ *.o
19
+ *.a
20
+ mkmf.log
21
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ notification :gntp
2
+ guard 'rspec' do
3
+ # watch /lib/ files
4
+ watch(%r{^lib/(.+).rb$}) do |m|
5
+ "spec/#{m[1]}_spec.rb"
6
+ end
7
+
8
+ # watch /spec/ files
9
+ watch(%r{^spec/(.+).rb$}) do |m|
10
+ "spec/#{m[1]}.rb"
11
+ end
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 SendGrid Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # SendGrid::Ruby
2
+
3
+ This Gem allows you to quickly and easily send emails through SendGrid's Web API using native Ruby.
4
+
5
+ You can read our official documentation on the Web API's Mail feature [here](https://sendgrid.com/docs/API_Reference/Web_API/mail.html).
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'sendgrid-ruby'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install sendgrid-ruby
21
+
22
+ ## Usage
23
+
24
+ Create a new client with your SendGrid Username and Password.
25
+
26
+ ```ruby
27
+ require 'sendgrid-ruby'
28
+
29
+ client = SendGrid::Client.new(api_user: 'SENDGRID_USERNAME', api_key: 'SENDGRID_PASSWORD')
30
+ ```
31
+
32
+ Create a new Mail object and send:
33
+ ```ruby
34
+ mail = SendGrid::Mail.new do |m|
35
+ m.to = 'test@sendgrid.com'
36
+ m.from = 'taco@cat.limo'
37
+ m.subject = 'Hello world!'
38
+ m.text = 'I heard you like pineapple.'
39
+ end
40
+
41
+ puts client.send(mail)
42
+ # {"message":"success"}
43
+ ```
44
+
45
+ You can also create a mail object with a hash.
46
+ ```ruby
47
+ client.send(SendGrid::Mail.new(to: 'example@example.com', from: 'taco@cat.limo', subject: 'Hello world!', text: 'Hi there!', html: '<b>Hi there!</b>'))
48
+
49
+ # {"message":"success"}
50
+ ```
51
+
52
+ #### Available Params
53
+
54
+ ```ruby
55
+ params = {
56
+ :to,
57
+ :to_name,
58
+ :from,
59
+ :from_name,
60
+ :subject,
61
+ :text,
62
+ :html,
63
+ :cc,
64
+ :bcc,
65
+ :reply_to,
66
+ :date,
67
+ :smtpapi,
68
+ :attachments
69
+ }
70
+ ```
71
+
72
+ #### Using the X-SMTPAPI Header
73
+
74
+ To utilize the X-SMTPAPI header, we have directly integrated the [smtpapi-ruby](https://github.com/SendGridJP/smtpapi-ruby) gem. To initialize, you have two options:
75
+
76
+ Create your own and pass it in to the initialize method:
77
+ ```ruby
78
+ header = Smtpapi::Header.new
79
+ # Do things to the header
80
+ header.add_substitution('keep', ['secret'])
81
+ mail = SendGrid::Mail.new(smtpapi: header)
82
+ ```
83
+ Or use the one that we create and expose:
84
+ ```ruby
85
+ mail = SendGrid::Mail.new
86
+ mail.smtpapi.add_substitution('keep', ['secret'])
87
+ ```
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it ( https://github.com/[my-github-username]/sendgrid-ruby/fork )
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create a new Pull Request
96
+
97
+ ***Hit up [@rbin](http://twitter.com/rbin) or [@eddiezane](http://twitter.com/eddiezane) on Twitter with any issues.***
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color', '--format', 'nested']
6
+ end
7
+
8
+ task default: :spec
@@ -0,0 +1,41 @@
1
+ require_relative 'sendgrid/exceptions'
2
+ require_relative 'sendgrid/mail'
3
+ require_relative 'sendgrid/version'
4
+
5
+ require 'rest-client'
6
+
7
+ module SendGrid
8
+ class Client
9
+ attr_reader :api_user, :api_key, :host, :endpoint
10
+
11
+ def initialize(params)
12
+ @api_user = params.fetch(:api_user)
13
+ @api_key = params.fetch(:api_key)
14
+ @host = params.fetch(:host, 'https://api.sendgrid.com')
15
+ @endpoint = params.fetch(:endpoint, '/api/mail.send.json')
16
+ @conn = params.fetch(:conn, create_conn)
17
+ @user_agent = params.fetch(:user_agent, 'sendgrid/' + SendGrid::VERSION + ';ruby')
18
+ end
19
+
20
+ def send(mail)
21
+ payload = mail.to_h.merge({api_user: @api_user, api_key: @api_key})
22
+ @conn[@endpoint].post(payload, {user_agent: @user_agent}) do |response, request, result|
23
+ case response.code.to_s
24
+ when /2\d\d/
25
+ response
26
+ when /4\d\d|5\d\d/
27
+ raise SendGrid::Exception.new(response)
28
+ else
29
+ # TODO: What will reach this?
30
+ "DEFAULT #{response.code} #{response}"
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def create_conn
38
+ @conn = RestClient::Resource.new(@host)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ module SendGrid
2
+ class Exception < StandardError
3
+ def initialize(message)
4
+ super(message)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,60 @@
1
+ require 'json'
2
+ require 'smtpapi'
3
+
4
+ module SendGrid
5
+ class Mail
6
+ attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc,
7
+ :bcc, :reply_to, :date, :smtpapi, :attachments
8
+
9
+ def initialize(params = {})
10
+ params.each do |k, v|
11
+ instance_variable_set("@#{k}", v) unless v.nil?
12
+ end
13
+ @headers ||= {}
14
+ @attachments ||= []
15
+ @smtpapi ||= Smtpapi::Header.new
16
+ yield self if block_given?
17
+ end
18
+
19
+ def add_to(to_email)
20
+ @smtpapi.add_to to_email
21
+ end
22
+
23
+ def add_attachment(path, name = nil)
24
+ file = File.new(path)
25
+ name ||= File.basename(file)
26
+ @attachments << {file: file, name: name}
27
+ end
28
+
29
+ def to_h
30
+ payload = {
31
+ :from => @from,
32
+ :fromname => @from_name,
33
+ :subject => @subject,
34
+ :to => @to,
35
+ :toname => @to_name,
36
+ :date => @date,
37
+ :replyto => @reply_to,
38
+ :cc => @cc,
39
+ :bcc => @bcc,
40
+ :text => @text,
41
+ :html => @html,
42
+ :'x-smtpapi' => @smtpapi.to_json,
43
+ :files => ({} unless @attachments.empty?)
44
+ }.reject {|k,v| v.nil?}
45
+
46
+ # smtpapi fixer
47
+ if @to.nil? and not @smtpapi.to.empty?
48
+ payload[:to] = payload[:from]
49
+ end
50
+
51
+ unless @attachments.empty?
52
+ @attachments.each do |file|
53
+ payload[:files][file[:name]] = file[:file]
54
+ end
55
+ end
56
+
57
+ payload
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module SendGrid
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sendgrid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sendgrid-ruby'
8
+ spec.version = SendGrid::VERSION
9
+ spec.authors = ['Robin Johnson', 'Eddie Zaneski']
10
+ spec.email = 'community@sendgrid.com'
11
+ spec.summary = 'Official SendGrid Gem'
12
+ spec.description = 'Interact with SendGrids API in Native Ruby'
13
+ spec.homepage = 'http://github.com/sendgrid/sendgrid-ruby'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(/^bin/) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)/)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'smtpapi'
22
+ spec.add_dependency 'rest-client'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'rspec-nc'
26
+ spec.add_development_dependency 'guard'
27
+ spec.add_development_dependency 'guard-rspec'
28
+ spec.add_development_dependency 'bundler', '~> 1.6'
29
+ end
data/test.rb ADDED
@@ -0,0 +1,7 @@
1
+ require './lib/sendgrid-ruby.rb'
2
+
3
+ client = SendGrid::Client.new ENV['SENDGRID_USERNAME'], ENV['SENDGRID_PASSWORD']
4
+ email = SendGrid::Mail.new(from: 'root@mail.doesnotscale.com', subject: 'Test', text: 'Body of sexy')
5
+ email.add_to 'eddiezane@sendgrid.com'
6
+ email.add_to 'rbin@sendgrid.com'
7
+ puts client.send(email)
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sendgrid-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Robin Johnson
8
+ - Eddie Zaneski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: smtpapi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rest-client
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec-nc
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: guard
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: guard-rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: bundler
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '1.6'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '1.6'
126
+ description: Interact with SendGrids API in Native Ruby
127
+ email: community@sendgrid.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - Guardfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - lib/sendgrid-ruby.rb
139
+ - lib/sendgrid/exceptions.rb
140
+ - lib/sendgrid/mail.rb
141
+ - lib/sendgrid/version.rb
142
+ - sendgrid-ruby.gemspec
143
+ - test.rb
144
+ homepage: http://github.com/sendgrid/sendgrid-ruby
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.4.1
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Official SendGrid Gem
168
+ test_files:
169
+ - test.rb