mautic 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/app/models/mautic/connection.rb +32 -1
- data/app/models/mautic/connections/oauth2.rb +3 -15
- data/app/models/mautic/contact.rb +2 -0
- data/lib/mautic.rb +35 -3
- data/lib/mautic/model.rb +31 -19
- data/lib/mautic/version.rb +1 -1
- data/spec/rails_helper.rb +3 -0
- data/spec/spec_helper.rb +1 -25
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c77ac23dffbdf501bf3193b5da058a6664aa887a
|
4
|
+
data.tar.gz: 5e70349a9999dae0f43725e6b2b0028eec68f858
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 403f7b5fefaffc8efd92088b916342051750d234a011d7f57e6bba1fa5a37d500f04cb2467146170e06ed34c6535e2bfad6cd93b7f6ef00641d17be7c0d4c2dc
|
7
|
+
data.tar.gz: 91744d8458f445803d0b43021183e1cd9339c739bce7b7382e13edfd906270f96b3dc4dd01499486db02a1b9e571d623900de5e1483d9c01364be2b3d1b227a5
|
data/README.md
CHANGED
@@ -6,6 +6,13 @@ RoR helper / wrapper for Mautic API and forms
|
|
6
6
|
### Gem provides API connection to your Mautic(s)
|
7
7
|
1. Create mautic connection
|
8
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
|
9
16
|
|
10
17
|
Find connection which you want to use:
|
11
18
|
```ruby
|
@@ -39,9 +39,40 @@ module Mautic
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def request(type, path, params = {})
|
42
|
-
|
42
|
+
@last_request = [type, path, params]
|
43
|
+
response = raise NotImplementedError
|
44
|
+
parse_response(response)
|
43
45
|
end
|
44
46
|
|
47
|
+
private
|
48
|
+
|
49
|
+
def parse_response(response)
|
50
|
+
case response.status
|
51
|
+
when 400
|
52
|
+
raise Mautic::ValidationError.new(response)
|
53
|
+
when 404
|
54
|
+
raise Mautic::RecordNotFound.new(response)
|
55
|
+
when 200, 201
|
56
|
+
json = JSON.parse(response.body) rescue {}
|
57
|
+
Array(json['errors']).each do |error|
|
58
|
+
case error['code'].to_i
|
59
|
+
when 401
|
60
|
+
raise Mautic::TokenExpiredError.new(response) if @try_to_refresh
|
61
|
+
@try_to_refresh = true
|
62
|
+
refresh!
|
63
|
+
json = request(*@last_request)
|
64
|
+
when 404
|
65
|
+
raise Mautic::RecordNotFound.new(response)
|
66
|
+
else
|
67
|
+
raise Mautic::RequestError.new(response)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
else
|
71
|
+
raise Mautic::RequestError.new(response)
|
72
|
+
end
|
73
|
+
|
74
|
+
json
|
75
|
+
end
|
45
76
|
|
46
77
|
end
|
47
78
|
end
|
@@ -30,21 +30,9 @@ module Mautic
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def request(type, path, params = {})
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
when 400
|
37
|
-
# Validation error
|
38
|
-
when 401
|
39
|
-
raise Mautic::TokenExpiredError.new(error['message']) if @try_to_refresh
|
40
|
-
@try_to_refresh = true
|
41
|
-
refresh!
|
42
|
-
json = request(type, path, params)
|
43
|
-
else
|
44
|
-
raise Mautic::AuthorizeError.new("#{error['code']} - #{error['message']}")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
json
|
33
|
+
@last_request = [type, path, params]
|
34
|
+
response = connection.request(type, path, params)
|
35
|
+
parse_response(response)
|
48
36
|
end
|
49
37
|
|
50
38
|
private
|
data/lib/mautic.rb
CHANGED
@@ -9,14 +9,46 @@ module Mautic
|
|
9
9
|
autoload :Proxy, 'mautic/proxy'
|
10
10
|
autoload :Model, 'mautic/model'
|
11
11
|
|
12
|
-
class
|
12
|
+
class RequestError < StandardError
|
13
|
+
|
14
|
+
attr_reader :response, :errors
|
15
|
+
|
16
|
+
def initialize(response, message = nil)
|
17
|
+
@errors ||= []
|
18
|
+
@response = response
|
19
|
+
json_body = JSON.parse(response.body) rescue {}
|
20
|
+
message ||= Array(json_body['errors']).collect do |error|
|
21
|
+
msg = error['code'].to_s
|
22
|
+
msg << " (#{error['type']}):" if error['type']
|
23
|
+
msg << " #{error['message']}"
|
24
|
+
@errors << error['message']
|
25
|
+
msg
|
26
|
+
end.join(', ')
|
27
|
+
|
28
|
+
super(message)
|
29
|
+
end
|
30
|
+
|
13
31
|
end
|
14
32
|
|
15
|
-
class
|
33
|
+
class TokenExpiredError < RequestError
|
34
|
+
end
|
35
|
+
|
36
|
+
class ValidationError < RequestError
|
37
|
+
|
38
|
+
def initialize(response, message = nil)
|
39
|
+
@response = response
|
40
|
+
json_body = JSON.parse(response.body) rescue {}
|
41
|
+
@errors = Array(json_body['errors']).inject({}) { |mem, var| mem.merge!(var['details']); mem }
|
42
|
+
message ||= @errors.collect { |field, msg| "#{field}: #{msg.join(', ')}" }.join('; ')
|
43
|
+
super(response, message)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
16
47
|
|
48
|
+
class AuthorizeError < RequestError
|
17
49
|
end
|
18
50
|
|
19
|
-
class
|
51
|
+
class RecordNotFound < RequestError
|
20
52
|
end
|
21
53
|
|
22
54
|
configure do |config|
|
data/lib/mautic/model.rb
CHANGED
@@ -32,6 +32,7 @@ module Mautic
|
|
32
32
|
@table = MauticHash.new
|
33
33
|
self.attributes = { created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
|
34
34
|
assign_attributes(hash)
|
35
|
+
clear_changes
|
35
36
|
end
|
36
37
|
|
37
38
|
def save(force = false)
|
@@ -40,29 +41,37 @@ module Mautic
|
|
40
41
|
|
41
42
|
def update(force = false)
|
42
43
|
return false if changes.blank?
|
43
|
-
|
44
|
-
|
45
|
-
self.errors = json['errors']
|
46
|
-
else
|
44
|
+
begin
|
45
|
+
json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_h })
|
47
46
|
self.attributes = json[endpoint.singularize]
|
47
|
+
clear_changes
|
48
|
+
rescue ValidationError => e
|
49
|
+
self.errors = e.errors
|
48
50
|
end
|
49
|
-
|
51
|
+
|
52
|
+
self.errors.blank?
|
50
53
|
end
|
51
54
|
|
52
55
|
def create
|
53
|
-
|
54
|
-
|
55
|
-
self.errors = json['errors']
|
56
|
-
else
|
56
|
+
begin
|
57
|
+
json = @connection.request(:post, "api/#{endpoint}/#{id}/new", { body: to_h })
|
57
58
|
self.attributes = json[endpoint.singularize]
|
59
|
+
clear_changes
|
60
|
+
rescue ValidationError => e
|
61
|
+
self.errors = e.errors
|
58
62
|
end
|
59
|
-
|
63
|
+
|
64
|
+
self.errors.blank?
|
60
65
|
end
|
61
66
|
|
62
67
|
def destroy
|
63
|
-
|
64
|
-
|
65
|
-
|
68
|
+
begin
|
69
|
+
@connection.request(:delete, "api/#{endpoint}/#{id}/delete")
|
70
|
+
true
|
71
|
+
rescue RequestError => e
|
72
|
+
self.errors = e.errors
|
73
|
+
false
|
74
|
+
end
|
66
75
|
end
|
67
76
|
|
68
77
|
def changes
|
@@ -73,20 +82,23 @@ module Mautic
|
|
73
82
|
@table.to_h
|
74
83
|
end
|
75
84
|
|
76
|
-
private
|
77
|
-
|
78
|
-
def endpoint
|
79
|
-
self.class.endpoint
|
80
|
-
end
|
81
|
-
|
82
85
|
def attributes=(hash)
|
83
86
|
hash.each_pair do |k, v|
|
84
87
|
k = k.to_sym
|
85
88
|
@table[k] = v
|
86
89
|
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def clear_changes
|
87
95
|
@table.instance_variable_set(:@changes, nil)
|
88
96
|
end
|
89
97
|
|
98
|
+
def endpoint
|
99
|
+
self.class.endpoint
|
100
|
+
end
|
101
|
+
|
90
102
|
def assign_attributes(source = nil)
|
91
103
|
source ||= {}
|
92
104
|
data = {}
|
data/lib/mautic/version.rb
CHANGED
data/spec/rails_helper.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start
|
4
|
+
|
2
5
|
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
3
6
|
# Prevent database truncation if the environment is production
|
4
7
|
abort('The Rails environment is running in production mode!') if Rails.env.production?
|
data/spec/spec_helper.rb
CHANGED
@@ -3,6 +3,7 @@ require 'factory_bot_rails'
|
|
3
3
|
require 'database_cleaner'
|
4
4
|
require 'faker'
|
5
5
|
require 'webmock/rspec'
|
6
|
+
require 'pry-rails'
|
6
7
|
# require 'capybara/rspec'
|
7
8
|
|
8
9
|
ActiveRecord::Migration.maintain_test_schema!
|
@@ -34,10 +35,6 @@ RSpec.configure do |config|
|
|
34
35
|
end
|
35
36
|
|
36
37
|
config.include FactoryBot::Syntax::Methods
|
37
|
-
# DatabaseCleaner.clean_with(
|
38
|
-
# :truncation,
|
39
|
-
# except: %w(ar_internal_metadata)
|
40
|
-
# )
|
41
38
|
|
42
39
|
config.before(:suite) do
|
43
40
|
DatabaseCleaner.clean_with(:truncation, except: %w(ar_internal_metadata))
|
@@ -47,10 +44,6 @@ RSpec.configure do |config|
|
|
47
44
|
DatabaseCleaner.strategy = :transaction
|
48
45
|
end
|
49
46
|
|
50
|
-
config.before(:each, js: true) do
|
51
|
-
DatabaseCleaner.strategy = :truncation
|
52
|
-
end
|
53
|
-
|
54
47
|
config.before(:each) do
|
55
48
|
DatabaseCleaner.start
|
56
49
|
end
|
@@ -65,21 +58,4 @@ RSpec.configure do |config|
|
|
65
58
|
|
66
59
|
config.filter_rails_from_backtrace!
|
67
60
|
|
68
|
-
# config.before(:each) do |ex|
|
69
|
-
# meta = ex.metadata
|
70
|
-
# unless meta[:null]
|
71
|
-
# case meta[:logged]
|
72
|
-
# when :admin
|
73
|
-
# logged(Symphonia::User, FactoryBot.create(:admin_user))
|
74
|
-
# when true
|
75
|
-
# logged(Symphonia::User, FactoryBot.create(:user))
|
76
|
-
# end
|
77
|
-
# end
|
78
|
-
# end
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
|
83
|
-
def logged(model, user)
|
84
|
-
allow(model).to receive(:current).and_return(user)
|
85
61
|
end
|
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: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukáš Pokorný
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|