mautic 2.3.1 → 2.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +71 -49
- data/app/models/mautic/connection.rb +21 -18
- data/app/models/mautic/contact.rb +98 -3
- data/app/models/mautic/form.rb +1 -1
- data/app/views/mautic/connections/_form.html.erb +19 -13
- data/app/views/mautic/connections/edit.html.erb +0 -1
- data/lib/mautic.rb +19 -4
- data/lib/mautic/engine.rb +3 -1
- data/lib/mautic/model.rb +44 -21
- data/lib/mautic/proxy.rb +22 -15
- data/lib/mautic/version.rb +1 -1
- data/spec/rails_helper.rb +3 -1
- metadata +17 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a1c181282f3dfa4350d619d42c0abdea2b310d2ba85b2f8ca06e875e5eece65
|
4
|
+
data.tar.gz: 5f9921e8982d447ff37493791b88d0a00a37cdc6d52058c10734e23ed55eaef4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4640a5a580ec8deb680a05eac9d558bb99d5685e84282e3d41d3edf38b4d3143a484cf3fd58f0d0fd7ac20c42a97f62144d03820540e4ad45dd9ffaf9e3b99a1
|
7
|
+
data.tar.gz: 4a12224e16bb2cd5adbf64538b81f17914699dacc84ddf57d9ba10e95b5e69c17e5672b00bfc8fe4e4d41501946d808a364c92eb3ea50269fb8ce78f2f26b9f8
|
data/README.md
CHANGED
@@ -1,19 +1,66 @@
|
|
1
|
-
# Mautic rails
|
1
|
+
# Mautic rails
|
2
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/c8cd605b5e021fb841d1/maintainability)](https://codeclimate.com/github/luk4s/mautic-rails/maintainability)
|
3
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/c8cd605b5e021fb841d1/test_coverage)](https://codeclimate.com/github/luk4s/mautic-rails/test_coverage)
|
4
|
+
|
2
5
|
RoR helper / wrapper for Mautic API and forms
|
3
6
|
|
4
7
|
*Rails 4.2.8+, 5.1+ compatible*
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'mautic', '~> 2.3'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
```bash
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
Also you need migrate database:
|
21
|
+
```bash
|
22
|
+
$ rails db:migrate
|
23
|
+
```
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
add to `config/initializers/mautic.rb`:
|
28
|
+
```ruby
|
29
|
+
Mautic.configure do |config|
|
30
|
+
# This is for oauth handshake token url. I need to know where your app listen
|
31
|
+
config.base_url = "https://my-rails-app.com"
|
32
|
+
# OR it can be Proc
|
33
|
+
# *optional* This is your default mautic URL - used in form helper
|
34
|
+
config.mautic_url = "https://mautic.my.app"
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
add to `config/routes.rb`
|
39
|
+
```ruby
|
40
|
+
mount Mautic::Engine => "/mautic"
|
41
|
+
```
|
42
|
+
### Create mautic connection
|
16
43
|
|
44
|
+
1. In your mautic, create new
|
45
|
+
2. Got to `/your-mount-point/connections`
|
46
|
+
3. Create new connection - enter URL to your mautic
|
47
|
+
4. Copy `callback url` then go to you mautic
|
48
|
+
|
49
|
+
> In mautic you need add API oauth2 login.
|
50
|
+
|
51
|
+
> ```
|
52
|
+
> http://localhost:3000/mautic/connections/:ID/oauth2
|
53
|
+
> ```
|
54
|
+
> ID = is your Mautic::Connection ID
|
55
|
+
|
56
|
+
5. Create new **Oauth2** API connections. Use `callback url` from previous step and copy `key` and `secret` to form in your app
|
57
|
+
6. Update and use `Authorize` button for handshake
|
58
|
+
|
59
|
+
> For example of integration check https://github.com/luk4s/redmine_mautic
|
60
|
+
|
61
|
+
|
62
|
+
## Usage
|
63
|
+
|
17
64
|
Find connection which you want to use:
|
18
65
|
```ruby
|
19
66
|
m = Mautic::Connection.last
|
@@ -41,6 +88,18 @@ RoR helper / wrapper for Mautic API and forms
|
|
41
88
|
contact.save # => false
|
42
89
|
contact.errors # => [{"code"=>400, "message"=>"email: This field is required.", "details"=>{"email"=>["This field is required."]}}]
|
43
90
|
```
|
91
|
+
Do not contact
|
92
|
+
```ruby
|
93
|
+
contact.do_not_contact? # => false
|
94
|
+
contact.do_not_contact! message: "Some reason"
|
95
|
+
contact.do_not_contact? # => true
|
96
|
+
```
|
97
|
+
Remove do not contact
|
98
|
+
```ruby
|
99
|
+
contact.do_not_contact? # => true
|
100
|
+
contact.remove_do_not_contact!
|
101
|
+
contact.do_not_contact? # => false
|
102
|
+
```
|
44
103
|
Of course you can use more than contact: `assets`, `emails`, `companies`, `forms`, `points` ...
|
45
104
|
### Gem provides simple Mautic form submit
|
46
105
|
There are two options of usage:
|
@@ -72,43 +131,6 @@ Receive webhook from mautic, parse it and prepare for use.
|
|
72
131
|
|
73
132
|
post "webhook/:mautic_id", action: "webhook", on: :collection
|
74
133
|
|
75
|
-
|
76
|
-
## Installation
|
77
|
-
Add this line to your application's Gemfile:
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
gem 'mautic', '~>0.1'
|
81
|
-
```
|
82
|
-
|
83
|
-
And then execute:
|
84
|
-
```bash
|
85
|
-
$ bundle
|
86
|
-
```
|
87
|
-
|
88
|
-
Or install it yourself as:
|
89
|
-
```bash
|
90
|
-
$ gem install mautic
|
91
|
-
```
|
92
|
-
|
93
|
-
## Configuration
|
94
|
-
|
95
|
-
add to `config/initializers/mautic.rb`:
|
96
|
-
```ruby
|
97
|
-
Mautic.configure do |config|
|
98
|
-
# This is for oauth handshake token url. I need to know where your app listen
|
99
|
-
config.base_url = "https://my-rails-app.com"
|
100
|
-
# OR it can be Proc
|
101
|
-
# *optional* This is your default mautic URL - used in form helper
|
102
|
-
config.mautic_url = "https://mautic.my.app"
|
103
|
-
end
|
104
|
-
```
|
105
|
-
|
106
|
-
add to `config/routes.rb`
|
107
|
-
```ruby
|
108
|
-
mount Mautic::Engine => "/mautic"
|
109
|
-
|
110
|
-
```
|
111
|
-
|
112
134
|
## Contributing
|
113
135
|
Ideas and pull requests are welcome!
|
114
136
|
|
@@ -66,34 +66,37 @@ module Mautic
|
|
66
66
|
def parse_response(response)
|
67
67
|
case response.status
|
68
68
|
when 400
|
69
|
-
raise Mautic::ValidationError
|
69
|
+
raise Mautic::ValidationError, response
|
70
70
|
when 401
|
71
|
-
|
71
|
+
try_to_refresh_and_parse(response)
|
72
72
|
when 404
|
73
|
-
raise Mautic::RecordNotFound
|
73
|
+
raise Mautic::RecordNotFound, response
|
74
74
|
when 200, 201
|
75
|
-
|
76
|
-
Array(json['errors']).each do |error|
|
77
|
-
case error['code'].to_i
|
78
|
-
when 401
|
79
|
-
json = try_to_refresh_and_parse(response)
|
80
|
-
when 404
|
81
|
-
raise Mautic::RecordNotFound.new(response)
|
82
|
-
else
|
83
|
-
raise Mautic::RequestError.new(response)
|
84
|
-
end
|
85
|
-
end
|
75
|
+
handle_success_response response
|
86
76
|
else
|
87
|
-
raise Mautic::RequestError
|
77
|
+
raise Mautic::RequestError, response
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def handle_success_response(response)
|
82
|
+
json = JSON.parse(response.body) rescue {}
|
83
|
+
Array(json['errors']).each do |error|
|
84
|
+
case error['code'].to_i
|
85
|
+
when 401
|
86
|
+
json = try_to_refresh_and_parse(response)
|
87
|
+
when 404
|
88
|
+
raise Mautic::RecordNotFound, response
|
89
|
+
else
|
90
|
+
raise Mautic::RequestError, response
|
91
|
+
end
|
88
92
|
end
|
89
93
|
|
90
94
|
json
|
91
95
|
end
|
92
96
|
|
93
|
-
private
|
94
|
-
|
95
97
|
def try_to_refresh_and_parse(response)
|
96
|
-
raise Mautic::TokenExpiredError
|
98
|
+
raise Mautic::TokenExpiredError, response if @try_to_refresh
|
99
|
+
|
97
100
|
@try_to_refresh = true
|
98
101
|
refresh!
|
99
102
|
request(*@last_request)
|
@@ -3,6 +3,7 @@ module Mautic
|
|
3
3
|
|
4
4
|
alias_attribute :first_name, :firstname
|
5
5
|
alias_attribute :last_name, :lastname
|
6
|
+
|
6
7
|
def self.in(connection)
|
7
8
|
Proxy.new(connection, endpoint, default_params: { search: '!is:anonymous' })
|
8
9
|
end
|
@@ -11,16 +12,110 @@ module Mautic
|
|
11
12
|
"#{firstname} #{lastname}"
|
12
13
|
end
|
13
14
|
|
15
|
+
# @param [Hash] hash
|
16
|
+
# option hash [Integer] :id
|
17
|
+
# option hash [String] :firstName
|
18
|
+
# option hash [String] :lastName
|
19
|
+
def owner=(hash)
|
20
|
+
raise ArgumentError, "must be a hash !" unless hash.is_a?(Hash)
|
21
|
+
|
22
|
+
@table[:owner] = hash["id"]
|
23
|
+
@owner = hash
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Hash]
|
27
|
+
# @example {id: 12, firstName: "Joe", lastName: "Doe"}
|
28
|
+
def owner
|
29
|
+
@owner || {}
|
30
|
+
end
|
31
|
+
|
32
|
+
# Assign mautic User ID as owner - for example for update author of contact
|
33
|
+
# @see https://developer.mautic.org/#edit-contact set owner
|
34
|
+
# @param [Integer] int
|
35
|
+
def owner_id=(int)
|
36
|
+
@table[:owner] = int
|
37
|
+
end
|
38
|
+
|
14
39
|
def assign_attributes(source = nil)
|
15
40
|
super
|
16
|
-
|
17
|
-
|
18
|
-
|
41
|
+
|
42
|
+
if source
|
43
|
+
self.owner = source['owner'] || {}
|
44
|
+
self.attributes = {
|
45
|
+
tags: (source['tags'] || []).collect { |t| Mautic::Tag.new(@connection, t) }.sort_by(&:name),
|
46
|
+
doNotContact: source['doNotContact'] || [],
|
47
|
+
owner: owner['id'],
|
48
|
+
}
|
49
|
+
end
|
19
50
|
end
|
20
51
|
|
21
52
|
def events
|
22
53
|
@proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
|
23
54
|
end
|
24
55
|
|
56
|
+
# @!group Do Not Contact
|
57
|
+
# @see https://developer.mautic.org/#add-do-not-contact
|
58
|
+
|
59
|
+
def do_not_contact?
|
60
|
+
doNotContact.present?
|
61
|
+
end
|
62
|
+
|
63
|
+
alias dnc? do_not_contact?
|
64
|
+
|
65
|
+
# @return [Array[Hash]]
|
66
|
+
def do_not_contact
|
67
|
+
return unless do_not_contact?
|
68
|
+
|
69
|
+
# Based on mautic docs => Contacts constants: Contacts::UNSUBSCRIBED (1), Contacts::BOUNCED (2), Contacts::MANUAL (3)
|
70
|
+
reason_list = { 1 => :unsubscribed, 2 => :bounced, 3 => :manual }
|
71
|
+
@do_not_contact ||= doNotContact.collect do |hsh|
|
72
|
+
{ reason_list[hsh["reason"]] => hsh["comments"] }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def bounced?
|
77
|
+
do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:bounced) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def unsubscribed?
|
81
|
+
do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:unsubscribed) }
|
82
|
+
end
|
83
|
+
|
84
|
+
def do_not_contact!(comments: '')
|
85
|
+
begin
|
86
|
+
json = @connection.request(:post, "api/contacts/#{id}/dnc/email/add", body: { comments: comments })
|
87
|
+
self.attributes = { doNotContact: json[endpoint.singularize]["doNotContact"] }
|
88
|
+
clear_changes
|
89
|
+
rescue ValidationError => e
|
90
|
+
self.errors = e.errors
|
91
|
+
end
|
92
|
+
|
93
|
+
self.errors.blank?
|
94
|
+
end
|
95
|
+
|
96
|
+
alias add_dnc do_not_contact!
|
97
|
+
|
98
|
+
def remove_do_not_contact!
|
99
|
+
begin
|
100
|
+
json = @connection.request(:post, "api/contacts/#{id}/dnc/email/remove", body: {})
|
101
|
+
self.attributes = { doNotContact: json[endpoint.singularize]["doNotContact"] }
|
102
|
+
clear_changes
|
103
|
+
rescue ValidationError => e
|
104
|
+
self.errors = e.errors
|
105
|
+
end
|
106
|
+
|
107
|
+
self.errors.blank?
|
108
|
+
end
|
109
|
+
|
110
|
+
alias remove_dnc remove_do_not_contact!
|
111
|
+
|
112
|
+
# !endgroup
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def clear_change
|
117
|
+
super
|
118
|
+
remove_instance_variable :@do_not_contact
|
119
|
+
end
|
25
120
|
end
|
26
121
|
end
|
data/app/models/mautic/form.rb
CHANGED
@@ -2,7 +2,7 @@ module Mautic
|
|
2
2
|
class Form < Model
|
3
3
|
|
4
4
|
def assign_attributes(source = nil)
|
5
|
-
self.attributes = {name: source['name'], fields: source['fields']} if source.is_a? Hash
|
5
|
+
self.attributes = { name: source['name'], fields: source['fields'] } if source.is_a? Hash
|
6
6
|
end
|
7
7
|
|
8
8
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
<%#= form_with(model: mautic_connection.becomes(Mautic::Connection), local: true) do |form| %>
|
2
1
|
<%= form_for(mautic_connection.becomes(Mautic::Connection)) do |form| %>
|
3
2
|
<% if mautic_connection.errors.any? %>
|
4
3
|
<div id="error_explanation">
|
5
|
-
<h2><%= pluralize(mautic_connection.errors.count, "error") %> prohibited this mautic_connection from being
|
4
|
+
<h2><%= pluralize(mautic_connection.errors.count, "error") %> prohibited this mautic_connection from being
|
5
|
+
saved:</h2>
|
6
6
|
|
7
7
|
<ul>
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
<% mautic_connection.errors.full_messages.each do |message| %>
|
9
|
+
<li><%= message %></li>
|
10
|
+
<% end %>
|
11
11
|
</ul>
|
12
12
|
</div>
|
13
13
|
<% end %>
|
@@ -21,17 +21,23 @@
|
|
21
21
|
<%= form.label :url %>
|
22
22
|
<%= form.text_field :url, id: :mautic_connection_url %>
|
23
23
|
</div>
|
24
|
+
<% if form.object.persisted? %>
|
25
|
+
<div class="field">
|
26
|
+
<label>Now generate pair of tokens with this callback url:</label>
|
27
|
+
<pre><%= @mautic_connection.send :callback_url %></pre>
|
28
|
+
</div>
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
<div class="field">
|
31
|
+
<%= form.label :client_id %>
|
32
|
+
<%= form.text_field :client_id, id: :mautic_connection_client_id %>
|
33
|
+
</div>
|
29
34
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
<div class="field">
|
36
|
+
<%= form.label :secret %>
|
37
|
+
<%= form.text_field :secret, id: :mautic_connection_secret %>
|
38
|
+
</div>
|
34
39
|
|
40
|
+
<% end %>
|
35
41
|
<div class="actions">
|
36
42
|
<%= form.submit %>
|
37
43
|
</div>
|
data/lib/mautic.rb
CHANGED
@@ -11,12 +11,23 @@ module Mautic
|
|
11
11
|
|
12
12
|
class RequestError < StandardError
|
13
13
|
|
14
|
-
attr_reader :response, :errors
|
14
|
+
attr_reader :response, :errors, :request_url
|
15
15
|
|
16
16
|
def initialize(response, message = nil)
|
17
17
|
@errors ||= []
|
18
18
|
@response = response
|
19
|
-
|
19
|
+
@request_url = response.response&.env&.url
|
20
|
+
body = if response.body.start_with? "<!DOCTYPE html>"
|
21
|
+
response.body.split("\n").last
|
22
|
+
else
|
23
|
+
response.body
|
24
|
+
end
|
25
|
+
|
26
|
+
json_body = begin
|
27
|
+
JSON.parse(body)
|
28
|
+
rescue JSON::ParserError
|
29
|
+
{ "errors" => [{ "code" => response.status, "message" => body }] }
|
30
|
+
end
|
20
31
|
message ||= Array(json_body['errors']).collect do |error|
|
21
32
|
msg = error['code'].to_s
|
22
33
|
msg << " (#{error['type']}):" if error['type']
|
@@ -25,7 +36,7 @@ module Mautic
|
|
25
36
|
msg
|
26
37
|
end.join(', ')
|
27
38
|
|
28
|
-
super(message)
|
39
|
+
super("#{@request_url} => #{message}")
|
29
40
|
end
|
30
41
|
|
31
42
|
end
|
@@ -37,7 +48,11 @@ module Mautic
|
|
37
48
|
|
38
49
|
def initialize(response, message = nil)
|
39
50
|
@response = response
|
40
|
-
json_body =
|
51
|
+
json_body = begin
|
52
|
+
JSON.parse(response.body)
|
53
|
+
rescue ParseError
|
54
|
+
{}
|
55
|
+
end
|
41
56
|
@errors = Array(json_body['errors']).inject({}) { |mem, var| mem.merge!(var['details']); mem }
|
42
57
|
message ||= @errors.collect { |field, msg| "#{field}: #{msg.join(', ')}" }.join('; ')
|
43
58
|
super(response, message)
|
data/lib/mautic/engine.rb
CHANGED
@@ -5,7 +5,8 @@ module Mautic
|
|
5
5
|
config.generators do |g|
|
6
6
|
g.test_framework :rspec, fixture: false
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
|
+
# :nocov:
|
9
10
|
initializer :append_migrations do |app|
|
10
11
|
unless app.root.to_s.match root.to_s
|
11
12
|
config.paths['db/migrate'].expanded.each do |expanded_path|
|
@@ -13,6 +14,7 @@ module Mautic
|
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
17
|
+
# :nocov:
|
16
18
|
|
17
19
|
end
|
18
20
|
end
|
data/lib/mautic/model.rb
CHANGED
@@ -38,9 +38,10 @@ module Mautic
|
|
38
38
|
end
|
39
39
|
|
40
40
|
attr_reader :connection
|
41
|
+
attr_accessor :errors
|
41
42
|
|
42
43
|
# @param [Mautic::Connection] connection
|
43
|
-
def initialize(connection, hash=nil)
|
44
|
+
def initialize(connection, hash = nil)
|
44
45
|
@connection = connection
|
45
46
|
@table = MauticHash.new
|
46
47
|
self.attributes = { id: hash['id'], created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
|
@@ -58,9 +59,10 @@ module Mautic
|
|
58
59
|
|
59
60
|
def update(force = false)
|
60
61
|
return false if changes.blank?
|
62
|
+
|
61
63
|
begin
|
62
|
-
json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit",
|
63
|
-
|
64
|
+
json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
|
65
|
+
assign_attributes json[endpoint.singularize]
|
64
66
|
clear_changes
|
65
67
|
rescue ValidationError => e
|
66
68
|
self.errors = e.errors
|
@@ -69,10 +71,18 @@ module Mautic
|
|
69
71
|
self.errors.blank?
|
70
72
|
end
|
71
73
|
|
74
|
+
def update_columns(attributes = {})
|
75
|
+
json = @connection.request(:patch, "api/#{endpoint}/#{id}/edit", body: to_mautic(attributes))
|
76
|
+
assign_attributes json[endpoint.singularize]
|
77
|
+
clear_changes
|
78
|
+
rescue ValidationError => e
|
79
|
+
self.errors = e.errors
|
80
|
+
end
|
81
|
+
|
72
82
|
def create
|
73
83
|
begin
|
74
|
-
json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new",
|
75
|
-
|
84
|
+
json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", body: to_mautic)
|
85
|
+
assign_attributes json[endpoint.singularize]
|
76
86
|
clear_changes
|
77
87
|
rescue ValidationError => e
|
78
88
|
self.errors = e.errors
|
@@ -106,6 +116,12 @@ module Mautic
|
|
106
116
|
end
|
107
117
|
end
|
108
118
|
|
119
|
+
def to_mautic(data = @table)
|
120
|
+
data.each_with_object({}) do |(key, val), mem|
|
121
|
+
mem[key] = val.is_a?(Array) ? val.join("|") : val
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
109
125
|
private
|
110
126
|
|
111
127
|
def clear_changes
|
@@ -119,27 +135,34 @@ module Mautic
|
|
119
135
|
def assign_attributes(source = nil)
|
120
136
|
@mautic_attributes = []
|
121
137
|
source ||= {}
|
138
|
+
|
139
|
+
data = if (fields = source['fields'])
|
140
|
+
attributes_from_fields(fields)
|
141
|
+
elsif source
|
142
|
+
source
|
143
|
+
end
|
144
|
+
self.attributes = data
|
145
|
+
end
|
146
|
+
|
147
|
+
def attributes_from_fields(fields)
|
122
148
|
data = {}
|
149
|
+
if fields['all']
|
150
|
+
@mautic_attributes = fields['all'].collect do |key, value|
|
151
|
+
data[key] = value
|
152
|
+
Attribute.new(alias: key, value: value)
|
153
|
+
end
|
154
|
+
else
|
155
|
+
fields.each do |_group, pairs|
|
156
|
+
next unless pairs.is_a?(Hash)
|
123
157
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
data[key] = value
|
128
|
-
Attribute.new(alias: key, value: value)
|
129
|
-
end
|
130
|
-
else
|
131
|
-
fields.each do |_group, pairs|
|
132
|
-
next unless pairs.is_a?(Hash)
|
133
|
-
pairs.each do |key, attrs|
|
134
|
-
@mautic_attributes << (a = Attribute.new(attrs))
|
135
|
-
data[key] = a.value
|
136
|
-
end
|
158
|
+
pairs.each do |key, attrs|
|
159
|
+
@mautic_attributes << (a = Attribute.new(attrs))
|
160
|
+
data[key] = a.value
|
137
161
|
end
|
138
162
|
end
|
139
|
-
elsif source
|
140
|
-
data = source
|
141
163
|
end
|
142
|
-
|
164
|
+
|
165
|
+
data
|
143
166
|
end
|
144
167
|
|
145
168
|
end
|
data/lib/mautic/proxy.rb
CHANGED
@@ -23,30 +23,20 @@ module Mautic
|
|
23
23
|
|
24
24
|
def all(options = {}, &block)
|
25
25
|
if options[:limit] == 'all'
|
26
|
-
|
27
26
|
options.delete(:limit)
|
28
|
-
|
29
|
-
records = results = where(options)
|
30
|
-
total = @last_response['total'].to_i
|
31
|
-
while records.any?
|
32
|
-
if block_given?
|
33
|
-
records.each &block
|
34
|
-
end
|
35
|
-
break if results.size >= total
|
36
|
-
|
37
|
-
records = where(options.merge(start: records.size))
|
38
|
-
results.concat records
|
39
|
-
end
|
27
|
+
limit_all(options, &block)
|
40
28
|
else
|
41
29
|
results = where(options)
|
42
30
|
results.each { |i| yield i } if block_given?
|
31
|
+
results
|
43
32
|
end
|
44
|
-
results
|
45
33
|
end
|
46
34
|
|
35
|
+
# @param [Hash] params
|
36
|
+
# @see https://developer.mautic.org
|
47
37
|
def where(params = {})
|
48
38
|
q = params.reverse_merge(@options[:default_params] || {})
|
49
|
-
json = @connection.request(:get, "api/#{@endpoint}",
|
39
|
+
json = @connection.request(:get, "api/#{@endpoint}", params: q)
|
50
40
|
@count = json["total"].to_i
|
51
41
|
@last_response = json
|
52
42
|
json[data_name].collect do |id, attributes|
|
@@ -71,5 +61,22 @@ module Mautic
|
|
71
61
|
@count = json["total"].to_i
|
72
62
|
end
|
73
63
|
|
64
|
+
protected
|
65
|
+
|
66
|
+
# @param [Hash] options
|
67
|
+
# @option options (see #where)
|
68
|
+
def limit_all(options, &block)
|
69
|
+
records = results = where(options)
|
70
|
+
total = @last_response['total'].to_i
|
71
|
+
while records.any?
|
72
|
+
records.each(&block) if block_given?
|
73
|
+
break if results.size >= total
|
74
|
+
|
75
|
+
records = where(options.merge(start: records.size))
|
76
|
+
results.concat records
|
77
|
+
end
|
78
|
+
results
|
79
|
+
end
|
80
|
+
|
74
81
|
end
|
75
82
|
end
|
data/lib/mautic/version.rb
CHANGED
data/spec/rails_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mautic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukáš Pokorný
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -58,84 +58,84 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.
|
61
|
+
version: '1.4'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 1.
|
68
|
+
version: '1.4'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec-rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '3.
|
75
|
+
version: '3.9'
|
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.
|
82
|
+
version: '3.9'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: factory_bot_rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '5.1'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '5.1'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: database_cleaner
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '1.
|
103
|
+
version: '1.7'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '1.
|
110
|
+
version: '1.7'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: faker
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '2.7'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '
|
124
|
+
version: '2.7'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: webmock
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
129
|
- - "~>"
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: '3.
|
131
|
+
version: '3.7'
|
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.
|
138
|
+
version: '3.7'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: pry-rails
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -205,7 +205,8 @@ files:
|
|
205
205
|
homepage: https://github.com/luk4s/mautic-rails
|
206
206
|
licenses:
|
207
207
|
- MIT
|
208
|
-
metadata:
|
208
|
+
metadata:
|
209
|
+
allowed_push_host: https://rubygems.org
|
209
210
|
post_install_message:
|
210
211
|
rdoc_options: []
|
211
212
|
require_paths:
|
@@ -221,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
221
222
|
- !ruby/object:Gem::Version
|
222
223
|
version: '0'
|
223
224
|
requirements: []
|
224
|
-
rubygems_version: 3.0.
|
225
|
+
rubygems_version: 3.0.8
|
225
226
|
signing_key:
|
226
227
|
specification_version: 4
|
227
228
|
summary: Ruby on Rails Mautic integration
|