sendgrid_web 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - 1.8.7
6
+
7
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'faraday'
4
+
5
+ group :test do
6
+ gem 'rake'
7
+ end
8
+
9
+ # Specify your gem's dependencies in sendgrid_web.gemspec
10
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jeremiah Heller
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.
@@ -0,0 +1,72 @@
1
+ # SendgridWeb
2
+
3
+ Ruby interface for working with SendGrid's Web (HTTP) API. Read more at http://docs.sendgrid.com/documentation/api/web-api/.
4
+
5
+ ## Build Status [<img src="https://secure.travis-ci.org/inertialbit/sendgrid_web.png"/>](http://travis-ci.org/inertialbit/sendgrid_web)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'sendgrid_web'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sendgrid_web
20
+
21
+ ## Usage
22
+
23
+ Something like the following would be nice.
24
+
25
+ ### Configuration
26
+
27
+ SendgridWeb.configure do |config|
28
+ config.api_user = 'sendgrid-user'
29
+ config.api_key = 'sendgrid-secret'
30
+ end
31
+
32
+ ### Running Programmatically
33
+
34
+ response = SendgridWeb.unsubscribes(:get, :xml) do |request|
35
+ request.option :timeout => 30
36
+ request.option :open_timeout => 30
37
+ request.header 'HTTP-HEADER' => 'value'
38
+
39
+ request.with_date # special to enforce api rule of required value of 1
40
+
41
+ request.param 'start_date' => 2012-07-31
42
+ request.param 'email' => 'me@test.com'
43
+ end
44
+
45
+ if response.success?
46
+ # do stuff...
47
+ else
48
+ # do other stuff...
49
+ end
50
+
51
+ ### Running Manually
52
+
53
+ not implemented yet!!
54
+
55
+ $ sendgrid_web get unsubscribes --with-date --days 30 --start_date 2012-07-01 --end_date 2012-07-31 --limit 20 --offset 0 --email 'blah@test.com'
56
+ $ sendgrid_web delete unsubscribes.xml --email 'blah@test.com'
57
+ $ sendgrid_web delete unsubscribes.json --after 2012-07-01 --before 2012-07-31
58
+ $ sendgrid_web get bounces --with-date --days-ago 30 # default is json
59
+
60
+ ## Todo
61
+
62
+ - more tests
63
+ - implement as Faraday middleware (support async calls)
64
+ - CLI
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ load "#{File.dirname(__FILE__)}/lib/tasks/tests.rake"
4
+
5
+ task :test => Rake::Task['test:sendgrid_web']
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sendgrid_web'
4
+
5
+ raise "TODO: implement CLI"
@@ -0,0 +1,62 @@
1
+ require "sendgrid_web/version"
2
+ require "sendgrid_web/request"
3
+
4
+ ##
5
+ #
6
+ # Module to provide thin ruby API to SendGrid Web API.
7
+ # Uses Faraday to make requests. Currently supports synchronous calls only.
8
+ #
9
+ # Exposes both Sendgrid and Faraday interfaces.
10
+ #
11
+ module SendgridWeb
12
+
13
+ ##
14
+ #
15
+ # Primary interface. Simply call the desired module directly on SendgridWeb
16
+ # and pass in the verb, format and a block to set any needed parameters.
17
+ #
18
+ # Example usage:
19
+ #
20
+ # # Make a request to SendGrid module, say Unsubscribes
21
+ # # fetching a specific email.
22
+ # faraday_response = SendgridWeb.unsubscribes(:get, :json) { request.param :email => 'me@test.com' }
23
+ #
24
+ # # Send an email.
25
+ # faraday_response = SendgridWeb.mail(:send, :xml) do
26
+ # request.param :to => 'me@test.com', :subject => 'Tada!', :from => 'you@test.com'
27
+ # request.param :text => 'sample message body'
28
+ # request.param :html => '<p>sample message body</p>'
29
+ # end
30
+ #
31
+ def self.method_missing(resource, verb, format=:json, &block)
32
+ Request.configure do |request|
33
+ request.verb = verb
34
+ request.resource = resource
35
+ request.format = format
36
+
37
+ yield request
38
+ end
39
+ process
40
+ end
41
+
42
+ class << self
43
+ attr_accessor :host, :scheme, :api_namespace, :api_user, :api_key
44
+
45
+ def configure
46
+ yield self
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def self.process
53
+ url = URI.join(Request.url, Request.url_action).to_s
54
+ Faraday.get(url, Request.params, Request.headers)
55
+ end
56
+ end
57
+
58
+ SendgridWeb.configure do |config|
59
+ config.scheme = "https"
60
+ config.host = "sendgrid.com"
61
+ config.api_namespace = "api"
62
+ end
@@ -0,0 +1,46 @@
1
+ require 'sendgrid_web/utils'
2
+
3
+ begin
4
+ require 'faraday'
5
+ rescue LoadError => e
6
+ require 'rubygems'
7
+ require 'faraday'
8
+ p "Warning: requiring rubygems since faraday failed to load on it's own."
9
+ end
10
+
11
+ module SendgridWeb
12
+ module Request
13
+ class << self
14
+ attr_accessor :headers, :options, :callbacks
15
+ include URL
16
+
17
+ ##
18
+ #
19
+ # Configure a Request. +@options+ and +@headers+ are
20
+ # passed through to Faraday.
21
+ #
22
+ def configure &block
23
+ @callbacks = {}
24
+ @headers = []
25
+ @options = []
26
+
27
+ unless SendgridWeb.api_user.nil? or SendgridWeb.api_key.nil?
28
+ param(:api_user => SendgridWeb.api_user)
29
+ param(:api_key => SendgridWeb.api_key)
30
+ end
31
+
32
+ yield self
33
+ end
34
+
35
+ def option(option)
36
+ @options ||= {}
37
+ @options.merge!(option)
38
+ end
39
+
40
+ def header(header)
41
+ @headers ||= {}
42
+ @headers.merge!(header)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ module SendgridWeb
2
+ ##
3
+ #
4
+ # Utility to assemble a URL for Sendgrid.
5
+ #
6
+ module URL
7
+ attr_accessor :url, :params, :format, :resource, :verb
8
+
9
+ def url
10
+ @url ||= "#{SendgridWeb.scheme}://#{SendgridWeb.host}/"
11
+ end
12
+
13
+ # Returns action portion of Sendgrid URL, e.g.
14
+ # "api/resource.verb.format"
15
+ def url_action
16
+ parts = []
17
+ parts << SendgridWeb.api_namespace
18
+ parts << [resource, verb, format].join('.')
19
+ parts.join('/')
20
+ end
21
+
22
+ # Add or overwrite a param in +@params+ hash.
23
+ # These are passed through to Faraday.
24
+ def param(param)
25
+ @params ||= {}
26
+ @params.merge!(param)
27
+ end
28
+
29
+ # Convenience method to add param with
30
+ # strict Sendgrid requirements.
31
+ def with_date
32
+ param(:date => 1)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module SendgridWeb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # run tests
2
+ begin
3
+ require 'rake/testtask'
4
+ rescue LoadError => e
5
+ end
6
+
7
+ if defined? Rake::TestTask
8
+ namespace :test do
9
+ SENDGRID_WEB_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..",".."))
10
+ Rake::TestTask.new(:sendgrid_web) do |t|
11
+ t.libs << "#{SENDGRID_WEB_ROOT}/test"
12
+ t.libs << "#{SENDGRID_WEB_ROOT}/lib"
13
+ t.pattern = "#{SENDGRID_WEB_ROOT}/test/**/*_test.rb"
14
+ t.verbose = true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sendgrid_web/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sendgrid_web"
8
+ gem.version = SendgridWeb::VERSION
9
+ gem.authors = ["Jeremiah Heller"]
10
+ gem.email = ["jeremiah@inertialbit.net"]
11
+ gem.description = %q{Ruby interface for working with SendGrid's Web (HTTP) API. Read more at http://docs.sendgrid.com/documentation/api/web-api/.}
12
+ gem.summary = %q{Wraps SendGrid Web API and works with rack on ruby 1.8.7, 1.9.2, 1.9.3.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency("faraday", ["= 0.8.4"])
21
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+ require 'cgi'
3
+ require 'uri'
4
+ require 'sendgrid_web'
5
+
6
+ class SendgridWebTest < Test::Unit::TestCase
7
+ def self.sendgrid_modules
8
+ [
9
+ {:bounces => [:get, :delete]},
10
+ {:blocks => [:get, :delete]},
11
+ {:parse => [:get, :set, :edit, :delete]},
12
+ {:filter => [:get, :activate, :deactivate, :setup, :getsettings]},
13
+ {:invalidemails => [:get, :delete]},
14
+ {:mail => [:send]},
15
+ {:profile => [:get, :set, :setUsername, :setPassword, :setEmail]},
16
+ {:spamreports => [:get, :delete]},
17
+ {:stats => [:get]},
18
+ {:unsubscribes => [:add, :get, :delete]}
19
+ ]
20
+ end
21
+
22
+ def normalize_query(url)
23
+ url.query.split('&').map{|pair| k,v = pair.split('='); {k.to_sym => CGI.unescape(v)}}
24
+ end
25
+
26
+ sendgrid_modules.each do |sg_module|
27
+ define_method "test_#{sg_module}_wrapper" do
28
+ sg_module.each do |sg_module_name, sg_module_actions|
29
+ sg_module_actions.each do |action|
30
+
31
+ email = 'me@test.com'
32
+ start_date = '2012-07-31'
33
+ api_user = 'user'
34
+ api_key = 'secret'
35
+ expected_url = URI("https://sendgrid.com/api/#{sg_module_name}.#{action}.json?api_user=#{api_user}&api_key=#{api_key}&start_date=#{start_date}&email=#{email}")
36
+
37
+ SendgridWeb.configure do |config|
38
+ config.api_user = api_user
39
+ config.api_key = api_key
40
+ end
41
+
42
+ response = SendgridWeb.send(sg_module_name, action, :json) do |request|
43
+ request.with_date # special to enforce api rule of required value of 1
44
+
45
+ request.param :start_date => start_date
46
+ request.param :email => email
47
+ end
48
+
49
+ generated_url = response.env[:url]
50
+
51
+ %w(scheme host path).each do |part|
52
+ assert_equal(expected_url.send(part), generated_url.send(part), "generated #{part} didn't match expected")
53
+ end
54
+
55
+ generated_query = normalize_query(generated_url)
56
+ expected_query = normalize_query(expected_url)
57
+
58
+ expected_query.each do |param|
59
+ assert(generated_query.include?(param), "generated query missing #{param.inspect}")
60
+ end
61
+
62
+ end # sg_module_actions#each
63
+ end # sg_module#each
64
+ end # define_method
65
+ end # sendgrid_modules#each
66
+ end # SendgridWebTest
@@ -0,0 +1 @@
1
+ require 'test/unit'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sendgrid_web
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremiah Heller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.4
30
+ description: Ruby interface for working with SendGrid's Web (HTTP) API. Read more
31
+ at http://docs.sendgrid.com/documentation/api/web-api/.
32
+ email:
33
+ - jeremiah@inertialbit.net
34
+ executables:
35
+ - sendgrid_web
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - .travis.yml
41
+ - Gemfile
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/sendgrid_web
46
+ - lib/sendgrid_web.rb
47
+ - lib/sendgrid_web/request.rb
48
+ - lib/sendgrid_web/utils.rb
49
+ - lib/sendgrid_web/version.rb
50
+ - lib/tasks/tests.rake
51
+ - sendgrid_web.gemspec
52
+ - test/integration/sendgrid_web_test.rb
53
+ - test/test_helper.rb
54
+ homepage: ''
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: 2860537481081348625
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: 2860537481081348625
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.24
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Wraps SendGrid Web API and works with rack on ruby 1.8.7, 1.9.2, 1.9.3.
84
+ test_files:
85
+ - test/integration/sendgrid_web_test.rb
86
+ - test/test_helper.rb