-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Klimenkov
committed
Feb 4, 2016
1 parent
48a32b7
commit 9660316
Showing
15 changed files
with
319 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export RUBYOPT="rubygems" | ||
export RUBYLIB="." | ||
rvm 1.9.3@sendgrid_toolkit --create | ||
rvm 2.2.3@sendgrid_toolkit --create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ group :development do | |
gem "fakeweb", "~> 1.3.0" | ||
gem "jeweler" | ||
gem "rspec", "~> 2.7.0" | ||
gem 'pry', '~> 0.10.3' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module SendgridToolkit | ||
module V3 | ||
class AbstractSendgridClient | ||
|
||
BASE_URI = 'https://api.sendgrid.com/v3/asm' | ||
|
||
def initialize(api_user = nil, api_key = nil) | ||
@api_user = api_user || SendgridToolkit.api_user || ENV['SMTP_USERNAME'] | ||
@api_key = api_key || SendgridToolkit.api_key || ENV['SMTP_PASSWORD'] | ||
|
||
fail SendgridToolkit::NoAPIUserSpecified if @api_user.nil? || @api_user.length == 0 | ||
fail SendgridToolkit::NoAPIKeySpecified if @api_key.nil? || @api_key.length == 0 | ||
end | ||
|
||
protected | ||
|
||
def api_post(action_name, options = {}) | ||
response = HTTParty.post("#{BASE_URI}/#{action_name}", | ||
body: options.to_json, format: :json, | ||
headers: { 'Authorization' => "Basic #{credentials}" }) | ||
check_response(response) | ||
end | ||
|
||
def api_get(action_name, options = {}) | ||
response = HTTParty.get("#{BASE_URI}/#{action_name}", | ||
query: options, format: :json, headers: { 'Authorization' => "Basic #{credentials}" }) | ||
check_response(response) | ||
end | ||
|
||
def api_delete(action_name, options = {}) | ||
response = HTTParty.delete("#{BASE_URI}/#{action_name}", | ||
body: options, format: :json, | ||
headers: { 'Authorization' => "Basic #{credentials}" }) | ||
check_response(response) | ||
end | ||
|
||
private | ||
|
||
def check_response(response) | ||
if response.code > 401 | ||
fail(SendgridToolkit::SendgridServerError, "The SendGrid server returned an error. #{response.inspect}") | ||
elsif error?(response) && response['error'].respond_to?(:has_key?) && | ||
response['error'].key?('code') && | ||
response['error']['code'].to_i == 401 | ||
fail SendgridToolkit::AuthenticationFailed | ||
elsif error?(response) | ||
fail(SendgridToolkit::APIError, response['error']) | ||
end | ||
response | ||
end | ||
|
||
def credentials | ||
Base64.encode64("#{@api_user}:#{@api_key}") | ||
end | ||
|
||
def error?(response) | ||
response.is_a?(Hash) && response.key?('error') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module SendgridToolkit | ||
module V3 | ||
class Groups < SendgridToolkit::V3::AbstractSendgridClient | ||
def add(options = {}) | ||
response = api_post('groups', options) | ||
fail GroupsError if response.is_a?(Hash) && response.key?('errors') | ||
response | ||
end | ||
|
||
def get(group_id = nil) | ||
action_name = 'groups' + (group_id ? "/#{group_id}" : '') | ||
response = api_get(action_name) | ||
fail GroupsError if response.is_a?(Hash) && response.key?('errors') | ||
response | ||
end | ||
|
||
def delete(options = {}) | ||
fail NoGroupIdSpecified unless options[:group_id] | ||
|
||
response = api_delete("groups/#{options[:group_id]}") | ||
fail GroupsError if response.is_a?(Hash) && response.key?('errors') | ||
response | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module SendgridToolkit | ||
module V3 | ||
class Unsubscribes < SendgridToolkit::V3::AbstractSendgridClient | ||
def add(options = {}) | ||
fail NoGroupIdSpecified unless options[:group_id] | ||
|
||
response = api_post("groups/#{options[:group_id]}/suppressions", options) | ||
fail APIError if response.is_a?(Hash) && response.key?('errors') | ||
response | ||
end | ||
|
||
def delete(options = {}) | ||
fail NoGroupIdSpecified unless options[:group_id] | ||
|
||
response = api_delete("groups/#{options[:group_id]}/suppressions/#{options[:email]}", options) | ||
fail APIError if response.is_a?(Hash) && response.key?('errors') | ||
response | ||
end | ||
|
||
def retrieve(options = {}) | ||
fail NoGroupIdSpecified unless options[:group_id] | ||
|
||
response = api_get("groups/#{options[:group_id]}/suppressions", options) | ||
response | ||
end | ||
end | ||
end | ||
end |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
spec/lib/sendgrid_toolkit/v3/abstract_sendgrid_client_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require File.expand_path("#{File.dirname(__FILE__)}/../../../helper") | ||
|
||
describe SendgridToolkit::V3::AbstractSendgridClient do | ||
before do | ||
backup_env | ||
end | ||
after do | ||
restore_env | ||
end | ||
|
||
describe '#api_post' do | ||
it "throws error when authentication fails" do | ||
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI_V3}/profile\.get\.json\?|, :body => '{"error":{"code":401,"message":"Permission denied, wrong credentials"}}') | ||
@obj = SendgridToolkit::AbstractSendgridClient.new("fakeuser", "fakepass") | ||
lambda { | ||
@obj.send(:api_post, "profile", "get", {}) | ||
}.should raise_error SendgridToolkit::AuthenticationFailed | ||
end | ||
it "thows error when sendgrid response is a server error" do | ||
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI_V3}/profile\.get\.json\?|, :body => '{}', :status => ['500', 'Internal Server Error']) | ||
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass") | ||
lambda { | ||
@obj.send(:api_post, "profile", "get", {}) | ||
}.should raise_error SendgridToolkit::AuthenticationFailed | ||
end | ||
it "thows error when sendgrid response is an API error" do | ||
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI_V3}/stats\.get\.json\?|, :body => '{"error": "error in end_date: end date is in the future"}', :status => ['400', 'Bad Request']) | ||
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass") | ||
lambda { | ||
@obj.send(:api_post, "stats", "get", {}) | ||
}.should raise_error SendgridToolkit::SendgridServerError | ||
end | ||
end | ||
|
||
describe "#initialize" do | ||
after(:each) do | ||
SendgridToolkit.api_user = nil | ||
SendgridToolkit.api_key = nil | ||
end | ||
it 'stores api credentials when passed in' do | ||
ENV['SMTP_USERNAME'] = 'env_username' | ||
ENV['SMTP_PASSWORD'] = 'env_apikey' | ||
|
||
@obj = SendgridToolkit::V3::AbstractSendgridClient.new('username', 'apikey') | ||
@obj.instance_variable_get('@api_user').should == 'username' | ||
@obj.instance_variable_get('@api_key').should == 'apikey' | ||
end | ||
it 'uses module level user and key if they are set' do | ||
SendgridToolkit.api_user = 'username' | ||
SendgridToolkit.api_key = 'apikey' | ||
|
||
SendgridToolkit.api_key.should == 'apikey' | ||
SendgridToolkit.api_user.should == 'username' | ||
|
||
@obj = SendgridToolkit::V3::AbstractSendgridClient.new | ||
@obj.instance_variable_get('@api_user').should == 'username' | ||
@obj.instance_variable_get('@api_key').should == 'apikey' | ||
end | ||
it 'resorts to environment variables when no credentials specified' do | ||
ENV['SMTP_USERNAME'] = 'env_username' | ||
ENV['SMTP_PASSWORD'] = 'env_apikey' | ||
|
||
@obj = SendgridToolkit::V3::AbstractSendgridClient.new() | ||
@obj.instance_variable_get('@api_user').should == 'env_username' | ||
@obj.instance_variable_get('@api_key').should == 'env_apikey' | ||
end | ||
it 'throws error when no credentials are found' do | ||
ENV['SMTP_USERNAME'] = nil | ||
ENV['SMTP_PASSWORD'] = nil | ||
|
||
lambda { | ||
@obj = SendgridToolkit::V3::AbstractSendgridClient.new() | ||
}.should raise_error SendgridToolkit::NoAPIUserSpecified | ||
|
||
lambda { | ||
@obj = SendgridToolkit::V3::AbstractSendgridClient.new(nil, 'password') | ||
}.should raise_error SendgridToolkit::NoAPIUserSpecified | ||
|
||
lambda { | ||
@obj = SendgridToolkit::V3::AbstractSendgridClient.new('user', nil) | ||
}.should raise_error SendgridToolkit::NoAPIKeySpecified | ||
end | ||
end | ||
end |
Oops, something went wrong.