sendgrid-object 1.0.1

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
+ SHA256:
3
+ metadata.gz: e19a523dcdbc5c6efe82406745c1b994ee713ca154f51da85e324c3d22201db1
4
+ data.tar.gz: 14ac6f2d33bc077f99e389e81555d80655cbcedb5240528982ad317f9c0be819
5
+ SHA512:
6
+ metadata.gz: a0e2c5ed76f84a0b5433d4863a9266af68275536bf311b069543a34de9734e225421ca2b87185b79a17defac6b07a9cab8b9091898767f6eeb9d8346c49ecc70
7
+ data.tar.gz: a1f740f02b68a1b6444e132e06c54b4a1f4b7ab72f7bab372ec67f494debf953a0973ac571e04d5ddd61209f4d17e1aef345e8330948ea0fc08c6e7f8e43cb8b
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # for a library or gem, you might want to ignore these files since the code is
14
+ # intended to run in multiple environments; otherwise, check them in:
15
+ Gemfile.lock
16
+ .ruby-version
17
+ .ruby-gemset
18
+
19
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
20
+ .rvmrc
21
+
22
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.4
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at technique@immateriel.fr. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sendgrid-object.gemspec
4
+ gemspec
5
+
6
+ gem 'sendgrid-ruby', "~> 6.6.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # Sendgrid::Object
2
+
3
+ This gem provide classes for an easy use of the [sendrig-ruby](https://github.com/sendgrid/sendgrid-ruby) official gem.
4
+
5
+ ## Prerequisites
6
+
7
+ Ruby version >= 2.4 (except version [2.6.0](https://github.com/sendgrid/sendgrid-ruby/blob/main/TROUBLESHOOTING.md#ruby-versions))
8
+
9
+ ## Setup Environment Variables
10
+
11
+ Update the development environment with your SENDGRID_API_KEY, for example:
12
+
13
+ ```
14
+ echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
15
+ echo "sendgrid.env" >> .gitignore
16
+ source ./sendgrid.env
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'sendgrid-object'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle install
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install sendgrid-object
34
+
35
+ ## Quick start
36
+
37
+ ### Find a recipient
38
+
39
+ ```
40
+ recipient = Sendgrid::Recipient.new()
41
+ recipient.find_by("email", "john@doe.com")
42
+ => {"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"}
43
+ unless recipient.errors
44
+ puts recipient.id
45
+ puts recipient.email
46
+ puts recipient.first_name
47
+ puts recipient.last_name
48
+ end
49
+ ```
50
+
51
+ ### Create a recipient
52
+
53
+ ```
54
+ recipient = Sendgrid::Recipient.new()
55
+ recipient.create({ email: "jane@doe.com" })
56
+ => "ID"
57
+ unless recipient.errors
58
+ puts recipient.id
59
+ puts recipient.email
60
+ puts recipient.first_name
61
+ puts recipient.last_name
62
+ end
63
+ ```
64
+
65
+ ### Add a recipient to an existing list
66
+
67
+ ```
68
+ recipient = Sendgrid::Recipient.new()
69
+ recipient.find_by("email", "john@doe.com")
70
+ list = Sendgrid::List.new()
71
+ list.add_recipient(recipient.id, ID_OF_THE_LIST)
72
+ => nil
73
+ unless list.errors
74
+ puts "User successfully added."
75
+ end
76
+ ```
77
+
78
+ OR
79
+
80
+ ```
81
+ recipient = Sendgrid::Recipient.new()
82
+ recipient.find_by("email", "john@doe.com")
83
+ list = Sendgrid::List.new()
84
+ sendgrid_list.find(ID_OF_THE_LIST)
85
+ => {"id"=>ID_OF_THE_LIST, "name"=>"NAME", "recipient_count"=>NB}
86
+ list.add_recipient(recipient.id)
87
+ => nil
88
+ puts "User successfully added." unless list.errors
89
+ ```
90
+
91
+ ### Destroy a recipient
92
+
93
+ ```
94
+ recipient = Sendgrid::Recipient.new()
95
+ recipient.find_by("email", "john@doe.com")
96
+ => {"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"}
97
+ unless recipient.errors
98
+ recipient.destroy
99
+ puts "User successfully destroyed." unless recipient.errors
100
+ end
101
+ ```
102
+
103
+ OR
104
+
105
+ ```
106
+ recipient = Sendgrid::Recipient.new()
107
+ recipient.destroy("ID")
108
+ puts "User successfully destroyed." unless recipient.errors
109
+ ```
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/immateriel/sendgrid-object. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sendgrid-object/blob/master/CODE_OF_CONDUCT.md).
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
118
+
119
+ ## Code of Conduct
120
+
121
+ Everyone interacting in the Sendgrid::Object project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sendgrid-object/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sendgrid/object"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,52 @@
1
+ class Sendgrid::List
2
+ attr_reader :id, :recipient_count, :errors
3
+ attr_accessor :name
4
+
5
+ def initialize(data = {})
6
+ @api = ::SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
7
+ @errors = nil
8
+ reset_properties
9
+ @name ||= data["name"]
10
+ end
11
+
12
+ # find a list
13
+ # @param [Integer] id of the list
14
+ # @return [Hash, nil]
15
+ def find(id)
16
+ list = nil
17
+ response = @api.client.contactdb.lists._(id).get()
18
+ if response.status_code == "200"
19
+ list = JSON.parse(response.body)
20
+ @id = list["id"]
21
+ @name = list["name"]
22
+ @recipient_count = list["recipient_count"]
23
+ else
24
+ reset_properties
25
+ @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
26
+ end
27
+ list
28
+ end
29
+
30
+ # Add a user to a list
31
+ # @param [Integer] id of the user
32
+ # @param [Integer, nil] id of the list
33
+ # @return [void]
34
+ def add_recipient(recipient_id, list_id = nil)
35
+ if recipient_id && (list_id || @id)
36
+ list_id ||= @id
37
+ response = @api.client.contactdb.lists._(list_id).recipients._(recipient_id).post()
38
+ if response.status_code != "201"
39
+ @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
40
+ end
41
+ end
42
+ nil
43
+ end
44
+
45
+ private
46
+
47
+ def reset_properties
48
+ @id = nil
49
+ @name = nil
50
+ @recipient_count = nil
51
+ end
52
+ end
@@ -0,0 +1,90 @@
1
+ class Sendgrid::Recipient
2
+ attr_reader :id, :errors
3
+ attr_accessor :email, :first_name, :last_name
4
+
5
+ def initialize
6
+ @api = ::SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
7
+ @errors = nil
8
+ reset_properties
9
+ end
10
+
11
+ # find a single user
12
+ # @param [String] field name
13
+ # @param [String] field value
14
+ # @return [Hash, nil]
15
+ def find_by(field, value)
16
+ user = nil
17
+ data = { conditions: [{ and_or: "", field: field, value: value, operator: "eq" }] }
18
+ response = @api.client.contactdb.recipients.search.post(request_body: data)
19
+ if response.status_code == "200"
20
+ @errors = nil
21
+ user = JSON.parse(response.body)["recipients"]&.first
22
+ if user
23
+ @id = user["id"]
24
+ @email = user["email"]
25
+ @first_name = user["first_name"]
26
+ @last_name = user["last_name"]
27
+ end
28
+ else
29
+ reset_properties
30
+ @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
31
+ end
32
+ user
33
+ end
34
+
35
+ # Create a user and return the id
36
+ # @param [Hash] email
37
+ # @return [String, void] user id
38
+ def create(data)
39
+ if !data || !data.is_a?(Hash) || data[:email].nil? || data[:email].empty?
40
+ reset_properties
41
+ if data.nil? || !data.is_a?(Hash)
42
+ @errors = { status: "400", body: [{ "message" => "request body is invalid" }] }
43
+ else
44
+ @errors = { status: "400", body: [{ "message" => "Type of provided email is invalid, email must be a non empty string" }] }
45
+ end
46
+ else
47
+ return @id if @id && @email == data[:email]
48
+ self.find_by("email", data[:email])
49
+ if @id.nil?
50
+ @errors = nil
51
+ request_data = [data]
52
+ response = @api.client.contactdb.recipients.post(request_body: request_data)
53
+ if response.status_code == "201"
54
+ @id = JSON.parse(response.body)["persisted_recipients"]&.first
55
+ @email = data[:email] if data[:email]
56
+ @first_name = data[:first_name] if data[:first_name]
57
+ @last_name = data[:last_name] if data[:last_name]
58
+ else
59
+ reset_properties
60
+ @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
61
+ end
62
+ end
63
+ end
64
+ @id
65
+ end
66
+
67
+ # Destroy an existing user
68
+ # @param [Integer, nil] id of the recipient
69
+ # @return [void]
70
+ def destroy(recipient_id = nil)
71
+ recipient_id ||= @id
72
+ response = @api.client.contactdb.recipients._(recipient_id).delete()
73
+ reset_properties
74
+ if response.status_code == "204"
75
+ @errors = nil
76
+ else
77
+ @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
78
+ end
79
+ nil
80
+ end
81
+
82
+ private
83
+
84
+ def reset_properties
85
+ @id = nil
86
+ @email = nil
87
+ @first_name = nil
88
+ @last_name = nil
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module Sendgrid
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'sendgrid-ruby'
2
+
3
+ require_relative 'sendgrid'
4
+ require_relative 'sendgrid/recipient'
5
+ require_relative 'sendgrid/list'
data/lib/sendgrid.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Sendgrid
2
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'sendgrid/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sendgrid-object"
7
+ spec.version = Sendgrid::VERSION
8
+ spec.authors = ["Elodie Ailleaume"]
9
+ spec.email = ["elodie@immateriel.fr"]
10
+
11
+ spec.summary = %q{OOP use of the sendgrid API}
12
+ spec.description = %q{OOP use of the sendgrid API}
13
+ spec.homepage = "https://github.com/immateriel/sendgrid-object"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(/^(test|spec|features)/)
17
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'sendgrid-ruby', "~> 6.6.1"
24
+
25
+ spec.add_development_dependency 'rspec', "~> 3.11"
26
+ spec.add_development_dependency 'webmock', "~> 3.14.0"
27
+ end
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Sendgrid::List do
4
+
5
+ describe "initialize" do
6
+ it "loads a new list object" do
7
+ sendgrid_list = Sendgrid::List.new()
8
+ expect { sendgrid_list.api }.to raise_error(NoMethodError)
9
+ expect(sendgrid_list.id).to be_nil
10
+ expect(sendgrid_list.name).to be_nil
11
+ expect(sendgrid_list.recipient_count).to be_nil
12
+ end
13
+ end
14
+
15
+ describe "find" do
16
+ it "retrieves a list by its id" do
17
+ sendgrid_list = Sendgrid::List.new()
18
+ ret = sendgrid_list.find(rand(1..40))
19
+ expect(ret).to be_a_kind_of(Hash)
20
+ expect(sendgrid_list.id).to be_a_kind_of(Integer)
21
+ expect(sendgrid_list.name).to be_a_kind_of(String)
22
+ expect(sendgrid_list.recipient_count).to be_a_kind_of(Integer)
23
+ expect(sendgrid_list.errors).to be_nil
24
+ end
25
+
26
+ it "returns an error with a non-existent id" do
27
+ sendgrid_list = Sendgrid::List.new()
28
+ ret = sendgrid_list.find(404) # The id 404 is used to test the not found case
29
+ expect(ret).to be_nil
30
+ expect(sendgrid_list.errors[:status]).to eq("404")
31
+ expect(sendgrid_list.errors[:body]).to eq([{ "message" => "List ID does not exist" }])
32
+ expect(sendgrid_list.id).to be_nil
33
+ expect(sendgrid_list.name).to be_nil
34
+ expect(sendgrid_list.recipient_count).to be_nil
35
+ end
36
+
37
+ it "returns an error with a badly formatted id" do
38
+ sendgrid_list = Sendgrid::List.new()
39
+ ret = sendgrid_list.find("Anything")
40
+ expect(ret).to be_nil
41
+ expect(sendgrid_list.errors[:status]).to eq("400")
42
+ expect(sendgrid_list.errors[:body]).to eq([{ "message" => "invalid ID" }])
43
+ expect(sendgrid_list.id).to be_nil
44
+ expect(sendgrid_list.name).to be_nil
45
+ expect(sendgrid_list.recipient_count).to be_nil
46
+ end
47
+ end
48
+
49
+ describe "add_recipient" do
50
+ it "adds the recipient to the list with his id" do
51
+ sendgrid_recipient = Sendgrid::Recipient.new()
52
+ sendgrid_recipient.find_by("email", "john@doe.com")
53
+ sendgrid_list = Sendgrid::List.new()
54
+ ret = sendgrid_list.add_recipient(sendgrid_recipient.id, 12846200)
55
+ expect(ret).to be_nil
56
+ expect(sendgrid_list.errors).to be_nil
57
+ expect(sendgrid_list.id).to be_nil
58
+ end
59
+
60
+ it "returns an error with a non-existent list id" do
61
+ recipient = Sendgrid::Recipient.new()
62
+ recipient.find_by("email", "john@doe.com")
63
+
64
+ sendgrid_list = Sendgrid::List.new()
65
+ ret = sendgrid_list.add_recipient(recipient.id, 404)
66
+ expect(ret).to be_nil
67
+ expect(sendgrid_list.errors[:status]).to eq("404")
68
+ expect(sendgrid_list.errors[:body]).to eq([{ "message" => "List ID does not exist" }])
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,149 @@
1
+ RSpec.describe Sendgrid::Recipient do
2
+ describe "initialize" do
3
+ it "loads a new recipient object" do
4
+ sendgrid_recipient = Sendgrid::Recipient.new()
5
+ expect { sendgrid_recipient.api }.to raise_error(NoMethodError)
6
+ expect(sendgrid_recipient.email).to be_nil
7
+ end
8
+ end
9
+
10
+ describe "find_by" do
11
+ it "returns the recipient" do
12
+ sendgrid_recipient = Sendgrid::Recipient.new()
13
+ ret = sendgrid_recipient.find_by("email", "john@doe.com")
14
+ expect(ret).to be_a_kind_of(Hash)
15
+ expect(ret["email"]).to eq("john@doe.com")
16
+ expect(sendgrid_recipient.id).to be_a_kind_of(String)
17
+ expect(sendgrid_recipient.email).to be_a_kind_of(String)
18
+ expect(sendgrid_recipient.email).to eq("john@doe.com")
19
+ expect(sendgrid_recipient.first_name).to be_a_kind_of(String)
20
+ expect(sendgrid_recipient.last_name).to be_a_kind_of(String)
21
+ expect(sendgrid_recipient.errors).to be_nil
22
+ end
23
+
24
+ it "returns an error for a non-existent field searched" do
25
+ sendgrid_recipient = Sendgrid::Recipient.new()
26
+ ret = sendgrid_recipient.find_by("name", "Doe")
27
+ expect(ret).to be_nil
28
+ expect(sendgrid_recipient.errors[:status]).to eq("400")
29
+ expect(sendgrid_recipient.errors[:body]).to eq([{ "message" => "Field does not exist" }])
30
+ expect(sendgrid_recipient.id).to be_nil
31
+ expect(sendgrid_recipient.email).to be_nil
32
+ expect(sendgrid_recipient.first_name).to be_nil
33
+ expect(sendgrid_recipient.last_name).to be_nil
34
+ end
35
+
36
+ it "returns an empty result for a not found user" do
37
+ sendgrid_recipient = Sendgrid::Recipient.new()
38
+ ret = sendgrid_recipient.find_by("email", "jane@doe.com")
39
+ expect(ret).to be_nil
40
+ expect(sendgrid_recipient.errors).to be_nil
41
+ expect(sendgrid_recipient.id).to be_nil
42
+ expect(sendgrid_recipient.email).to be_nil
43
+ expect(sendgrid_recipient.first_name).to be_nil
44
+ expect(sendgrid_recipient.last_name).to be_nil
45
+ end
46
+
47
+ it "returns an empty result for a not found user with badly formatted email" do
48
+ sendgrid_recipient = Sendgrid::Recipient.new()
49
+ ret = sendgrid_recipient.find_by("email", "jane.doe.com")
50
+ expect(ret).to be_nil
51
+ expect(sendgrid_recipient.errors).to be_nil
52
+ expect(sendgrid_recipient.id).to be_nil
53
+ expect(sendgrid_recipient.email).to be_nil
54
+ expect(sendgrid_recipient.first_name).to be_nil
55
+ expect(sendgrid_recipient.last_name).to be_nil
56
+ end
57
+ end
58
+
59
+ describe "create" do
60
+ it "creates a user with only an email" do
61
+ sendgrid_recipient = Sendgrid::Recipient.new()
62
+ ret = sendgrid_recipient.create({ email: "jane@doe.com" })
63
+ expect(ret).to be_a_kind_of(String)
64
+ expect(sendgrid_recipient.id).to be_a_kind_of(String)
65
+ expect(sendgrid_recipient.email).to eq("jane@doe.com")
66
+ expect(sendgrid_recipient.first_name).to be_nil
67
+ expect(sendgrid_recipient.last_name).to be_nil
68
+ expect(sendgrid_recipient.errors).to be_nil
69
+ end
70
+
71
+ it "returns an error with nil data" do
72
+ sendgrid_recipient = Sendgrid::Recipient.new()
73
+ ret = sendgrid_recipient.create(nil)
74
+ expect(ret).to be_nil
75
+ expect(sendgrid_recipient.id).to be_nil
76
+ expect(sendgrid_recipient.email).to be_nil
77
+ expect(sendgrid_recipient.first_name).to be_nil
78
+ expect(sendgrid_recipient.last_name).to be_nil
79
+ expect(sendgrid_recipient.errors).to be_a_kind_of(Hash)
80
+ expect(sendgrid_recipient.errors[:status]).to eq("400")
81
+ expect(sendgrid_recipient.errors[:body]).to eq([{ "message" => "request body is invalid" }])
82
+ end
83
+
84
+ it "returns an error without an email" do
85
+ sendgrid_recipient = Sendgrid::Recipient.new()
86
+ ret = sendgrid_recipient.create({ name: "Jane Doe" })
87
+ expect(ret).to be_nil
88
+ expect(sendgrid_recipient.id).to be_nil
89
+ expect(sendgrid_recipient.email).to be_nil
90
+ expect(sendgrid_recipient.first_name).to be_nil
91
+ expect(sendgrid_recipient.last_name).to be_nil
92
+ expect(sendgrid_recipient.errors).to be_a_kind_of(Hash)
93
+ expect(sendgrid_recipient.errors[:status]).to eq("400")
94
+ expect(sendgrid_recipient.errors[:body]).to eq([{ "message" => "Type of provided email is invalid, email must be a non empty string" }])
95
+ end
96
+ end
97
+
98
+ describe "destroy" do
99
+ it "deletes the user" do
100
+ sendgrid_recipient = Sendgrid::Recipient.new()
101
+ sendgrid_recipient.find_by("email", "john@doe.com")
102
+ ret = sendgrid_recipient.destroy
103
+ expect(ret).to be_nil
104
+ expect(sendgrid_recipient.errors).to be_nil
105
+ expect(sendgrid_recipient.id).to be_nil
106
+ expect(sendgrid_recipient.email).to be_nil
107
+ expect(sendgrid_recipient.first_name).to be_nil
108
+ expect(sendgrid_recipient.last_name).to be_nil
109
+ end
110
+
111
+ it "does not returns an error with a non-existent user" do
112
+ sendgrid_recipient = Sendgrid::Recipient.new()
113
+ ret = sendgrid_recipient.destroy("Anything")
114
+ expect(ret).to be_nil
115
+ expect(sendgrid_recipient.errors).to be_nil
116
+ expect(sendgrid_recipient.id).to be_nil
117
+ expect(sendgrid_recipient.email).to be_nil
118
+ expect(sendgrid_recipient.first_name).to be_nil
119
+ expect(sendgrid_recipient.last_name).to be_nil
120
+ end
121
+
122
+ it "returns an error with an invalid id" do
123
+ sendgrid_recipient = Sendgrid::Recipient.new()
124
+ ret = sendgrid_recipient.destroy(400)
125
+ expect(ret).to be_nil
126
+ expect(sendgrid_recipient.errors).to be_a_kind_of(Hash)
127
+ expect(sendgrid_recipient.errors[:status]).to eq("400")
128
+ expect(sendgrid_recipient.errors[:body]).to eq([{ "message" => "invalid ID" }])
129
+ expect(sendgrid_recipient.id).to be_nil
130
+ expect(sendgrid_recipient.email).to be_nil
131
+ expect(sendgrid_recipient.first_name).to be_nil
132
+ expect(sendgrid_recipient.last_name).to be_nil
133
+ end
134
+
135
+ it "returns an error with a nil id" do
136
+ sendgrid_recipient = Sendgrid::Recipient.new()
137
+ # sendgrid_recipient.find_by("email", "jane@doe.com")
138
+ ret = sendgrid_recipient.destroy
139
+ expect(ret).to be_nil
140
+ expect(sendgrid_recipient.errors).to be_a_kind_of(Hash)
141
+ expect(sendgrid_recipient.errors[:status]).to eq("400")
142
+ expect(sendgrid_recipient.errors[:body]).to eq([{ "message" => "request body is invalid" }])
143
+ expect(sendgrid_recipient.id).to be_nil
144
+ expect(sendgrid_recipient.email).to be_nil
145
+ expect(sendgrid_recipient.first_name).to be_nil
146
+ expect(sendgrid_recipient.last_name).to be_nil
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,126 @@
1
+ require "bundler/setup"
2
+ require "sendgrid-object"
3
+ require 'webmock/rspec'
4
+
5
+ ENV['SENDGRID_API_KEY']="ThisIsAFakeKey"
6
+
7
+ RSpec.configure do |config|
8
+
9
+ # Used to stub the API calls
10
+ config.before(:each) do
11
+
12
+ headers = {
13
+ 'Accept'=>'application/json',
14
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
15
+ 'Authorization'=>"Bearer #{ENV['SENDGRID_API_KEY']}",
16
+ 'Content-Type'=>'application/json',
17
+ 'User-Agent'=>'sendgrid/6.6.1;ruby'
18
+ }
19
+ headers_without_content_type = {
20
+ 'Accept'=>'application/json',
21
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
22
+ 'Authorization'=>"Bearer #{ENV['SENDGRID_API_KEY']}",
23
+ 'User-Agent'=>'sendgrid/6.6.1;ruby'
24
+ }
25
+
26
+ # Sendgrid::List
27
+ list_found_response = {
28
+ id: rand(1..40),
29
+ name: "Anything",
30
+ recipient_count: rand(1..40)
31
+ }
32
+ list_not_found_response = {
33
+ errors: [ message: "List ID does not exist" ]
34
+ }
35
+ invalid_id_response = {
36
+ errors: [ message: "invalid ID" ]
37
+ }
38
+ stub_request(:get, /https:\/\/api.sendgrid.com\/v\d\/contactdb\/lists\/(?!404$)\d+$/).
39
+ to_return(status: 200, body: list_found_response.to_json, headers: {})
40
+
41
+ stub_request(:get, /https:\/\/api.sendgrid.com\/v\d\/contactdb\/lists\/404$/).
42
+ to_return(status: 404, body: list_not_found_response.to_json, headers: {})
43
+
44
+ stub_request(:get, /https:\/\/api.sendgrid.com\/v\d\/contactdb\/lists\/(?!404$)\D+$/).
45
+ to_return(status: 400, body: invalid_id_response.to_json, headers: {})
46
+
47
+ # Sendgrid::Recipient
48
+ recipient_found_response = {
49
+ recipient_count: 1,
50
+ recipients: [
51
+ { id: "Anything",
52
+ email: "john@doe.com",
53
+ first_name: "John",
54
+ last_name: "Doe" }
55
+ ]
56
+ }
57
+ recipient_not_found_response = {
58
+ recipient_count: 0,
59
+ recipients: []
60
+ }
61
+ not_existing_field_response = {
62
+ errors: [ message: "Field does not exist" ]
63
+ }
64
+ request_body_invalid_response = {
65
+ errors: [ message: "request body is invalid" ]
66
+ }
67
+ created_recipient_response = { new_count: 1, updated_count: 0, error_count: 0, error_indices: [],
68
+ unmodified_indices: [], persisted_recipients: ["Anything"], errors: []}
69
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/recipients/search").
70
+ with(
71
+ body: "{\"conditions\":[{\"and_or\":\"\",\"field\":\"email\",\"value\":\"john@doe.com\",\"operator\":\"eq\"}]}",
72
+ headers: headers).
73
+ to_return(status: 200, body: recipient_found_response.to_json, headers: {})
74
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/recipients/search").
75
+ with(
76
+ body: "{\"conditions\":[{\"and_or\":\"\",\"field\":\"name\",\"value\":\"Doe\",\"operator\":\"eq\"}]}",
77
+ headers: headers).
78
+ to_return(status: 400, body: not_existing_field_response.to_json, headers: {})
79
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/recipients/search").
80
+ with(
81
+ body: "{\"conditions\":[{\"and_or\":\"\",\"field\":\"email\",\"value\":\"jane@doe.com\",\"operator\":\"eq\"}]}",
82
+ headers: headers).
83
+ to_return(status: 200, body: recipient_not_found_response.to_json, headers: {})
84
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/recipients/search").
85
+ with(
86
+ body: "{\"conditions\":[{\"and_or\":\"\",\"field\":\"email\",\"value\":\"jane.doe.com\",\"operator\":\"eq\"}]}",
87
+ headers: headers).
88
+ to_return(status: 200, body: recipient_not_found_response.to_json, headers: {})
89
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/recipients").
90
+ with(
91
+ body: "[{\"email\":\"jane@doe.com\"}]",
92
+ headers: headers).
93
+ to_return(status: 201, body: created_recipient_response.to_json, headers: {})
94
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/lists/12846200/recipients/Anything").
95
+ with(
96
+ headers: headers_without_content_type).
97
+ to_return(status: 201, body: "".to_json, headers: {})
98
+
99
+ stub_request(:post, "https://api.sendgrid.com/v3/contactdb/lists/404/recipients/Anything").
100
+ with(
101
+ headers: headers_without_content_type).
102
+ to_return(status: 404, body: list_not_found_response.to_json, headers: {})
103
+ stub_request(:delete, "https://api.sendgrid.com/v3/contactdb/recipients/Anything").
104
+ with(
105
+ headers: headers_without_content_type).
106
+ to_return(status: 204, body: nil.to_json, headers: {})
107
+ stub_request(:delete, "https://api.sendgrid.com/v3/contactdb/recipients/400").
108
+ with(
109
+ headers: headers_without_content_type).
110
+ to_return(status: 400, body: invalid_id_response.to_json, headers: {})
111
+ stub_request(:delete, "https://api.sendgrid.com/v3/contactdb/recipients").
112
+ with(
113
+ headers: headers_without_content_type).
114
+ to_return(status: 400, body: request_body_invalid_response.to_json, headers: {})
115
+ end
116
+
117
+ # Enable flags like --only-failures and --next-failure
118
+ config.example_status_persistence_file_path = ".rspec_status"
119
+
120
+ # Disable RSpec exposing methods globally on `Module` and `main`
121
+ config.disable_monkey_patching!
122
+
123
+ config.expect_with :rspec do |c|
124
+ c.syntax = :expect
125
+ end
126
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sendgrid-object
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Elodie Ailleaume
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sendgrid-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.14.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.14.0
55
+ description: OOP use of the sendgrid API
56
+ email:
57
+ - elodie@immateriel.fr
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/sendgrid-object.rb
73
+ - lib/sendgrid.rb
74
+ - lib/sendgrid/list.rb
75
+ - lib/sendgrid/recipient.rb
76
+ - lib/sendgrid/version.rb
77
+ - sendgrid-object.gemspec
78
+ - spec/sendgrid/list_spec.rb
79
+ - spec/sendgrid/recipent_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/immateriel/sendgrid-object
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ homepage_uri: https://github.com/immateriel/sendgrid-object
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.4.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.9
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: OOP use of the sendgrid API
105
+ test_files:
106
+ - spec/sendgrid/list_spec.rb
107
+ - spec/sendgrid/recipent_spec.rb
108
+ - spec/spec_helper.rb