kani_laser 0.1.0

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: f3d6c0af59adddc29260563de855215f25310cb4d8681926f591bc28d7827c13
4
+ data.tar.gz: 407a7a83f4f1590092ae746a92ac55a8d85b37a51285561496266165c522f880
5
+ SHA512:
6
+ metadata.gz: 6408e0ac2d077c16fd05692546a9f0030f48823bd85aba0cdfea9e6e48587d77e36d09db7431dab67e7d5d2fe82e849ad0ff8ffecac029ac00b381fc0c66a9b8
7
+ data.tar.gz: 69ed49def9327a7123b2716b3cab5a6d051e2d611ec8858f74d0517c1d7a3dca69f4e04b89e5cda270aaa6c268b366f394d986c89ed99e65fe9269995691263a
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
+ test.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - lib/kani_laser/schema.rb
5
+ - vendor/bundle/**/*
6
+ Layout/LineLength:
7
+ Max: 120
8
+ Style/Documentation:
9
+ Enabled: false
10
+ Metrics/AbcSize:
11
+ Enabled: false
12
+ Metrics/MethodLength:
13
+ Enabled: false
14
+ Metrics/ParameterLists:
15
+ Enabled: false
16
+ Lint/AssignmentInCondition:
17
+ Enabled: false
18
+ Metrics/CyclomaticComplexity:
19
+ Enabled: false
20
+ Metrics/PerceivedComplexity:
21
+ Enabled: false
22
+ Style/GuardClause:
23
+ Enabled: false
24
+ Metrics/BlockLength:
25
+ Enabled: false
26
+ Style/TrailingCommaInArrayLiteral:
27
+ EnforcedStyleForMultiline: comma
28
+ Style/TrailingCommaInHashLiteral:
29
+ EnforcedStyleForMultiline: comma
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache:
5
+ - bundler
6
+ # http://rubies.travis-ci.org/
7
+ rvm:
8
+ - 2.3.8
9
+ - 2.4.9
10
+ - 2.5.7
11
+ - 2.6.5
12
+ - 2.7.0
13
+ before_install:
14
+ - gem install bundler -v 2.0.2
15
+ script:
16
+ - bundle exec rake schema:update
17
+ - bundle exec rake
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 kani_laser.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kani_laser (0.1.0)
5
+ faraday (~> 0.17.1)
6
+ faraday_middleware
7
+ json-schema
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.7.0)
13
+ public_suffix (>= 2.0.2, < 5.0)
14
+ ast (2.4.0)
15
+ diff-lcs (1.3)
16
+ faraday (0.17.1)
17
+ multipart-post (>= 1.2, < 3)
18
+ faraday_middleware (0.13.1)
19
+ faraday (>= 0.7.4, < 1.0)
20
+ jaro_winkler (1.5.4)
21
+ json-schema (2.8.1)
22
+ addressable (>= 2.4)
23
+ mini_portile2 (2.4.0)
24
+ multipart-post (2.1.1)
25
+ nokogiri (1.10.7)
26
+ mini_portile2 (~> 2.4.0)
27
+ parallel (1.19.1)
28
+ parser (2.7.0.0)
29
+ ast (~> 2.4.0)
30
+ public_suffix (4.0.2)
31
+ rainbow (3.0.0)
32
+ rake (10.5.0)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.1)
38
+ rspec-support (~> 3.9.1)
39
+ rspec-expectations (3.9.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.1)
46
+ rubocop (0.78.0)
47
+ jaro_winkler (~> 1.5.1)
48
+ parallel (~> 1.10)
49
+ parser (>= 2.6)
50
+ rainbow (>= 2.2.2, < 4.0)
51
+ ruby-progressbar (~> 1.7)
52
+ unicode-display_width (>= 1.4.0, < 1.7)
53
+ ruby-progressbar (1.10.1)
54
+ unicode-display_width (1.6.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ bundler (~> 2.0)
61
+ kani_laser!
62
+ nokogiri
63
+ rake (~> 10.0)
64
+ rspec (~> 3.0)
65
+ rubocop
66
+
67
+ BUNDLED WITH
68
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 winebarrel
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,53 @@
1
+ # KaniLaser
2
+
3
+ [SendGrid v3 Mail Send API](https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html) Ruby Client.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/kani_laser.svg)](http://badge.fury.io/rb/kani_laser)
6
+ [![Build Status](https://travis-ci.org/winebarrel/kani_laser.svg?branch=master)](https://travis-ci.org/winebarrel/kani_laser)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'kani_laser'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install kani_laser
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require 'kani_laser'
28
+
29
+ client = KaniLaser::Client.new(api_key: 'ZAPZAPZAP')
30
+
31
+ # see https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
32
+ client.send_mail(
33
+ personalizations: [
34
+ {
35
+ to: [
36
+ {
37
+ email: 'john@example.com'
38
+ }
39
+ ],
40
+ subject: 'Hello, World!'
41
+ }
42
+ ],
43
+ from: {
44
+ email: 'from_address@example.com'
45
+ },
46
+ content: [
47
+ {
48
+ type: 'text/plain',
49
+ value: 'Hello, World!'
50
+ }
51
+ ]
52
+ )
53
+ ```
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ Dir.glob('tasks/*.rake').each do |tasks|
8
+ load tasks
9
+ end
10
+
11
+ RSpec::Core::RakeTask.new(:spec)
12
+ RuboCop::RakeTask.new
13
+
14
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'kani_laser'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,41 @@
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 'kani_laser/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'kani_laser'
9
+ spec.version = KaniLaser::VERSION
10
+ spec.authors = ['winebarrel']
11
+ spec.email = ['sugawara@winebarrel.jp']
12
+
13
+ spec.summary = 'SendGrid v3 Mail Send API Ruby Client.'
14
+ spec.description = spec.summary
15
+ spec.homepage = 'https://github.com/winebarrel/kani_laser'
16
+ spec.license = 'MIT'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/winebarrel/kani_laser'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.required_ruby_version = '>= 2.3'
31
+
32
+ spec.add_dependency 'faraday', '~> 0.17.1'
33
+ spec.add_dependency 'faraday_middleware'
34
+ spec.add_dependency 'json-schema'
35
+
36
+ spec.add_development_dependency 'bundler', '~> 2.0'
37
+ spec.add_development_dependency 'nokogiri'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'rubocop'
41
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KaniLaser
4
+ class Client
5
+ INITIALIZE_OPTIONS = %i[
6
+ api_key
7
+ debug
8
+ ].freeze
9
+
10
+ DEFAULT_ADAPTERS = [
11
+ Faraday::Adapter::NetHttp,
12
+ Faraday::Adapter::Test,
13
+ ].freeze
14
+
15
+ USER_AGENT = "KaniLaser #{KaniLaser::VERSION}"
16
+
17
+ # https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html#-Authentication
18
+ DEFAULT_API_URL = 'https://api.sendgrid.com/v3/mail/send'
19
+
20
+ def initialize(options)
21
+ unless options.is_a?(Hash)
22
+ raise ArgumentError, "wrong type argument (given: #{options}:#{options.class}, expected Hash)"
23
+ end
24
+
25
+ @options = {}
26
+
27
+ INITIALIZE_OPTIONS.each do |key|
28
+ @options[key] = options.delete(key)
29
+ end
30
+
31
+ raise ArgumentError, ':api_key is required for initialize' unless @options[:api_key]
32
+
33
+ options[:url] ||= DEFAULT_API_URL
34
+
35
+ @conn = Faraday.new(options) do |faraday|
36
+ faraday.response :raise_error
37
+ faraday.response :logger, ::Logger.new(STDOUT), bodies: true if @options[:debug]
38
+
39
+ yield(faraday) if block_given?
40
+
41
+ unless DEFAULT_ADAPTERS.any? { |i| faraday.builder.handlers.include?(i) }
42
+ faraday.adapter Faraday.default_adapter
43
+ end
44
+ end
45
+
46
+ @conn.headers[:user_agent] = USER_AGENT
47
+ end
48
+
49
+ def send_mail(body)
50
+ raise ArgumentError, "wrong type body (given: #{body}:#{body.class}, expected Hash)" unless body.is_a?(Hash)
51
+
52
+ validate!(body)
53
+
54
+ api_key = @options.fetch(:api_key)
55
+ @conn.authorization(:Bearer, api_key)
56
+
57
+ res = @conn.post do |req|
58
+ req.body = JSON.dump(body)
59
+ req.headers['Content-Type'] = 'application/json'
60
+ end
61
+
62
+ res
63
+ end
64
+
65
+ private
66
+
67
+ def validate!(body)
68
+ JSON::Validator.validate!(KaniLaser::SCHEMA, body)
69
+
70
+ attachments = body.fetch('attachments', body.fetch(:attachments, []))
71
+ subject = body.fetch('subject', body[:subject])
72
+ content = body.fetch('content ', body[:content])
73
+ personalizations = body.fetch('personalizations', body.fetch(:personalizations))
74
+
75
+ unless subject || personalizations.any? { |i| i.fetch('subject', i[:subject]) }
76
+ raise KaniLaser::Error, "Contains no subject: #{body}"
77
+ end
78
+
79
+ unless content || attachments.any? { |i| i.fetch('content', i[:content]) }
80
+ raise KaniLaser::Error, "Contains no content: #{body}"
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KaniLaser
4
+ class Error < StandardError
5
+ end
6
+ end