workarea-zendesk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +37 -0
  3. data/.github/ISSUE_TEMPLATE/documentation-request.md +17 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/workflows/ci.yml +54 -0
  6. data/.gitignore +22 -0
  7. data/.rubocop.yml +3 -0
  8. data/CHANGELOG.md +13 -0
  9. data/Gemfile +15 -0
  10. data/LICENSE +52 -0
  11. data/README.md +73 -0
  12. data/Rakefile +57 -0
  13. data/app/assets/images/zendesk/.keep +0 -0
  14. data/app/assets/javascripts/zendesk/.keep +0 -0
  15. data/app/assets/stylesheets/zendesk/.keep +0 -0
  16. data/app/controllers/.keep +0 -0
  17. data/app/helpers/.keep +0 -0
  18. data/app/lib/workarea/zendesk/bogus_gateway.rb +57 -0
  19. data/app/lib/workarea/zendesk/gateway.rb +53 -0
  20. data/app/lib/workarea/zendesk/response.rb +27 -0
  21. data/app/mailers/.keep +0 -0
  22. data/app/models/.keep +0 -0
  23. data/app/views/workarea/storefront/_zendesk_web_widget.html.haml +2 -0
  24. data/app/workers/zendesk/create_inquiry_request.rb +46 -0
  25. data/bin/rails +25 -0
  26. data/config/initializers/append_points.rb +4 -0
  27. data/config/initializers/workarea.rb +8 -0
  28. data/config/routes.rb +2 -0
  29. data/lib/workarea/zendesk.rb +38 -0
  30. data/lib/workarea/zendesk/engine.rb +10 -0
  31. data/lib/workarea/zendesk/version.rb +5 -0
  32. data/test/dummy/.ruby-version +1 -0
  33. data/test/dummy/Rakefile +6 -0
  34. data/test/dummy/app/assets/config/manifest.js +3 -0
  35. data/test/dummy/app/assets/images/.keep +0 -0
  36. data/test/dummy/app/assets/javascripts/application.js +14 -0
  37. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  38. data/test/dummy/app/controllers/application_controller.rb +2 -0
  39. data/test/dummy/app/controllers/concerns/.keep +0 -0
  40. data/test/dummy/app/helpers/application_helper.rb +2 -0
  41. data/test/dummy/app/jobs/application_job.rb +2 -0
  42. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  43. data/test/dummy/app/models/concerns/.keep +0 -0
  44. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  45. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  46. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  47. data/test/dummy/bin/bundle +3 -0
  48. data/test/dummy/bin/rails +4 -0
  49. data/test/dummy/bin/rake +4 -0
  50. data/test/dummy/bin/setup +25 -0
  51. data/test/dummy/bin/update +25 -0
  52. data/test/dummy/config.ru +5 -0
  53. data/test/dummy/config/application.rb +34 -0
  54. data/test/dummy/config/boot.rb +5 -0
  55. data/test/dummy/config/environment.rb +5 -0
  56. data/test/dummy/config/environments/development.rb +52 -0
  57. data/test/dummy/config/environments/production.rb +83 -0
  58. data/test/dummy/config/environments/test.rb +45 -0
  59. data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
  60. data/test/dummy/config/initializers/assets.rb +12 -0
  61. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/test/dummy/config/initializers/content_security_policy.rb +25 -0
  63. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  64. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  65. data/test/dummy/config/initializers/inflections.rb +16 -0
  66. data/test/dummy/config/initializers/mime_types.rb +4 -0
  67. data/test/dummy/config/initializers/workarea.rb +5 -0
  68. data/test/dummy/config/initializers/wrap_parameters.rb +9 -0
  69. data/test/dummy/config/locales/en.yml +33 -0
  70. data/test/dummy/config/puma.rb +34 -0
  71. data/test/dummy/config/routes.rb +5 -0
  72. data/test/dummy/config/spring.rb +6 -0
  73. data/test/dummy/db/seeds.rb +2 -0
  74. data/test/dummy/lib/assets/.keep +0 -0
  75. data/test/dummy/log/.keep +0 -0
  76. data/test/integration/workarea/storefront/zendesk_integration_test.rb +24 -0
  77. data/test/lib/workarea/zendesk/gateway_test.rb +64 -0
  78. data/test/support/workarea/zendesk_api_config.rb +19 -0
  79. data/test/teaspoon_env.rb +6 -0
  80. data/test/test_helper.rb +10 -0
  81. data/test/vcr_cassettes/zendesk/create_request.yml +87 -0
  82. data/test/vcr_cassettes/zendesk/create_request_error.yml +81 -0
  83. data/workarea-zendesk.gemspec +20 -0
  84. metadata +139 -0
@@ -0,0 +1,53 @@
1
+ module Workarea
2
+ module Zendesk
3
+ class Gateway
4
+ attr_reader :options
5
+
6
+ def initialize(options = {})
7
+ @options = options
8
+ end
9
+
10
+ def create_request(request)
11
+ resp = connection.post('api/v2/requests.json') do |req|
12
+ req.body = request.to_json
13
+ end
14
+
15
+ Response.new(resp)
16
+ end
17
+
18
+ private
19
+
20
+ def encoded_basic_auth
21
+ "Basic #{Base64.encode64(auth_string)}"
22
+ end
23
+
24
+ def connection
25
+ headers = { 'Content-Type' => 'application/json', 'Authorization' => encoded_basic_auth }
26
+
27
+ request_timeouts = {
28
+ timeout: Workarea.config.zendesk[:api_timeout],
29
+ open_timeout: Workarea.config.zendesk[:open_timeout]
30
+ }
31
+
32
+ conn = Faraday.new(url: rest_endpoint, headers: headers, request: request_timeouts)
33
+ conn
34
+ end
35
+
36
+ def api_user_name
37
+ options[:api_user_name]
38
+ end
39
+
40
+ def api_token
41
+ options[:api_token]
42
+ end
43
+
44
+ def auth_string
45
+ "#{api_user_name}/token:#{api_token}"
46
+ end
47
+
48
+ def rest_endpoint
49
+ options[:rest_endpoint]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,27 @@
1
+ module Workarea
2
+ module Zendesk
3
+ class Response
4
+ attr_reader :response
5
+
6
+ def initialize(response = {})
7
+ @response = response
8
+ end
9
+
10
+ def success?
11
+ @response.status.to_s =~ /^2/
12
+ end
13
+
14
+ def body
15
+ return {} unless @response.body.present?
16
+ JSON.parse(@response.body)
17
+ end
18
+
19
+ def error_details
20
+ return unless body.present?
21
+ return unless body["error"].present?
22
+
23
+ body["details"].to_s
24
+ end
25
+ end
26
+ end
27
+ end
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ - if Workarea::Zendesk.config.public_key.present?
2
+ %script#ze-snippet{src: "https://static.zdassets.com/ekr/snippet.js?key=#{Workarea::Zendesk.config.public_key}"}
@@ -0,0 +1,46 @@
1
+ module Workarea
2
+ module Zendesk
3
+ class CreateInquiryRequest
4
+ class ZendeskTicketError < StandardError; end
5
+
6
+ include Sidekiq::Worker
7
+ include Sidekiq::CallbacksWorker
8
+
9
+ sidekiq_options(
10
+ enqueue_on: { Workarea::Inquiry => [:create] }
11
+ )
12
+
13
+ def perform(id)
14
+ inquiry = Workarea::Inquiry.find(id)
15
+ response = Workarea::Zendesk.gateway.create_request(zendesk_request(inquiry))
16
+
17
+ raise ZendeskTicketError, response.error_details unless response.success?
18
+ end
19
+
20
+ private
21
+
22
+ def test_prefix
23
+ !Rails.env.production? ? "** IGNORE * TEST * IGNORE ** " : ""
24
+ end
25
+
26
+ def zendesk_request(inquiry)
27
+ {
28
+ request: {
29
+ requester: {
30
+ name: inquiry.name,
31
+ email: inquiry.email
32
+ },
33
+ subject: test_prefix + inquiry.full_subject,
34
+ comment: {
35
+ body: comment_body(inquiry)
36
+ }
37
+ }
38
+ }
39
+ end
40
+
41
+ def comment_body(inquiry)
42
+ "Name: #{inquiry.name}\nEmail: #{inquiry.email}\nSubject: #{inquiry.full_subject}\nOrder Id: #{inquiry.order_id || ""}\nMessage: #{inquiry.message}"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path('..', __dir__)
6
+ ENGINE_PATH = File.expand_path('../lib/workarea/zendesk/engine', __dir__)
7
+ APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
8
+
9
+ # Set up gems listed in the Gemfile.
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
11
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
12
+
13
+ require "rails"
14
+ # Pick the frameworks you want:
15
+ require "active_model/railtie"
16
+ require "active_job/railtie"
17
+ # require "active_record/railtie"
18
+ # require "active_storage/engine"
19
+ require "action_controller/railtie"
20
+ require "action_mailer/railtie"
21
+ require "action_view/railtie"
22
+ # require "action_cable/engine"
23
+ require "sprockets/railtie"
24
+ require "rails/test_unit/railtie"
25
+ require 'rails/engine/commands'
@@ -0,0 +1,4 @@
1
+ Workarea::Plugin.append_partials(
2
+ 'storefront.document_head',
3
+ 'workarea/storefront/zendesk_web_widget'
4
+ )
@@ -0,0 +1,8 @@
1
+ Workarea.configure do |config|
2
+ config.zendesk = ActiveSupport::Configurable::Configuration.new
3
+ config.zendesk.public_key = ""
4
+
5
+ config.zendesk.rest_endpoint = ""
6
+ config.zendesk.api_timeout = 2
7
+ config.zendesk.open_timeout = 2
8
+ end
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,38 @@
1
+ require 'workarea'
2
+ require 'workarea/storefront'
3
+ require 'workarea/admin'
4
+
5
+ require 'workarea/zendesk/engine'
6
+ require 'workarea/zendesk/version'
7
+
8
+ module Workarea
9
+ module Zendesk
10
+ def self.config
11
+ Workarea.config.zendesk
12
+ end
13
+
14
+ def self.credentials
15
+ (Rails.application.secrets.zendesk || '')
16
+ end
17
+
18
+ def self.rest_endpoint
19
+ config.rest_endpoint
20
+ end
21
+
22
+ def self.api_user_name
23
+ credentials[:api_user_name]
24
+ end
25
+
26
+ def self.api_token
27
+ credentials[:api_token]
28
+ end
29
+
30
+ def self.gateway
31
+ if credentials.present?
32
+ Zendesk::Gateway.new(rest_endpoint: rest_endpoint, api_user_name: api_user_name, api_token: api_token)
33
+ else
34
+ Zendesk::BogusGateway.new
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ require 'workarea/zendesk'
2
+
3
+ module Workarea
4
+ module Zendesk
5
+ class Engine < ::Rails::Engine
6
+ include Workarea::Plugin
7
+ isolate_namespace Workarea::Zendesk
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Workarea
2
+ module Zendesk
3
+ VERSION = "1.0.0".freeze
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ 2.4.2
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
File without changes
@@ -0,0 +1,14 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag 'application', media: 'all' %>
9
+ <%= javascript_include_tag 'application' %>
10
+ </head>
11
+
12
+ <body>
13
+ <%= yield %>
14
+ </body>
15
+ </html>
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <style>
6
+ /* Email styles need to be inline */
7
+ </style>
8
+ </head>
9
+
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../config/application', __dir__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ include FileUtils
4
+
5
+ # path to your application root.
6
+ APP_ROOT = File.expand_path('..', __dir__)
7
+
8
+ def system!(*args)
9
+ system(*args) || abort("\n== Command #{args} failed ==")
10
+ end
11
+
12
+ chdir APP_ROOT do
13
+ # This script is a starting point to setup your application.
14
+ # Add necessary setup steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ puts "\n== Removing old logs and tempfiles =="
21
+ system! 'bin/rails log:clear tmp:clear'
22
+
23
+ puts "\n== Restarting application server =="
24
+ system! 'bin/rails restart'
25
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ include FileUtils
4
+
5
+ # path to your application root.
6
+ APP_ROOT = File.expand_path('..', __dir__)
7
+
8
+ def system!(*args)
9
+ system(*args) || abort("\n== Command #{args} failed ==")
10
+ end
11
+
12
+ chdir APP_ROOT do
13
+ # This script is a way to update your development environment automatically.
14
+ # Add necessary update steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ puts "\n== Removing old logs and tempfiles =="
21
+ system! 'bin/rails log:clear tmp:clear'
22
+
23
+ puts "\n== Restarting application server =="
24
+ system! 'bin/rails restart'
25
+ end
@@ -0,0 +1,5 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require_relative 'config/environment'
4
+
5
+ run Rails.application
@@ -0,0 +1,34 @@
1
+ require_relative 'boot'
2
+
3
+ require "rails"
4
+ # Pick the frameworks you want:
5
+ require "active_model/railtie"
6
+ require "active_job/railtie"
7
+ # require "active_record/railtie"
8
+ # require "active_storage/engine"
9
+ require "action_controller/railtie"
10
+ require "action_mailer/railtie"
11
+ require "action_view/railtie"
12
+ # require "action_cable/engine"
13
+ require "sprockets/railtie"
14
+ require "rails/test_unit/railtie"
15
+
16
+ # Workarea must be required before other gems to ensure control over Rails.env
17
+ # for running tests
18
+ require 'workarea/core'
19
+ require 'workarea/admin'
20
+ require 'workarea/storefront'
21
+ Bundler.require(*Rails.groups)
22
+ require "workarea/zendesk"
23
+
24
+ module Dummy
25
+ class Application < Rails::Application
26
+ # Initialize configuration defaults for originally generated Rails version.
27
+ config.load_defaults 5.2
28
+
29
+ # Settings in config/environments/* take precedence over those specified here.
30
+ # Application configuration can go into files in config/initializers
31
+ # -- all .rb files in that directory are automatically loaded after loading
32
+ # the framework and any gems in your application.
33
+ end
34
+ end