unigo-sender-simple 0.0.13

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: add8396e00e8814105cc33b799d844af62761459753fe8e530aa5fb9a49564f7
4
+ data.tar.gz: 2ce5d2ac34cf856dae85e3fedea0e8505d8e5a2f38d05e34bc7b12c68f8155bc
5
+ SHA512:
6
+ metadata.gz: 2aca13358f40cefc43fa0bf637d584b46dc15373817ddc7d61aff096e725a059bc24b65ca7e87046350262617c9be53fa1cf77670f45ec76a693cc89e7281793
7
+ data.tar.gz: 137ad9994a9ce8b2b96b7bc1b6bf5e2dbe66f0a6effe216b91ec9a8eed81853e453b81acc9e915509c9ece249801fe605184ce74243c1172d076505680150810
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
11
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
12
+ #
13
+ # If you find yourself ignoring temporary files generated by your text editor
14
+ # or operating system, you probably want to add a global ignore instead:
15
+ # git config --global core.excludesfile '~/.gitignore_global'
16
+
17
+ # Ignore bundler config.
18
+ /.bundle
19
+
20
+ #Ignore SimpleCov
21
+ /coverage/*
22
+
23
+ # Ignore the default SQLite database.
24
+ /db/*.sqlite3
25
+ /db/*.sqlite3-journal
26
+
27
+ # Ignore all logfiles and tempfiles.
28
+ /log/*
29
+ /tmp/*
30
+ !/log/.keep
31
+ !/tmp/.keep
32
+
33
+ # Ignore uploaded files in development
34
+ /storage/*
35
+ !/storage/.keep
36
+
37
+ .byebug_history
38
+
39
+ # Ignore master key for decrypting credentials and more.
40
+ /config/master.key
41
+
42
+
43
+ /.idea/*
44
+ /public/uploads/*
45
+
46
+ start.sh
47
+ /.vscode/launch.json
48
+ /client/.eslintcache
49
+ .vscode/launch.json
50
+ config/secrets.yml
51
+ rspec_results.html
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ inherit_gem:
2
+ rubocop-shopify: rubocop.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 3.1
6
+ Exclude:
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
10
+
11
+ Style/Documentation:
12
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in unigo-sender-simple.gemspec
4
+ gemspec
5
+ gem "dotenv", "~> 2.8"
6
+ gem "nokogiri", "~> 1.13"
7
+ gem "pry"
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "vcr", "~> 6.0"
11
+
12
+ gem "rubocop", "~> 1.54.1"
13
+ gem "rubocop-shopify"
14
+ gem "rubocop-performance", "~> 1.18"
15
+ gem "rubocop-rake", "~> 0.6.0"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 UniGo
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ Based on official UniGo Gem https://gitflic.ru/project/unisender/unigo-ruby but using gem faraday", "~> 2.0" to prevent conflicts
2
+
3
+ For the moment only send_email is supported from the base library
4
+
5
+ ```ruby
6
+ gem 'unigo-sender-simple'
7
+ ```
8
+ ```ruby
9
+ bundle install
10
+ ```
11
+ configuration example
12
+ ```ruby
13
+ Unigo::Sender.configure do |config|
14
+ config.host = "go1.unisender.ru"
15
+ config.api_key = Rails.application.secrets.api_key
16
+ end
17
+
18
+ ```
19
+
20
+ usage
21
+ ```ruby
22
+ message = {}
23
+
24
+ message[:subject] = @subject
25
+ message[:body] = { "html": "<h3>#{@title}</h3><div><p>#{@main_text}<br></p></div><div><p>#{@footer}</p></div>", "plaintext": @main_text } unless @template
26
+
27
+ message[:from_email] = "from@test.ru"
28
+ message[:from_name] = @from_name
29
+
30
+ if @bypass
31
+ message[:bypass_unsubscribed] = 1
32
+ message[:bypass_global] = 1
33
+ message[:bypass_unavailable] = 1
34
+ message[:skip_unsubscribe] = 1
35
+ end
36
+ message[:recipients] = []
37
+
38
+ message[:template_engine] = "simple"
39
+ message[:template_id] = @template if @template
40
+
41
+ message[:recipients] << {
42
+ email: "test@test.com",
43
+ substitutions: {
44
+ "title" => @title || "",
45
+ "main_text" => @main_text || "",
46
+ "link" => @link || "",
47
+ "link_name" => @link_name || "",
48
+ "secondary_text" => @secondary_text || "",
49
+ "footer" => @footer || "",
50
+ },
51
+ }
52
+
53
+ begin
54
+ client = Unigo::Sender::Client.new
55
+ client.send_email(message: message)
56
+ rescue Faraday::Error => err
57
+ raise err.response[:body] # raise Exception, { message: "Send error to #{@email} with subject #{@subject}" }
58
+ end
59
+ ```
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unigo
4
+ module Sender
5
+ class Client
6
+ module Email
7
+ extend Unigo::Sender::Validation::ClassMethods
8
+ include Unigo::Sender::Validation::InstanceMethods
9
+ def send_email(params = {})
10
+ params[:message][:options][:send_at] =
11
+ handle_time_param(params.dig(:message, :options, :send_at)) if params.dig(
12
+ :message, :options, :send_at
13
+ )
14
+ post("email/send.json", params)
15
+ end
16
+
17
+ add_response_validations(
18
+ :email,
19
+ [
20
+ "send_email",
21
+ ],
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unigo
4
+ module Sender
5
+ class Client
6
+ include Unigo::Sender::Connection
7
+ include Unigo::Sender::Client::Email
8
+
9
+ class BaseException < StandardError; end
10
+ class InvalidCallbackAuth < BaseException; end
11
+
12
+ class << self
13
+ def run(*args, &block)
14
+ new(*args).run(&block)
15
+ end
16
+ end
17
+
18
+ API_ENDPOINT =
19
+ "https://%{hostname}/%{lang}/transactional/api/v1/"
20
+
21
+ # * *Args* :
22
+ # - +host+ -> string, API hostname, for example 'go1.unisender.ru'
23
+ # - +api_key_in_params+ -> boolean, pass API key in parameters, otherwise pass in headers (default)
24
+ def initialize(params = {})
25
+ @hostname = params[:host] || Unigo::Sender.configuration.host
26
+ end
27
+
28
+ def run
29
+ yield self
30
+ end
31
+
32
+ private
33
+
34
+ def api_endpoint
35
+ @api_endpoint ||=
36
+ format(API_ENDPOINT, hostname: @hostname, lang: Unigo::Sender.configuration.lang)
37
+ end
38
+
39
+ def handle_time_param(param)
40
+ param.respond_to?(:strftime) ? param.strftime("%Y-%m-%d %H:%M:%S") : param
41
+ end
42
+
43
+ def handle_date_param(param)
44
+ param.respond_to?(:strftime) ? param.strftime("%Y-%m-%d") : param
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ send_email:
2
+ type: object
3
+ required:
4
+ - status
5
+ - job_id
6
+ - emails
7
+ properties:
8
+ status:
9
+ type: string
10
+ job_id:
11
+ type: string
12
+ emails:
13
+ items:
14
+ type: string
15
+ failed_emails:
16
+ type: object
17
+ subscribe_email:
18
+ type: object
19
+ required:
20
+ - status
21
+ properties:
22
+ status:
23
+ type: string
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unigo
4
+ module Sender
5
+ class Configuration
6
+ attr_accessor :adapter,
7
+ :connection_open_timeout,
8
+ :connection_timeout,
9
+ :enable_logging,
10
+ :lang,
11
+ :api_key,
12
+ :host,
13
+ :api_key_in_params
14
+
15
+ def initialize
16
+ @adapter = Faraday.default_adapter
17
+ @connection_open_timeout = 20
18
+ @connection_timeout = 20
19
+ @enable_logging = false
20
+ @lang = "ru"
21
+ @host = "go1.unisender.ru"
22
+ @api_key_in_params = false
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ require "json"
2
+ require "faraday"
3
+ require "faraday/mashify"
4
+ module Unigo
5
+ module Sender
6
+ module Connection
7
+ def get(url, params = {})
8
+ prepare_params!(params)
9
+
10
+ # Assume HTTP library receives params as Hash
11
+ request(:get, url, params)
12
+ end
13
+
14
+ def post(url, params = {})
15
+ prepare_params!(params)
16
+
17
+ # Assume HTTP library receives payload body as String
18
+ request(:post, url, JSON.dump(params))
19
+ end
20
+
21
+ private
22
+
23
+ def request(method, path, data)
24
+ @last_response = connection.send(method, path, data)
25
+ end
26
+
27
+ def connection
28
+ headers = { "Content-Type" => "application/json" }
29
+ prepare_headers!(headers)
30
+
31
+ @connection ||= Faraday.new(
32
+ url: api_endpoint,
33
+ headers: headers,
34
+ ) do |conn|
35
+ conn.response(:mashify, content_type: /\bjson$/)
36
+ conn.response(:json, content_type: /\bjson$/)
37
+ conn.response(:raise_error)
38
+ conn.adapter(Unigo::Sender.configuration.adapter)
39
+ conn.options.timeout = Unigo::Sender.configuration.connection_timeout
40
+ conn.options.open_timeout = Unigo::Sender.configuration.connection_open_timeout
41
+
42
+ if Unigo::Sender.configuration.enable_logging
43
+ conn.response(:logger, nil, { headers: true, bodies: true, errors: true })
44
+ end
45
+ end
46
+ end
47
+
48
+ def prepare_params!(params)
49
+ params.merge!({ api_key: Unigo::Sender.configuration.api_key }) if Unigo::Sender.configuration.api_key_in_params
50
+ end
51
+
52
+ def prepare_headers!(headers)
53
+ headers.merge!("X-API-KEY" => Unigo::Sender.configuration.api_key) unless Unigo::Sender.configuration.api_key_in_params
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,44 @@
1
+ module Unigo
2
+ module Sender
3
+ class Mail
4
+ attr_accessor :template, :body, :track, :from, :subject, :global_metadata, :headers, :options,
5
+ :global_substitutions, :recipients, :attachments, :inline_attachments,
6
+ :reply_to, :skip_unsubscribe, :force_send
7
+
8
+ def initialize
9
+ @template = {}
10
+ @from = {}
11
+ @track = {}
12
+ @global_substitutions = {}
13
+ @recipients = []
14
+ @attachments = []
15
+ @inline_attachments = []
16
+ end
17
+
18
+ # backward compatibility
19
+ alias_method :"metadata=", :"global_metadata="
20
+
21
+ def as_json
22
+ {
23
+ message: {
24
+ global_substitutions: self.global_substitutions,
25
+ body: self.body,
26
+ subject: self.subject,
27
+ reply_to: self.reply_to,
28
+ recipients: self.recipients,
29
+ global_metadata: self.global_metadata,
30
+ headers: self.headers,
31
+ attachments: self.attachments,
32
+ inline_attachments: self.inline_attachments,
33
+ options: self.options,
34
+ skip_unsubscribe: self.skip_unsubscribe,
35
+ force_send: self.force_send
36
+ }.merge(self.template)
37
+ .merge(self.from)
38
+ .merge(self.track)
39
+ .delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
40
+ }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,47 @@
1
+ require 'json-schema'
2
+ require 'yaml'
3
+
4
+ module Unigo
5
+ module Sender
6
+ module Validation
7
+ module ClassMethods
8
+ def add_response_validations(klass, methods)
9
+ methods.each do |method|
10
+ orig = "#{method}_without_hook"
11
+ alias_method orig, method
12
+
13
+ define_method method do |*args|
14
+ send(orig, *args)
15
+ schema = get_response_schema(klass, method)
16
+ validate_response(schema)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+ private
24
+
25
+ def validate_response(schema)
26
+ JSON::Validator.validate!(schema, @last_response.body)
27
+ @last_response
28
+ end
29
+
30
+ def get_response_schema(klass, method)
31
+ @response_schemas ||= {}
32
+ @response_schemas[klass] ||= begin
33
+ directory = File.join(File.dirname(__FILE__), 'config', 'response_schema')
34
+ filename = "#{klass}.yml"
35
+ YAML.load_file(File.join(directory, filename))
36
+ end
37
+
38
+ @response_schemas[klass][method]
39
+ end
40
+
41
+ def undescore_class_name(class_name)
42
+ class_name.gsub(/([^\^])([A-Z])/,'\1_\2').downcase
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Unigo
2
+ module Sender
3
+ VERSION = "0.0.13"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "json-schema"
5
+ require "faraday"
6
+ require "zeitwerk"
7
+
8
+ module Unigo
9
+ module Sender
10
+ class << self
11
+ attr_writer :configuration
12
+
13
+ def configuration
14
+ @configuration ||= Unigo::Sender::Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ loader = Zeitwerk::Loader.new
25
+ loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
26
+ loader.push_dir("#{__dir__}/sender", namespace: Unigo::Sender)
27
+ loader.setup
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ require_relative "lib/unigo/sender/version"
4
+ # frozen_string_literal: true
5
+ lib = File.expand_path("../lib", __FILE__)
6
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.authors = ["Alexander Tarasov"]
10
+ spec.description = "Based on official UniGo Gem https://gitflic.ru/project/unisender/unigo-ruby"
11
+ spec.metadata = { "github_repo" => "ssh://github.com/tarassov/unigo-sender-simple" }
12
+ spec.homepage = "https://github.com/tarassov/unigo-sender-simple"
13
+ spec.email = "tarassov.al@gmail.com"
14
+ spec.files = %x(git ls-files -z).split("\x0")
15
+ spec.licenses = ["MIT"]
16
+ spec.name = "unigo-sender-simple"
17
+ spec.require_paths = ["lib"]
18
+ spec.required_ruby_version = ">= 2.7"
19
+ spec.summary = "Simple UniGo sender Gem"
20
+ spec.version = Unigo::Sender::VERSION
21
+
22
+ spec.add_dependency("faraday", "~> 2.0")
23
+ spec.add_dependency("faraday-mashify", "~> 0.1")
24
+ spec.add_dependency("json", "~> 2.0")
25
+ spec.add_dependency("json-schema", "~> 2.0")
26
+ spec.add_dependency("zeitwerk", "~> 2.6")
27
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unigo-sender-simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.13
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Tarasov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-mashify
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json-schema
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: zeitwerk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.6'
83
+ description: Based on official UniGo Gem https://gitflic.ru/project/unisender/unigo-ruby
84
+ email: tarassov.al@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".rubocop.yml"
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - lib/unigo/sender.rb
95
+ - lib/unigo/sender/client.rb
96
+ - lib/unigo/sender/client/email.rb
97
+ - lib/unigo/sender/config/response_schema/email.yml
98
+ - lib/unigo/sender/configuration.rb
99
+ - lib/unigo/sender/connection.rb
100
+ - lib/unigo/sender/mail.rb
101
+ - lib/unigo/sender/validation.rb
102
+ - lib/unigo/sender/version.rb
103
+ - unigo-sender-simple.gemspec
104
+ homepage: https://github.com/tarassov/unigo-sender-simple
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ github_repo: ssh://github.com/tarassov/unigo-sender-simple
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '2.7'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.9
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Simple UniGo sender Gem
128
+ test_files: []