mautic 2.2.2 → 2.3.6

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
2
  SHA256:
3
- metadata.gz: 89cef64a0de1e7e4e5e9646ec53c483d95d8bf533918ed2560118c0f3d7652b8
4
- data.tar.gz: a0a34b7c04bb6ccc11ce78290190655103b1d7ad7f6f919fcd4e5fb30f5fbcbb
3
+ metadata.gz: 78b5aecca30c838b83909bf68ad262c18f202b4a1f4ebb91d25b3e0db9ef6050
4
+ data.tar.gz: c0a942ada27b2ede12b190d59c72f1ad8eb902f476f172ed4fd1601a577c1efb
5
5
  SHA512:
6
- metadata.gz: b666c92dbfeeb6bd73c4a29007c064c7c29bd8dbf6f9af26d4b24885a6abd7decfcb22535e93009dac190c54f0631c55ea0d2c6c28164ad7ff835355905cd12c
7
- data.tar.gz: '08a8ac1556b51dc267e84bcb20ceb2b556fe879053b0c2abe961439d9858a1106bb78cd43fa670fdd9c08dd6e3dc02b20159f8d134ae1a6cde832eb22c0b9bcd'
6
+ metadata.gz: 983fef37892340a7f03754baf4de27c509ec14ea61d0d323427050f7e36992ef2025adbb8ab193cf3c2b75c9e8edd4a5d198597805c6dc8b3dcff9f42113d4b9
7
+ data.tar.gz: 2e34254779f1c5c651f93ce358462bd135e9da2df6e09676c815dd4b1deed7ebe431a6ca7f8e141ebd84354eb81160b30ed92846fd6173c26c586dda4afc6ee1
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
- ## Usage
6
- ### Gem provides API connection to your Mautic(s)
7
- 1. Create mautic connection
8
- 2. Authorize it
9
-
10
- In mautic you need add API oauth2 login.
11
- For URI callback allow:
12
- ```
13
- http://localhost:3000/mautic/connections/:ID/oauth2
14
- ```
15
- ID = is your Mautic::Connection ID
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
 
@@ -8,7 +8,7 @@ module Mautic
8
8
 
9
9
  protected
10
10
 
11
- # params path need +:mautic_id+
11
+ # @note params path need +:mautic_connection_id+ . => Its Mautic::Connection ID
12
12
  def webhook
13
13
  @webhook ||= Mautic::Connection.receive_webhook params
14
14
  end
@@ -10,7 +10,7 @@ module Mautic
10
10
 
11
11
  # @param [ActionController::Parameters] params
12
12
  def self.receive_webhook(params)
13
- WebHook.new(find(params.require(:mautic_id)), params)
13
+ WebHook.new(find(params.require(:mautic_connection_id)), params)
14
14
  end
15
15
 
16
16
  def client
@@ -66,30 +66,41 @@ module Mautic
66
66
  def parse_response(response)
67
67
  case response.status
68
68
  when 400
69
- raise Mautic::ValidationError.new(response)
69
+ raise Mautic::ValidationError, response
70
+ when 401
71
+ try_to_refresh_and_parse(response)
70
72
  when 404
71
- raise Mautic::RecordNotFound.new(response)
73
+ raise Mautic::RecordNotFound, response
72
74
  when 200, 201
73
- json = JSON.parse(response.body) rescue {}
74
- Array(json['errors']).each do |error|
75
- case error['code'].to_i
76
- when 401
77
- raise Mautic::TokenExpiredError.new(response) if @try_to_refresh
78
- @try_to_refresh = true
79
- refresh!
80
- json = request(*@last_request)
81
- when 404
82
- raise Mautic::RecordNotFound.new(response)
83
- else
84
- raise Mautic::RequestError.new(response)
85
- end
86
- end
75
+ handle_success_response response
87
76
  else
88
- raise Mautic::RequestError.new(response)
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
89
92
  end
90
93
 
91
94
  json
92
95
  end
93
96
 
97
+ def try_to_refresh_and_parse(response)
98
+ raise Mautic::TokenExpiredError, response if @try_to_refresh
99
+
100
+ @try_to_refresh = true
101
+ refresh!
102
+ request(*@last_request)
103
+ end
104
+
94
105
  end
95
106
  end
@@ -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,12 +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
- self.attributes = {
17
- tags: (source['tags'] || []).collect{|t| Mautic::Tag.new(@connection, t)}
18
- } if source
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
50
+ end
51
+
52
+ def events
53
+ @proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
54
+ end
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) }
19
82
  end
20
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
21
120
  end
22
121
  end
@@ -0,0 +1,23 @@
1
+ module Mautic
2
+ class Event < Model
3
+
4
+ def initialize(connection, hash = nil)
5
+ hash["id"] ||= hash["eventId"]
6
+ hash["dateAdded"] ||= hash["timestamp"]&.to_time
7
+ super
8
+ end
9
+
10
+ def event_label
11
+ eventLabel
12
+ end
13
+
14
+ def label
15
+ event_label.is_a?(Hash) && event_label["label"] || event_label.to_s
16
+ end
17
+
18
+ def source_url
19
+ event_label.is_a?(Hash) ? "#{connection.url}#{event_label["href"]}" : nil
20
+ end
21
+
22
+ end
23
+ end
@@ -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
@@ -12,7 +12,7 @@ module Mautic
12
12
 
13
13
  def form_submissions
14
14
  @forms ||= Array.wrap(@params.require("mautic.form_on_submit")).collect do |data|
15
- p = data.permit(submission: [:id, form: {}, lead: {}]).to_h
15
+ p = data.permit(submission: [:id, form: {}, lead: {}, results: {}]).to_h
16
16
  ::Mautic::Submissions::Form.new(@connection, p["submission"]) if p["submission"]
17
17
  end.compact
18
18
  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 saved:</h2>
4
+ <h2><%= pluralize(mautic_connection.errors.count, "error") %> prohibited this mautic_connection from being
5
+ saved:</h2>
6
6
 
7
7
  <ul>
8
- <% mautic_connection.errors.full_messages.each do |message| %>
9
- <li><%= message %></li>
10
- <% end %>
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
- <div class="field">
26
- <%= form.label :client_id %>
27
- <%= form.text_field :client_id, id: :mautic_connection_client_id %>
28
- </div>
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
- <div class="field">
31
- <%= form.label :secret %>
32
- <%= form.text_field :secret, id: :mautic_connection_secret %>
33
- </div>
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>
@@ -5,6 +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
+ # post "webhook/:mautic_connection_id", action: "webhook", on: :collection
9
9
  end
10
10
  end
@@ -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
- json_body = JSON.parse(response.body) rescue {}
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 = JSON.parse(response.body) rescue {}
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)
@@ -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
@@ -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", { body: to_h })
63
- self.attributes = json[endpoint.singularize]
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", { body: to_h })
75
- self.attributes = json[endpoint.singularize]
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
- if (fields = source['fields'])
125
- if fields['all']
126
- @mautic_attributes = fields['all'].collect do |key, value|
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
- self.attributes = data
164
+
165
+ data
143
166
  end
144
167
 
145
168
  end
@@ -2,46 +2,45 @@ module Mautic
2
2
  class Proxy
3
3
 
4
4
  def initialize(connection, endpoint, options = nil)
5
+ @options = options || {}
5
6
  @connection = connection
6
- klass = "Mautic::#{endpoint.classify}"
7
+ klass = @options.delete(:klass) || "Mautic::#{endpoint.classify}"
7
8
  @target = klass.safe_constantize || Mautic.const_set(endpoint.classify, Class.new(Mautic::Model))
8
9
  @endpoint = endpoint
9
- @options = options || {}
10
10
  end
11
11
 
12
12
  def new(attributes = {})
13
- @target.new(@connection, attributes)
13
+ build_instance attributes
14
+ end
15
+
16
+ def data_name
17
+ @endpoint.split("/").last
18
+ end
19
+
20
+ def build_instance(data)
21
+ @target.new(@connection, data)
14
22
  end
15
23
 
16
24
  def all(options = {}, &block)
17
25
  if options[:limit] == 'all'
18
-
19
26
  options.delete(:limit)
20
-
21
- records = results = where(options)
22
- total = @last_response['total'].to_i
23
- while records.any?
24
- if block_given?
25
- records.each &block
26
- end
27
- break if results.size >= total
28
-
29
- records = where(options.merge(start: records.size))
30
- results.concat records
31
- end
27
+ limit_all(options, &block)
32
28
  else
33
29
  results = where(options)
34
- results.each{|i| yield i } if block_given?
30
+ results.each { |i| yield i } if block_given?
31
+ results
35
32
  end
36
- results
37
33
  end
38
34
 
35
+ # @param [Hash] params
36
+ # @see https://developer.mautic.org
39
37
  def where(params = {})
40
- q = params.reverse_merge(@options[:default_params] || {})
41
- json = @connection.request(:get, "api/#{@endpoint}", {params: q })
38
+ q = params.reverse_merge(@options[:default_params] || {})
39
+ json = @connection.request(:get, "api/#{@endpoint}", params: q)
40
+ @count = json["total"].to_i
42
41
  @last_response = json
43
- json[@endpoint].collect do |id, attributes|
44
- @target.new(@connection, attributes || id)
42
+ json[data_name].collect do |id, attributes|
43
+ build_instance attributes || id
45
44
  end
46
45
  end
47
46
 
@@ -52,9 +51,32 @@ module Mautic
52
51
  def find(id)
53
52
  json = @connection.request(:get, "api/#{@endpoint}/#{id}")
54
53
  @last_response = json
55
- @target.new(@connection, json[@endpoint.singularize])
54
+ build_instance json[data_name.singularize]
56
55
  end
57
56
 
57
+ def count
58
+ return @count if defined? @count
59
+
60
+ json = @connection.request(:get, "api/#{@endpoint}", { limit: 1 })
61
+ @count = json["total"].to_i
62
+ end
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
58
80
 
59
81
  end
60
82
  end
@@ -3,26 +3,32 @@ module Mautic
3
3
  class Form
4
4
  attr_reader :id
5
5
 
6
+ # @param [Mautic::Connection] connection
7
+ # @param [Hash] data
6
8
  def initialize(connection, data)
7
9
  @connection = connection
8
10
  @raw = data
9
- @id = data["id"]
11
+ @id = data["id"].to_i
10
12
  end
11
13
 
14
+ # @return [Integer]
12
15
  def form_id
13
- @raw["form"]["id"]
16
+ @form_id ||= @raw["form"]["id"].to_i
14
17
  end
15
18
 
19
+ # @return [Integer]
16
20
  def contact_id
17
- @raw["lead"]["id"]
21
+ @contact_id ||= @raw["lead"]["id"]
18
22
  end
19
23
 
24
+ # @return [Mautic::Form]
20
25
  def form
21
26
  @form ||= @connection.forms.new(@raw["form"].merge("fields" => @raw["results"]))
22
27
  end
23
28
 
29
+ # @return [Mautic::Contact]
24
30
  def contact
25
- @connection.contacts.new(@raw["lead"])
31
+ @contact ||= @connection.contacts.new(@raw["lead"])
26
32
  end
27
33
  end
28
34
  end
@@ -1,3 +1,3 @@
1
1
  module Mautic
2
- VERSION = '2.2.2'
2
+ VERSION = '2.3.6'
3
3
  end
@@ -1,6 +1,8 @@
1
1
  ENV['RAILS_ENV'] ||= 'test'
2
2
  require 'simplecov'
3
- SimpleCov.start
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ end
4
6
 
5
7
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
8
  # Prevent database truncation if the environment is production
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.2.2
4
+ version: 2.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukáš Pokorný
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-01 00:00:00.000000000 Z
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.3'
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.3'
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.7'
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.7'
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: '4.8'
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: '4.8'
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.6'
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.6'
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: '1.8'
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: '1.8'
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.4'
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.4'
138
+ version: '3.7'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: pry-rails
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -177,6 +177,7 @@ files:
177
177
  - app/models/mautic/connection.rb
178
178
  - app/models/mautic/connections/oauth2.rb
179
179
  - app/models/mautic/contact.rb
180
+ - app/models/mautic/event.rb
180
181
  - app/models/mautic/form.rb
181
182
  - app/models/mautic/tag.rb
182
183
  - app/models/mautic/web_hook.rb
@@ -204,7 +205,8 @@ files:
204
205
  homepage: https://github.com/luk4s/mautic-rails
205
206
  licenses:
206
207
  - MIT
207
- metadata: {}
208
+ metadata:
209
+ allowed_push_host: https://rubygems.org
208
210
  post_install_message:
209
211
  rdoc_options: []
210
212
  require_paths:
@@ -220,8 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
222
  - !ruby/object:Gem::Version
221
223
  version: '0'
222
224
  requirements: []
223
- rubyforge_project:
224
- rubygems_version: 2.7.6
225
+ rubygems_version: 3.0.8
225
226
  signing_key:
226
227
  specification_version: 4
227
228
  summary: Ruby on Rails Mautic integration