From 4b4c9fdc2207aeb29eef5ef4a0d3e61dfdc8b7b1 Mon Sep 17 00:00:00 2001 From: Elodie Ailleaume Date: Mon, 7 Mar 2022 15:47:45 +0100 Subject: [PATCH 1/2] Add Sendgrid.api_key parameter to remove it from env. --- lib/sendgrid.rb | 20 ++++++++++++++++++++ lib/sendgrid/list.rb | 2 +- lib/sendgrid/recipient.rb | 2 +- lib/sendgrid/version.rb | 2 +- spec/spec_helper.rb | 6 +++--- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 84b4c47..1defa43 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -1,2 +1,22 @@ module Sendgrid + extend self + + def parameter(*names) + names.each do |name| + attr_accessor name + + define_method name do |*values| + value = values.first + value ? self.send("#{name}=", value) : instance_variable_get("@#{name}") + end + end + end + + def config(&block) + instance_eval &block + end end + +Sendgrid.config do + parameter :api_key +end \ No newline at end of file diff --git a/lib/sendgrid/list.rb b/lib/sendgrid/list.rb index c2d08a6..88e5936 100644 --- a/lib/sendgrid/list.rb +++ b/lib/sendgrid/list.rb @@ -3,7 +3,7 @@ class Sendgrid::List attr_accessor :name def initialize(data = {}) - @api = ::SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + @api = ::SendGrid::API.new(api_key: Sendgrid.api_key) @errors = nil reset_properties @name ||= data["name"] diff --git a/lib/sendgrid/recipient.rb b/lib/sendgrid/recipient.rb index 143d3c8..6581d26 100644 --- a/lib/sendgrid/recipient.rb +++ b/lib/sendgrid/recipient.rb @@ -3,7 +3,7 @@ class Sendgrid::Recipient attr_accessor :email, :first_name, :last_name def initialize - @api = ::SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) + @api = ::SendGrid::API.new(api_key: Sendgrid.api_key) @errors = nil reset_properties end diff --git a/lib/sendgrid/version.rb b/lib/sendgrid/version.rb index eb27598..4778ead 100644 --- a/lib/sendgrid/version.rb +++ b/lib/sendgrid/version.rb @@ -1,3 +1,3 @@ module Sendgrid - VERSION = "1.0.1" + VERSION = "1.1.0" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 67ee704..ec95271 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,7 @@ require "sendgrid-object" require 'webmock/rspec' -ENV['SENDGRID_API_KEY']="ThisIsAFakeKey" +Sendgrid.api_key = "ThisIsAFakeKey" RSpec.configure do |config| @@ -12,14 +12,14 @@ headers = { 'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Authorization'=>"Bearer #{ENV['SENDGRID_API_KEY']}", + 'Authorization'=>"Bearer #{Sendgrid.api_key}", 'Content-Type'=>'application/json', 'User-Agent'=>'sendgrid/6.6.1;ruby' } headers_without_content_type = { 'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Authorization'=>"Bearer #{ENV['SENDGRID_API_KEY']}", + 'Authorization'=>"Bearer #{Sendgrid.api_key}", 'User-Agent'=>'sendgrid/6.6.1;ruby' } From 4baa6d147c624aa0bc636ea32b70ce450e34401d Mon Sep 17 00:00:00 2001 From: Elodie Ailleaume Date: Mon, 7 Mar 2022 17:29:41 +0100 Subject: [PATCH 2/2] Add configuration part in the Readme. --- README.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6fd3caa..4fbe5e1 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,6 @@ This gem provide classes for an easy use of the [sendrig-ruby](https://github.co Ruby version >= 2.4 (except version [2.6.0](https://github.com/sendgrid/sendgrid-ruby/blob/main/TROUBLESHOOTING.md#ruby-versions)) -## Setup Environment Variables - -Update the development environment with your SENDGRID_API_KEY, for example: - -``` -echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env -echo "sendgrid.env" >> .gitignore -source ./sendgrid.env -``` - ## Installation Add this line to your application's Gemfile: @@ -32,11 +22,23 @@ Or install it yourself as: $ gem install sendgrid-object +## Configuration + +You need to configure the api key with the following code. You can add it in `/config/initializers/sendgrid.rb` for example. + +```ruby +require 'sendgrid-object' + +Sendgrid.config do + api_key 'YOUR_API_KEY_HERE' +end +``` + ## Quick start ### Find a recipient -``` +```ruby recipient = Sendgrid::Recipient.new() recipient.find_by("email", "john@doe.com") => {"id"=>"ID", "email"=>"john@doe.com", "created_at"=>TIMESTAMP, "updated_at"=>TIMESTAMP, "last_emailed"=>TIMESTAMP, "last_clicked"=>TIMESTAMP, "last_opened"=>TIMESTAMP, "first_name"=>"John", "last_name"=>"Doe"} @@ -50,7 +52,7 @@ end ### Create a recipient -``` +```ruby recipient = Sendgrid::Recipient.new() recipient.create({ email: "jane@doe.com" }) => "ID" @@ -64,7 +66,7 @@ end ### Add a recipient to an existing list -``` +```ruby recipient = Sendgrid::Recipient.new() recipient.find_by("email", "john@doe.com") list = Sendgrid::List.new() @@ -77,7 +79,7 @@ end OR -``` +```ruby recipient = Sendgrid::Recipient.new() recipient.find_by("email", "john@doe.com") list = Sendgrid::List.new() @@ -90,7 +92,7 @@ puts "User successfully added." unless list.errors ### Destroy a recipient -``` +```ruby recipient = Sendgrid::Recipient.new() recipient.find_by("email", "john@doe.com") => {"id"=>"ID", "email"=>"john@doe.com", "created_at"=>TIMESTAMP, "updated_at"=>TIMESTAMP, "last_emailed"=>TIMESTAMP, "last_clicked"=>TIMESTAMP, "last_opened"=>TIMESTAMP, "first_name"=>"John", "last_name"=>"Doe"} @@ -102,7 +104,7 @@ end OR -``` +```ruby recipient = Sendgrid::Recipient.new() recipient.destroy("ID") puts "User successfully destroyed." unless recipient.errors