sendgrid_template 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e0b21fbdbe882cc0628af5936a8616ff59e11cc7f00cb3279bd1100910a6a223
4
+ data.tar.gz: fb6e8825b3a936a5ae968dd1d3316aa5f76b6cb0b126a8b544a35d8b726a3086
5
+ SHA512:
6
+ metadata.gz: aabfd3654e232f8eb7fcdc026f44de91234ff39b77a116d834a39bb987ca498ee7a13c211669be9ac3139faa566d5734603b6c497e603a70e423695ed4d4b3da
7
+ data.tar.gz: 2479d81e7d00e702e79e9d5e152afb6601075c72391211e7c32bc8b923dff0d0a20f54ccc04d6ffbc11d0495a4512b4f18a0c1705128b3177251ef5ac5ff06ee
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in sendgrid_template.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Maxim Pechnikov
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SendgridTemplate
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sendgrid_template'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sendgrid_template
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/sendgrid_template/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'multi_json'
5
+ require 'sendgrid_template/version'
6
+ require 'sendgrid_template/configuration'
7
+ require 'sendgrid_template/template'
8
+
9
+ module SendgridTemplate
10
+ class << self
11
+ attr_writer :configuration
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield(configuration)
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SendgridTemplate
4
+ class Configuration
5
+ API_URL = 'https://api.sendgrid.com'
6
+ attr_accessor :api_key
7
+
8
+ def initialize(options = {})
9
+ @api_key = options[:api_key]
10
+ end
11
+
12
+ def connect
13
+ @conn ||= Faraday.new(url: API_URL) do |h|
14
+ h.headers[:content_type] = 'application/json'
15
+ h.headers['Authorization'] = "Bearer #{@api_key}"
16
+ h.adapter(Faraday.default_adapter)
17
+ end
18
+
19
+ @conn
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SendgridTemplate
4
+ Template = Struct.new(:id, :name, :versions) do
5
+ def initialize(attrs = {})
6
+ super(
7
+ attrs['id'],
8
+ attrs['name'],
9
+ attrs['versions'].map { |j| Version.new(j) }
10
+ )
11
+ end
12
+
13
+ def attributes
14
+ Hash[each_pair.to_a]
15
+ end
16
+
17
+ def attributes=(attrs)
18
+ attrs.each do |k, v|
19
+ self[k.to_sym] = v if respond_to?(k)
20
+ end
21
+ end
22
+
23
+ def save
24
+ id.nil? ? create : update
25
+ end
26
+
27
+ def update
28
+ response = SendgridTemplate.configuration.connect.patch("/v3/templates/#{id}") do |req|
29
+ req.headers[:content_type] = 'application/json'
30
+ req.body = MultiJson.dump(attributes.reject { |k, v| v.nil? || k.to_s[/id|_id\Z/] })
31
+ end
32
+
33
+ self.attributes = response.body if response.success?
34
+ end
35
+
36
+ def create
37
+ # create
38
+ response = SendgridTemplate.configuration.connect.post('/v3/templates/') do |req|
39
+ req.headers[:content_type] = 'application/json'
40
+ req.body = MultiJson.dump(attributes.reject { |k, v| v.nil? || k.to_s[/id|_id\Z/] })
41
+ end
42
+ self.attributes = response.body if response.success?
43
+ end
44
+
45
+ def delete(force = false)
46
+ if versions.empty?
47
+ SendgridTemplate.configuration.connect.delete("/v3/templates/#{id}").success?
48
+ else
49
+ return false unless force
50
+
51
+ versions.each(&:delete)
52
+ self.versions = []
53
+ delete
54
+ end
55
+ end
56
+
57
+ class << self
58
+ def all(connect: nil, opts: {})
59
+ conn = connect || SendgridTemplate.configuration.connect
60
+ req_opts = {
61
+ page_size: 200,
62
+ generations: 'legacy,dynamic'
63
+ }.merge(opts)
64
+
65
+ resp = MultiJson.load(
66
+ conn.get('/v3/templates', req_opts).body
67
+ )
68
+ resp['result'].map do |attrs|
69
+ Template.new(attrs)
70
+ end
71
+ end
72
+
73
+ def find(template_id, connect: nil)
74
+ conn = connect || SendgridTemplate.configuration.connect
75
+ response = conn.get("/v3/templates/#{template_id}")
76
+ if response.success?
77
+ Template.new(MultiJson.load(response.body))
78
+ else
79
+ # raise
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SendgridTemplate
4
+ VERSION = '0.0.5'
5
+
6
+ Version = Struct.new(:id, :template_id,
7
+ :active, :name, :subject, :updated_at,
8
+ :html_content, :plain_content, :user_id,
9
+ :generate_plain_content,
10
+ :editor,
11
+ :thumbnail_url) do
12
+ def initialize(attrs = {})
13
+ super(attrs['id'],
14
+ attrs['template_id'],
15
+ attrs['active'],
16
+ attrs['name'],
17
+ attrs['subject'],
18
+ attrs['updated_at'],
19
+ attrs['html_content'],
20
+ attrs['plain_content'],
21
+ attrs['user_id'],
22
+ attrs['generate_plain_content'],
23
+ attrs['editor'],
24
+ attrs['thumbnail_url']
25
+ )
26
+ end
27
+
28
+ def attributes
29
+ Hash[each_pair.to_a]
30
+ end
31
+
32
+ def attributes=(attrs)
33
+ attrs.each do |k, v|
34
+ self[k.to_sym] = v if respond_to?(k)
35
+ end
36
+ end
37
+
38
+ def save
39
+ if id.nil?
40
+ create
41
+ else
42
+ update
43
+ end
44
+ end
45
+
46
+ def update
47
+ # update
48
+ response = SendgridTemplate.configuration.connect.patch("/v3/templates/#{v.template_id}/versions/#{v.id}") do |req|
49
+ req.headers[:content_type] = 'application/json'
50
+ req.body = JSON.generate(v.attributes.reject { |k, v| v.nil? || k.to_s[/id|_id\Z/] })
51
+ end
52
+ self.attributes = response.body if response.success?
53
+ end
54
+
55
+ def create
56
+ # create
57
+ response = SendgridTemplate.configuration.connect.post("/v3/templates/#{template_id}/versions") do |req|
58
+ req.headers[:content_type] = 'application/json'
59
+ req.body = JSON.generate(attributes.reject { |k, v| v.nil? || k.to_s[/id|_id\Z/] })
60
+ end
61
+ self.attributes = response.body if response.success?
62
+ end
63
+
64
+ def delete
65
+ SendgridTemplate.configuration.connect.delete("/v3/templates/#{template_id}/versions/#{id}").success?
66
+ end
67
+
68
+ class << self
69
+ def find(template_id, _version_id)
70
+ response = SendgridTemplate.configuration.connect.get("/v3/templates/#{template_id}/versions/#{id}")
71
+ new(response.body) if response.success?
72
+ end
73
+
74
+ def activate(template_id, _version_id)
75
+ response = SendgridTemplate.configuration.connect.get("/v3/templates/#{template_id}/versions/#{id}/activate")
76
+ new(response.body) if response.success?
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'sendgrid_template/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'sendgrid_template'
9
+ spec.version = SendgridTemplate::VERSION
10
+ spec.authors = ['Maxim Pechnikov']
11
+ spec.email = ['parallel588@gmail.com']
12
+ spec.summary = 'Sendgrid Template'
13
+ spec.description = 'Sendgrid Template'
14
+ spec.homepage = ''
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'rake', '~> 12.3.3'
23
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
24
+ spec.add_development_dependency('vcr', '~> 2.9')
25
+ spec.add_development_dependency('webmock', '~> 1.8.0')
26
+ spec.add_dependency 'faraday', '~> 1.6.0'
27
+ spec.add_dependency 'multi_json', '~> 1.13'
28
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module SendgridTemplate
6
+ describe Configuration do
7
+ subject { Configuration.new(login: 'test', password: 'test') }
8
+ describe '#connect' do
9
+ it 'should return connect' do
10
+ expect(subject.connect.class).to eq(Faraday::Connection)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module SendgridTemplate
6
+ describe Template do
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module SendgridTemplate
6
+ describe Version do
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe SendgridTemplate do
6
+ describe '.configure' do
7
+ before do
8
+ SendgridTemplate.configure do |config|
9
+ config.login = 'test'
10
+ config.password = 'testpass'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
+ require 'sendgrid_template'
5
+ require 'vcr'
6
+
7
+ VCR.configure do |c|
8
+ c.cassette_library_dir = 'spec/cassettes'
9
+ c.hook_into :webmock
10
+ c.configure_rspec_metadata!
11
+ end
12
+
13
+ RSpec.configure do |config|
14
+ # The settings below are suggested to provide a good initial experience
15
+ # with RSpec, but feel free to customize to your heart's content.
16
+ # # These two settings work together to allow you to limit a spec run
17
+ # # to individual examples or groups you care about by tagging them with
18
+ # # `:focus` metadata. When nothing is tagged with `:focus`, all examples
19
+ # # get run.
20
+ # config.filter_run :focus
21
+ # config.run_all_when_everything_filtered = true
22
+ #
23
+ # # Many RSpec users commonly either run the entire suite or an individual
24
+ # # file, and it's useful to allow more verbose output when running an
25
+ # # individual spec file.
26
+ # if config.files_to_run.one?
27
+ # # Use the documentation formatter for detailed output,
28
+ # # unless a formatter has already been configured
29
+ # # (e.g. via a command-line flag).
30
+ # config.default_formatter = 'doc'
31
+ # end
32
+ #
33
+ # # Print the 10 slowest examples and example groups at the
34
+ # # end of the spec run, to help surface which specs are running
35
+ # # particularly slow.
36
+ # config.profile_examples = 10
37
+ #
38
+ # # Run specs in random order to surface order dependencies. If you find an
39
+ # # order dependency and want to debug it, you can fix the order by providing
40
+ # # the seed, which is printed after each run.
41
+ # # --seed 1234
42
+ # config.order = :random
43
+ #
44
+ # # Seed global randomization in this process using the `--seed` CLI option.
45
+ # # Setting this allows you to use `--seed` to deterministically reproduce
46
+ # # test failures related to randomization by passing the same `--seed` value
47
+ # # as the one that triggered the failure.
48
+ # Kernel.srand config.seed
49
+ #
50
+ # # rspec-expectations config goes here. You can use an alternate
51
+ # # assertion/expectation library such as wrong or the stdlib/minitest
52
+ # # assertions if you prefer.
53
+ # config.expect_with :rspec do |expectations|
54
+ # # Enable only the newer, non-monkey-patching expect syntax.
55
+ # # For more details, see:
56
+ # # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # expectations.syntax = :expect
58
+ # end
59
+ #
60
+ # # rspec-mocks config goes here. You can use an alternate test double
61
+ # # library (such as bogus or mocha) by changing the `mock_with` option here.
62
+ # config.mock_with :rspec do |mocks|
63
+ # # Enable only the newer, non-monkey-patching expect syntax.
64
+ # # For more details, see:
65
+ # # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
66
+ # mocks.syntax = :expect
67
+ #
68
+ # # Prevents you from mocking or stubbing a method that does not exist on
69
+ # # a real object. This is generally recommended.
70
+ # mocks.verify_partial_doubles = true
71
+ # end
72
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sendgrid_template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Pechnikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 12.3.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 12.3.3
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.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: vcr
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: multi_json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.13'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.13'
97
+ description: Sendgrid Template
98
+ email:
99
+ - parallel588@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/sendgrid_template.rb
112
+ - lib/sendgrid_template/configuration.rb
113
+ - lib/sendgrid_template/template.rb
114
+ - lib/sendgrid_template/version.rb
115
+ - sendgrid_template.gemspec
116
+ - spec/sendgrid_template/configuration_spec.rb
117
+ - spec/sendgrid_template/template_spec.rb
118
+ - spec/sendgrid_template/version_spec.rb
119
+ - spec/sendgrid_template_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: ''
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.2.15
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Sendgrid Template
144
+ test_files:
145
+ - spec/sendgrid_template/configuration_spec.rb
146
+ - spec/sendgrid_template/template_spec.rb
147
+ - spec/sendgrid_template/version_spec.rb
148
+ - spec/sendgrid_template_spec.rb
149
+ - spec/spec_helper.rb