diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..c809d5f --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,110 @@ +version: 2 +jobs: + test: + parallelism: 1 + docker: + - image: ruby:2.7.6 + + working_directory: ~/pbxx + steps: + - checkout + + - restore_cache: + keys: + - ruby-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "sendgrid_toolkit.gemspec" }} + - ruby-cache-{{ arch }}-{{ .Branch }} + - ruby-cache + + - run: + name: Update RubyGems + command: gem update --system 3.4.22 + + - run: + name: Install bundler + command: gem install bundler -v 2.4.22 + + - run: + name: Configure bundler + command: bundle config set --local path 'vendor/bundle' + + - run: + name: Bundle Install + command: bundle check || bundle install --jobs=4 --retry=3 + + - save_cache: + key: ruby-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "sendgrid_toolkit.gemspec" }} + paths: + - vendor/bundle + + - run: + name: Run tests + command: | + bundle exec rspec spec + + - store_test_results: + path: test_results + + build: + parallelism: 1 + docker: + - image: ruby:2.7.6 + working_directory: ~/pbxx + steps: + - checkout + + - restore_cache: + keys: + - ruby-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "sendgrid_toolkit.gemspec" }} + + - run: + name: Build gem + context: gem + command: | + gem build sendgrid_toolkit.gemspec + + - save_cache: + key: ruby-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "sendgrid_toolkit.gemspec" }}-build + paths: + - . + + upload: + parallelism: 1 + docker: + - image: ruby:2.7.6 + working_directory: ~/pbxx + steps: + - checkout + + - restore_cache: + keys: + - ruby-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "sendgrid_toolkit.gemspec" }}-build + + - run: + name: Upload to Gemfury + context: gem + command: | + curl -F package=@sendgrid_toolkit-1.12.0.gem https://$GEMFURY_PUSH_TOKEN@push.fury.io/promoboxx/ + +workflows: + version: 2 + build_and_upload: + jobs: + - test + - build: + requires: + - test + - ready_to_upload: + type: approval + requires: + - build + filters: + branches: + only: master + - upload: + requires: + - ready_to_upload + filters: + branches: + only: master + + diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..49cdd66 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.6 diff --git a/.rvmrc b/.rvmrc deleted file mode 100644 index 25649f7..0000000 --- a/.rvmrc +++ /dev/null @@ -1,3 +0,0 @@ -export RUBYOPT="rubygems" -export RUBYLIB="." -rvm 1.9.3@sendgrid_toolkit --create diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 28cf207..0000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -language: ruby -rvm: - - 1.9.3 - - jruby-19mode - - 2.0.0 - - 2.1.0 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9df8708 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM pbxx/docker-passenger-ruby-base:ruby-2.7.6-latest + +ARG FOO=bar + +USER root + +RUN apt-get update \ + && apt-get install -y --no-install-recommends --fix-missing \ + wget \ + nano \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Switch to the app user for app installation +USER app + +# Define where our application will live inside the image +ENV HOMEDIR /home/app/ +WORKDIR $HOMEDIR/sendgrid_toolkit + +# Set environment +ENV RUBYOPT='-W:no-deprecated -W:no-experimental' + +COPY --chown=app:app . . + +# Install rubygems and bundler +RUN rm -rf .bundle +RUN gem update --system 3.4.22 +RUN gem install bundler -v 2.4.22 + +# Install gems +ARG CACHEBUST +RUN bundle install --full-index + +USER root diff --git a/Gemfile b/Gemfile index fea4cf8..86f8913 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,18 @@ source "http://rubygems.org" +ruby file: ".ruby-version" + gem "httparty", ">= 0.7.6" group :development do - gem "fakeweb", "~> 1.3.0" - gem "jeweler" - gem "rspec", "~> 2.7.0" - gem 'pry', '~> 0.10.3' + gem 'fakeweb', '~> 1.3.0' + gem 'jeweler' + gem 'rspec' + gem 'pry' + gem 'pry-byebug' + gem 'rspec_junit_formatter' +end + +group :test do + gem 'rspec_junit_formatter' end diff --git a/Gemfile.lock b/Gemfile.lock index 2873b4a..583a46a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,59 +1,69 @@ GEM remote: http://rubygems.org/ specs: - addressable (2.3.5) - builder (3.2.2) - coderay (1.1.0) + addressable (2.4.0) + builder (3.2.4) + byebug (11.1.3) + coderay (1.1.3) + descendants_tracker (0.0.4) + thread_safe (~> 0.3, >= 0.3.1) diff-lcs (1.1.3) fakeweb (1.3.0) - faraday (0.8.8) - multipart-post (~> 1.2.0) - git (1.2.6) - github_api (0.10.1) - addressable - faraday (~> 0.8.1) - hashie (>= 1.2) - multi_json (~> 1.4) - nokogiri (~> 1.5.2) - oauth2 - hashie (2.0.5) - highline (1.6.20) - httparty (0.12.0) - json (~> 1.8) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) + git (1.11.0) + rchardet (~> 1.8) + github_api (0.16.0) + addressable (~> 2.4.0) + descendants_tracker (~> 0.0.4) + faraday (~> 0.8, < 0.10) + hashie (>= 3.4) + mime-types (>= 1.16, < 3.0) + oauth2 (~> 1.0) + hashie (5.0.0) + highline (2.1.0) + httparty (0.21.0) + mini_mime (>= 1.0.0) multi_xml (>= 0.5.2) - httpauth (0.2.0) - jeweler (1.8.8) + jeweler (2.3.9) builder - bundler (~> 1.0) + bundler git (>= 1.2.5) - github_api (= 0.10.1) + github_api (~> 0.16.0) highline (>= 1.6.15) - nokogiri (= 1.5.10) + nokogiri (>= 1.5.10) + psych rake rdoc - json (1.8.1) - jwt (0.1.8) - multi_json (>= 1.5) + semver2 + jwt (2.3.0) method_source (0.8.2) - multi_json (1.8.2) - multi_xml (0.5.5) - multipart-post (1.2.0) - nokogiri (1.5.10) - oauth2 (0.9.2) - faraday (~> 0.8) - httpauth (~> 0.2) - jwt (~> 0.1.4) - multi_json (~> 1.0) + mime-types (2.99.3) + mini_mime (1.1.2) + mini_portile2 (2.4.0) + multi_json (1.15.0) + multi_xml (0.6.0) + multipart-post (2.3.0) + nokogiri (1.10.10) + mini_portile2 (~> 2.4.0) + oauth2 (1.4.8) + faraday (>= 0.8, < 3.0) + jwt (>= 1.0, < 3.0) + multi_json (~> 1.3) multi_xml (~> 0.5) - rack (~> 1.2) - pry (0.10.3) + rack (>= 1.2, < 3) + pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - rack (1.5.2) - rake (10.1.0) - rdoc (4.0.1) - json (~> 1.4) + pry-byebug (3.8.0) + byebug (~> 11.0) + pry (~> 0.10) + psych (3.1.0) + rack (2.2.8) + rake (13.1.0) + rchardet (1.8.0) + rdoc (6.2.0) rspec (2.7.0) rspec-core (~> 2.7.0) rspec-expectations (~> 2.7.0) @@ -62,17 +72,27 @@ GEM rspec-expectations (2.7.0) diff-lcs (~> 1.1.2) rspec-mocks (2.7.0) + rspec_junit_formatter (0.6.0) + rspec-core (>= 2, < 4, != 2.12.0) + semver2 (3.4.2) slop (3.6.0) + thread_safe (0.3.6) PLATFORMS ruby + x86_64-linux DEPENDENCIES fakeweb (~> 1.3.0) httparty (>= 0.7.6) jeweler - pry (~> 0.10.3) - rspec (~> 2.7.0) + pry + pry-byebug + rspec + rspec_junit_formatter + +RUBY VERSION + ruby 2.7.6p219 BUNDLED WITH - 1.11.2 + 2.4.22 diff --git a/Rakefile b/Rakefile index bebf02f..78744d3 100644 --- a/Rakefile +++ b/Rakefile @@ -18,8 +18,7 @@ Jeweler::Tasks.new do |gem| gem.license = "MIT" gem.summary = "sendgrid_toolkit = Sendgrid + Ruby" gem.description = "A Ruby wrapper and utility library for communicating with the Sendgrid API." - gem.email = "robby@freerobby.com" - gem.authors = ["Robby Grossman"] + gem.authors = ["promoboxx people"] # dependencies defined in Gemfile end Jeweler::RubygemsDotOrgTasks.new diff --git a/VERSION b/VERSION index e21e727..0eed1a2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.0 \ No newline at end of file +1.12.0 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..bb045c7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.4' +services: + sendgrid_toolkit: + build: + context: . + environment: + RUBY_OPT: -W:no-deprecated -W:no-experimental + volumes: + - .:/home/app/sendgrid_toolkit diff --git a/lib/sendgrid_toolkit.rb b/lib/sendgrid_toolkit.rb index 19d19dd..3abd768 100644 --- a/lib/sendgrid_toolkit.rb +++ b/lib/sendgrid_toolkit.rb @@ -13,7 +13,9 @@ require 'sendgrid_toolkit/mail' require 'sendgrid_toolkit/v3/abstract_sendgrid_client' +require 'sendgrid_toolkit/v3/bounces' require 'sendgrid_toolkit/v3/groups' +require 'sendgrid_toolkit/v3/mail' require 'sendgrid_toolkit/v3/unsubscribes' require 'sendgrid_toolkit/newsletter/newsletter_sendgrid_client' diff --git a/lib/sendgrid_toolkit/abstract_sendgrid_client.rb b/lib/sendgrid_toolkit/abstract_sendgrid_client.rb index 49a2d16..4657344 100644 --- a/lib/sendgrid_toolkit/abstract_sendgrid_client.rb +++ b/lib/sendgrid_toolkit/abstract_sendgrid_client.rb @@ -13,14 +13,18 @@ def initialize(api_user = nil, api_key = nil) def api_post(module_name, action_name, opts = {}) base_path = compose_base_path(module_name, action_name) - response = HTTParserParty.post("https://#{SendgridToolkit.base_uri}/#{base_path}.json", - :body => get_credentials.merge(opts), - :format => :json) + response = nil + if (@api_user == "apikey") + response = HTTParserParty.post("https://#{BASE_URI}/#{base_path}.json?", :body => opts, :format => :json, :headers => { "Authorization" => "Bearer #{@api_key}"}) + else + response = HTTParserParty.post("https://#{BASE_URI}/#{base_path}.json?", :body => get_credentials.merge(opts), :format => :json) + end + if response.code > 401 raise(SendgridToolkit::SendgridServerError, "The SendGrid server returned an error. #{response.inspect}") - elsif has_error?(response) and - response['error'].respond_to?(:has_key?) and - response['error'].has_key?('code') and + elsif has_error?(response) && + response['error'].respond_to?(:has_key?) && + response['error'].has_key?('code') && response['error']['code'].to_i == 401 raise SendgridToolkit::AuthenticationFailed elsif has_error?(response) @@ -39,7 +43,7 @@ def compose_base_path(module_name, action_name) private def has_error?(response) - response.kind_of?(Hash) && response.has_key?('error') + response != nil && response.respond_to?(:key?) && response.key?('error') end end end diff --git a/lib/sendgrid_toolkit/common.rb b/lib/sendgrid_toolkit/common.rb index 275f45f..f845b9d 100644 --- a/lib/sendgrid_toolkit/common.rb +++ b/lib/sendgrid_toolkit/common.rb @@ -10,7 +10,7 @@ def retrieve(options = {}) def retrieve_with_timestamps(options = {}) options.merge! :date => 1 response = retrieve options - if response.is_a? Array + if response.respond_to?(:each) response.each do |message| parse_message_time message end diff --git a/lib/sendgrid_toolkit/v3/abstract_sendgrid_client.rb b/lib/sendgrid_toolkit/v3/abstract_sendgrid_client.rb index b8f6ba5..78ecaa3 100644 --- a/lib/sendgrid_toolkit/v3/abstract_sendgrid_client.rb +++ b/lib/sendgrid_toolkit/v3/abstract_sendgrid_client.rb @@ -2,7 +2,7 @@ module SendgridToolkit module V3 class AbstractSendgridClient - BASE_URI = 'https://api.sendgrid.com/v3/asm' + BASE_URI = 'https://api.sendgrid.com/v3' def initialize(api_user = nil, api_key = nil) @api_user = api_user || SendgridToolkit.api_user || ENV['SMTP_USERNAME'] @@ -15,22 +15,41 @@ def initialize(api_user = nil, api_key = nil) protected def api_post(action_name, options = {}) - response = HTTParserParty.post("#{BASE_URI}/#{action_name}", - body: options.to_json, format: :json, - headers: { 'Authorization' => "Basic #{credentials}" }) + if (@api_user == "apikey") + response = HTTParserParty.post("#{BASE_URI}/#{action_name}", + body: options.to_json, format: :json, + headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{@api_key}" }) + else + response = HTTParserParty.post("#{BASE_URI}/#{action_name}", + body: options.to_json, format: :json, + headers: { 'Content-Type' => 'application/json', 'Authorization' => "Basic #{credentials}" }) + end check_response(response) end def api_get(action_name, options = {}) - response = HTTParserParty.get("#{BASE_URI}/#{action_name}", - query: options, format: :json, headers: { 'Authorization' => "Basic #{credentials}" }) + if (@api_user == "apikey") + response = HTTParserParty.get("#{BASE_URI}/#{action_name}", + query: options, format: :json, + headers: { 'Authorization' => "Bearer #{@api_key}" }) + else + response = HTTParserParty.get("#{BASE_URI}/#{action_name}", + query: options, format: :json, + headers: { 'Authorization' => "Basic #{credentials}" }) + end check_response(response) end def api_delete(action_name, options = {}) - response = HTTParserParty.delete("#{BASE_URI}/#{action_name}", - body: options, format: :json, - headers: { 'Authorization' => "Basic #{credentials}" }) + if (@api_user == "apikey") + response = HTTParserParty.delete("#{BASE_URI}/#{action_name}", + body: options, format: :json, + headers: { 'Authorization' => "Bearer #{@api_key}" }) + else + response = HTTParserParty.delete("#{BASE_URI}/#{action_name}", + body: options, format: :json, + headers: { 'Authorization' => "Basic #{credentials}" }) + end check_response(response) end @@ -39,11 +58,12 @@ def api_delete(action_name, options = {}) 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 + elsif has_error?(response) && + response['error'].respond_to?(:has_key?) && + response['error'].has_key?('code') && + response['error']['code'].to_i == 401 fail SendgridToolkit::AuthenticationFailed - elsif error?(response) + elsif has_error?(response) fail(SendgridToolkit::APIError, response['error']) end response @@ -53,8 +73,8 @@ def credentials Base64.encode64("#{@api_user}:#{@api_key}") end - def error?(response) - response.is_a?(Hash) && response.key?('error') + def has_error?(response) + response != nil && response.respond_to?(:key?) && response.key?('error') end end end diff --git a/lib/sendgrid_toolkit/v3/bounces.rb b/lib/sendgrid_toolkit/v3/bounces.rb new file mode 100644 index 0000000..842b734 --- /dev/null +++ b/lib/sendgrid_toolkit/v3/bounces.rb @@ -0,0 +1,12 @@ +module SendgridToolkit + module V3 + class Bounces < SendgridToolkit::V3::AbstractSendgridClient + def get(options = {}) + action_name = 'suppression/bounces' + response = api_get(action_name, options) + fail(SendgridToolkit::APIError, response['error']) if response.key?('errors') + response + end + end + end +end diff --git a/lib/sendgrid_toolkit/v3/groups.rb b/lib/sendgrid_toolkit/v3/groups.rb index 6420c0b..c77f8db 100644 --- a/lib/sendgrid_toolkit/v3/groups.rb +++ b/lib/sendgrid_toolkit/v3/groups.rb @@ -3,14 +3,14 @@ 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') + fail GroupsError if 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') + fail GroupsError if response.key?('errors') response end @@ -18,7 +18,7 @@ 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') + fail GroupsError if response.key?('errors') response end end diff --git a/lib/sendgrid_toolkit/v3/mail.rb b/lib/sendgrid_toolkit/v3/mail.rb new file mode 100644 index 0000000..557ec57 --- /dev/null +++ b/lib/sendgrid_toolkit/v3/mail.rb @@ -0,0 +1,20 @@ +require 'json' + +module SendgridToolkit + module V3 + class Mail < SendgridToolkit::V3::AbstractSendgridClient + def send_mail(options = {}) + response = api_post('mail/send', convert_params(options)) + fail(SendgridToolkit::SendEmailError, response['error']) if response.key?('errors') + response + end + + private + + def convert_params(options) + options["x-smtpapi"] = options["x-smtpapi"].to_json if options.has_key?("x-smtpapi") + options + end + end + end +end diff --git a/lib/sendgrid_toolkit/v3/unsubscribes.rb b/lib/sendgrid_toolkit/v3/unsubscribes.rb index 2931734..e522245 100644 --- a/lib/sendgrid_toolkit/v3/unsubscribes.rb +++ b/lib/sendgrid_toolkit/v3/unsubscribes.rb @@ -4,23 +4,23 @@ 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 = api_post("asm/groups/#{options[:group_id]}/suppressions", options) + fail APIError if 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 = api_delete("asm/groups/#{options[:group_id]}/suppressions/#{options[:email]}", options) + fail APIError if response.key?('errors') response end def retrieve(options = {}) fail NoGroupIdSpecified unless options[:group_id] - response = api_get("groups/#{options[:group_id]}/suppressions", options) + response = api_get("asm.groups/#{options[:group_id]}/suppressions", options) response end end diff --git a/sendgrid_toolkit-1.4.0.gem b/sendgrid_toolkit-1.4.0.gem deleted file mode 100644 index 719f452..0000000 Binary files a/sendgrid_toolkit-1.4.0.gem and /dev/null differ diff --git a/sendgrid_toolkit.gemspec b/sendgrid_toolkit.gemspec index 4f0efdf..f187878 100644 --- a/sendgrid_toolkit.gemspec +++ b/sendgrid_toolkit.gemspec @@ -2,24 +2,20 @@ # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- -# stub: sendgrid_toolkit 1.4.0 ruby lib +# stub: sendgrid_toolkit 1.11.0 ruby lib Gem::Specification.new do |s| s.name = "sendgrid_toolkit" - s.version = "1.4.0" - + s.version = "1.12.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Robby Grossman"] - s.date = "2014-03-04" + s.authors = ["promoboxx people"] + s.date = Time.now.strftime("%Y-%m-%d") s.description = "A Ruby wrapper and utility library for communicating with the Sendgrid API." - s.email = "robby@freerobby.com" s.extra_rdoc_files = [ "LICENSE.txt", "README.md" ] s.files = [ - ".rvmrc", - ".travis.yml", "Gemfile", "Gemfile.lock", "LICENSE.txt", @@ -31,6 +27,7 @@ Gem::Specification.new do |s| "lib/sendgrid_toolkit/blocks.rb", "lib/sendgrid_toolkit/bounces.rb", "lib/sendgrid_toolkit/common.rb", + "lib/sendgrid_toolkit/http_parser_party.rb", "lib/sendgrid_toolkit/invalid_emails.rb", "lib/sendgrid_toolkit/mail.rb", "lib/sendgrid_toolkit/newsletter/list_emails.rb", @@ -40,7 +37,9 @@ Gem::Specification.new do |s| "lib/sendgrid_toolkit/spam_reports.rb", "lib/sendgrid_toolkit/statistics.rb", "lib/sendgrid_toolkit/unsubscribes.rb", + 'lib/sendgrid_toolkit/v3/bounces.rb', 'lib/sendgrid_toolkit/v3/groups.rb', + 'lib/sendgrid_toolkit/v3/mail.rb', 'lib/sendgrid_toolkit/v3/unsubscribes.rb', 'lib/sendgrid_toolkit/v3/abstract_sendgrid_client.rb', "sendgrid_toolkit.gemspec", @@ -60,10 +59,9 @@ Gem::Specification.new do |s| 'spec/lib/sendgrid_toolkit/v3/abstract_sendgrid_client_spec.rb', 'spec/lib/sendgrid_toolkit/v3/unsubscribes_spec.rb' ] - s.homepage = "http://github.com/freerobby/sendgrid_toolkit" s.licenses = ["MIT"] s.require_paths = ["lib"] - s.rubygems_version = "2.1.11" + s.rubygems_version = "2.3.27" s.summary = "sendgrid_toolkit = Sendgrid + Ruby" if s.respond_to? :specification_version then diff --git a/spec/helper.rb b/spec/helper.rb index 4f9070d..67bee3b 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -2,11 +2,12 @@ require 'sendgrid_toolkit' require 'rspec' require 'json' +require 'pry' FakeWeb.allow_net_connect = false REGEX_ESCAPED_BASE_URI = "api\.sendgrid\.com/api" -REGEX_ESCAPED_BASE_URI_V3 = 'api.sendgrid.com/v3/asm' +REGEX_ESCAPED_BASE_URI_V3 = "api.sendgrid.com/v3" def backup_env @env_backup = Hash.new diff --git a/spec/lib/sendgrid_toolkit/mail_spec.rb b/spec/lib/sendgrid_toolkit/mail_spec.rb index 969fb10..06fdf68 100644 --- a/spec/lib/sendgrid_toolkit/mail_spec.rb +++ b/spec/lib/sendgrid_toolkit/mail_spec.rb @@ -6,7 +6,7 @@ @obj = SendgridToolkit::Mail.new("fakeuser", "fakepass") end - describe "#send" do + describe "#send_mail" do it "raises error when sendgrid returns an error" do FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/mail\.send\.json|, :body => '{"message": "error", "errors": ["Missing destination email"]}') lambda { diff --git a/spec/lib/sendgrid_toolkit/newsletter/newsletter_sendgrid_client_spec.rb b/spec/lib/sendgrid_toolkit/newsletter/newsletter_sendgrid_client_spec.rb index 9f8c901..5b4ca8d 100644 --- a/spec/lib/sendgrid_toolkit/newsletter/newsletter_sendgrid_client_spec.rb +++ b/spec/lib/sendgrid_toolkit/newsletter/newsletter_sendgrid_client_spec.rb @@ -19,7 +19,7 @@ sendgrid_client = SendgridToolkit::NewsletterSendgridClient.new("fakeuser", "fakepass") - sendgrid_client.send(:api_post, "lists", "add", opts).should eql({ 'message' => 'success' }) + sendgrid_client.send(:api_post, "lists", "add", opts).parsed_response.should eql({ 'message' => 'success' }) end end end diff --git a/spec/lib/sendgrid_toolkit/v3/abstract_sendgrid_client_spec.rb b/spec/lib/sendgrid_toolkit/v3/abstract_sendgrid_client_spec.rb index fd057e3..5718958 100644 --- a/spec/lib/sendgrid_toolkit/v3/abstract_sendgrid_client_spec.rb +++ b/spec/lib/sendgrid_toolkit/v3/abstract_sendgrid_client_spec.rb @@ -10,32 +10,32 @@ 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") + FakeWeb.register_uri(:get, "https://#{REGEX_ESCAPED_BASE_URI_V3}/user/profile", :body => '{"error":{"code":401,"message":"Permission denied, wrong credentials"}}') + @obj = SendgridToolkit::V3::AbstractSendgridClient.new("apikey", "fakepass") lambda { - @obj.send(:api_post, "profile", "get", {}) + @obj.send(:api_get, "user/profile", {}) }.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") + FakeWeb.register_uri(:get, "https://#{REGEX_ESCAPED_BASE_URI_V3}/user/profile", :body => '{}', :status => ['500', 'Internal Server Error']) + @obj = SendgridToolkit::V3::AbstractSendgridClient.new("apikey", "fakepass") lambda { - @obj.send(:api_post, "profile", "get", {}) - }.should raise_error SendgridToolkit::AuthenticationFailed + @obj.send(:api_get, "user/profile", {}) + }.should raise_error SendgridToolkit::SendgridServerError end it "handles unexpected error responses gracefully" do - FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/profile\.get\.json|, :body => 'Html formatted error', :status => ['500', 'Internal Server Error']) - @obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass") + FakeWeb.register_uri(:get, "https://#{REGEX_ESCAPED_BASE_URI_V3}/user/profile", :body => 'Html formatted error', :status => ['500', 'Internal Server Error']) + @obj = SendgridToolkit::V3::AbstractSendgridClient.new("apikey", "fakepass") lambda { - @obj.send(:api_post, "profile", "get", {}) + @obj.send(:api_get, "user/profile", {}) }.should raise_error SendgridToolkit::SendgridServerError, /Html formatted error<\/html> - SendgridToolkit warning/ 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") + FakeWeb.register_uri(:get, "https://#{REGEX_ESCAPED_BASE_URI_V3}/stats", :body => '{"error": "error in end_date: end date is in the future"}', :status => ['400', 'Bad Request']) + @obj = SendgridToolkit::V3::AbstractSendgridClient.new("apikey", "fakepass") lambda { - @obj.send(:api_post, "stats", "get", {}) - }.should raise_error SendgridToolkit::SendgridServerError + @obj.send(:api_get, "stats", {}) + }.should raise_error SendgridToolkit::APIError end end diff --git a/spec/lib/sendgrid_toolkit/v3/bounces_spec.rb b/spec/lib/sendgrid_toolkit/v3/bounces_spec.rb new file mode 100644 index 0000000..89ebf60 --- /dev/null +++ b/spec/lib/sendgrid_toolkit/v3/bounces_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path("#{File.dirname(__FILE__)}/../../../helper") + +describe SendgridToolkit::V3::Bounces do + before do + FakeWeb.clean_registry + api_name = ENV['TEST_SMTP_USERNAME'] || 'apikey' + api_key = ENV['TEST_SMTP_PASSWORD'] || 'fakepass' + @obj = SendgridToolkit::V3::Bounces.new(api_name, api_key) + end + + describe "#retrieve" do + it "returns array of bounced emails" do + FakeWeb.register_uri(:get, "https://#{REGEX_ESCAPED_BASE_URI_V3}/suppression/bounces", query: {}) + bounces = @obj.get + end + end +end diff --git a/spec/lib/sendgrid_toolkit/v3/groups_spec.rb b/spec/lib/sendgrid_toolkit/v3/groups_spec.rb index 4010ea5..a6c2055 100644 --- a/spec/lib/sendgrid_toolkit/v3/groups_spec.rb +++ b/spec/lib/sendgrid_toolkit/v3/groups_spec.rb @@ -3,7 +3,7 @@ describe SendgridToolkit::V3::Groups do before do FakeWeb.clean_registry - api_name = ENV['TEST_SMTP_USERNAME'] || 'fakeuser' + api_name = ENV['TEST_SMTP_USERNAME'] || 'apikey' api_key = ENV['TEST_SMTP_PASSWORD'] || 'fakepass' @obj = SendgridToolkit::V3::Groups.new(api_name, api_key) end diff --git a/spec/lib/sendgrid_toolkit/v3/mail_spec.rb b/spec/lib/sendgrid_toolkit/v3/mail_spec.rb new file mode 100644 index 0000000..cb37c0d --- /dev/null +++ b/spec/lib/sendgrid_toolkit/v3/mail_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path("#{File.dirname(__FILE__)}/../../../helper") + +describe SendgridToolkit::V3::Mail do + before do + FakeWeb.clean_registry + api_name = ENV['TEST_SMTP_USERNAME'] || 'apikey' + api_key = ENV['TEST_SMTP_PASSWORD'] || 'fakepass' + @obj = SendgridToolkit::V3::Mail.new(api_name, api_key) + end + + describe "#send_mail" do + it "raises error when sendgrid returns an error" do + FakeWeb.register_uri(:post, "https://#{REGEX_ESCAPED_BASE_URI_V3}/mail/send", :body => '{"message": "error", "errors": ["Missing destination email"]}') + lambda { + response = @obj.send_mail :from => "testing@fiverr.com", :subject => "Subject", :text => "Text", "x-smtpapi" => {:category => "Testing", :to => ["elad@fiverr.com"]} + }.should raise_error SendgridToolkit::SendEmailError + end + + it "posts x-smtpapi parameters as json" do + FakeWeb.register_uri(:post, "https://#{REGEX_ESCAPED_BASE_URI_V3}/mail/send", :body => '{"message":"success"}') + xsmtpapi = {:category => "Testing", :to => ["scottb@sendgrid.com"]} + response = @obj.send_mail :to => "scottb@sendgrid.com", :from => "testing@fiverr.com", :subject => "Subject", :text => "Text", "x-smtpapi" => xsmtpapi + JSON.parse(response.request.options[:body])["x-smtpapi"].should == xsmtpapi.to_json + end + end +end diff --git a/spec/lib/sendgrid_toolkit/v3/unsubscribes_spec.rb b/spec/lib/sendgrid_toolkit/v3/unsubscribes_spec.rb index be9abc6..42aee2e 100644 --- a/spec/lib/sendgrid_toolkit/v3/unsubscribes_spec.rb +++ b/spec/lib/sendgrid_toolkit/v3/unsubscribes_spec.rb @@ -3,7 +3,7 @@ describe SendgridToolkit::V3::Unsubscribes do before do FakeWeb.clean_registry - api_name = ENV['TEST_SMTP_USERNAME'] || 'fakeuser' + api_name = ENV['TEST_SMTP_USERNAME'] || 'apikey' api_key = ENV['TEST_SMTP_PASSWORD'] || 'fakepass' @obj = SendgridToolkit::V3::Unsubscribes.new(api_name, api_key) end @@ -21,12 +21,12 @@ describe '#add' do xit 'raises no errors on success' do - FakeWeb.register_uri(:post, "https://#{REGEX_ESCAPED_BASE_URI_V3}/suppressions/global", + FakeWeb.register_uri(:post, "https://#{REGEX_ESCAPED_BASE_URI_V3}/asm/suppressions/global", body: '{"recipient_emails":[{"user@domain.com"}]}') -> { @obj.add(recipient_emails: ['user@domain.com']) }.should_not raise_error end xit 'not raises error when email already exists' do - FakeWeb.register_uri(:post, "https://#{REGEX_ESCAPED_BASE_URI_V3}/suppressions/global", + FakeWeb.register_uri(:post, "https://#{REGEX_ESCAPED_BASE_URI_V3}/asm/suppressions/global", body: '{"recipient_emails":[{"user@domain.com"}]}') -> { @obj.add(recipient_emails: ['user@domain.com']) }.should_not raise_error end