mautic 1.0.5 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d61af0eb918446c6edf125b168fd769dca21e6bf
4
- data.tar.gz: 6e3707b229934a703d7065feaf6596c71e0a500c
2
+ SHA256:
3
+ metadata.gz: 1f4e005c1b32fba6f6609b90752ef110cb2f1a2cbf8379ec01ebb8afc02120f6
4
+ data.tar.gz: 864ad4d3796f591f1b0280bcd9d7ca3c9ff989fa7e094b8dcddf32d3b1b1be7f
5
5
  SHA512:
6
- metadata.gz: 0da6bcb99a484a7a4a2d2ecbb17a563a06f04687e44faf9f596f55bbad4e9dbe92029284f90ec6d60e1005cb117e6da8251e76afac1f5394ce92bc2d4f35a28e
7
- data.tar.gz: 92d3a2903427d32d74a15fb523b60ffab5ef4563119ddf0405ec7d5c567102bb7947eeda4b7c1d77258c183280eb308eca512b88f2e482eec84a6ad515050d93
6
+ metadata.gz: 2dc666eb9370ed8a8de3e0896180c73bb57979192f2534a6950fc5edcc35283eff3ff187ee2138d189f60f85b9872abecccb990ddb3701d6e46a9e47dfd4b0d2
7
+ data.tar.gz: 6ddd8b536df58ab14e21c6cff3407ef0a768737db7bce81fb3751036c455802ebd6b1831293e23a038e6597225c91c514dd158b0eb449f0d771837c22765b049
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Mautic rails
2
2
  RoR helper / wrapper for Mautic API and forms
3
3
 
4
+ *Rails 4.2.8+, 5.1+ compatible*
4
5
  ## Usage
5
6
  ### Gem provides API connection to your Mautic(s)
6
7
  1. Create mautic connection
@@ -60,12 +61,23 @@ There are two options of usage:
60
61
  m.data = {} # hash of attributes
61
62
  m.push # push data to mautic
62
63
  ```
64
+
65
+ ### Webhook receiver
66
+ Receive webhook from mautic, parse it and prepare for use.
67
+
68
+ 1. add concern to your controller
69
+
70
+ include Mautic::ReceiveWebHooks
71
+ 2. in routes must be specify `:mautic_id`, for example:
72
+
73
+ post "webhook/:mautic_id", action: "webhook", on: :collection
74
+
63
75
 
64
76
  ## Installation
65
77
  Add this line to your application's Gemfile:
66
78
 
67
79
  ```ruby
68
- gem 'mautic'
80
+ gem 'mautic', '~>0.1'
69
81
  ```
70
82
 
71
83
  And then execute:
@@ -85,6 +97,7 @@ add to `config/initializers/mautic.rb`:
85
97
  Mautic.configure do |config|
86
98
  # This is for oauth handshake token url. I need to know where your app listen
87
99
  config.base_url = "https://my-rails-app.com"
100
+ # OR it can be Proc
88
101
  # *optional* This is your default mautic URL - used in form helper
89
102
  config.mautic_url = "https://mautic.my.app"
90
103
  end
@@ -10,5 +10,5 @@
10
10
  // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
11
  // about supported directives.
12
12
  //
13
- //= require rails-ujs
13
+ // require rails-ujs
14
14
  //= require_tree .
@@ -0,0 +1,118 @@
1
+ module Mautic
2
+ module ConnectionsControllerConcern
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ before_action :set_mautic_connection, only: [:show, :edit, :update, :destroy, :oauth2, :authorize]
8
+
9
+ end
10
+
11
+
12
+ # GET /mautic_connections
13
+ def index
14
+ @mautic_connections = Mautic::Connection.order(:url)
15
+ respond_to do |format|
16
+ format.html { render layout: !request.xhr? }
17
+ format.json { render json: @mautic_connections }
18
+ end
19
+ end
20
+
21
+ # GET /mautic_connections/1
22
+ def show
23
+ respond_to do |format|
24
+ format.html { render layout: !request.xhr? }
25
+ end
26
+ end
27
+
28
+ # GET /mautic_connections/new
29
+ def new
30
+ @mautic_connection = Mautic::Connection.new
31
+ respond_to do |format|
32
+ format.html { render layout: !request.xhr? }
33
+ end
34
+ end
35
+
36
+ # GET /mautic_connections/1/edit
37
+ def edit
38
+ respond_to do |format|
39
+ format.html { render layout: !request.xhr? }
40
+ end
41
+ end
42
+
43
+ # POST /mautic_connections
44
+ def create
45
+ @mautic_connection = Mautic::Connection.new(mautic_connection_params)
46
+
47
+ respond_to do |format|
48
+ if @mautic_connection.save
49
+ format.html { render(:edit, notice: t('mautic.text_mautic_connection_created')) }
50
+ format.js { head :no_content }
51
+ format.json { render json: @mautic_connection }
52
+ else
53
+ format.html { render :new }
54
+ format.js { head :unprocessable_entity }
55
+ format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
56
+ end
57
+ end
58
+ end
59
+
60
+ # PATCH/PUT /mautic_connections/1
61
+ def update
62
+ respond_to do |format|
63
+ if @mautic_connection.update(mautic_connection_params)
64
+ format.html { redirect_to({ action: :index }, notice: t('mautic.text_mautic_connection_updated')) }
65
+ format.js { head :no_content }
66
+ format.json { render json: @mautic_connection }
67
+ else
68
+ format.html { render :edit }
69
+ format.js { head :unprocessable_entity }
70
+ format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
71
+ end
72
+ end
73
+ end
74
+
75
+ # DELETE /mautic_connections/1
76
+ def destroy
77
+ @mautic_connection.destroy
78
+ respond_to do |format|
79
+ format.html { redirect_to action: "index", notice: t('mautic.text_mautic_connection_destroyed') }
80
+ format.js { render js: "document.getElementById('#{view_context.dom_id(@mautic_connection)}').remove()" }
81
+ format.json { render json: @mautic_connection }
82
+ end
83
+ end
84
+
85
+ # ==--==--==--==--
86
+
87
+ def authorize
88
+ redirect_to @mautic_connection.authorize
89
+ end
90
+
91
+ def oauth2
92
+ begin
93
+ response = @mautic_connection.get_code(params.require(:code))
94
+ @mautic_connection.update(token: response.token, refresh_token: response.refresh_token)
95
+ return render plain: t('mautic.text_mautic_authorize_successfully')
96
+ rescue OAuth2::Error => e
97
+ flash[:error] = e.message
98
+ end
99
+
100
+ render :show
101
+ end
102
+
103
+ private
104
+
105
+ # Use callbacks to share common setup or constraints between actions.
106
+ def set_mautic_connection
107
+ @mautic_connection = Mautic::Connection.find(params[:id])
108
+ rescue ActiveRecord::RecordNotFound => e
109
+ return render head: 404, plain: e.message
110
+ end
111
+
112
+ # Only allow a trusted parameter "white list" through.
113
+ def mautic_connection_params
114
+ params.require(:connection).permit(:url, :client_id, :secret, :type)
115
+ end
116
+
117
+ end
118
+ end
@@ -0,0 +1,16 @@
1
+ module Mautic
2
+ module ReceiveWebHooks
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ end
8
+
9
+ protected
10
+
11
+ # params path need +:mautic_id+
12
+ def webhook
13
+ @webhook ||= Mautic::Connection.receive_webhook params
14
+ end
15
+ end
16
+ end
@@ -1,109 +1,5 @@
1
1
  module Mautic
2
2
  class ConnectionsController < ApplicationController
3
- before_action :set_mautic_connection, only: [:show, :edit, :update, :destroy, :oauth2, :authorize]
4
-
5
- # GET /mautic_connections
6
- def index
7
- @mautic_connections = Connection.order(:url)
8
- respond_to do |format|
9
- format.html { render layout: !request.xhr? }
10
- format.json { render json: @mautic_connections }
11
- end
12
- end
13
-
14
- # GET /mautic_connections/1
15
- def show
16
- respond_to do |format|
17
- format.html { render layout: !request.xhr? }
18
- end
19
- end
20
-
21
- # GET /mautic_connections/new
22
- def new
23
- @mautic_connection = Connection.new
24
- respond_to do |format|
25
- format.html { render layout: !request.xhr? }
26
- end
27
- end
28
-
29
- # GET /mautic_connections/1/edit
30
- def edit
31
- respond_to do |format|
32
- format.html { render layout: !request.xhr? }
33
- end
34
- end
35
-
36
- # POST /mautic_connections
37
- def create
38
- @mautic_connection = Connection.new(mautic_connection_params)
39
-
40
- respond_to do |format|
41
- if @mautic_connection.save
42
- format.html { redirect_to mautic.connection_path(@mautic_connection), notice: t('mautic.text_mautic_connection_created') }
43
- format.js { head :no_content }
44
- format.json { render json: @mautic_connection }
45
- else
46
- format.html { render :new }
47
- format.js { head :unprocessable_entity }
48
- format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
49
- end
50
- end
51
- end
52
-
53
- # PATCH/PUT /mautic_connections/1
54
- def update
55
- respond_to do |format|
56
- if @mautic_connection.update(mautic_connection_params)
57
- format.html { redirect_to mautic.connection_path(@mautic_connection), notice: t('mautic.text_mautic_connection_updated') }
58
- format.js { head :no_content }
59
- format.json { render json: @mautic_connection }
60
- else
61
- format.html { render :edit }
62
- format.js { head :unprocessable_entity }
63
- format.json { render json: @mautic_connection.errors, status: :unprocessable_entity }
64
- end
65
- end
66
- end
67
-
68
- # DELETE /mautic_connections/1
69
- def destroy
70
- @mautic_connection.destroy
71
- respond_to do |format|
72
- format.html { redirect_to :connections, notice: t('mautic.text_mautic_connection_destroyed') }
73
- format.js { render js: "document.getElementById('#{view_context.dom_id(@mautic_connection)}').remove()" }
74
- format.json { render json: @mautic_connection }
75
- end
76
- end
77
-
78
- # ==--==--==--==--
79
-
80
- def authorize
81
- redirect_to @mautic_connection.authorize
82
- end
83
-
84
- def oauth2
85
- begin
86
- response = @mautic_connection.get_code(params.require(:code))
87
- @mautic_connection.update(token: response.token, refresh_token: response.refresh_token)
88
- return render plain: t('mautic.text_mautic_authorize_successfully')
89
- rescue OAuth2::Error => e
90
- flash[:error] = e.message
91
- end
92
-
93
- render :show
94
- end
95
-
96
- private
97
- # Use callbacks to share common setup or constraints between actions.
98
- def set_mautic_connection
99
- @mautic_connection = Connection.find(params[:id])
100
- rescue ActiveRecord::RecordNotFound => e
101
- return render head: 404, plain: e.message
102
- end
103
-
104
- # Only allow a trusted parameter "white list" through.
105
- def mautic_connection_params
106
- params.require(:connection).permit(:url, :client_id, :secret, :type)
107
- end
3
+ include ::Mautic::ConnectionsControllerConcern
108
4
  end
109
5
  end
@@ -3,11 +3,16 @@ module Mautic
3
3
 
4
4
  self.table_name = 'mautic_connections'
5
5
 
6
- validates :url, :client_id, :secret, presence: true
7
- validates :url, format: URI::regexp(%w(http https))
6
+ validates :url, presence: true, format: URI::regexp(%w(http https))
7
+ validates :client_id, :secret, presence: true, unless: :new_record?
8
8
 
9
9
  alias_attribute :access_token, :token
10
10
 
11
+ # @param [ActionController::Parameters] params
12
+ def self.receive_webhook(params)
13
+ WebHook.new(find(params.require(:mautic_id)), params)
14
+ end
15
+
11
16
  def client
12
17
  raise NotImplementedError
13
18
  end
@@ -46,6 +51,14 @@ module Mautic
46
51
 
47
52
  private
48
53
 
54
+ def callback_url
55
+ if (conf = Mautic.config.base_url).is_a?(Proc)
56
+ conf = conf.call(self)
57
+ end
58
+
59
+ URI.parse(conf)
60
+ end
61
+
49
62
  def parse_response(response)
50
63
  case response.status
51
64
  when 400
@@ -38,8 +38,7 @@ module Mautic
38
38
  private
39
39
 
40
40
  def callback_url
41
- # return Mautic.config.oauth2_callback_url if Mautic.config.oauth2_callback_url
42
- uri = URI.parse(Mautic.config.base_url)
41
+ uri = super
43
42
  uri.path = Mautic::Engine.routes.url_helpers.oauth2_connection_path(self)
44
43
  uri.to_s
45
44
  end
@@ -0,0 +1,18 @@
1
+ module Mautic
2
+ # Represent received web hook
3
+ class WebHook
4
+
5
+ attr_reader :connection
6
+ # @param [Mautic::Connection] connection
7
+ # @param [ActionController::Parameters] params
8
+ def initialize(connection, params)
9
+ @connection = connection
10
+ @params = params
11
+ end
12
+
13
+ def form_submissions
14
+ @forms ||= Array.wrap(@params.require("mautic.form_on_submit")).collect { |data| ::Mautic::Submissions::Form.new(@connection, data["submission"]) if data["submission"] }.compact
15
+ end
16
+
17
+ end
18
+ end
@@ -1,4 +1,5 @@
1
- <%= form_with(model: mautic_connection.becomes(Mautic::Connection), local: true) do |form| %>
1
+ <%#= form_with(model: mautic_connection.becomes(Mautic::Connection), local: true) do |form| %>
2
+ <%= form_for(mautic_connection.becomes(Mautic::Connection)) do |form| %>
2
3
  <% if mautic_connection.errors.any? %>
3
4
  <div id="error_explanation">
4
5
  <h2><%= pluralize(mautic_connection.errors.count, "error") %> prohibited this mautic_connection from being saved:</h2>
@@ -5,5 +5,6 @@ Mautic::Engine.routes.draw do
5
5
  get :oauth2
6
6
 
7
7
  end
8
+ # post "webhook/:mautic_id", action: "webhook", on: :collection
8
9
  end
9
10
  end
@@ -1,4 +1,4 @@
1
- class CreateMauticMauticConnections < ActiveRecord::Migration[5.1]
1
+ class CreateMauticMauticConnections < Mautic::DummyMigrationClass
2
2
  def change
3
3
  create_table :mautic_connections do |t|
4
4
  t.string :type
@@ -10,7 +10,7 @@ class CreateMauticMauticConnections < ActiveRecord::Migration[5.1]
10
10
  t.string :token
11
11
  t.string :refresh_token
12
12
 
13
- t.timestamps
13
+ t.timestamps null: false
14
14
  end
15
15
  end
16
- end
16
+ end
@@ -7,6 +7,7 @@ module Mautic
7
7
  autoload :FormHelper, 'mautic/form_helper'
8
8
  autoload :Proxy, 'mautic/proxy'
9
9
  autoload :Model, 'mautic/model'
10
+ autoload :Submissions, 'mautic/submissions'
10
11
 
11
12
  class RequestError < StandardError
12
13
 
@@ -58,4 +59,12 @@ module Mautic
58
59
  end
59
60
  # Your code goes here...
60
61
 
62
+ if Rails.version.start_with? "4"
63
+ class DummyMigrationClass < ActiveRecord::Migration
64
+ end
65
+ else
66
+ class DummyMigrationClass < ActiveRecord::Migration[4.2]
67
+ end
68
+ end
69
+
61
70
  end
@@ -1,4 +1,6 @@
1
1
  module Mautic
2
+ # Virtual model for Mautic endpoint
3
+ # @see https://developer.mautic.org/#endpoints
2
4
  class Model < OpenStruct
3
5
 
4
6
  class MauticHash < Hash
@@ -35,6 +37,9 @@ module Mautic
35
37
 
36
38
  end
37
39
 
40
+ attr_reader :connection
41
+
42
+ # @param [Mautic::Connection] connection
38
43
  def initialize(connection, hash=nil)
39
44
  @connection = connection
40
45
  @table = MauticHash.new
@@ -43,6 +48,10 @@ module Mautic
43
48
  clear_changes
44
49
  end
45
50
 
51
+ def mautic_id
52
+ "#{id}/#{@connection.id}"
53
+ end
54
+
46
55
  def save(force = false)
47
56
  id.present? ? update(force) : create
48
57
  end
@@ -62,7 +71,7 @@ module Mautic
62
71
 
63
72
  def create
64
73
  begin
65
- json = @connection.request(:post, "api/#{endpoint}/#{id}/new", { body: to_h })
74
+ json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", { body: to_h })
66
75
  self.attributes = json[endpoint.singularize]
67
76
  clear_changes
68
77
  rescue ValidationError => e
@@ -0,0 +1,4 @@
1
+ require File.expand_path('../../../spec/spec_helper', __FILE__)
2
+ Dir.glob(File.expand_path('../../../spec/{factories,support}/*.rb', __FILE__)).each do |file|
3
+ require file
4
+ end
@@ -0,0 +1,5 @@
1
+ module Mautic
2
+ module Submissions
3
+ autoload :Form, "mautic/submissions/form"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ module Mautic
2
+ module Submissions
3
+ class Form
4
+ attr_reader :id
5
+
6
+ def initialize(connection, data)
7
+ @connection = connection
8
+ @raw = data
9
+ @id = data["id"]
10
+ end
11
+
12
+ def form_id
13
+ @raw["form"]["id"]
14
+ end
15
+
16
+ def contact_id
17
+ @raw["lead"]["id"]
18
+ end
19
+
20
+ def form
21
+ @form ||= @connection.forms.new(@raw["form"].merge("fields" => @raw["results"]))
22
+ end
23
+
24
+ def contact
25
+ @connection.contacts.new(@raw["lead"])
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '1.0.5'
2
+ VERSION = '2.1.1'
3
3
  end
@@ -8,6 +8,10 @@ require 'pry-rails'
8
8
 
9
9
  ActiveRecord::Migration.maintain_test_schema!
10
10
 
11
+ Dir.glob(File.expand_path('../support/*.rb', __FILE__)).each do |file|
12
+ require file
13
+ end
14
+
11
15
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
16
  RSpec.configure do |config|
13
17
  # config.infer_spec_type_from_file_location!
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mautic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-28 00:00:00.000000000 Z
11
+ date: 2018-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.1'
19
+ version: 4.2.8
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.1'
26
+ version: 4.2.8
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: oauth2
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.6'
75
+ version: '3.7'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.6'
82
+ version: '3.7'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: factory_bot_rails
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: '3.1'
131
+ version: 3.4.2
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: '3.1'
138
+ version: 3.4.2
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: pry-rails
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -166,6 +166,8 @@ files:
166
166
  - app/assets/stylesheets/mautic/application.css
167
167
  - app/assets/stylesheets/mautic/mautic_connections.css
168
168
  - app/assets/stylesheets/scaffold.css
169
+ - app/controllers/concerns/mautic/connections_controller_concern.rb
170
+ - app/controllers/concerns/mautic/receive_web_hooks.rb
169
171
  - app/controllers/mautic/application_controller.rb
170
172
  - app/controllers/mautic/connections_controller.rb
171
173
  - app/helpers/mautic/application_helper.rb
@@ -176,6 +178,7 @@ files:
176
178
  - app/models/mautic/connections/oauth2.rb
177
179
  - app/models/mautic/contact.rb
178
180
  - app/models/mautic/form.rb
181
+ - app/models/mautic/web_hook.rb
179
182
  - app/views/layouts/mautic/application.html.erb
180
183
  - app/views/mautic/connections/_form.html.erb
181
184
  - app/views/mautic/connections/edit.html.erb
@@ -190,6 +193,9 @@ files:
190
193
  - lib/mautic/form_helper.rb
191
194
  - lib/mautic/model.rb
192
195
  - lib/mautic/proxy.rb
196
+ - lib/mautic/spec_helper.rb
197
+ - lib/mautic/submissions.rb
198
+ - lib/mautic/submissions/form.rb
193
199
  - lib/mautic/version.rb
194
200
  - lib/tasks/mautic_tasks.rake
195
201
  - spec/rails_helper.rb
@@ -214,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
220
  version: '0'
215
221
  requirements: []
216
222
  rubyforge_project:
217
- rubygems_version: 2.6.13
223
+ rubygems_version: 2.7.6
218
224
  signing_key:
219
225
  specification_version: 4
220
226
  summary: Ruby on Rails Mautic integration